Bug 236324 – [Forms] Eclipse 3.4 crashes / failes to repaint when "Close Others" is used with SharedHeaderFormEditor
diff --git a/bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormImages.java b/bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormImages.java
index b3b5aef..e8dda26 100644
--- a/bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormImages.java
+++ b/bundles/org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/FormImages.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007 IBM Corporation and others.
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -16,6 +16,7 @@
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Display;
 
 public class FormImages {
@@ -35,22 +36,26 @@
 	
 	private abstract class ImageIdentifier {
 		Display fDisplay;
-		Color[] fColors;
+		RGB[] fRGBs;
 		int fLength;
 		
 		ImageIdentifier(Display display, Color[] colors, int length) {
 			fDisplay = display;
-			fColors = colors;
+			fRGBs = new RGB[colors.length];
+			for (int i = 0; i < colors.length; i++) {
+				Color color = colors[i];
+				fRGBs[i] = color == null ? null : color.getRGB();
+			}
 			fLength = length;
 		}
 		
 		public boolean equals(Object obj) {
 			if (obj instanceof ImageIdentifier) {
 				ImageIdentifier id = (ImageIdentifier)obj;
-				if (id.fColors.length == fColors.length) {
+				if (id.fRGBs.length == fRGBs.length) {
 					boolean result = id.fDisplay.equals(fDisplay) && id.fLength == fLength;
-					for (int i = 0; i < fColors.length && result; i++) {
-						result = result && id.fColors[i].equals(fColors[i]);
+					for (int i = 0; i < fRGBs.length && result; i++) {
+						result = result && id.fRGBs[i].equals(fRGBs[i]);
 					}
 					return result;
 				}
@@ -60,8 +65,8 @@
 		
 		public int hashCode() {
 			int hash = fDisplay.hashCode();
-			for (int i = 0; i < fColors.length; i++)
-				hash = hash * 7 + fColors[i].hashCode();
+			for (int i = 0; i < fRGBs.length; i++)
+				hash = hash * 7 + fRGBs[i].hashCode();
 			hash = hash * 7 + fLength;
 			return hash;
 		}
@@ -97,14 +102,14 @@
 	}
 	
 	private class ComplexImageIdentifier extends ImageIdentifier {
-		Color fBg;
+		RGB fBgRGB;
 		boolean fVertical;
 		int[] fPercents;
 		
 		public ComplexImageIdentifier(Display display, Color[] colors, int length,
 				int[] percents, boolean vertical, Color bg) {
 			super(display, colors, length);
-			fBg = bg;
+			fBgRGB = bg == null ? null : bg.getRGB();
 			fVertical = vertical;
 			fPercents = percents;
 		}
@@ -114,8 +119,8 @@
 				ComplexImageIdentifier id = (ComplexImageIdentifier) obj;
 				if (super.equals(obj)  &&
 						id.fVertical == fVertical && Arrays.equals(id.fPercents, fPercents)) {
-					if ((id.fBg == null && fBg == null) ||
-							(id.fBg != null && id.fBg.equals(fBg)))
+					if ((id.fBgRGB == null && fBgRGB == null) ||
+							(id.fBgRGB != null && id.fBgRGB.equals(fBgRGB)))
 						return true;
 					// if the only thing that isn't the same is the background color
 					// still return true if it does not matter (percents add up to 100)
diff --git a/tests/org.eclipse.ui.tests.forms/forms/org/eclipse/ui/tests/forms/util/FormImagesTests.java b/tests/org.eclipse.ui.tests.forms/forms/org/eclipse/ui/tests/forms/util/FormImagesTests.java
index 5913ca5..316b3b4 100755
--- a/tests/org.eclipse.ui.tests.forms/forms/org/eclipse/ui/tests/forms/util/FormImagesTests.java
+++ b/tests/org.eclipse.ui.tests.forms/forms/org/eclipse/ui/tests/forms/util/FormImagesTests.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007 IBM Corporation and others.
+ * Copyright (c) 2007, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,7 +13,9 @@
 
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.forms.widgets.FormToolkit;
 import org.eclipse.ui.internal.forms.widgets.FormImages;
 
 import junit.framework.Assert;
@@ -150,6 +152,34 @@
 		FormImages.getInstance().markFinished(image2);
 	}
 	
+	public void testToolkitColors() {
+		String blueKey = "blue";
+		String redKey = "red";
+		
+		Display display = Display.getCurrent();
+		FormToolkit kit1 = new FormToolkit(display);
+		kit1.getColors().createColor(blueKey, new RGB(0,0,255));
+		kit1.getColors().createColor(redKey, new RGB(255,0,0));
+		FormToolkit kit2 = new FormToolkit(display);
+		kit2.getColors().createColor(blueKey, new RGB(0,0,255));
+		kit2.getColors().createColor(redKey, new RGB(255,0,0));
+		Image image1 = FormImages.getInstance().getGradient(display, kit1.getColors().getColor(blueKey), kit1.getColors().getColor(redKey), 21, 21, 0);
+		Image image2 = FormImages.getInstance().getGradient(display, kit2.getColors().getColor(blueKey), kit2.getColors().getColor(redKey), 21, 21, 0);
+		Assert.assertEquals("different images were created for the same RGBs with different Color instances", image1, image2);
+		Image image3 = FormImages.getInstance().getGradient(display, new Color(display,0,0,255), new Color(display,255,0,0), 21, 21, 0);
+		Assert.assertEquals("different images were created for the same RGBs with different Color instances", image1, image3);
+		kit1.dispose();
+		Assert.assertFalse("image was disposed after toolkits were disposed", image1.isDisposed());
+		kit2.dispose();
+		Assert.assertFalse("image was disposed after toolkits were disposed", image2.isDisposed());
+		FormImages.getInstance().markFinished(image1);
+		Assert.assertFalse("image was disposed early", image1.isDisposed());
+		FormImages.getInstance().markFinished(image2);
+		Assert.assertFalse("image was disposed early", image2.isDisposed());
+		FormImages.getInstance().markFinished(image3);
+		Assert.assertTrue("image was not disposed", image3.isDisposed());
+	}
+	
 	public void testDisposeUnknown() {
 		Display display = Display.getCurrent();
 		Image image = new Image(display, 10, 10);