diff options
| author | Daniel Rolka | 2013-12-07 15:08:22 +0000 |
|---|---|---|
| committer | Daniel Rolka | 2013-12-09 10:35:53 +0000 |
| commit | 9d35505dbe80ad631a62f89ff02e2f4af663976e (patch) | |
| tree | fd61ba7ccff156ce3ee61b56328821b1d1f6d6a8 | |
| parent | dbf657e3e38cf18741f827fbe99f9147e454a155 (diff) | |
| download | eclipse.platform.ui-9d35505dbe80ad631a62f89ff02e2f4af663976e.tar.gz eclipse.platform.ui-9d35505dbe80ad631a62f89ff02e2f4af663976e.tar.xz eclipse.platform.ui-9d35505dbe80ad631a62f89ff02e2f4af663976e.zip | |
Bug 419016 - [CSS] Provide some reasonable bridge interactions betweenI20131209-0800
CSS and our older Colors and Fonts properties
Change-Id: Ibbe98e0769c065ac649cdbe6bac09cfe9d284476
Signed-off-by: Daniel Rolka <daniel.rolka@pl.ibm.com>
28 files changed, 988 insertions, 323 deletions
diff --git a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/impl/engine/AbstractCSSEngine.java b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/impl/engine/AbstractCSSEngine.java index 51e430de934..9cf348fb588 100644 --- a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/impl/engine/AbstractCSSEngine.java +++ b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/impl/engine/AbstractCSSEngine.java @@ -49,8 +49,8 @@ import org.eclipse.e4.ui.css.core.impl.dom.CSSStyleSheetImpl; import org.eclipse.e4.ui.css.core.impl.dom.DocumentCSSImpl; import org.eclipse.e4.ui.css.core.impl.dom.ViewCSSImpl; import org.eclipse.e4.ui.css.core.impl.sac.ExtendedSelector; -import org.eclipse.e4.ui.css.core.resources.CSSResourcesHelpers; import org.eclipse.e4.ui.css.core.resources.IResourcesRegistry; +import org.eclipse.e4.ui.css.core.resources.ResourceRegistryKeyFactory; import org.eclipse.e4.ui.css.core.util.impl.resources.ResourcesLocatorManager; import org.eclipse.e4.ui.css.core.util.resources.IResourcesLocatorManager; import org.eclipse.e4.ui.css.core.utils.StringUtils; @@ -136,6 +136,8 @@ public abstract class AbstractCSSEngine implements CSSEngine { private boolean parseImport; + private ResourceRegistryKeyFactory keyFactory; + public AbstractCSSEngine() { this(new DocumentCSSImpl()); } @@ -143,6 +145,7 @@ public abstract class AbstractCSSEngine implements CSSEngine { public AbstractCSSEngine(ExtendedDocumentCSS documentCSS) { this.documentCSS = documentCSS; this.viewCSS = new ViewCSSImpl(documentCSS); + keyFactory = new ResourceRegistryKeyFactory(); } /*--------------- Parse style sheet -----------------*/ @@ -1123,32 +1126,33 @@ public abstract class AbstractCSSEngine implements CSSEngine { public Object convert(CSSValue value, Object toType, Object context) throws Exception { - Object newValue = null; - String key = CSSResourcesHelpers.getCSSValueKey(value); - IResourcesRegistry resourcesRegistry = getResourcesRegistry(); - if (resourcesRegistry != null) { - if (key != null) { - newValue = resourcesRegistry.getResource(toType, key); - } - } + Object key = keyFactory.createKey(value); + Object newValue = getResource(toType, key); + if (newValue == null) { ICSSValueConverter converter = getCSSValueConverter(toType); if (converter != null) { newValue = converter.convert(value, this, context); - if (newValue != null) { - // cache it - if (resourcesRegistry != null) { - if (key != null) { - resourcesRegistry.registerResource(toType, key, - newValue); - } - } - } + // cache it + registerResource(toType, key, newValue); } } return newValue; } + private Object getResource(Object toType, Object key) { + if (key != null && getResourcesRegistry() != null) { + return getResourcesRegistry().getResource(toType, key); + } + return null; + } + + private void registerResource(Object toType, Object key, Object resource) { + if (key != null && resource != null && getResourcesRegistry() != null) { + getResourcesRegistry().registerResource(toType, key, resource); + } + } + public String convert(Object value, Object toType, Object context) throws Exception { if (value == null) { @@ -1168,4 +1172,8 @@ public abstract class AbstractCSSEngine implements CSSEngine { */ public abstract CSSParser makeCSSParser(); + protected void setResourceRegistryKeyFactory( + ResourceRegistryKeyFactory keyFactory) { + this.keyFactory = keyFactory; + } } diff --git a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/AbstractResourcesRegistry.java b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/AbstractResourcesRegistry.java index e0bd48a32cf..abe354c8a2d 100644 --- a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/AbstractResourcesRegistry.java +++ b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/AbstractResourcesRegistry.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Angelo Zerr and others. + * Copyright (c) 2008, 2013 Angelo Zerr 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 @@ -7,9 +7,11 @@ * * Contributors: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation + * IBM Corporation *******************************************************************************/ package org.eclipse.e4.ui.css.core.resources; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; @@ -33,11 +35,13 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { * java.lang.Object) */ public Object getResource(Object type, Object key) { - if (allResourcesMap == null) + if (allResourcesMap == null) { return null; + } Map resourcesMap = (Map) allResourcesMap.get(type); - if (resourcesMap == null) + if (resourcesMap == null) { return null; + } return resourcesMap.get(key); } @@ -48,8 +52,9 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { * java.lang.Object, java.lang.Object) */ public void registerResource(Object type, Object key, Object resource) { - if (allResourcesMap == null) + if (allResourcesMap == null) { allResourcesMap = new HashMap(); + } Map resourcesMap = (Map) allResourcesMap.get(type); if (resourcesMap == null) { resourcesMap = new HashMap(); @@ -58,6 +63,16 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { resourcesMap.put(key, resource); } + protected Map getCacheByType(Object type) { + if (allResourcesMap != null) { + Map resourcesMap = (Map) allResourcesMap.get(type); + if (resourcesMap != null) { + return resourcesMap; + } + } + return Collections.EMPTY_MAP; + } + /* * (non-Javadoc) * @@ -65,14 +80,17 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { * java.lang.Object) */ public void unregisterResource(Object type, Object key) { - if (allResourcesMap == null) + if (allResourcesMap == null) { return; + } Map resourcesMap = (Map) allResourcesMap.get(type); - if (resourcesMap == null) + if (resourcesMap == null) { return; + } Object resource = resourcesMap.get(key); - if (resource != null) + if (resource != null) { resourcesMap.remove(resource); + } } public void unregisterResource(Object resource) { @@ -95,8 +113,9 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { * @see org.eclipse.e4.ui.core.css.resources.IResourcesRegistry#dispose() */ public void dispose() { - if (allResourcesMap == null) + if (allResourcesMap == null) { return; + } // Loop for all resources stored into cache Set allResources = allResourcesMap.entrySet(); for (Iterator iterator = allResources.iterator(); iterator.hasNext();) { @@ -107,13 +126,13 @@ public abstract class AbstractResourcesRegistry implements IResourcesRegistry { for (Iterator iterator2 = resources.iterator(); iterator2.hasNext();) { Map.Entry entry2 = (Map.Entry) iterator2.next(); // Dispose the current resource. - disposeResource(type, (String) entry2.getKey(), entry2 + disposeResource(type, entry2.getKey(), entry2 .getValue()); } } allResourcesMap = null; } - public abstract void disposeResource(Object type, String key, + public abstract void disposeResource(Object type, Object key, Object resource); } diff --git a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpers.java b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpers.java index ae24747f4a7..412d06ae089 100644 --- a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpers.java +++ b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpers.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008 Angelo Zerr and others. + * Copyright (c) 2008, 2013 Angelo Zerr 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 @@ -7,6 +7,7 @@ * * Contributors: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation + * IBM Corporation *******************************************************************************/ package org.eclipse.e4.ui.css.core.resources; @@ -26,10 +27,12 @@ import org.w3c.dom.css.RGBColor; public class CSSResourcesHelpers { public static String getCSSValueKey(CSSValue value) { - if (value instanceof CSS2FontProperties) + if (value instanceof CSS2FontProperties) { return getCSSFontPropertiesKey((CSS2FontProperties) value); - if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) + } + if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) { return getCSSPrimitiveValueKey((CSSPrimitiveValue) value); + } return null; } @@ -56,13 +59,16 @@ public class CSSResourcesHelpers { case CSSPrimitiveValue.CSS_RGBCOLOR: RGBColor rgbColor = value.getRGBColorValue(); return getCSSRGBColorKey(rgbColor); + case CSSPrimitiveValue.CSS_STRING: + return value.getCssText(); } return null; } public static String getCSSRGBColorKey(RGBColor rgbColor) { - if (rgbColor == null) + if (rgbColor == null) { return null; + } String rgb = ((int) rgbColor.getGreen().getFloatValue( CSSPrimitiveValue.CSS_NUMBER)) + "_"; @@ -109,10 +115,12 @@ public class CSSResourcesHelpers { */ public static Object getResource(IResourcesRegistry resourcesRegistry, Object type, String key) { - if (key == null) + if (key == null) { return null; - if (resourcesRegistry != null) + } + if (resourcesRegistry != null) { return resourcesRegistry.getResource(type, key); + } return null; } @@ -130,8 +138,9 @@ public class CSSResourcesHelpers { Object type, CSSPrimitiveValue value, Object resource) { if (resourcesRegistry != null) { String key = getCSSPrimitiveValueKey(value); - if (key != null) + if (key != null) { resourcesRegistry.registerResource(type, key, resource); + } } } @@ -146,8 +155,9 @@ public class CSSResourcesHelpers { */ public static void registerResource(IResourcesRegistry resourcesRegistry, Object type, String key, Object resource) { - if (key == null) + if (key == null) { return; + } if (resourcesRegistry != null) { resourcesRegistry.registerResource(type, key, resource); } diff --git a/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/ResourceRegistryKeyFactory.java b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/ResourceRegistryKeyFactory.java new file mode 100644 index 00000000000..ded44f699b6 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.core/src/org/eclipse/e4/ui/css/core/resources/ResourceRegistryKeyFactory.java @@ -0,0 +1,19 @@ +/******************************************************************************* + * Copyright (c) 2013 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.ui.css.core.resources; + +import org.w3c.dom.css.CSSValue; + +public class ResourceRegistryKeyFactory { + public Object createKey(CSSValue value) { + return CSSResourcesHelpers.getCSSValueKey(value); + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/AbstractCSSSWTEngineImpl.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/AbstractCSSSWTEngineImpl.java index eaa5ef40bea..9a2a5ef5ac3 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/AbstractCSSSWTEngineImpl.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/AbstractCSSSWTEngineImpl.java @@ -20,6 +20,7 @@ import org.eclipse.e4.ui.css.swt.properties.converters.CSSValueSWTFontDataConver import org.eclipse.e4.ui.css.swt.properties.converters.CSSValueSWTGradientConverterImpl; import org.eclipse.e4.ui.css.swt.properties.converters.CSSValueSWTImageConverterImpl; import org.eclipse.e4.ui.css.swt.properties.converters.CSSValueSWTRGBConverterImpl; +import org.eclipse.e4.ui.css.swt.resources.SWTResourceRegistryKeyFactory; import org.eclipse.e4.ui.css.swt.resources.SWTResourcesRegistry; import org.eclipse.swt.widgets.Display; @@ -37,7 +38,7 @@ public abstract class AbstractCSSSWTEngineImpl extends CSSEngineImpl { public AbstractCSSSWTEngineImpl(Display display, boolean lazyApplyingStyles) { this.display = display; - + /** Initialize SWT CSSValue converter * */ // Register SWT RGB CSSValue Converter @@ -46,28 +47,31 @@ public abstract class AbstractCSSSWTEngineImpl extends CSSEngineImpl { super.registerCSSValueConverter(CSSValueSWTColorConverterImpl.INSTANCE); // Register SWT Gradient CSSValue Converter super - .registerCSSValueConverter(CSSValueSWTGradientConverterImpl.INSTANCE); + .registerCSSValueConverter(CSSValueSWTGradientConverterImpl.INSTANCE); // Register SWT Cursor CSSValue Converter super - .registerCSSValueConverter(CSSValueSWTCursorConverterImpl.INSTANCE); + .registerCSSValueConverter(CSSValueSWTCursorConverterImpl.INSTANCE); // Register SWT Font CSSValue Converter super.registerCSSValueConverter(CSSValueSWTFontConverterImpl.INSTANCE); // Register SWT FontData CSSValue Converter super - .registerCSSValueConverter(CSSValueSWTFontDataConverterImpl.INSTANCE); + .registerCSSValueConverter(CSSValueSWTFontDataConverterImpl.INSTANCE); // Register SWT Image CSSValue Converter super.registerCSSValueConverter(CSSValueSWTImageConverterImpl.INSTANCE); if (lazyApplyingStyles) { new CSSSWTApplyStylesListener(display, this); } - + initializeCSSPropertyHandlers(); -// SWTElement.setEngine(display, this); + // SWTElement.setEngine(display, this); + + setResourceRegistryKeyFactory(new SWTResourceRegistryKeyFactory()); } protected abstract void initializeCSSPropertyHandlers(); + @Override public IResourcesRegistry getResourcesRegistry() { IResourcesRegistry resourcesRegistry = super.getResourcesRegistry(); if (resourcesRegistry == null) { 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 d02907886b3..7c1b92aa659 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 @@ -13,13 +13,11 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; -import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; 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; @@ -27,6 +25,9 @@ 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.e4.ui.internal.css.swt.CSSActivator; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; +import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.widgets.Display; @@ -34,13 +35,14 @@ import org.w3c.dom.css.CSSPrimitiveValue; import org.w3c.dom.css.CSSValue; import org.w3c.dom.css.CSSValueList; import org.w3c.dom.css.RGBColor; -import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; public class CSSSWTColorHelper { public static final String COLOR_DEFINITION_MARKER = "#"; - + + private static final String HEX_COLOR_VALUE_PATTERN = "#[a-fA-F0-9]{6}"; + private static Field[] cachedFields; - + /*--------------- SWT Color Helper -----------------*/ public static Color getSWTColor(RGBColor rgbColor, Display display) { @@ -54,15 +56,17 @@ public class CSSSWTColorHelper { } Color color = display.getSystemColor(SWT.COLOR_BLACK); RGB rgb = getRGB((CSSPrimitiveValue) value, display); - if (rgb != null) 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; } private static RGB getRGB(CSSPrimitiveValue value, Display display) { RGB rgb = getRGB(value); if (rgb == null && display != null) { - String name = value.getStringValue(); - if (name.startsWith(COLOR_DEFINITION_MARKER)) { + String name = value.getStringValue(); + if (hasColorDefinitionAsValue(name)) { rgb = findColorByDefinition(name); } else if (name.contains("-")) { name = name.replace('-', '_'); @@ -72,6 +76,24 @@ public class CSSSWTColorHelper { return rgb; } + public static boolean hasColorDefinitionAsValue(CSSValue value) { + if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) { + CSSPrimitiveValue primitiveValue = (CSSPrimitiveValue) value; + if (primitiveValue.getPrimitiveType() == CSSPrimitiveValue.CSS_STRING) { + return hasColorDefinitionAsValue(primitiveValue + .getStringValue()); + } + } + return false; + } + + public static boolean hasColorDefinitionAsValue(String name) { + if (name.startsWith(COLOR_DEFINITION_MARKER)) { + return !name.matches(HEX_COLOR_VALUE_PATTERN); + } + return false; + } + /** * Process the given string and return a corresponding RGB object. * @@ -83,8 +105,7 @@ public class CSSSWTColorHelper { private static RGB process(Display display, String value) { Field [] fields = getFields(); try { - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; + for (Field field : fields) { if (field.getName().equals(value)) { return display.getSystemColor(field.getInt(null)).getRGB(); } @@ -107,18 +128,17 @@ public class CSSSWTColorHelper { */ private static Field[] getFields() { if (cachedFields == null) { - Class clazz = SWT.class; + 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]; + + for (Field field : allFields) { 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); } } @@ -126,7 +146,7 @@ public class CSSSWTColorHelper { } return cachedFields; } - + public static RGB getRGB(String name) { RGBColor color = CSS2ColorHelper.getRGBColor(name); if (color != null) { @@ -170,7 +190,7 @@ public class CSSSWTColorHelper { switch (value.getPrimitiveType()) { case CSSPrimitiveValue.CSS_PERCENTAGE: percent = (int) value - .getFloatValue(CSSPrimitiveValue.CSS_PERCENTAGE); + .getFloatValue(CSSPrimitiveValue.CSS_PERCENTAGE); } return new Integer(percent); } @@ -220,8 +240,8 @@ public class CSSSWTColorHelper { List values = grad.getValues(); IResourcesRegistry registry = engine.getResourcesRegistry(); Color[] colors = new Color[values.size()]; - - for (int i = 0; i < values.size(); i++) { + + 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 @@ -289,7 +309,7 @@ public class CSSSWTColorHelper { int blue = color.blue; return new CSS2RGBColorImpl(red, green, blue); } - + private static RGB findColorByDefinition(String name) { IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); if (provider != null) { diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java index 5c1fe23e87c..f586b8b45fb 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelper.java @@ -12,14 +12,16 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; -import org.eclipse.e4.ui.internal.css.swt.CSSActivator; -import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; +import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; + import org.eclipse.e4.ui.css.core.css2.CSS2FontHelper; import org.eclipse.e4.ui.css.core.css2.CSS2FontPropertiesHelpers; import org.eclipse.e4.ui.css.core.css2.CSS2PrimitiveValueImpl; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontPropertiesImpl; import org.eclipse.e4.ui.css.core.engine.CSSElementContext; +import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabItem; import org.eclipse.swt.graphics.Font; @@ -28,7 +30,7 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Widget; import org.w3c.dom.css.CSSPrimitiveValue; -import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; +import org.w3c.dom.css.CSSValue; /** * CSS SWT Font Helper to : @@ -38,9 +40,7 @@ import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.nor * </ul> */ public class CSSSWTFontHelper { - public static final String FONT_DEFINITION_MARKER = "#"; - - public static final String VALUE_FROM_FONT_DEFINITION = "default"; + public static final String FONT_DEFINITION_MARKER = "#"; /** * Get CSS2FontProperties from Control stored into Data of Control. If @@ -148,14 +148,13 @@ public class CSSSWTFontHelper { */ public static FontData getFontData(CSS2FontProperties fontProperties, FontData oldFontData) { - FontData newFontData = new FontData(); - + FontData newFontData = new FontData(); + // Family CSSPrimitiveValue cssFontFamily = fontProperties.getFamily(); FontData[] fontDataByDefinition = new FontData[0]; - boolean fontDefinitionAsFamily = cssFontFamily != null - && cssFontFamily.getStringValue().startsWith(FONT_DEFINITION_MARKER); - + boolean fontDefinitionAsFamily = hasFontDefinitionAsFamily(fontProperties); + if (fontDefinitionAsFamily) { fontDataByDefinition = findFontDataByDefinition(cssFontFamily); if (fontDataByDefinition.length > 0) { @@ -163,28 +162,26 @@ public class CSSSWTFontHelper { } } else if (cssFontFamily != null) { newFontData.setName(cssFontFamily.getStringValue()); - } - + } + boolean fontFamilySet = newFontData.getName() != null && newFontData.getName().trim().length() > 0; if (!fontFamilySet && oldFontData != null) { newFontData.setName(oldFontData.getName()); } - - + // Style - CSSPrimitiveValue cssFontStyle = fontProperties.getStyle(); - if (fontDefinitionAsFamily && fontDataByDefinition.length > 0 && isValueFromDefinition(cssFontStyle)) { + int style = getSWTStyle(fontProperties, oldFontData); + if (fontDefinitionAsFamily && fontDataByDefinition.length > 0 && style == SWT.NORMAL) { newFontData.setStyle(fontDataByDefinition[0].getStyle()); } else { - newFontData.setStyle(getSWTStyle(fontProperties, oldFontData)); + newFontData.setStyle(style); } - - + // Height CSSPrimitiveValue cssFontSize = fontProperties.getSize(); boolean fontHeightSet = false; - - if (isValueFromDefinition(cssFontSize)) { + + if (cssFontSize == null || cssFontSize.getCssText() == null) { if (fontDefinitionAsFamily && fontDataByDefinition.length > 0) { newFontData.setHeight(fontDataByDefinition[0].getHeight()); fontHeightSet = true; @@ -196,11 +193,21 @@ public class CSSSWTFontHelper { if (!fontHeightSet && oldFontData != null) { newFontData.setHeight(oldFontData.getHeight()); } - + return newFontData; } - - private static FontData[] findFontDataByDefinition(CSSPrimitiveValue cssFontFamily) { + + public static boolean hasFontDefinitionAsFamily(CSSValue value) { + if (value instanceof CSS2FontProperties) { + CSS2FontProperties props = (CSS2FontProperties) value; + return props.getFamily() != null + && props.getFamily().getStringValue() + .startsWith(FONT_DEFINITION_MARKER); + } + return false; + } + + private static FontData[] findFontDataByDefinition(CSSPrimitiveValue cssFontFamily) { IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); FontData[] result = new FontData[0]; if (provider != null) { @@ -211,11 +218,7 @@ public class CSSSWTFontHelper { } return result; } - - private static boolean isValueFromDefinition(CSSPrimitiveValue value) { - return value != null && VALUE_FROM_FONT_DEFINITION.equals(value.getCssText()); - } - + /** * Return SWT style Font from {@link CSS2FontProperties}. * @@ -225,8 +228,9 @@ public class CSSSWTFontHelper { */ public static int getSWTStyle(CSS2FontProperties fontProperties, FontData fontData) { - if (fontData == null) + if (fontData == null) { return SWT.NONE; + } int fontStyle = fontData.getStyle(); // CSS2 font-style @@ -236,8 +240,9 @@ public class CSSSWTFontHelper { if ("italic".equals(style)) { fontStyle = fontStyle | SWT.ITALIC; } else { - if (fontStyle == (fontStyle | SWT.ITALIC)) + if (fontStyle == (fontStyle | SWT.ITALIC)) { fontStyle = fontStyle ^ SWT.ITALIC; + } } } // CSS font-weight @@ -247,8 +252,9 @@ public class CSSSWTFontHelper { if ("bold".equals(weight.toLowerCase())) { fontStyle = fontStyle | SWT.BOLD; } else { - if (fontStyle == (fontStyle | SWT.BOLD)) + if (fontStyle == (fontStyle | SWT.BOLD)) { fontStyle = fontStyle ^ SWT.BOLD; + } } } return fontStyle; @@ -305,8 +311,9 @@ public class CSSSWTFontHelper { } public static String getFontSize(FontData fontData) { - if (fontData != null) + if (fontData != null) { return CSS2FontHelper.getFontSize(fontData.getHeight()); + } return null; } @@ -426,8 +433,9 @@ public class CSSSWTFontHelper { */ public static FontData getFirstFontData(Control control) { Font font = control.getFont(); - if (font == null) + if (font == null) { return null; + } return getFirstFontData(font); } @@ -440,11 +448,12 @@ public class CSSSWTFontHelper { */ public static FontData getFirstFontData(Font font) { FontData[] fontDatas = font.getFontData(); - if (fontDatas == null || fontDatas.length < 1) + if (fontDatas == null || fontDatas.length < 1) { return null; + } return fontDatas[0]; } - + private static Font getFont(Widget widget) { if (widget instanceof CTabItem) { return ((CTabItem) widget).getFont(); diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyFontSWTHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyFontSWTHandler.java index 72e6e63bb2c..d20ea94affe 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyFontSWTHandler.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/css2/CSSPropertyFontSWTHandler.java @@ -9,6 +9,7 @@ * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation * Remy Chi Jian Suen <remy.suen@gmail.com> * Lars Vogel <Lars.Vogel@gmail.com> - Bug 422702 + * IBM Corporation *******************************************************************************/ package org.eclipse.e4.ui.css.swt.properties.css2; @@ -46,10 +47,26 @@ implements ICSSPropertyHandler2 { private static final String CSS_CTABITEM_SELECTED_FONT_LISTENER_KEY = "CSS_CTABFOLDER_SELECTED_FONT_LISTENER_KEY"; //$NON-NLS-1$ private static void setFont(Widget widget, Font font) { + if (widget instanceof CTabItem) { ((CTabItem) widget).setFont(font); } else if (widget instanceof Control) { - ((Control) widget).setFont(font); + Control control = (Control) widget; + try { + control.setRedraw(false); + control.setFont(font); + if (control instanceof CTabFolder) { + updateChildrenFonts((CTabFolder) widget, font); + } + } finally { + control.setRedraw(true); + } + } + } + + private static void updateChildrenFonts(CTabFolder folder, Font font) { + for (CTabItem item : folder.getItems()) { + item.setFont(font); } } diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/ResourceByDefinitionKey.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/ResourceByDefinitionKey.java new file mode 100644 index 00000000000..eea2928daf3 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/ResourceByDefinitionKey.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2013 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.ui.css.swt.resources; + +public class ResourceByDefinitionKey { + private Object key; + + public ResourceByDefinitionKey(Object key) { + this.key = key; + } + + @Override + public int hashCode() { + return key.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof ResourceByDefinitionKey + && key.equals(((ResourceByDefinitionKey) obj).key); + } + + @Override + public String toString() { + return key.toString(); + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourceRegistryKeyFactory.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourceRegistryKeyFactory.java new file mode 100644 index 00000000000..7f06d43e140 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourceRegistryKeyFactory.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2009, 2013 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + ******************************************************************************/ +package org.eclipse.e4.ui.css.swt.resources; + +import org.eclipse.e4.ui.css.core.resources.ResourceRegistryKeyFactory; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper; +import org.w3c.dom.css.CSSValue; + +public class SWTResourceRegistryKeyFactory extends ResourceRegistryKeyFactory { + @Override + public Object createKey(CSSValue value) { + Object key = super.createKey(value); + if (CSSSWTColorHelper.hasColorDefinitionAsValue(value) + || CSSSWTFontHelper.hasFontDefinitionAsFamily(value)) { + return new ResourceByDefinitionKey(key); + } + return key; + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourcesRegistry.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourcesRegistry.java index 4947c20d9b5..8af10d1ac6a 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourcesRegistry.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/resources/SWTResourcesRegistry.java @@ -11,6 +11,10 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.resources; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.eclipse.e4.ui.css.core.resources.AbstractResourcesRegistry; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; @@ -28,8 +32,9 @@ import org.eclipse.swt.widgets.Listener; public class SWTResourcesRegistry extends AbstractResourcesRegistry { public SWTResourcesRegistry(Display display) { - if (display == null) + if (display == null) { return; + } // When SWT Display will dispose, all SWT resources stored // into cache will be dispose it too. display.addListener(SWT.Dispose, new Listener() { @@ -39,6 +44,7 @@ public class SWTResourcesRegistry extends AbstractResourcesRegistry { }); } + @Override public Object getResource(Object type, Object key) { Object resource = super.getResource(type, key); if (resource != null) { @@ -59,25 +65,28 @@ public class SWTResourcesRegistry extends AbstractResourcesRegistry { * @see org.eclipse.e4.ui.core.css.resources.AbstractResourcesRegistry#registerResource(java.lang.String, * java.lang.Object, java.lang.Object) */ + @Override public void registerResource(Object type, Object key, Object resource) { if (resource == null) + { return; -// String hit = getResource(type, key) != null -// ? " hit " -// : " "; - //TODO replace with eclipse logging -// if (resource instanceof Color) { -// System.out.println("key class = " + key.getClass()); -// System.out.println("Cache " + hit + "SWT Color key= " + key); -// } else if (resource instanceof Cursor) { -// System.out.println("Cache" + hit + "SWT Cursor key=" + key); -// } else if (resource instanceof Font) { -// System.out.println("Cache" + hit + "SWT Font key=" + key); -// } else if (resource instanceof Image) { -// System.out.println("Cache" + hit + "SWT Image key=" + key); -// } else -// System.out.println("Cache" + hit + "Resource key=" + key); - + // String hit = getResource(type, key) != null + // ? " hit " + // : " "; + //TODO replace with eclipse logging + // if (resource instanceof Color) { + // System.out.println("key class = " + key.getClass()); + // System.out.println("Cache " + hit + "SWT Color key= " + key); + // } else if (resource instanceof Cursor) { + // System.out.println("Cache" + hit + "SWT Cursor key=" + key); + // } else if (resource instanceof Font) { + // System.out.println("Cache" + hit + "SWT Font key=" + key); + // } else if (resource instanceof Image) { + // System.out.println("Cache" + hit + "SWT Image key=" + key); + // } else + // System.out.println("Cache" + hit + "Resource key=" + key); + } + super.registerResource(type, key, resource); } @@ -87,32 +96,33 @@ public class SWTResourcesRegistry extends AbstractResourcesRegistry { * @see org.eclipse.e4.ui.core.css.resources.AbstractResourcesRegistry#disposeResource(java.lang.Object, * java.lang.String, java.lang.Object) */ - public void disposeResource(Object type, String key, Object resource) { + @Override + public void disposeResource(Object type, Object key, Object resource) { // Dispose SWT Resource if (resource instanceof Color) { ((Color)resource).dispose(); //TODO replace with eclipse logging -// if (logger.isDebugEnabled()) -// logger.debug("Dispose SWT Color key=" + key); + // if (logger.isDebugEnabled()) + // logger.debug("Dispose SWT Color key=" + key); } else if (resource instanceof Cursor) { ((Cursor)resource).dispose(); //TODO replace with eclipse logging -// if (logger.isDebugEnabled()) -// logger.debug("Dispose SWT Cursor key=" + key); + // if (logger.isDebugEnabled()) + // logger.debug("Dispose SWT Cursor key=" + key); } else if (resource instanceof Font) { ((Font)resource).dispose(); //TODO replace with eclipse logging -// if (logger.isDebugEnabled()) -// logger.debug("Dispose SWT Font key=" + key); + // if (logger.isDebugEnabled()) + // logger.debug("Dispose SWT Font key=" + key); } else if (resource instanceof Image) { ((Image) resource).dispose(); //TODO replace with eclipse logging -// if (logger.isDebugEnabled()) -// logger.debug("Dispose SWT Image key=" + key); - } + // if (logger.isDebugEnabled()) + // logger.debug("Dispose SWT Image key=" + key); + } //TODO replace with eclipse logging -// else if (logger.isDebugEnabled()) -// logger.debug("Dispose Resource key=" + key); + // else if (logger.isDebugEnabled()) + // logger.debug("Dispose Resource key=" + key); } protected boolean isDisposed(Object resource) { @@ -127,4 +137,21 @@ public class SWTResourcesRegistry extends AbstractResourcesRegistry { } return false; } + + @SuppressWarnings("unchecked") + public List<Object> removeResourcesByKeyTypeAndType(Class<?> keyType, + Class<?>... types) { + List<Object> removedResources = new ArrayList<Object>(); + for (Class<?> cls : types) { + Iterator<Map.Entry<?, ?>> iter = getCacheByType(cls).entrySet().iterator(); + while (iter.hasNext()) { + Map.Entry<?, ?> entry = iter.next(); + if (keyType.isAssignableFrom(entry.getKey().getClass())) { + removedResources.add(entry.getValue()); + iter.remove(); + } + } + } + return removedResources; + } } diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java index 73a36578f5f..a7dfce90255 100644 --- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java +++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/WBWRenderer.java @@ -10,21 +10,27 @@ *******************************************************************************/ package org.eclipse.e4.ui.workbench.renderers.swt; -import org.eclipse.e4.ui.internal.workbench.renderers.swt.SWTRenderersMessages; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.inject.Inject; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.services.events.IEventBroker; import org.eclipse.e4.core.services.log.Logger; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.core.resources.IResourcesRegistry; +import org.eclipse.e4.ui.css.swt.dom.WidgetElement; +import org.eclipse.e4.ui.css.swt.resources.ResourceByDefinitionKey; +import org.eclipse.e4.ui.css.swt.resources.SWTResourcesRegistry; import org.eclipse.e4.ui.internal.workbench.E4Workbench; import org.eclipse.e4.ui.internal.workbench.PartServiceSaveHandler; +import org.eclipse.e4.ui.internal.workbench.renderers.swt.SWTRenderersMessages; import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.model.application.ui.MContext; import org.eclipse.e4.ui.model.application.ui.MElementContainer; @@ -58,7 +64,10 @@ import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.events.TraverseEvent; import org.eclipse.swt.events.TraverseListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.graphics.Resource; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -117,6 +126,7 @@ public class WBWRenderer extends SWTPartRenderer { private EventHandler shellUpdater; private EventHandler visibilityHandler; private EventHandler sizeHandler; + private EventHandler themeDefinitionChanged; public WBWRenderer() { super(); @@ -293,6 +303,10 @@ public class WBWRenderer extends SWTPartRenderer { }; eventBroker.subscribe(UIEvents.Window.TOPIC_ALL, sizeHandler); + + themeDefinitionChanged = new ThemeDefinitionChangedHandler(); + eventBroker.subscribe(UIEvents.UILifeCycle.THEME_DEFINITION_CHANGED, + themeDefinitionChanged); } @PreDestroy @@ -301,6 +315,7 @@ public class WBWRenderer extends SWTPartRenderer { eventBroker.unsubscribe(shellUpdater); eventBroker.unsubscribe(visibilityHandler); eventBroker.unsubscribe(sizeHandler); + eventBroker.unsubscribe(themeDefinitionChanged); } public Object createWidget(MUIElement element, Object parent) { @@ -765,4 +780,58 @@ public class WBWRenderer extends SWTPartRenderer { } + @SuppressWarnings("restriction") + protected static class ThemeDefinitionChangedHandler implements + EventHandler { + public void handleEvent(Event event) { + Object element = event.getProperty(IEventBroker.DATA); + + if (!(element instanceof MApplication)) { + return; + } + + List<Object> unusedResources = new ArrayList<Object>(); + Set<CSSEngine> engines = new HashSet<CSSEngine>(); + + // In theory we can have multiple engines since API allows it. + // It doesn't hurt to be prepared for such case + for (MWindow window : ((MApplication) element).getChildren()) { + CSSEngine engine = getEngine(window); + if (engine != null) { + engines.add(engine); + } + } + + for (CSSEngine engine : engines) { + unusedResources.addAll(removeResources(engine + .getResourcesRegistry())); + engine.reapply(); + } + + for (Object resource : unusedResources) { + disposeResource(resource); + } + } + + protected CSSEngine getEngine(MWindow window) { + return WidgetElement.getEngine((Widget) window.getWidget()); + } + + protected List<Object> removeResources(IResourcesRegistry registry) { + if (registry instanceof SWTResourcesRegistry) { + return ((SWTResourcesRegistry) registry) + .removeResourcesByKeyTypeAndType( + ResourceByDefinitionKey.class, Font.class, + Color.class); + } + return Collections.emptyList(); + } + + protected void disposeResource(Object resource) { + if (resource instanceof Resource + && !((Resource) resource).isDisposed()) { + ((Resource) resource).dispose(); + } + } + } } diff --git a/bundles/org.eclipse.e4.ui.workbench/.settings/.api_filters b/bundles/org.eclipse.e4.ui.workbench/.settings/.api_filters index 6a3abe8c598..008fb232e0b 100644 --- a/bundles/org.eclipse.e4.ui.workbench/.settings/.api_filters +++ b/bundles/org.eclipse.e4.ui.workbench/.settings/.api_filters @@ -28,6 +28,12 @@ <message_argument value="THEME_CHANGED"/> </message_arguments> </filter> + <filter id="403767336"> + <message_arguments> + <message_argument value="org.eclipse.e4.ui.workbench.UIEvents.UILifeCycle"/> + <message_argument value="THEME_DEFINITION_CHANGED"/> + </message_arguments> + </filter> </resource> <resource path="src/org/eclipse/e4/ui/workbench/modeling/EObjModelHandler.java" type="org.eclipse.e4.ui.workbench.modeling.EObjModelHandler"> <filter comment="Bug 411257 - .api_description files in Kepler release do not contain @noreference" id="338944126"> diff --git a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/UIEvents.java b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/UIEvents.java index 05c58b94080..499e03de560 100644 --- a/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/UIEvents.java +++ b/bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/UIEvents.java @@ -266,6 +266,14 @@ public class UIEvents { * @since 1.1 */ public static final String THEME_CHANGED = TOPIC + TOPIC_SEP + "themeChanged"; //$NON-NLS-1$ + + /** + * Sent when the theme definition is changed + * + * @since 1.1 + */ + public static final String THEME_DEFINITION_CHANGED = TOPIC + TOPIC_SEP + + "themeDefinitionChanged"; //$NON-NLS-1$ } /** diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java index f8ad8d1d4a2..305c54a6734 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorsAndFontsPreferencePage.java @@ -22,6 +22,8 @@ import java.util.ResourceBundle; import java.util.Set; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; +import org.eclipse.e4.core.services.events.IEventBroker; +import org.eclipse.e4.ui.workbench.UIEvents; import org.eclipse.jface.preference.PreferenceConverter; import org.eclipse.jface.preference.PreferencePage; import org.eclipse.jface.resource.JFaceResources; @@ -689,6 +691,8 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage private Text descriptionText; + private IEventBroker eventBroker; + /** * Create a new instance of the receiver. */ @@ -1230,6 +1234,7 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage */ public void init(IWorkbench aWorkbench) { this.workbench = (Workbench) aWorkbench; + eventBroker = (IEventBroker) workbench.getService(IEventBroker.class); setPreferenceStore(PrefUtil.getInternalPreferenceStore()); final IThemeManager themeManager = aWorkbench.getThemeManager(); @@ -1383,8 +1388,10 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage getApplyButton().setFont(appliedDialogFont); getDefaultsButton().setFont(appliedDialogFont); - if (oldFont != null) + if (oldFont != null) { oldFont.dispose(); + } + publishThemeChangedEvent(); } private void performColorDefaults() { @@ -1468,8 +1475,10 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage saveTreeExpansion(); saveTreeSelection(); boolean result = performColorOk() && performFontOk(); - if(result) + if (result) { PrefUtil.savePrefs(); + publishThemeChangedEvent(); + } return result; } @@ -2086,4 +2095,11 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage descriptionText.setLayoutData(data); myApplyDialogFont(descriptionText); } + + private void publishThemeChangedEvent() { + if (eventBroker != null) { + eventBroker.send(UIEvents.UILifeCycle.THEME_DEFINITION_CHANGED, + workbench.getApplication()); + } + } } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/WorkbenchThemeManager.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/WorkbenchThemeManager.java index b699b3d6383..c2fc15ac8ec 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/WorkbenchThemeManager.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/WorkbenchThemeManager.java @@ -17,6 +17,7 @@ import org.eclipse.core.commands.common.EventManager; import org.eclipse.core.runtime.Platform; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.e4.core.services.events.IEventBroker; +import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.services.IStylingEngine; import org.eclipse.e4.ui.workbench.UIEvents; import org.eclipse.jface.resource.ColorRegistry; @@ -404,6 +405,8 @@ public class WorkbenchThemeManager extends EventManager implements { if (oldTheme != null && eventBroker != null) { eventBroker.send(UIEvents.UILifeCycle.THEME_CHANGED, null); + eventBroker.send(UIEvents.UILifeCycle.THEME_DEFINITION_CHANGED, + context.get(MApplication.class.getName())); } } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF b/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF index a53bd64ebef..6e1a2318d6f 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.e4.ui.tests.css.swt/META-INF/MANIFEST.MF @@ -9,7 +9,10 @@ Require-Bundle: org.junit, org.eclipse.e4.ui.css.swt, org.w3c.css.sac, org.eclipse.e4.ui.css.swt.theme;bundle-version="0.9.1", - org.eclipse.ui;bundle-version="3.106.0" + org.eclipse.ui;bundle-version="3.106.0", + org.hamcrest;bundle-version="1.1.0", + org.objenesis;bundle-version="1.0.0", + org.mockito;bundle-version="1.8.4" Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: J2SE-1.5 Import-Package: org.osgi.framework;version="1.7.0";resolution:=optional, diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpersTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpersTest.java new file mode 100644 index 00000000000..477f67262cf --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/CSSResourcesHelpersTest.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2013 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 http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.e4.ui.css.core.resources; + +import static org.eclipse.e4.ui.css.core.resources.CSSResourcesHelpers.getCSSValueKey; +import static org.eclipse.e4.ui.css.core.resources.CSSResourcesHelpers.getCSSFontPropertiesKey; + +import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTHelperTestCase; +import org.w3c.dom.css.CSSPrimitiveValue; + +@SuppressWarnings("restriction") +public class CSSResourcesHelpersTest extends CSSSWTHelperTestCase { + public void testGetCSSValueKeyWhenFont() throws Exception { + CSS2FontProperties fontProperties = fontProperties("Arial", 10, null); + + String result = getCSSValueKey(fontProperties); + + assertNotNull(result); + assertEquals(getCSSFontPropertiesKey(fontProperties), result); + } + + public void testGetCSSValueKeyWhenDefinitionAsFontFamily() throws Exception { + CSS2FontProperties fontProperties = fontProperties(addFontDefinitionMarker("symbolicName"), 10, null); + + String result = getCSSValueKey(fontProperties); + + assertNotNull(result); + assertEquals(getCSSFontPropertiesKey(fontProperties), result); + } + + public void testGetCSSValueKeyWhenRgbAsColorValue() throws Exception { + CSSPrimitiveValue colorValue = colorValue("rgb(255,0,0)"); + + String result = getCSSValueKey(colorValue); + + assertNotNull(result); + assertEquals("rgb(255,0,0)", result); + } + + public void testGetCSSValueKeyWhenDefinitionAsColorValue() throws Exception { + CSSPrimitiveValue colorValue = colorValue(addColorDefinitionMarker("symbolicName")); + + String result = getCSSValueKey(colorValue); + + assertNotNull(result); + assertEquals("#symbolicName", result); + } +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourceRegistryKeyFactoryTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourceRegistryKeyFactoryTest.java new file mode 100644 index 00000000000..50cdd871efb --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourceRegistryKeyFactoryTest.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2013 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 http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.e4.ui.css.core.resources; + +import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTHelperTestCase; +import org.eclipse.e4.ui.css.swt.resources.ResourceByDefinitionKey; +import org.eclipse.e4.ui.css.swt.resources.SWTResourceRegistryKeyFactory; +import org.eclipse.swt.SWT; +import org.w3c.dom.css.CSSPrimitiveValue; + +@SuppressWarnings("restriction") +public class SWTResourceRegistryKeyFactoryTest extends CSSSWTHelperTestCase { + private SWTResourceRegistryKeyFactory factory = new SWTResourceRegistryKeyFactory(); + + public void testCreateKeyWhenFontProperty() throws Exception { + CSS2FontProperties fontProperties = fontProperties("Arial", 12, + SWT.ITALIC); + + Object result = factory.createKey(fontProperties); + + assertEquals(String.class, result.getClass()); + assertEquals(CSSResourcesHelpers.getCSSValueKey(fontProperties), result); + } + + public void testCreateKeyWhenColorValue() throws Exception { + CSSPrimitiveValue colorValue = colorValue("red"); + + Object result = factory.createKey(colorValue); + + assertEquals(String.class, result.getClass()); + assertEquals(CSSResourcesHelpers.getCSSValueKey(colorValue), result); + } + + public void testCreateKeyWhenFontByDefinition() throws Exception { + CSS2FontProperties fontProperties = fontProperties( + "#font-by-definition", 12, + SWT.ITALIC); + + Object result = factory.createKey(fontProperties); + + assertEquals(ResourceByDefinitionKey.class, result.getClass()); + assertEquals(CSSResourcesHelpers.getCSSValueKey(fontProperties).toString(), result.toString()); + } + + public void testCreateKeyWhenColorByDefinition() throws Exception { + CSSPrimitiveValue colorValue = colorValue("#color-by-definition"); + + Object result = factory.createKey(colorValue); + + assertEquals(ResourceByDefinitionKey.class, result.getClass()); + assertEquals(CSSResourcesHelpers.getCSSValueKey(colorValue).toString(), + result.toString()); + } +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourcesRegistryTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourcesRegistryTest.java new file mode 100644 index 00000000000..50a9826ec7f --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/core/resources/SWTResourcesRegistryTest.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2013 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 http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.e4.ui.css.core.resources; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.eclipse.e4.ui.css.swt.resources.ResourceByDefinitionKey; +import org.eclipse.e4.ui.css.swt.resources.SWTResourcesRegistry; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.Resource; + +@SuppressWarnings("restriction") +public class SWTResourcesRegistryTest extends TestCase { + public void testRemoveResourcesByKeyTypeAndType() throws Exception { + //given + final Resource resource1 = mock(Resource.class); + final Resource resource2 = mock(Resource.class); + final Object resource3 = new Object(); + final Resource resource4 = mock(Resource.class); + + Map<Object, Object> resources = new LinkedHashMap<Object, Object>(); + resources.put("key1", resource1); + resources.put(new ResourceByDefinitionKey("key2"), resource2); + resources.put(new ResourceByDefinitionKey("key3"), resource3); + resources.put(new ResourceByDefinitionKey("key4"), resource4); + + SWTResourcesRegistryTestable registry = spy(new SWTResourcesRegistryTestable()); + doReturn(resources).when(registry).getCacheByType(any(Font.class)); + + // when + List<?> result = registry.removeResourcesByKeyTypeAndType( + ResourceByDefinitionKey.class, Font.class); + + // then + assertEquals(3, result.size()); + assertEquals(resource2, result.get(0)); + assertEquals(resource3, result.get(1)); + assertEquals(resource4, result.get(2)); + + assertEquals(1, resources.size()); + assertTrue(resources.containsKey("key1")); + } + + public static class SWTResourcesRegistryTestable extends + SWTResourcesRegistry { + public SWTResourcesRegistryTestable() { + super(null); + } + + @SuppressWarnings("rawtypes") + @Override + public Map getCacheByType(Object type) { + return super.getCacheByType(type); + } + } +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.java index a32742b4cdb..a65cc86e7f8 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.java @@ -19,44 +19,47 @@ import org.w3c.dom.css.CSSValue; @SuppressWarnings("restriction") public class CSSSWTColorHelperTest extends CSSSWTHelperTestCase { private Display display; - + @Override protected void setUp() throws Exception { display = Display.getDefault(); } - + public void testGetSWTColor() throws Exception { - Color result = getSWTColor(colorProperties("red"), display); - + Color result = getSWTColor(colorValue("red"), display); + assertNotNull(result); assertEquals(255, result.getRed()); assertEquals(0, result.getBlue()); assertEquals(0, result.getGreen()); } - + public void testGetSWTColorWhenNotSupportedColorType() throws Exception { - Color result = getSWTColor(colorProperties("123213", CSSValue.CSS_CUSTOM), display); - + Color result = getSWTColor(colorValue("123213", CSSValue.CSS_CUSTOM), + display); + assertNull(result); } - + public void testGetSWTColorWhenInvalidColorValue() throws Exception { - Color result = getSWTColor(colorProperties("asdsad12"), display); - + Color result = getSWTColor(colorValue("asdsad12"), display); + assertNotNull(result); assertEquals(0, result.getRed()); assertEquals(0, result.getBlue()); assertEquals(0, result.getGreen()); } - + public void testGetSWTColorWhenColorFromDefinition() throws Exception { registerColorProviderWith("org.eclipse.jdt.debug.ui.InDeadlockColor", new RGB(0, 255, 0)); - - Color result = getSWTColor(colorProperties(colorDefinition("org-eclipse-jdt-debug-ui-InDeadlockColor")), display); - + + Color result = getSWTColor( + colorValue(addColorDefinitionMarker("org-eclipse-jdt-debug-ui-InDeadlockColor")), + display); + assertNotNull(result); assertEquals(0, result.getRed()); assertEquals(0, result.getBlue()); assertEquals(255, result.getGreen()); - } + } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelperTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelperTest.java index a0a65ceedfc..e8287dc6e3f 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelperTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelperTest.java @@ -9,7 +9,6 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; -import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper.VALUE_FROM_FONT_DEFINITION; import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper.getFontData; import org.eclipse.swt.SWT; @@ -17,72 +16,79 @@ import org.eclipse.swt.graphics.FontData; @SuppressWarnings("restriction") public class CSSSWTFontHelperTest extends CSSSWTHelperTestCase { - public void testGetFontData() throws Exception { - FontData result = getFontData(fontProperties("Times", 11, SWT.NORMAL), + public void testGetFontData() throws Exception { + FontData result = getFontData(fontProperties("Times", 11, SWT.NORMAL), new FontData()); - + assertEquals("Times", result.getName()); assertEquals(11, result.getHeight()); assertEquals(SWT.NORMAL, result.getStyle()); } - - public void testGetFontDataWhenMissingFamilyInCss() throws Exception { - FontData result = getFontData(fontProperties(null, 11, SWT.NORMAL), + + public void testGetFontDataWhenMissingFamilyInCss() throws Exception { + FontData result = getFontData(fontProperties(null, 11, SWT.NORMAL), new FontData("Courier", 5, SWT.ITALIC)); - + assertEquals("Courier", result.getName()); assertEquals(11, result.getHeight()); assertEquals(SWT.NORMAL, result.getStyle()); } - - public void testGetFontDataWhenMissingSizeInCss() throws Exception { - FontData result = getFontData(fontProperties("Arial", null, SWT.NORMAL), + + public void testGetFontDataWhenMissingSizeInCss() throws Exception { + FontData result = getFontData(fontProperties("Arial", null, SWT.NORMAL), new FontData("Courier", 5, SWT.ITALIC)); - + assertEquals("Arial", result.getName()); assertEquals(5, result.getHeight()); assertEquals(SWT.NORMAL, result.getStyle()); } - - public void testGetFontDataWhenMissingStyleInCss() throws Exception { - FontData result = getFontData(fontProperties("Times", 11, null), + + public void testGetFontDataWhenMissingStyleInCss() throws Exception { + FontData result = getFontData(fontProperties("Times", 11, null), new FontData("Courier", 5, SWT.ITALIC)); - + assertEquals("Times", result.getName()); assertEquals(11, result.getHeight()); assertEquals(SWT.ITALIC, result.getStyle()); } - + public void testGetFontDataWhenFontFamilyFromDefinition() throws Exception { registerFontProviderWith("org.eclipse.jface.bannerfont", "Arial", 15, SWT.ITALIC); - - FontData result = getFontData(fontProperties(fontDefinition("org-eclipse-jface-bannerfont"), 10, SWT.NORMAL), - new FontData()); - + + FontData result = getFontData( + fontProperties( + addFontDefinitionMarker("org-eclipse-jface-bannerfont"), + 10, null), + new FontData()); + assertEquals("Arial", result.getName()); assertEquals(10, result.getHeight()); - assertEquals(SWT.NORMAL, result.getStyle()); + assertEquals(SWT.ITALIC, result.getStyle()); } - + public void testGetFontDataWhenFontFamilyAndSizeFromDefinition() throws Exception { registerFontProviderWith("org.eclipse.jface.bannerfont", "Arial", 15, SWT.ITALIC); - - FontData result = getFontData(fontProperties(fontDefinition("org-eclipse-jface-bannerfont"), VALUE_FROM_FONT_DEFINITION, SWT.NORMAL), - new FontData()); - - assertEquals("Arial", result.getName()); + + FontData result = getFontData( + fontProperties( + addFontDefinitionMarker("org-eclipse-jface-bannerfont"), + null, SWT.NORMAL), + new FontData()); + + assertEquals("Arial", result.getName()); assertEquals(15, result.getHeight()); - assertEquals(SWT.NORMAL, result.getStyle()); + assertEquals(SWT.ITALIC, result.getStyle()); } - + public void testGetFontDataWhenFontFamilySizeAndStyleFromDefinition() throws Exception { registerFontProviderWith("org.eclipse.jface.bannerfont", "Arial", 15, SWT.ITALIC); - - FontData result = getFontData(fontProperties(fontDefinition("org-eclipse-jface-bannerfont"), VALUE_FROM_FONT_DEFINITION, VALUE_FROM_FONT_DEFINITION), + + FontData result = getFontData( + fontProperties(addFontDefinitionMarker("org-eclipse-jface-bannerfont")), new FontData()); - - assertEquals("Arial", result.getName()); + + assertEquals("Arial", result.getName()); assertEquals(15, result.getHeight()); assertEquals(SWT.ITALIC, result.getStyle()); - } + } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java index 5b24f19a815..50c9dceee21 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java @@ -9,10 +9,10 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - +import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper.COLOR_DEFINITION_MARKER; +import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper.FONT_DEFINITION_MARKER; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; import junit.framework.TestCase; import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; @@ -25,39 +25,23 @@ import org.w3c.dom.DOMException; import org.w3c.dom.css.CSSPrimitiveValue; import org.w3c.dom.css.CSSValue; -import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper.FONT_DEFINITION_MARKER; -import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper.COLOR_DEFINITION_MARKER; - @SuppressWarnings("restriction") -public abstract class CSSSWTHelperTestCase extends TestCase { - protected void registerFontProviderWith(final String expectedSymbolicName, final String family, final int size, final int style) throws Exception { - registerProvider(new IColorAndFontProvider() { - public FontData[] getFont(String symbolicName) { - if (symbolicName.equals(expectedSymbolicName)) { - return new FontData[]{new FontData(family, size, style)}; - } - return null; - } - public RGB getColor(String symbolicName) { - return null; - } - }); +public abstract class CSSSWTHelperTestCase extends TestCase { + protected void registerFontProviderWith(String expectedSymbolicName, + String family, int size, int style) throws Exception { + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(new FontData[] { new FontData(family, size, style) }).when( + provider).getFont(expectedSymbolicName); + registerProvider(provider); } - - protected void registerColorProviderWith(final String expectedSymbolicName, final RGB rgb) throws Exception { - registerProvider(new IColorAndFontProvider() { - public FontData[] getFont(String symbolicName) { - return null; - } - public RGB getColor(String symbolicName) { - if (symbolicName.equals(expectedSymbolicName)) { - return rgb; - } - return null; - } - }); + + protected void registerColorProviderWith(String expectedSymbolicName, + RGB rgb) throws Exception { + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(rgb).when(provider).getColor(expectedSymbolicName); + registerProvider(provider); } - + private void registerProvider(final IColorAndFontProvider provider) throws Exception { new CSSActivator() { @Override @@ -66,72 +50,64 @@ public abstract class CSSSWTHelperTestCase extends TestCase { }; }.start(null); } - - protected CSS2FontProperties fontProperties(final String family, final Object size, final Object style) throws Exception { - return (CSS2FontProperties) Proxy.newProxyInstance(getClass().getClassLoader(),new Class<?>[] {CSS2FontProperties.class}, - new InvocationHandler() { - public Object invoke(Object arg0, Method method, Object[] arg2) - throws Throwable { - if (method.getName().equals("getFamily")) { - return valueImpl(family); - } - if (method.getName().equals("getSize")) { - return valueImpl(size); - } - if (method.getName().equals("getStyle")) { - return valueImpl(style); + + protected CSS2FontProperties fontProperties(String family) throws Exception { + return fontProperties(family, null, null); + } + + protected CSS2FontProperties fontProperties(String family, Object size, + Object style) throws Exception { + CSS2FontProperties result = mock(CSS2FontProperties.class); + doReturn(valueImpl(family)).when(result).getFamily(); + if (size != null) { + doReturn(valueImpl(size)).when(result).getSize(); + } + if (style != null) { + doReturn(valueImpl(style)).when(result).getStyle(); + } + return result; + } + + private CSSValueImpl valueImpl(final Object value) { + if (value != null) { + return new CSSValueImpl() { + @Override + public String getCssText() { + return value.toString(); + } + + @Override + public String getStringValue() { + return getCssText(); } - return null; - } - - private CSSValueImpl valueImpl(final Object value) { - if (value != null) { - return new CSSValueImpl() { - @Override - public String getCssText() { - return value.toString(); - } - @Override - public String getStringValue() { - return getCssText(); - } - @Override - public float getFloatValue(short valueType) throws DOMException { - return Float.parseFloat(getCssText()); - } - }; + + @Override + public float getFloatValue(short valueType) throws DOMException { + return Float.parseFloat(getCssText()); } - return null; - } - }); - } - - protected CSSValueImpl colorProperties(final String value) { - return colorProperties(value, CSSValue.CSS_PRIMITIVE_VALUE); + }; + } + return null; } - - protected CSSValueImpl colorProperties(final String value, final short type) { - return new CSSValueImpl() { - @Override - public short getPrimitiveType() { - return CSSPrimitiveValue.CSS_STRING; - } - @Override - public short getCssValueType() { - return type; - } - @Override - public String getStringValue() throws DOMException { - return value; - } - }; + + protected CSSValueImpl colorValue(String value) { + return colorValue(value, CSSValue.CSS_PRIMITIVE_VALUE); + } + + protected CSSValueImpl colorValue(String value, short type) { + CSSValueImpl result = mock(CSSValueImpl.class); + doReturn(CSSPrimitiveValue.CSS_STRING).when(result).getPrimitiveType(); + doReturn(type).when(result).getCssValueType(); + doReturn(value).when(result).getStringValue(); + doReturn(value).when(result).getCssText(); + return result; } - - protected String fontDefinition(String fontDefinitionId) { + + protected String addFontDefinitionMarker(String fontDefinitionId) { return FONT_DEFINITION_MARKER + fontDefinitionId; } - - protected String colorDefinition(String colorDefinitionId) { + + protected String addColorDefinitionMarker(String colorDefinitionId) { return COLOR_DEFINITION_MARKER + colorDefinitionId; } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java index 9c88bcacce7..791f9362e6c 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java @@ -9,12 +9,15 @@ *******************************************************************************/ package org.eclipse.e4.ui.tests.css.swt; -import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelperTest; -import org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelperTest; - import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.e4.ui.css.core.resources.CSSResourcesHelpersTest; +import org.eclipse.e4.ui.css.core.resources.SWTResourceRegistryKeyFactoryTest; +import org.eclipse.e4.ui.css.core.resources.SWTResourcesRegistryTest; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelperTest; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelperTest; + public class CssSwtTestSuite extends TestSuite { /** * Returns the suite. This is required to use the JUnit Launcher. @@ -29,28 +32,31 @@ public class CssSwtTestSuite extends TestSuite { public CssSwtTestSuite() { addTestSuite(CSSSWTFontHelperTest.class); addTestSuite(CSSSWTColorHelperTest.class); + addTestSuite(CSSResourcesHelpersTest.class); + addTestSuite(SWTResourceRegistryKeyFactoryTest.class); + addTestSuite(SWTResourcesRegistryTest.class); addTestSuite(FontDefinitionTest.class); addTestSuite(ColorDefinitionTest.class); - addTestSuite(CSSSWTWidgetTest.class); + addTestSuite(CSSSWTWidgetTest.class); addTestSuite(LabelTest.class); addTestSuite(CTabFolderTest.class); addTestSuite(CTabItemTest.class); -// addTestSuite(ETabFolderTest.class); -// addTestSuite(ETabItemTest.class); + // addTestSuite(ETabFolderTest.class); + // addTestSuite(ETabItemTest.class); addTestSuite(IdClassLabelColorTest.class); addTestSuite(ShellTest.class); addTestSuite(ButtonTest.class); -// addTestSuite(ShellActiveTest.class); //TODO see bug #273582 + // addTestSuite(ShellActiveTest.class); //TODO see bug #273582 addTestSuite(GradientTest.class); addTestSuite(MarginTest.class); - + // text-transform tests addTestSuite(ButtonTextTransformTest.class); addTestSuite(LabelTextTransformTest.class); addTestSuite(TextTextTransformTest.class); - + //other - addTestSuite(DescendentTest.class); + addTestSuite(DescendentTest.class); addTestSuite(ThemeTest.class); } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java index 80c38d89b44..ef215b297c1 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java @@ -25,23 +25,23 @@ import org.eclipse.ui.internal.themes.FontDefinition; @SuppressWarnings("restriction") public class FontDefinitionTest extends CSSSWTTestCase { private Display display; - + @Override protected void setUp() throws Exception { display = Display.getDefault(); } - + public void testFontDefinition() throws Exception { //given - CSSEngine engine = createEngine("FontDefinition#org-eclipse-jface-bannerfont {font-family: 'Times';font-size: 12;font-style: italic;}", display); - FontDefinition definition = fontDefinition("org.eclipse.jface.bannerfont"); - + CSSEngine engine = createEngine("FontDefinition#org-eclipse-jface-bannerfont {font-family: 'Times';font-size: 12;font-style: italic;}", display); + FontDefinition definition = fontDefinition("org.eclipse.jface.bannerfont"); + assertNull(definition.getValue()); - assertFalse(definition.isOverridden()); - + assertFalse(definition.isOverridden()); + //when engine.applyStyles(definition, true); - + //then assertNotNull(definition.getValue()); assertEquals("Times", definition.getValue()[0].getName()); @@ -49,68 +49,69 @@ public class FontDefinitionTest extends CSSSWTTestCase { assertEquals(SWT.ITALIC, definition.getValue()[0].getStyle()); assertTrue(definition.isOverridden()); } - + public void testFontDefinitionWhenDefinitionStylesheetNotFound() throws Exception{ //given - CSSEngine engine = createEngine("FontDefinition#org-eclipse-jface-bannerfont {font-family: 'Times';font-size: 12;font-style: italic;}", display); - FontDefinition definition = fontDefinition("font definition uniqueId without matching stylesheet"); - + CSSEngine engine = createEngine("FontDefinition#org-eclipse-jface-bannerfont {font-family: 'Times';font-size: 12;font-style: italic;}", display); + FontDefinition definition = fontDefinition("font definition uniqueId without matching stylesheet"); + assertNull(definition.getValue()); assertFalse(definition.isOverridden()); - + //when engine.applyStyles(definition, true); - + //then assertNull(definition.getValue()); assertFalse(definition.isOverridden()); } - + public void testWidgetWithFontDefinitionAsFontFamily() throws Exception { //given registerFontProviderWith("org.eclipse.jface.bannerfont", new FontData("Times", 12, SWT.ITALIC)); - - CSSEngine engine = createEngine("Label {font-family: '#org-eclipse-jface-bannerfont'; font-size: default; font-style: default}", + + CSSEngine engine = createEngine( + "Label {font-family: '#org-eclipse-jface-bannerfont'}", display); - + Shell shell = new Shell(display, SWT.SHELL_TRIM); - Label label = new Label(shell, SWT.NONE); + Label label = new Label(shell, SWT.NONE); Font font = new Font(display, "Arial", 9, SWT.NORMAL); label.setFont(font); label.setText("Some label text"); - - + + //when - engine.applyStyles(label, true); - - + engine.applyStyles(label, true); + + //then assertEquals("Times", label.getFont().getFontData()[0].getName()); assertEquals(12, label.getFont().getFontData()[0].getHeight()); assertEquals(SWT.ITALIC, label.getFont().getFontData()[0].getStyle()); - + shell.dispose(); font.dispose(); } - + private FontDefinition fontDefinition(String uniqueId) { - return new FontDefinition(new FontDefinition("fontName", uniqueId, "defaultsId", - "value", "categoryId", true, "fontDescription"), + return new FontDefinition(new FontDefinition("fontName", uniqueId, "defaultsId", + "value", "categoryId", true, "fontDescription"), new FontData[] {new FontData("Arial", 10, SWT.NORMAL)}); } - + private void registerFontProviderWith(final String expectedSymbolicName, final FontData fontData) throws Exception { new CSSActivator() { @Override public IColorAndFontProvider getColorAndFontProvider() { - return new IColorAndFontProvider() { - public FontData[] getFont(String symbolicName) { + return new IColorAndFontProvider() { + public FontData[] getFont(String symbolicName) { if (expectedSymbolicName.equals(symbolicName)) { return new FontData[]{fontData}; } return null; - } - public RGB getColor(String symbolicName) { + } + public RGB getColor(String symbolicName) { return null; } }; diff --git a/tests/org.eclipse.e4.ui.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.e4.ui.tests/META-INF/MANIFEST.MF index ecf0adab484..0a1a2bb7b66 100644 --- a/tests/org.eclipse.e4.ui.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.e4.ui.tests/META-INF/MANIFEST.MF @@ -30,7 +30,11 @@ Require-Bundle: org.eclipse.emf.ecore.xmi;bundle-version="2.4.0", org.eclipse.e4.core.di.extensions;bundle-version="0.9.0", org.eclipse.core.expressions;bundle-version="3.4.200", org.eclipse.e4.ui.workbench.addons.swt;bundle-version="0.9.0", - org.eclipse.e4.ui.css.swt;bundle-version="0.11.0" + org.eclipse.e4.ui.css.swt;bundle-version="0.11.0", + org.hamcrest;bundle-version="1.1.0", + org.objenesis;bundle-version="1.0.0", + org.mockito;bundle-version="1.8.4", + org.eclipse.e4.ui.css.core;bundle-version="0.10.100" Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-ActivationPolicy: lazy Import-Package: javax.annotation;version="1.0.0", diff --git a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java index ece2b88ef1b..257d9c8bc77 100644 --- a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java +++ b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/UIAllTests.java @@ -38,6 +38,7 @@ import org.eclipse.e4.ui.tests.workbench.PartRenderingEngineTests; import org.eclipse.e4.ui.tests.workbench.SashRendererTest; import org.eclipse.e4.ui.workbench.renderers.swt.StackRendererTest; import org.eclipse.e4.ui.workbench.renderers.swt.TabStateHandlerTest; +import org.eclipse.e4.ui.workbench.renderers.swt.ThemeDefinitionChangedHandlerTest; //import org.eclipse.e4.ui.workbench.renderers.swt.StackRendererTest; @@ -76,6 +77,7 @@ public class UIAllTests extends TestSuite { addTestSuite(ModelElementTest.class); addTestSuite(StackRendererTest.class); addTestSuite(TabStateHandlerTest.class); + addTestSuite(ThemeDefinitionChangedHandlerTest.class); // addTestSuite(SWTPartRendererTest.class); } } diff --git a/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/ThemeDefinitionChangedHandlerTest.java b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/ThemeDefinitionChangedHandlerTest.java new file mode 100644 index 00000000000..23113c85492 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/workbench/renderers/swt/ThemeDefinitionChangedHandlerTest.java @@ -0,0 +1,176 @@ +/******************************************************************************* + * Copyright (c) 2013 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + ******************************************************************************/ + +package org.eclipse.e4.ui.workbench.renderers.swt; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import junit.framework.TestCase; +import org.eclipse.e4.core.services.events.IEventBroker; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.core.resources.IResourcesRegistry; +import org.eclipse.e4.ui.css.swt.resources.ResourceByDefinitionKey; +import org.eclipse.e4.ui.css.swt.resources.SWTResourcesRegistry; +import org.eclipse.e4.ui.model.application.MApplication; +import org.eclipse.e4.ui.model.application.impl.ApplicationFactoryImpl; +import org.eclipse.e4.ui.model.application.ui.basic.MBasicFactory; +import org.eclipse.e4.ui.model.application.ui.basic.MWindow; +import org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.ThemeDefinitionChangedHandler; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.Resource; +import org.osgi.service.event.Event; + +/** + * + */ +public class ThemeDefinitionChangedHandlerTest extends TestCase { + public void testHandleEventWhenThemeChanged() throws Exception { + // given + final MApplication application = ApplicationFactoryImpl.eINSTANCE + .createApplication(); + application.getChildren().add(MBasicFactory.INSTANCE.createWindow()); + application.getChildren().add(MBasicFactory.INSTANCE.createWindow()); + + HashMap<String, Object> params = new HashMap<String, Object>(); + params.put(IEventBroker.DATA, application); + + Event event = new Event("topic", params); + + // resources removed from registry that have to be disposed + Resource resource1 = mock(Resource.class); + doReturn(false).when(resource1).isDisposed(); + + Resource resource2 = mock(Resource.class); + doReturn(true).when(resource2).isDisposed(); + + Object resource3 = new Object(); + + List<Object> removedResources = new ArrayList<Object>(); + removedResources.add(resource1); + removedResources.add(resource2); + removedResources.add(resource3); + // + + SWTResourcesRegistry registry = mock(SWTResourcesRegistry.class); + doReturn(removedResources).when(registry) + .removeResourcesByKeyTypeAndType(ResourceByDefinitionKey.class, + Font.class, Color.class); + + CSSEngine engine = mock(CSSEngine.class); + doReturn(registry).when(engine).getResourcesRegistry(); + + ThemeDefinitionChangedHandlerTestable handler = spy(new ThemeDefinitionChangedHandlerTestable()); + doReturn(engine).when(handler).getEngine(any(MWindow.class)); + + // when + handler.handleEvent(event); + + // then + verify(engine, times(1)).reapply(); + + verify(handler, times(1)).removeResources(registry); + verify(handler, times(3)).disposeResource(any(Object.class)); + verify(handler, times(1)).disposeResource(resource1); + verify(handler, times(1)).disposeResource(resource2); + verify(handler, times(1)).disposeResource(resource3); + + verify(resource1, times(1)).isDisposed(); + verify(resource1, times(1)).dispose(); + + verify(resource2, times(1)).isDisposed(); + verify(resource2, never()).dispose(); + } + + public void testHandleEventWhenElementIsNotMApplication() throws Exception { + // given + HashMap<String, Object> params = new HashMap<String, Object>(); + params.put(IEventBroker.DATA, MBasicFactory.INSTANCE.createWindow()); + + Event event = new Event("topic", params); + + CSSEngine engine = mock(CSSEngine.class); + + ThemeDefinitionChangedHandlerTestable handler = spy(new ThemeDefinitionChangedHandlerTestable()); + doReturn(engine).when(handler).getEngine(any(MWindow.class)); + + // when + handler.handleEvent(event); + + // then + verify(engine, never()).reapply(); + verify(handler, never()).removeResources(any(IResourcesRegistry.class)); + verify(handler, never()).disposeResource(any(Object.class)); + } + + public void testHandleEventWhenCSSEngineNotFoundForWidget() + throws Exception { + // given + MWindow window1 = MBasicFactory.INSTANCE.createWindow(); + MWindow window2 = MBasicFactory.INSTANCE.createWindow(); + + final MApplication application = ApplicationFactoryImpl.eINSTANCE + .createApplication(); + application.getChildren().add(window1); + application.getChildren().add(window2); + + HashMap<String, Object> params = new HashMap<String, Object>(); + params.put(IEventBroker.DATA, application); + + Event event = new Event("topic", params); + + SWTResourcesRegistry registry = mock(SWTResourcesRegistry.class); + + CSSEngine engine = mock(CSSEngine.class); + doReturn(registry).when(engine).getResourcesRegistry(); + + ThemeDefinitionChangedHandlerTestable handler = spy(new ThemeDefinitionChangedHandlerTestable()); + doReturn(null).when(handler).getEngine(window1); + doReturn(engine).when(handler).getEngine(window2); + + // when + handler.handleEvent(event); + + // then + verify(engine, times(1)).reapply(); + verify(handler, times(1)).removeResources(registry); + verify(handler, never()).disposeResource(any(Object.class)); + } + + protected static class ThemeDefinitionChangedHandlerTestable extends + ThemeDefinitionChangedHandler { + List<Object> processedRemovedResources; + + @Override + public CSSEngine getEngine(MWindow window) { + return super.getEngine(window); + } + + @Override + public List<Object> removeResources(IResourcesRegistry registry) { + return super.removeResources(registry); + } + + @Override + public void disposeResource(Object resource) { + super.disposeResource(resource); + } + } +}
\ No newline at end of file |
