--- /dev/null
+/*
+ * Common ui
+ *
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Changhyun Lee <changhyun1.lee@samsung.com>
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * Hyeongseok Heo <hyeongseok.heo@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.ui.decorators;
+
+import java.awt.image.BufferedImage;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResourceChangeEvent;
+import org.eclipse.core.resources.IResourceChangeListener;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.BaseLabelProvider;
+import org.eclipse.jface.viewers.DecorationContext;
+import org.eclipse.jface.viewers.IDecoration;
+import org.eclipse.jface.viewers.IDecorationContext;
+import org.eclipse.jface.viewers.ILightweightLabelDecorator;
+import org.eclipse.swt.graphics.Image;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.tizen.common.CommonPlugin;
+import org.tizen.common.util.ImageUtil;
+import org.tizen.common.util.SWTUtil;
+
+/**
+ * ImageFileDecorator.
+ *
+ * Icon file preview in Project Explorer and Package Explorer.
+ *
+ * @author Changhyun Lee {@literal <changhyun1.lee@samsung.com>} (S-Core)
+ */
+public class ImageFileDecorator extends BaseLabelProvider implements ILightweightLabelDecorator, IResourceChangeListener {
+
+ protected final Logger logger = LoggerFactory.getLogger( getClass() );
+
+ public static final String IMAGE_FILE_DECORATOR_ID = "org.tizen.common.ui.decorators.ImageFileDecorator";
+
+ public static final int IMAGE_FILE_WIDTH = 16;
+ public static final int IMAGE_FILE_HEIGHT = 16;
+
+ private static HashMap<IFile, Image> decoratedImageFiles = new HashMap<IFile, Image>();
+
+ public ImageFileDecorator() {
+ ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
+ }
+
+ @Override
+ public void dispose() {
+ ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
+ for ( Image image : decoratedImageFiles.values()) {
+ SWTUtil.tryDispose(image);
+ }
+ super.dispose();
+ }
+
+ /* Implement for ILightweightLabelDecorator */
+ @Override
+ public void decorate(Object element, IDecoration decoration) {
+ IFile file = ((IFile) element);
+ if ( !file.exists() ) {
+ decoratedImageFiles.remove(file);
+ return;
+ }
+
+ Image image = decoratedImageFiles.get(file);
+ if ( image == null ) {
+ BufferedImage bImage = null;
+ try {
+ bImage = ImageUtil.getBufferedImage(file.getContents());
+ image = ImageUtil.convertImageToSWT(SWTUtil.getDisplay(), ImageUtil.getScaledImage( bImage, IMAGE_FILE_WIDTH, IMAGE_FILE_HEIGHT ));
+ } catch (CoreException e) {
+ logger.error( e.getMessage(), e );
+ } catch (NullPointerException e ) {
+ logger.error( "Invalid image file: " + file.getName(), e );
+ } finally {
+ if ( bImage != null ) {
+ bImage.flush();
+ }
+ }
+ }
+
+ if ( image != null ) {
+ ImageDescriptor imageDescriptor = ImageDescriptor.createFromImage(image);
+ IDecorationContext decorationCtx = decoration.getDecorationContext();
+ if ( decorationCtx instanceof DecorationContext ) {
+ DecorationContext decorationImpl = (DecorationContext) decorationCtx;
+ decorationImpl.putProperty(IDecoration.ENABLE_REPLACE, Boolean.TRUE);
+ }
+ decoration.addOverlay(imageDescriptor, IDecoration.REPLACE);
+
+ decoratedImageFiles.put(file, image);
+ }
+ }
+
+ /* Implement for IResourceChangeListener */
+ @Override
+ public void resourceChanged(final IResourceChangeEvent event) {
+ if ( event != null && event.getDelta() != null ) {
+ ArrayList<IFile> arrayList = new ArrayList<IFile>(decoratedImageFiles.keySet());
+ for ( IFile file : arrayList ) {
+ IResourceDelta delta = event.getDelta().findMember(file.getFullPath());
+ if ( delta != null ) {
+ if ( delta.getKind() == IResourceDelta.REMOVED ) {
+ SWTUtil.tryDispose(decoratedImageFiles.get(file));
+ decoratedImageFiles.remove(file);
+ } else {
+ decoratedImageFiles.put(file, null);
+ refresh();
+ }
+ }
+ }
+ }
+ }
+
+ public void refresh() {
+ SWTUtil.asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ CommonPlugin.getDefault().getWorkbench().getDecoratorManager().update(IMAGE_FILE_DECORATOR_ID);
+ }
+ });
+ }
+
+}
/*\r
-* Common\r
+* Common ui\r
*\r
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
*\r
*\r
*/\r
\r
-package org.tizen.common.ui;\r
+package org.tizen.common.ui.decorators;\r
\r
import org.eclipse.core.resources.IProject;\r
import org.eclipse.jface.viewers.IDecoration;\r
import org.tizen.common.util.ProjectUtil;\r
import org.tizen.common.util.StringUtil;\r
\r
-public class LightweightProjectDecorator implements ILightweightLabelDecorator {\r
- private static final String SUFFIX_ROOT = " [Root]";\r
+public class ReferencedProjectDecorator implements ILightweightLabelDecorator {\r
+ //private static final String SUFFIX_ROOT = " [Root]";\r
private static final String SUFFIX_REFERENCED = " [with %s]";\r
//private final ImageDescriptor OVERLAY = Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/lock.gif");\r
\r
- public LightweightProjectDecorator() {\r
+ public ReferencedProjectDecorator() {\r
super();\r
}\r
\r
} catch (CoreException e) {\r
}\r
*/\r
- \r
+\r
ITizenProject tizenProject = ProjectUtil.getTizenProject(project);\r
if ( tizenProject == null ) {\r
return;\r
if ( projectType == null || projectType.isReferencedProject() == false ) {\r
return;\r
}\r
- \r
+\r
IProject[] refPrjs = project.getReferencingProjects();\r
if (refPrjs != null && refPrjs.length > 0) {\r
String refs = "";\r
\r
if ( StringUtil.isEmpty(refs) ) {\r
refs += refPrjs[i].getName();\r
- }\r
- else {\r
+ } else {\r
refs += ", " + refPrjs[i].getName();\r
}\r
}\r
@Override\r
public void removeListener(ILabelProviderListener listener) {\r
}\r
+\r
}\r