Bug 284750 [CSS] 26 colors leaked on every view open/close
diff --git a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/dom/properties/Gradient.java b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/dom/properties/Gradient.java
index 8eaa879..a1a3867 100644
--- a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/dom/properties/Gradient.java
+++ b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/dom/properties/Gradient.java
@@ -8,17 +8,17 @@
  * Contributors:
  *     Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
  *     Kai Toedter - added radial gradient support
+ *     IBM Corporation
  *******************************************************************************/
 package org.eclipse.e4.ui.css.core.dom.properties;
 
 import java.util.ArrayList;
 import java.util.List;
 
+import org.w3c.dom.css.CSSPrimitiveValue;
+
 /**
- * Generic class to store informations to manage Gradiant color.
- * 
- * @version 1.0.0
- * @author <a href="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
+ * Generic class to store informations to manage Gradient color.
  * 
  */
 public class Gradient {
@@ -26,6 +26,9 @@
 	private final List rgbs = new ArrayList();
 	private final List percents = new ArrayList();
 
+	//TODO see bug #278077
+	private final List values = new ArrayList();
+
 	private boolean isLinear = true;
 
 	/* TODO: enhance Gradient with focus points */
@@ -42,8 +45,10 @@
 		return !isLinear;
 	}
 
-	public void addRGB(Object rgb) {
+	//TODO see bug #278077
+	public void addRGB(Object rgb, CSSPrimitiveValue value) {
 		rgbs.add(rgb);
+		values.add(value);
 	}
 
 	public void addPercent(Integer percent) {
@@ -54,6 +59,10 @@
 		return rgbs;
 	}
 
+	public List getValues() {
+		return values;
+	}
+	
 	public List getPercents() {
 		return percents;
 	}
diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java
index 2f8c413..740af0e 100644
--- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java
+++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java
@@ -12,9 +12,14 @@
  *******************************************************************************/
 package org.eclipse.e4.ui.css.swt.helpers;
 
+import java.util.List;
+
 import org.eclipse.e4.ui.css.core.css2.CSS2ColorHelper;
 import org.eclipse.e4.ui.css.core.css2.CSS2RGBColorImpl;
 import org.eclipse.e4.ui.css.core.dom.properties.Gradient;
+import org.eclipse.e4.ui.css.core.engine.CSSEngine;
+import org.eclipse.e4.ui.css.core.resources.CSSResourcesHelpers;
+import org.eclipse.e4.ui.css.core.resources.IResourcesRegistry;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Display;
@@ -118,7 +123,7 @@
 				case CSSPrimitiveValue.CSS_RGBCOLOR:
 					RGB rgb = getRGB((CSSPrimitiveValue) value);
 					if (rgb != null) {
-						gradient.addRGB(rgb);
+						gradient.addRGB(rgb, (CSSPrimitiveValue) value);
 					}
 					break;
 				case CSSPrimitiveValue.CSS_PERCENTAGE:
@@ -130,13 +135,17 @@
 		return gradient;
 	}
 
-	public static Color[] getSWTColors(Gradient grad, Display display) {
-		// TODO watch out for leaking Colors, need to cache them in the resource
-		// registry
-		Color[] colors = new Color[grad.getRGBs().size()];
-		for (int i = 0; i < colors.length; i++) {
-			RGB rgb = (RGB) grad.getRGBs().get(i);
-			colors[i] = new Color(display, rgb.red, rgb.green, rgb.blue);
+	public static Color[] getSWTColors(Gradient grad, Display display, CSSEngine engine) {
+		List values = grad.getValues();
+		IResourcesRegistry registry = engine.getResourcesRegistry();
+		Color[] colors = new Color[values.size()];
+		
+		for (int i = 0; i < values.size(); i++) {		
+			CSSPrimitiveValue value = (CSSPrimitiveValue) values.get(i);
+			//We rely on the fact that when a gradient is created, it's colors are converted and in the registry
+			//TODO see bug #278077
+			Color color = (Color) registry.getResource(Color.class, CSSResourcesHelpers.getCSSPrimitiveValueKey(value));
+			colors[i] = color;
 		}
 		return colors;
 	}
diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTGradientConverterImpl.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTGradientConverterImpl.java
index f72dee5..b8f5811 100644
--- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTGradientConverterImpl.java
+++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTGradientConverterImpl.java
@@ -7,15 +7,20 @@
  *
  * Contributors:
  *     Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
+ *     IBM Corporation
  *******************************************************************************/
 package org.eclipse.e4.ui.css.swt.properties.converters;
 
+import java.util.List;
+
 import org.eclipse.e4.ui.css.core.dom.properties.Gradient;
 import org.eclipse.e4.ui.css.core.dom.properties.converters.AbstractCSSValueConverter;
 import org.eclipse.e4.ui.css.core.dom.properties.converters.ICSSValueConverter;
 import org.eclipse.e4.ui.css.core.dom.properties.converters.ICSSValueConverterConfig;
 import org.eclipse.e4.ui.css.core.engine.CSSEngine;
 import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper;
+import org.eclipse.swt.graphics.Color;
+import org.w3c.dom.css.CSSPrimitiveValue;
 import org.w3c.dom.css.CSSValue;
 import org.w3c.dom.css.CSSValueList;
 
@@ -25,10 +30,6 @@
  * <li>CSS Value to {@link Gradient}</li>.
  * <li>{@link Gradient} to String CSS Value</li>
  * </ul>
- * 
- * @version 1.0.0
- * @author <a href="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
- * 
  */
 public class CSSValueSWTGradientConverterImpl extends AbstractCSSValueConverter {
 
@@ -38,9 +39,19 @@
 		super(Gradient.class);
 	}
 
-	public Object convert(CSSValue value, CSSEngine engine, Object context) {
-		if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST)
-			return CSSSWTColorHelper.getGradient((CSSValueList) value);
+	public Object convert(CSSValue value, CSSEngine engine, Object context) throws Exception {
+		if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
+			Gradient grad = CSSSWTColorHelper.getGradient((CSSValueList) value);
+			List values = grad.getValues();
+			for (int i = 0; i < values.size(); i++) {
+				//Ensure all the colors are already converted and in the registry
+				//TODO see bug #278077	
+				CSSPrimitiveValue prim = (CSSPrimitiveValue) values.get(i);
+				engine.convert(prim, Color.class, context);
+			}
+			return grad;
+		}
+
 		return null;
 	}
 
diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyBackgroundSWTHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyBackgroundSWTHandler.java
index ec40e32..f143fc4 100644
--- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyBackgroundSWTHandler.java
+++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyBackgroundSWTHandler.java
@@ -89,14 +89,16 @@
 					widget.getDisplay());
 			if (widget instanceof CTabItem && "selected".equals(pseudo)) {
 				CTabFolder folder = ((CTabItem) widget).getParent();
-				folder.setSelectionBackground(CSSSWTColorHelper.getSWTColors(
-						grad, folder.getDisplay()), CSSSWTColorHelper
-						.getPercents(grad), true);
+				folder.setSelectionBackground(
+						CSSSWTColorHelper.getSWTColors(grad, folder.getDisplay(), engine),
+						CSSSWTColorHelper.getPercents(grad),
+						true);
 			} else if (widget instanceof ETabItem && "selected".equals(pseudo)) {
 				ETabFolder folder = ((ETabItem) widget).getETabParent();
-				folder.setSelectionBackground(CSSSWTColorHelper.getSWTColors(
-						grad, folder.getDisplay()), CSSSWTColorHelper
-						.getPercents(grad), true);
+				folder.setSelectionBackground(
+						CSSSWTColorHelper.getSWTColors(grad, folder.getDisplay(), engine),
+						CSSSWTColorHelper.getPercents(grad),
+						true);
 			} else if (widget instanceof Control) {
 				GradientBackgroundListener.handle((Control) widget, grad);
 			}