Bug 495840 - Memory leak if Docker Views left open for long period

- additional fixes where images are not being disposed of properly
  in Docker

Change-Id: I06ae37c7ebd21217ca70cc8d1b1b081b55b9df95
Reviewed-on: https://git.eclipse.org/r/75088
Tested-by: Hudson CI
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BuildDockerImageShortcut.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BuildDockerImageShortcut.java
index 246137c..3c44115 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BuildDockerImageShortcut.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/BuildDockerImageShortcut.java
@@ -197,6 +197,10 @@
 
 
 	private class ConnectionSelectionLabelProvider implements ILabelProvider {
+
+		private Image CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLE
+				.createImage();
+
 		@Override
 		public void removeListener(ILabelProviderListener listener) {
 		}
@@ -208,6 +212,7 @@
 
 		@Override
 		public void dispose() {
+			CONNECTION_IMAGE.dispose();
 		}
 
 		@Override
@@ -221,7 +226,7 @@
 
 		@Override
 		public Image getImage(Object element) {
-			return SWTImagesFactory.DESC_REPOSITORY_MIDDLE.createImage();
+			return CONNECTION_IMAGE;
 		}
 	}
 
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunDockerImageLaunchConfigurationDelegate.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunDockerImageLaunchConfigurationDelegate.java
index 0eae69b..8b013db 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunDockerImageLaunchConfigurationDelegate.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunDockerImageLaunchConfigurationDelegate.java
@@ -269,6 +269,10 @@
 	}
 
 	private class ConnectionSelectionLabelProvider implements ILabelProvider {
+
+		private Image CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLE
+				.createImage();
+
 		@Override
 		public void removeListener(ILabelProviderListener listener) {
 		}
@@ -280,6 +284,7 @@
 
 		@Override
 		public void dispose() {
+			CONNECTION_IMAGE.dispose();
 		}
 
 		@Override
@@ -293,7 +298,7 @@
 
 		@Override
 		public Image getImage(Object element) {
-			return SWTImagesFactory.DESC_REPOSITORY_MIDDLE.createImage();
+			return CONNECTION_IMAGE;
 		}
 	}
 
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunImageVolumesTab.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunImageVolumesTab.java
index 0aa86e0..4822502 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunImageVolumesTab.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/launch/RunImageVolumesTab.java
@@ -355,24 +355,37 @@
 	private static final class DataVolumesLabelProvider
 			extends ObservableMapLabelProvider {
 
+		private Image CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER
+				.createImage();
+		private Image FOLDER_CLOSED_IMAGE = SWTImagesFactory.DESC_FOLDER_CLOSED
+				.createImage();
+		private Image FILE_IMAGE = SWTImagesFactory.DESC_FILE.createImage();
+
 		public DataVolumesLabelProvider(final IObservableMap[] attributeMaps) {
 			super(attributeMaps);
 		}
 
 		@Override
+		public void dispose() {
+			CONTAINER_IMAGE.dispose();
+			FOLDER_CLOSED_IMAGE.dispose();
+			FILE_IMAGE.dispose();
+			super.dispose();
+		}
+
+		@Override
 		public Image getColumnImage(Object element, int columnIndex) {
 			final DataVolumeModel dataVolume = ((DataVolumeModel) element);
 			if (dataVolume.getMountType() != null && columnIndex == 1) {
 				switch (dataVolume.getMountType()) {
 				case CONTAINER:
-					return SWTImagesFactory.DESC_CONTAINER.createImage();
+					return CONTAINER_IMAGE;
 				case HOST_FILE_SYSTEM:
 					final File hostFile = new File(dataVolume.getMount());
 					if (!hostFile.exists() || hostFile.isDirectory()) {
-						return SWTImagesFactory.DESC_FOLDER_CLOSED
-								.createImage();
+						return FOLDER_CLOSED_IMAGE;
 					} else {
-						return SWTImagesFactory.DESC_FILE.createImage();
+						return FILE_IMAGE;
 					}
 				default:
 					return null;
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerLabelProvider.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerLabelProvider.java
index b1cf26f..ebb2784 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerLabelProvider.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerLabelProvider.java
@@ -45,6 +45,26 @@
 public class DockerExplorerLabelProvider implements IStyledLabelProvider, ILabelProvider {
 
 	private static final String UNNAMED_CONNECTION = "Connection.unnamed"; //$NON-NLS-1$
+	private Image OPEN_CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLE
+			.createImage();
+	private Image UNOPEN_CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLED
+			.createImage();
+	private Image CATEGORY_IMAGE = SWTImagesFactory.DESC_DB_GROUP.createImage();
+	private Image IMAGE_IMAGE = SWTImagesFactory.DESC_IMAGE.createImage();
+	private Image STARTED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_STARTED
+			.createImage();
+	private Image PAUSED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_PAUSED
+			.createImage();
+	private Image STOPPED_CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER_STOPPED
+			.createImage();
+	private Image CONTAINER_LINK_IMAGE = SWTImagesFactory.DESC_CONTAINER_LINK
+			.createImage();
+	private Image CONTAINER_VOLUME_IMAGE = SWTImagesFactory.DESC_CONTAINER_VOLUME
+			.createImage();
+	private Image CONTAINER_PORT_IMAGE = SWTImagesFactory.DESC_CONTAINER_PORT
+			.createImage();
+	private Image LOADING_IMAGE = SWTImagesFactory.DESC_SYSTEM_PROCESS
+			.createImage();
 
 	@Override
 	public void addListener(ILabelProviderListener listener) {
@@ -52,6 +72,17 @@
 
 	@Override
 	public void dispose() {
+		OPEN_CONNECTION_IMAGE.dispose();
+		UNOPEN_CONNECTION_IMAGE.dispose();
+		CATEGORY_IMAGE.dispose();
+		IMAGE_IMAGE.dispose();
+		STARTED_CONTAINER_IMAGE.dispose();
+		PAUSED_CONTAINER_IMAGE.dispose();
+		STOPPED_CONTAINER_IMAGE.dispose();
+		CONTAINER_LINK_IMAGE.dispose();
+		CONTAINER_VOLUME_IMAGE.dispose();
+		CONTAINER_PORT_IMAGE.dispose();
+		LOADING_IMAGE.dispose();
 	}
 
 	@Override
@@ -67,38 +98,38 @@
 	public Image getImage(final Object element) {
 		if(element instanceof IDockerConnection) {
 			if (((IDockerConnection) element).isOpen()) {
-				return SWTImagesFactory.DESC_REPOSITORY_MIDDLE.createImage();
+				return OPEN_CONNECTION_IMAGE;
 			} else {
-				return SWTImagesFactory.DESC_REPOSITORY_MIDDLED.createImage();
+				return UNOPEN_CONNECTION_IMAGE;
 			}
 		} else if(element instanceof DockerImagesCategory) {
-			return SWTImagesFactory.DESC_DB_GROUP.createImage();
+			return CATEGORY_IMAGE;
 		} else if(element instanceof DockerContainersCategory) {
-			return SWTImagesFactory.DESC_DB_GROUP.createImage();
+			return CATEGORY_IMAGE;
 		} else if(element instanceof IDockerImage) {
-			return SWTImagesFactory.DESC_IMAGE.createImage();
+			return IMAGE_IMAGE;
 		} else if(element instanceof IDockerContainer) {
 			final IDockerContainer container = (IDockerContainer) element;
 			final EnumDockerStatus containerStatus = EnumDockerStatus
 					.fromStatusMessage(container.status());
 			if (containerStatus == EnumDockerStatus.RUNNING) {
-				return SWTImagesFactory.DESC_CONTAINER_STARTED.createImage();
+				return STARTED_CONTAINER_IMAGE;
 			} else if (containerStatus == EnumDockerStatus.PAUSED) {
-				return SWTImagesFactory.DESC_CONTAINER_PAUSED.createImage();
+				return PAUSED_CONTAINER_IMAGE;
 			} else {
-				return SWTImagesFactory.DESC_CONTAINER_STOPPED.createImage();
+				return STOPPED_CONTAINER_IMAGE;
 			}
 		} else if (element instanceof DockerContainerLinksCategory
 				|| element instanceof DockerContainerLink) {
-			return SWTImagesFactory.DESC_CONTAINER_LINK.createImage();
+			return CONTAINER_LINK_IMAGE;
 		} else if (element instanceof DockerContainerVolumesCategory
 				|| element instanceof DockerContainerVolume) {
-			return SWTImagesFactory.DESC_CONTAINER_VOLUME.createImage();
+			return CONTAINER_VOLUME_IMAGE;
 		} else if (element instanceof DockerContainerPortMappingsCategory
 				|| element instanceof IDockerPortMapping) {
-			return SWTImagesFactory.DESC_CONTAINER_PORT.createImage();
+			return CONTAINER_PORT_IMAGE;
 		} else if(element instanceof LoadingStub) {
-			return SWTImagesFactory.DESC_SYSTEM_PROCESS.createImage();
+			return LOADING_IMAGE;
 		}
 		return null;
 	}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageRunResourceVolumesVariablesPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageRunResourceVolumesVariablesPage.java
index c24ca50..b467435 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageRunResourceVolumesVariablesPage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageRunResourceVolumesVariablesPage.java
@@ -745,24 +745,37 @@
 	private static final class DataVolumesLabelProvider
 			extends ObservableMapLabelProvider {
 
+		private Image CONTAINER_IMAGE = SWTImagesFactory.DESC_CONTAINER
+				.createImage();
+		private Image FOLDER_CLOSED_IMAGE = SWTImagesFactory.DESC_FOLDER_CLOSED
+				.createImage();
+		private Image FILE_IMAGE = SWTImagesFactory.DESC_FILE.createImage();
+
 		public DataVolumesLabelProvider(final IObservableMap[] attributeMaps) {
 			super(attributeMaps);
 		}
 
 		@Override
+		public void dispose() {
+			CONTAINER_IMAGE.dispose();
+			FOLDER_CLOSED_IMAGE.dispose();
+			FILE_IMAGE.dispose();
+			super.dispose();
+		}
+
+		@Override
 		public Image getColumnImage(Object element, int columnIndex) {
 			final DataVolumeModel dataVolume = ((DataVolumeModel) element);
 			if (dataVolume.getMountType() != null && columnIndex == 1) {
 				switch (dataVolume.getMountType()) {
 				case CONTAINER:
-					return SWTImagesFactory.DESC_CONTAINER.createImage();
+					return CONTAINER_IMAGE;
 				case HOST_FILE_SYSTEM:
 					final File hostFile = new File(dataVolume.getMount());
 					if (!hostFile.exists() || hostFile.isDirectory()) {
-						return SWTImagesFactory.DESC_FOLDER_CLOSED
-								.createImage();
+						return FOLDER_CLOSED_IMAGE;
 					} else {
-						return SWTImagesFactory.DESC_FILE.createImage();
+						return FILE_IMAGE;
 					}
 				default:
 					return null;
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageSearchPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageSearchPage.java
index 321b5d5..3c0a0e2 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageSearchPage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/ImageSearchPage.java
@@ -452,6 +452,12 @@
 			}
 		}
 
+		@Override
+		public void dispose() {
+			ICON.dispose();
+			super.dispose();
+		}
+
 		abstract boolean doPaint(final Object element);
 	}
 
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
index 6df8d3e..35433fb 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java
@@ -977,6 +977,10 @@
 	}
 
 	private class ConnectionSelectionLabelProvider implements ILabelProvider {
+
+		private Image CONNECTION_IMAGE = SWTImagesFactory.DESC_REPOSITORY_MIDDLE
+				.createImage();
+
 		@Override
 		public void removeListener(ILabelProviderListener listener) {
 		}
@@ -988,6 +992,7 @@
 
 		@Override
 		public void dispose() {
+			CONNECTION_IMAGE.dispose();
 		}
 
 		@Override
@@ -1001,7 +1006,7 @@
 
 		@Override
 		public Image getImage(Object element) {
-			return SWTImagesFactory.DESC_REPOSITORY_MIDDLE.createImage();
+			return CONNECTION_IMAGE;
 		}
 	}