Commits

Roman Dawydkin committed ac6006b

Fix Issue 12: Resource leak - to many images created

Patch idea by Michael Scharf <michael_scharf_google@scharf.gr>

Comments (0)

Files changed (2)

EclipseRunner/src/com/eclipserunner/RunnerPlugin.java

 import java.io.File;
 import java.util.HashMap;
 import java.util.Map;
-
 import org.eclipse.core.resources.ISaveContext;
 import org.eclipse.core.resources.ISaveParticipant;
 import org.eclipse.core.resources.ISavedState;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.ImageRegistry;
+import org.eclipse.swt.graphics.Image;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 
 /**
  * Eclipse runner plugin activator class.
- * 
+ *
  * @author bary, vachacz
+ * @author Michael Scharf &lt;michael_scharf_google@scharf.gr&gt; - patch idea
  */
 public class RunnerPlugin extends AbstractUIPlugin {
 
 		return imageDescriptor;
 	}
 
-	/**
+    public Image getImage(String imageFileName) {
+        final ImageRegistry registry = getImageRegistry();
+        Image image = registry.get(imageFileName);
+        if (image == null) {
+            image = getImageDescriptor(imageFileName).createImage();
+            registry.put(imageFileName, image);
+        }
+        return image;
+    }
+
+    /**
 	 * @return SWT active Shell.
 	 */
 	public static Shell getRunnerShell() {
 		return Display.getCurrent().getActiveShell();
 	}
-	
+
 	/**
 	 * @return Display instance
 	 */
 			RunnerStateExternalizer.readDefaultRunnerModel();
 		}
 	}
-
 }

EclipseRunner/src/com/eclipserunner/views/impl/LaunchTreeLabelProvider.java

 package com.eclipserunner.views.impl;
 
+import com.eclipserunner.RunnerPlugin;
+import com.eclipserunner.model.ICategoryNode;
+import com.eclipserunner.model.ILaunchNode;
+import com.eclipserunner.model.ILaunchTypeNode;
+import com.eclipserunner.model.IRunnerModel;
 import org.eclipse.debug.ui.DebugUITools;
 import org.eclipse.debug.ui.IDebugModelPresentation;
 import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.ImageRegistry;
 import org.eclipse.jface.viewers.DecorationOverlayIcon;
 import org.eclipse.jface.viewers.IDecoration;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.swt.graphics.Image;
 
-import com.eclipserunner.RunnerPlugin;
-import com.eclipserunner.model.ICategoryNode;
-import com.eclipserunner.model.ILaunchNode;
-import com.eclipserunner.model.ILaunchTypeNode;
-import com.eclipserunner.model.IRunnerModel;
-
 /**
  * Launch configuration tree decorator.
- * 
+ *
  * @author vachacz, bary
+ * @author Michael Scharf &lt;michael_scharf_google@scharf.gr&gt; - patch idea
  */
 public class LaunchTreeLabelProvider extends LabelProvider {
 
 	private static final String IMG_CATEGORY         = "category.gif";
 	private static final String IMG_DEFAULT_CATEGORY = "category-archive.gif";
 	private static final String IMG_DECORATION       = "star_min.gif";
+	private static final String MISSING_IMG_KEY      = "MISSING";
 
 	private IDebugModelPresentation debugModelPresentation;
 	private final IRunnerModel runnerModel;
 		else if (element instanceof ILaunchTypeNode) {
 			return getLaunchConfigurationTypeImage((ILaunchTypeNode) element);
 		}
-		return ImageDescriptor.getMissingImageDescriptor().createImage();
+		else {
+		    return getMissingImage();
+		}
 	}
 
+    private Image getMissingImage() {
+        final ImageRegistry imageRegistry = RunnerPlugin.getDefault().getImageRegistry();
+        Image image = imageRegistry.get(MISSING_IMG_KEY);
+        if (image == null) {
+            image = ImageDescriptor.getMissingImageDescriptor().createImage();
+            imageRegistry.put(MISSING_IMG_KEY, image);
+        }
+        return image;
+    }
+
 	private Image getCategoryImage(ICategoryNode launchConfigurationCategory) {
 		if (runnerModel.getDefaultCategoryNode() == launchConfigurationCategory) {
 			return createImage(IMG_DEFAULT_CATEGORY);
 		}
 		return image;
 	}
-	
+
 	private Image getLaunchConfigurationTypeImage(ILaunchTypeNode typeNode) {
 		return debugModelPresentation.getImage(typeNode.getLaunchConfigurationType());
 	}
-	
-	private Image createImage(String image) {
-		return RunnerPlugin.getDefault().getImageDescriptor(image).createImage();
-	}
 
-	private Image overlyBookmarkIcon(Image image, String decoration) {
-		ImageDescriptor decorationDescriptor = RunnerPlugin.getDefault().getImageDescriptor(decoration);
-		return new DecorationOverlayIcon(image, decorationDescriptor, IDecoration.TOP_RIGHT).createImage();
+	private Image createImage(String imageFileName) {
+		return RunnerPlugin.getDefault().getImage(imageFileName);
 	}
 
+	private Image overlyBookmarkIcon(Image sourceImage, String decorationFileName) {
+		final RunnerPlugin runnerPlugin = RunnerPlugin.getDefault();
+		final ImageRegistry imageRegistry = runnerPlugin.getImageRegistry();
+		final String key = sourceImage.toString() + "/" + decorationFileName;
+		Image image = imageRegistry.get(key);
+		if (image == null) {
+	        final ImageDescriptor decorationDescriptor = runnerPlugin.getImageDescriptor(decorationFileName);
+	        final DecorationOverlayIcon resultDescriptor =
+	                new DecorationOverlayIcon(sourceImage, decorationDescriptor, IDecoration.TOP_RIGHT);
+	        image = resultDescriptor.createImage();
+	        imageRegistry.put(key, image);
+        }
+		return image;
+	}
 }