diff options
| author | Bogdan Gheorghe | 2013-03-06 22:39:57 +0000 |
|---|---|---|
| committer | Bogdan Gheorghe | 2013-03-06 22:39:57 +0000 |
| commit | 8fab17122c0d0e9d0181ef533f0f5d4b56d31092 (patch) | |
| tree | a4a13df94405cc67aa12dbfcf427d010ca7c390c | |
| parent | 5ea7e0f296c53798aa8340e5da69a4ca441b2355 (diff) | |
| download | eclipse.platform.ui-8fab17122c0d0e9d0181ef533f0f5d4b56d31092.tar.gz eclipse.platform.ui-8fab17122c0d0e9d0181ef533f0f5d4b56d31092.tar.xz eclipse.platform.ui-8fab17122c0d0e9d0181ef533f0f5d4b56d31092.zip | |
Bug 402530 - [CSS] Styling Engine should be able to parse SWT color constantsI20130306-2330I20130306-2000
| -rw-r--r-- | bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java | 72 |
1 files changed, 69 insertions, 3 deletions
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 f1c9f63b9b5..8c445798e39 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,8 +12,12 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; -import java.util.List; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import org.eclipse.swt.SWT; +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; @@ -30,6 +34,8 @@ import org.w3c.dom.css.RGBColor; public class CSSSWTColorHelper { + private static Field[] cachedFields; + /*--------------- SWT Color Helper -----------------*/ public static Color getSWTColor(RGBColor rgbColor, Display display) { @@ -41,14 +47,74 @@ public class CSSSWTColorHelper { if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) { return null; } + Color color = display.getSystemColor(SWT.COLOR_BLACK); RGB rgb = getRGB((CSSPrimitiveValue) value); if (rgb == null) { - return null; + String name = ((CSSPrimitiveValue) value).getStringValue(); + if (name.contains("-")) { + name = name.replace('-', '_'); + rgb = process(display, name); + } } - Color color = new Color(display, rgb.red, rgb.green, rgb.blue); + if (rgb != null) color = new Color(display, rgb.red, rgb.green, rgb.blue); return color; } + /** + * Process the given string and return a corresponding RGB object. + * + * @param value + * the SWT constant <code>String</code> + * @return the value of the SWT constant, or <code>SWT.COLOR_BLACK</code> + * if it could not be determined + */ + private static RGB process(Display display, String value) { + Field [] fields = getFields(); + try { + for (int i = 0; i < fields.length; i++) { + Field field = fields[i]; + if (field.getName().equals(value)) { + return display.getSystemColor(field.getInt(null)).getRGB(); + } + } + } catch (IllegalArgumentException e) { + // no op - shouldnt happen. We check for static before calling + // getInt(null) + } catch (IllegalAccessException e) { + // no op - shouldnt happen. We check for public before calling + // getInt(null) + } + return display.getSystemColor(SWT.COLOR_BLACK).getRGB(); + } + + /** + * Get the SWT constant fields. + * + * @return the fields + * @since 3.3 + */ + private static Field[] getFields() { + if (cachedFields == null) { + Class clazz = SWT.class; + Field[] allFields = clazz.getDeclaredFields(); + ArrayList applicableFields = new ArrayList(allFields.length); + + for (int i = 0; i < allFields.length; i++) { + Field field = allFields[i]; + if (field.getType() == Integer.TYPE + && Modifier.isStatic(field.getModifiers()) + && Modifier.isPublic(field.getModifiers()) + && Modifier.isFinal(field.getModifiers()) + && field.getName().startsWith("COLOR")) { //$NON-NLS-1$ + + applicableFields.add(field); + } + } + cachedFields = (Field []) applicableFields.toArray(new Field [applicableFields.size()]); + } + return cachedFields; + } + public static RGB getRGB(String name) { RGBColor color = CSS2ColorHelper.getRGBColor(name); if (color != null) { |
