diff options
| author | Daniel Rolka | 2013-10-23 13:45:25 +0000 |
|---|---|---|
| committer | Gerrit Code Review @ Eclipse.org | 2013-10-28 17:04:02 +0000 |
| commit | f99429bb1e1e016497dad9e5a86f0941a2f314ac (patch) | |
| tree | 2b3038f7b9fc98bb5ddc3a7a5ea3d946698ecac2 | |
| parent | 9eaef928efdd3459fb24dac837140909c9ffb338 (diff) | |
| download | eclipse.platform.ui-f99429bb1e1e016497dad9e5a86f0941a2f314ac.tar.gz eclipse.platform.ui-f99429bb1e1e016497dad9e5a86f0941a2f314ac.tar.xz eclipse.platform.ui-f99429bb1e1e016497dad9e5a86f0941a2f314ac.zip | |
Bug 419016 - [CSS] Provide some reasonable bridge interactions between
CSS and our older Colors and Fonts properties
Change-Id: Ie8869d913cac3a9321e37f4544b61abf1fc57823
Signed-off-by: Daniel Rolka <daniel.rolka@pl.ibm.com>
36 files changed, 1230 insertions, 58 deletions
diff --git a/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF b/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF index eb4b79ea6db..29a34fe5279 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.e4.ui.css.swt/META-INF/MANIFEST.MF @@ -17,7 +17,8 @@ Export-Package: org.eclipse.e4.ui.css.swt;x-internal:=true, org.eclipse.e4.ui.css.swt.properties.custom;x-internal:=true, org.eclipse.e4.ui.css.swt.resources;x-internal:=true, org.eclipse.e4.ui.css.swt.serializers;x-internal:=true, - org.eclipse.e4.ui.internal.css.swt;x-internal:=true + org.eclipse.e4.ui.internal.css.swt;x-internal:=true, + org.eclipse.e4.ui.internal.css.swt.definition;x-friends:="org.eclipse.ui.workbench" Require-Bundle: org.eclipse.e4.ui.css.core;bundle-version="0.9.0", org.eclipse.swt;bundle-version="[3.4.1,4.0.0)", org.w3c.css.sac;bundle-version="1.3.0", diff --git a/bundles/org.eclipse.e4.ui.css.swt/plugin.xml b/bundles/org.eclipse.e4.ui.css.swt/plugin.xml index 9347eddd430..1e73eee3cd4 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/plugin.xml +++ b/bundles/org.eclipse.e4.ui.css.swt/plugin.xml @@ -36,6 +36,13 @@ <!-- these should be obviated by 4.2M6 --> </provider> + + <!-- Theme element definitions --> + <provider + class="org.eclipse.e4.ui.css.swt.dom.definition.ThemeElementDefinitionProvider"> + <widget class="org.eclipse.ui.internal.themes.FontDefinition"/> + <widget class="org.eclipse.ui.internal.themes.ColorDefinition"/> + </provider> </extension> @@ -464,6 +471,21 @@ name="swt-background-mode"> </property-name> </handler> + + + <!-- Theme element definitions --> + <handler + adapter="org.eclipse.e4.ui.css.swt.dom.definition.FontDefinitionElement" + handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyFontDefinitionHandler"> + <property-name name="font-family"/> + <property-name name="font-size"/> + <property-name name="font-style"/> + </handler> + <handler + adapter="org.eclipse.e4.ui.css.swt.dom.definition.ColorDefinitionElement" + handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyColorDefinitionHandler"> + <property-name name="color"/> + </handler> </extension> diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ColorDefinitionElement.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ColorDefinitionElement.java new file mode 100644 index 00000000000..bcc2bd91207 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ColorDefinitionElement.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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.dom.definition; + +import org.eclipse.e4.ui.internal.css.swt.definition.IColorDefinitionOverridable; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; + +public class ColorDefinitionElement extends ThemeDefinitionElement<IColorDefinitionOverridable> { + public ColorDefinitionElement(IColorDefinitionOverridable definition, + CSSEngine engine) { + super(definition, engine); + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/FontDefinitionElement.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/FontDefinitionElement.java new file mode 100644 index 00000000000..9e3008bf65f --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/FontDefinitionElement.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * 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.dom.definition; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; + +import org.eclipse.e4.ui.internal.css.swt.definition.IFontDefinitionOverridable; + +public class FontDefinitionElement extends ThemeDefinitionElement<IFontDefinitionOverridable> { + public FontDefinitionElement(IFontDefinitionOverridable definition, + CSSEngine engine) { + super(definition, engine); + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeDefinitionElement.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeDefinitionElement.java new file mode 100644 index 00000000000..5de7e86872e --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeDefinitionElement.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * 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.dom.definition; + +import static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.escapeId; + +import org.eclipse.e4.ui.css.core.dom.ElementAdapter; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.core.utils.ClassUtils; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemeElementDefinitionOverridable; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class ThemeDefinitionElement<T extends IThemeElementDefinitionOverridable<?>> extends ElementAdapter { + private String localName; + + private String namespaceURI; + + private String id; + + public ThemeDefinitionElement(T definition, CSSEngine engine) { + super(definition, engine); + } + + public NodeList getChildNodes() { + return null; + } + + public String getNamespaceURI() { + if (namespaceURI == null) { + namespaceURI = ClassUtils.getPackageName(getNativeWidget().getClass()); + } + return namespaceURI; + } + + public Node getParentNode() { + return null; + } + + public String getCSSId() { + if (id == null) { + id = escapeId(((IThemeElementDefinitionOverridable<?>) getNativeWidget()).getId()); + } + return id; + } + + public String getCSSClass() { + return null; + } + + public String getCSSStyle() { + return null; + } + + @Override + public String getLocalName() { + if (localName == null) { + localName = ClassUtils.getSimpleName(getNativeWidget().getClass()); + } + return localName; + } + + @Override + public String getAttribute(String attr) { + return null; + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeElementDefinitionProvider.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeElementDefinitionProvider.java new file mode 100644 index 00000000000..2718e1c7659 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemeElementDefinitionProvider.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * 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.dom.definition; + +import org.eclipse.e4.ui.internal.css.swt.definition.IColorDefinitionOverridable; + +import org.eclipse.e4.ui.internal.css.swt.definition.IFontDefinitionOverridable; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.w3c.dom.Element; +import org.eclipse.e4.ui.css.core.dom.IElementProvider; + +public class ThemeElementDefinitionProvider implements IElementProvider { + public Element getElement(Object element, CSSEngine engine) { + if (element instanceof IFontDefinitionOverridable) { + return new FontDefinitionElement((IFontDefinitionOverridable) element, engine); + } + if (element instanceof IColorDefinitionOverridable) { + return new ColorDefinitionElement((IColorDefinitionOverridable) element, engine); + } + return null; + } + +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/CSSSWTEngineImpl.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/CSSSWTEngineImpl.java index 3f910c8d66a..c3744f5b793 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/CSSSWTEngineImpl.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/engine/CSSSWTEngineImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 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 @@ -48,8 +48,10 @@ public class CSSSWTEngineImpl extends AbstractCSSSWTEngineImpl { @Override protected void hookNativeWidget(Object widget) { - Widget swtWidget = (Widget) widget; - swtWidget.addDisposeListener(disposeListener); + if (widget instanceof Widget) { + Widget swtWidget = (Widget) widget; + swtWidget.addDisposeListener(disposeListener); + } } @Override 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 868af2fa195..d02907886b3 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,10 +13,12 @@ *******************************************************************************/ 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 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; @@ -32,9 +34,11 @@ 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 Field[] cachedFields; /*--------------- SWT Color Helper -----------------*/ @@ -57,8 +61,10 @@ public class CSSSWTColorHelper { private static RGB getRGB(CSSPrimitiveValue value, Display display) { RGB rgb = getRGB(value); if (rgb == null && display != null) { - String name = value.getStringValue(); - if (name.contains("-")) { + String name = value.getStringValue(); + if (name.startsWith(COLOR_DEFINITION_MARKER)) { + rgb = findColorByDefinition(name); + } else if (name.contains("-")) { name = name.replace('-', '_'); rgb = process(display, name); } @@ -283,4 +289,12 @@ 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) { + return provider.getColor(normalizeId(name.substring(1))); + } + return 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 78ba3e4cedc..5c1fe23e87c 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 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 @@ -12,6 +12,8 @@ *******************************************************************************/ 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 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; @@ -26,6 +28,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; /** * CSS SWT Font Helper to : @@ -35,6 +38,9 @@ import org.w3c.dom.css.CSSPrimitiveValue; * </ul> */ public class CSSSWTFontHelper { + public static final String FONT_DEFINITION_MARKER = "#"; + + public static final String VALUE_FROM_FONT_DEFINITION = "default"; /** * Get CSS2FontProperties from Control stored into Data of Control. If @@ -142,30 +148,74 @@ 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); + + if (fontDefinitionAsFamily) { + fontDataByDefinition = findFontDataByDefinition(cssFontFamily); + if (fontDataByDefinition.length > 0) { + newFontData.setName(fontDataByDefinition[0].getName()); + } + } 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 - int style = getSWTStyle(fontProperties, oldFontData); - newFontData.setStyle(style); + CSSPrimitiveValue cssFontStyle = fontProperties.getStyle(); + if (fontDefinitionAsFamily && fontDataByDefinition.length > 0 && isValueFromDefinition(cssFontStyle)) { + newFontData.setStyle(fontDataByDefinition[0].getStyle()); + } else { + newFontData.setStyle(getSWTStyle(fontProperties, oldFontData)); + } + + // Height CSSPrimitiveValue cssFontSize = fontProperties.getSize(); - if (cssFontSize != null) { - newFontData.setHeight((int) (cssFontSize) - .getFloatValue(CSSPrimitiveValue.CSS_PT)); - } else { - if (oldFontData != null) - newFontData.setHeight(oldFontData.getHeight()); + boolean fontHeightSet = false; + + if (isValueFromDefinition(cssFontSize)) { + if (fontDefinitionAsFamily && fontDataByDefinition.length > 0) { + newFontData.setHeight(fontDataByDefinition[0].getHeight()); + fontHeightSet = true; + } + } else if (cssFontSize != null) { + newFontData.setHeight((int) (cssFontSize).getFloatValue(CSSPrimitiveValue.CSS_PT)); + fontHeightSet = true; } - // Family - CSSPrimitiveValue cssFontFamily = fontProperties.getFamily(); - if (cssFontFamily != null) - newFontData.setName(cssFontFamily.getStringValue()); - else { - if (oldFontData != null) - newFontData.setName(oldFontData.getName()); + if (!fontHeightSet && oldFontData != null) { + newFontData.setHeight(oldFontData.getHeight()); } + return newFontData; } - + + private static FontData[] findFontDataByDefinition(CSSPrimitiveValue cssFontFamily) { + IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); + FontData[] result = new FontData[0]; + if (provider != null) { + FontData[] fontData = provider.getFont(normalizeId(cssFontFamily.getStringValue().substring(1))); + if (fontData != null) { + result = fontData; + } + } + 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}. * diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/ThemeElementDefinitionHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/ThemeElementDefinitionHelper.java new file mode 100644 index 00000000000..e5127221732 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/ThemeElementDefinitionHelper.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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.helpers; + +public class ThemeElementDefinitionHelper { + public static String escapeId(String id) { + return id.replaceAll("\\.", "-"); + } + + public static String normalizeId(String id) { + return id.replaceAll("-", "."); + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java new file mode 100644 index 00000000000..0a8c648c976 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * 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.properties.definition; + +import org.eclipse.e4.ui.css.core.dom.properties.ICSSPropertyHandler; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.swt.dom.definition.ColorDefinitionElement; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorDefinitionOverridable; +import org.w3c.dom.css.CSSValue; + +public class CSSPropertyColorDefinitionHandler implements ICSSPropertyHandler { + private final static String COLOR_PROP = "color"; + + public boolean applyCSSProperty(Object element, String property, + CSSValue value, String pseudo, CSSEngine engine) throws Exception { + if (element instanceof ColorDefinitionElement && COLOR_PROP.equals(property)) { + IColorDefinitionOverridable definition = + (IColorDefinitionOverridable) ((ColorDefinitionElement) element).getNativeWidget(); + definition.setValue(CSSSWTColorHelper.getRGB(value)); + } + return false; + } + + public String retrieveCSSProperty(Object element, String property, + String pseudo, CSSEngine engine) throws Exception { + return null; + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyFontDefinitionHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyFontDefinitionHandler.java new file mode 100644 index 00000000000..11d46d451bb --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyFontDefinitionHandler.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * 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.properties.definition; + +import org.eclipse.e4.ui.css.core.dom.properties.css2.AbstractCSSPropertyFontHandler; +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.CSSEngine; +import org.eclipse.e4.ui.css.swt.dom.definition.FontDefinitionElement; +import org.eclipse.e4.ui.css.swt.helpers.CSSSWTFontHelper; +import org.eclipse.e4.ui.internal.css.swt.definition.IFontDefinitionOverridable; +import org.eclipse.swt.graphics.FontData; +import org.w3c.dom.css.CSSValue; + +public class CSSPropertyFontDefinitionHandler extends AbstractCSSPropertyFontHandler { + public boolean applyCSSProperty(Object element, String property, + CSSValue value, String pseudo, CSSEngine engine) throws Exception { + if (element instanceof FontDefinitionElement) { + CSS2FontProperties properties = new CSS2FontPropertiesImpl(); + IFontDefinitionOverridable definition = (IFontDefinitionOverridable) ((FontDefinitionElement) element).getNativeWidget(); + + super.applyCSSProperty(properties, property, value, pseudo, engine); + setFontProperties(definition, properties); + } + return false; + } + + public String retrieveCSSProperty(Object element, String property, + String pseudo, CSSEngine engine) throws Exception { + return null; + } + + private void setFontProperties(IFontDefinitionOverridable definition, CSS2FontProperties properties) { + FontData fontData = definition.getValue() != null? definition.getValue()[0]: null; + definition.setValue(new FontData[]{CSSSWTFontHelper.getFontData(properties, fontData)}); + } + + public String retrieveCSSPropertyFontFamily(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontSize(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontAdjust(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontStretch(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontStyle(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontVariant(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } + + public String retrieveCSSPropertyFontWeight(Object element, String pseudo, + CSSEngine engine) throws Exception { + return null; + } +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java index 332eda392cb..5c120884637 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/CSSActivator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 IBM Corporation and others. + * Copyright (c) 2010, 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 @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.e4.ui.internal.css.swt; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; + import org.osgi.framework.Bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -24,6 +26,7 @@ public class CSSActivator implements BundleActivator { private BundleContext context; private ServiceTracker pkgAdminTracker; private ServiceTracker logTracker; + private ServiceTracker colorAndFontProviderTracker; public static CSSActivator getDefault() { return activator; @@ -79,6 +82,10 @@ public class CSSActivator implements BundleActivator { logTracker.close(); logTracker = null; } + if (colorAndFontProviderTracker != null) { + colorAndFontProviderTracker.close(); + colorAndFontProviderTracker = null; + } context = null; } @@ -100,5 +107,16 @@ public class CSSActivator implements BundleActivator { } } + public IColorAndFontProvider getColorAndFontProvider() { + if (colorAndFontProviderTracker == null) { + if (context == null) { + return null; + } + colorAndFontProviderTracker = new ServiceTracker(context, + IColorAndFontProvider.class.getName(), null); + colorAndFontProviderTracker.open(); + } + return (IColorAndFontProvider) colorAndFontProviderTracker.getService(); + } } diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorAndFontProvider.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorAndFontProvider.java new file mode 100644 index 00000000000..32e7d8923a1 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorAndFontProvider.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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.internal.css.swt.definition; + +import org.eclipse.swt.graphics.RGB; + +import org.eclipse.swt.graphics.FontData; + +public interface IColorAndFontProvider { + FontData[] getFont(String symbolicName); + + RGB getColor(String symbolicName); +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorDefinitionOverridable.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorDefinitionOverridable.java new file mode 100644 index 00000000000..bcd74c18191 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IColorDefinitionOverridable.java @@ -0,0 +1,16 @@ +/******************************************************************************* + * 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.internal.css.swt.definition; + +import org.eclipse.swt.graphics.RGB; + +public interface IColorDefinitionOverridable extends IThemeElementDefinitionOverridable<RGB>{ +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IFontDefinitionOverridable.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IFontDefinitionOverridable.java new file mode 100644 index 00000000000..ece81f1239a --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IFontDefinitionOverridable.java @@ -0,0 +1,16 @@ +/******************************************************************************* + * 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.internal.css.swt.definition; + +import org.eclipse.swt.graphics.FontData; + +public interface IFontDefinitionOverridable extends IThemeElementDefinitionOverridable<FontData[]> { +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemeElementDefinitionOverridable.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemeElementDefinitionOverridable.java new file mode 100644 index 00000000000..e2212d6d462 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemeElementDefinitionOverridable.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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.internal.css.swt.definition; + +public interface IThemeElementDefinitionOverridable<T> { + String getId(); + + void setValue(T data); + + T getValue(); + + boolean isOverridden(); +} diff --git a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java index 692acfac00f..b6207386abe 100644 --- a/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java +++ b/bundles/org.eclipse.e4.ui.workbench.swt/src/org/eclipse/e4/ui/internal/workbench/swt/PartRenderingEngine.java @@ -1253,7 +1253,7 @@ public class PartRenderingEngine implements IPresentationEngine { } public void style(Object widget) { - engine.applyStyles((Widget) widget, true); + engine.applyStyles(widget, true); } public CSSStyleDeclaration getStyle(Object widget) { @@ -1354,5 +1354,9 @@ public class PartRenderingEngine implements IPresentationEngine { CSSRenderingUtils.class, appContext); appContext.set(CSSRenderingUtils.class, cssUtils); + IEventBroker broker = appContext.get(IEventBroker.class); + if (broker != null) { + broker.send(UIEvents.UILifeCycle.THEME_CHANGED, null); + } } } 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 123752b1061..774cb0e44ac 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 @@ -259,6 +259,11 @@ public class UIEvents { * Sent when application startup is complete */ public static final String APP_STARTUP_COMPLETE = TOPIC + TOPIC_SEP + "appStartupComplete"; //$NON-NLS-1$ + + /** + * Sent when the theme is changed + */ + public static final String THEME_CHANGED = TOPIC + TOPIC_SEP + "themeChanged"; //$NON-NLS-1$ } /** diff --git a/bundles/org.eclipse.ui.workbench/.project b/bundles/org.eclipse.ui.workbench/.project index 996239d270c..730decf1197 100644 --- a/bundles/org.eclipse.ui.workbench/.project +++ b/bundles/org.eclipse.ui.workbench/.project @@ -25,6 +25,11 @@ <arguments> </arguments> </buildCommand> + <buildCommand> + <name>org.eclipse.pde.ds.core.builder</name> + <arguments> + </arguments> + </buildCommand> </buildSpec> <natures> <nature>org.eclipse.jdt.core.javanature</nature> diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorAndFontProviderImpl.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorAndFontProviderImpl.java new file mode 100644 index 00000000000..aa6ebaa347c --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorAndFontProviderImpl.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * 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.ui.internal.themes; + +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.ui.internal.Workbench; +import org.eclipse.ui.themes.ITheme; + +/** + * @since 3.5 + * + */ +public class ColorAndFontProviderImpl implements IColorAndFontProvider { + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider#getFont + * (java.lang.String) + */ + public FontData[] getFont(String symbolicName) { + return getCurrentTheme().getFontRegistry().getFontData(symbolicName); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider#getColor + * (java.lang.String) + */ + public RGB getColor(String symbolicName) { + return getCurrentTheme().getColorRegistry().getRGB(symbolicName); + } + + private ITheme getCurrentTheme() { + return Workbench.getInstance().getThemeManager().getCurrentTheme(); + } +} diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorDefinition.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorDefinition.java index 20e24f7c082..ba7d7657ca8 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorDefinition.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ColorDefinition.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2007 IBM Corporation and others. + * Copyright (c) 2003, 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 @@ -10,7 +10,9 @@ *******************************************************************************/ package org.eclipse.ui.internal.themes; +import java.util.ResourceBundle; import org.eclipse.core.runtime.IStatus; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorDefinitionOverridable; import org.eclipse.jface.resource.DataFormatException; import org.eclipse.swt.graphics.RGB; import org.eclipse.ui.IPluginContribution; @@ -26,8 +28,11 @@ import org.eclipse.ui.themes.ColorUtil; */ public class ColorDefinition implements IPluginContribution, IHierarchalThemeElementDefinition, ICategorizedThemeElementDefinition, - IEditable { + IEditable, IColorDefinitionOverridable { + private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Theme.class + .getName()); + /** * Default color value - black - for colors that cannot be parsed. */ @@ -51,6 +56,8 @@ public class ColorDefinition implements IPluginContribution, private RGB parsedValue; + private boolean overridden; + /** * Create a new instance of the receiver. * @@ -197,4 +204,30 @@ public class ColorDefinition implements IPluginContribution, public int hashCode() { return id.hashCode(); } + + /* + * (non-Javadoc) + * + * @see org.eclipse.e4.ui.internal.css.swt.definition. + * IThemeElementDefinitionOverridable#setValue(java.lang.Object) + */ + public void setValue(RGB data) { + if (data != null) { + parsedValue = data; + if (!isOverridden()) { + description += ' ' + RESOURCE_BUNDLE.getString("Overridden.by.css.label"); //$NON-NLS-1$ + overridden = true; + } + } + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.e4.ui.internal.css.swt.definition. + * IThemeElementDefinitionOverridable#isOverriden() + */ + public boolean isOverridden() { + return overridden; + } } 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 928d9a133d6..f8ad8d1d4a2 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 @@ -1851,11 +1851,11 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage if (fontDefinition != null) { boolean isDefault = isDefault(fontDefinition); boolean hasDefault = fontDefinition.getDefaultsTo() != null; - fontChangeButton.setEnabled(true); - fontSystemButton.setEnabled(true); - fontResetButton.setEnabled(!isDefault); - editDefaultButton.setEnabled(hasDefault && isDefault); - goToDefaultButton.setEnabled(hasDefault); + fontChangeButton.setEnabled(!fontDefinition.isOverridden()); + fontSystemButton.setEnabled(!fontDefinition.isOverridden()); + fontResetButton.setEnabled(!isDefault && !fontDefinition.isOverridden()); + editDefaultButton.setEnabled(hasDefault && isDefault && !fontDefinition.isOverridden()); + goToDefaultButton.setEnabled(hasDefault && !fontDefinition.isOverridden()); setCurrentFont(fontDefinition); return; } @@ -1863,11 +1863,12 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage if (colorDefinition != null) { boolean isDefault = isDefault(getSelectedColorDefinition()); boolean hasDefault = colorDefinition.getDefaultsTo() != null; - fontChangeButton.setEnabled(true); + fontChangeButton.setEnabled(!colorDefinition.isOverridden()); fontSystemButton.setEnabled(false); - fontResetButton.setEnabled(!isDefault); - editDefaultButton.setEnabled(hasDefault && isDefault); - goToDefaultButton.setEnabled(hasDefault); + fontResetButton.setEnabled(!isDefault && !colorDefinition.isOverridden()); + editDefaultButton + .setEnabled(hasDefault && isDefault && !colorDefinition.isOverridden()); + goToDefaultButton.setEnabled(hasDefault && !colorDefinition.isOverridden()); setCurrentColor(colorDefinition); return; } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/FontDefinition.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/FontDefinition.java index b0b1700697c..e2028c061e3 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/FontDefinition.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/FontDefinition.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 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 @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.ui.internal.themes; +import java.util.ResourceBundle; +import org.eclipse.e4.ui.internal.css.swt.definition.IFontDefinitionOverridable; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.StringConverter; import org.eclipse.swt.graphics.FontData; @@ -20,7 +22,9 @@ import org.eclipse.ui.PlatformUI; * from the plugin.xml of a type. */ public class FontDefinition implements IHierarchalThemeElementDefinition, - ICategorizedThemeElementDefinition, IEditable { + ICategorizedThemeElementDefinition, IEditable, IFontDefinitionOverridable { + private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Theme.class + .getName()); private String label; @@ -36,6 +40,8 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, private boolean isEditable; + private boolean overridden; + private FontData[] parsedValue; /** @@ -157,5 +163,33 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, */ public int hashCode() { return id.hashCode(); - } + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.e4.ui.css.swt.definition.IDefinitionOverridable#setData(java + * .lang.Object) + */ + public void setValue(FontData[] data) { + if (data != null && data.length > 0) { + value = data[0].getName(); + parsedValue = data; + if (!isOverridden()) { + description += ' ' + RESOURCE_BUNDLE.getString("Overridden.by.css.label"); //$NON-NLS-1$ + overridden = true; + } + } + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.e4.ui.css.swt.definition.IDefinitionOverridable#isOverriden() + */ + public boolean isOverridden() { + return overridden; + } } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/Theme.properties b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/Theme.properties index c4b1986489e..dafde16a73a 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/Theme.properties +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/Theme.properties @@ -9,3 +9,4 @@ # IBM Corporation - initial API and implementation ############################################################################### DefaultTheme.label=Default +Overridden.by.css.label=(overridden by CSS) 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 bf05bed5521..b699b3d6383 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2011 IBM Corporation and others. + * Copyright (c) 2004, 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 @@ -15,6 +15,10 @@ import java.util.Iterator; import java.util.Map; 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.services.IStylingEngine; +import org.eclipse.e4.ui.workbench.UIEvents; import org.eclipse.jface.resource.ColorRegistry; import org.eclipse.jface.resource.FontRegistry; import org.eclipse.jface.resource.JFaceResources; @@ -28,12 +32,14 @@ import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.ui.IWorkbenchPreferenceConstants; import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.internal.Workbench; import org.eclipse.ui.internal.WorkbenchPlugin; import org.eclipse.ui.internal.misc.StatusUtil; import org.eclipse.ui.internal.util.PrefUtil; import org.eclipse.ui.statushandlers.StatusManager; import org.eclipse.ui.themes.ITheme; import org.eclipse.ui.themes.IThemeManager; +import org.osgi.service.event.EventHandler; /** * Theme manager for the Workbench. @@ -47,6 +53,10 @@ public class WorkbenchThemeManager extends EventManager implements private static WorkbenchThemeManager instance; + private IEclipseContext context; + + private IEventBroker eventBroker; + /** * Returns the singelton instance of the WorkbenchThemeManager * @@ -102,6 +112,29 @@ public class WorkbenchThemeManager extends EventManager implements private Map themes = new HashMap(7); + private EventHandler themeChangedHandler = new EventHandler() { + public void handleEvent(org.osgi.service.event.Event event) { + IStylingEngine engine = (IStylingEngine) context.get(IStylingEngine.SERVICE_NAME); + IThemeRegistry themeRegistry = (IThemeRegistry) context.get(IThemeRegistry.class + .getName()); + FontRegistry fontRegistry = getCurrentTheme().getFontRegistry(); + ColorRegistry colorRegistry = getCurrentTheme().getColorRegistry(); + + for (FontDefinition fontDefinition : themeRegistry.getFonts()) { + engine.style(fontDefinition); + if (fontDefinition.isOverridden()) { + fontRegistry.put(fontDefinition.getId(), fontDefinition.getValue()); + } + } + for (ColorDefinition colorDefinition : themeRegistry.getColors()) { + engine.style(colorDefinition); + if (colorDefinition.isOverridden()) { + colorRegistry.put(colorDefinition.getId(), colorDefinition.getValue()); + } + } + } + }; + /* * Initialize the WorkbenchThemeManager. * Determine the default theme according to the following rules: @@ -146,6 +179,12 @@ public class WorkbenchThemeManager extends EventManager implements PrefUtil.getAPIPreferenceStore().setDefault( IWorkbenchPreferenceConstants.CURRENT_THEME_ID, themeId); + + context = (IEclipseContext) Workbench.getInstance().getService(IEclipseContext.class); + eventBroker = (IEventBroker) Workbench.getInstance().getService(IEventBroker.class); + if (eventBroker != null) { + eventBroker.subscribe(UIEvents.UILifeCycle.THEME_CHANGED, themeChangedHandler); + } } /* @@ -188,6 +227,10 @@ public class WorkbenchThemeManager extends EventManager implements * Disposes all ThemeEntries. */ public void dispose() { + if (eventBroker != null) { + eventBroker.unsubscribe(themeChangedHandler); + } + for (Iterator i = themes.values().iterator(); i.hasNext();) { ITheme theme = (ITheme) i.next(); theme.removePropertyChangeListener(currentThemeListener); @@ -358,6 +401,11 @@ public class WorkbenchThemeManager extends EventManager implements jfaceFonts.put(key, themeFonts.getFontData(key)); } } + { + if (oldTheme != null && eventBroker != null) { + eventBroker.send(UIEvents.UILifeCycle.THEME_CHANGED, null); + } + } } } } diff --git a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF index 11fc0cc0d90..e5715df4f09 100644 --- a/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF @@ -132,3 +132,4 @@ Import-Package: com.ibm.icu.text, org.w3c.dom.css;version="2.0.0", org.xml.sax Bundle-RequiredExecutionEnvironment: J2SE-1.5 +Service-Component: OSGI-INF/ColorAndFontProvider.xml diff --git a/bundles/org.eclipse.ui.workbench/OSGI-INF/ColorAndFontProvider.xml b/bundles/org.eclipse.ui.workbench/OSGI-INF/ColorAndFontProvider.xml new file mode 100644 index 00000000000..ed85fee491c --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/OSGI-INF/ColorAndFontProvider.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.ui.workbench"> + <implementation class="org.eclipse.ui.internal.themes.ColorAndFontProviderImpl"/> + <service> + <provide interface="org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider"/> + </service> +</scr:component> diff --git a/bundles/org.eclipse.ui.workbench/build.properties b/bundles/org.eclipse.ui.workbench/build.properties index 160c99e19dd..4494d8619ed 100644 --- a/bundles/org.eclipse.ui.workbench/build.properties +++ b/bundles/org.eclipse.ui.workbench/build.properties @@ -1,20 +1,11 @@ -############################################################################### -# Copyright (c) 2000, 2012 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 -############################################################################### bin.includes = plugin.properties,\ plugin.xml,\ about.html,\ .,\ META-INF/,\ .options,\ - LegacyIDE.e4xmi + LegacyIDE.e4xmi,\ + OSGI-INF/ColorAndFontProvider.xml src.includes = about.html source.. = Eclipse UI/,\ - Eclipse UI Editor Support/ + Eclipse UI Editor Support/
\ No newline at end of file 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 dd288b8ddd7..0fdb6aa4e66 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 @@ -8,7 +8,8 @@ Require-Bundle: org.junit, org.eclipse.e4.ui.css.core, org.eclipse.e4.ui.css.swt, org.w3c.css.sac, - org.eclipse.e4.ui.css.swt.theme;bundle-version="0.9.1" + org.eclipse.e4.ui.css.swt.theme;bundle-version="0.9.1", + org.eclipse.ui.workbench;bundle-version="3.105.100" 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/swt/helpers/CSSSWTColorHelperTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.java new file mode 100644 index 00000000000..a32742b4cdb --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelperTest.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.swt.helpers; + +import static org.eclipse.e4.ui.css.swt.helpers.CSSSWTColorHelper.getSWTColor; + +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +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); + + 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); + + assertNull(result); + } + + public void testGetSWTColorWhenInvalidColorValue() throws Exception { + Color result = getSWTColor(colorProperties("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); + + 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 new file mode 100644 index 00000000000..a0a65ceedfc --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTFontHelperTest.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * 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.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; +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), + 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), + 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), + 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), + 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()); + + assertEquals("Arial", result.getName()); + assertEquals(10, result.getHeight()); + assertEquals(SWT.NORMAL, 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()); + assertEquals(15, result.getHeight()); + assertEquals(SWT.NORMAL, 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), + new FontData()); + + 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 new file mode 100644 index 00000000000..5b24f19a815 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTHelperTestCase.java @@ -0,0 +1,137 @@ +/******************************************************************************* + * 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.helpers; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import junit.framework.TestCase; + +import org.eclipse.e4.ui.css.core.dom.properties.css2.CSS2FontProperties; +import org.eclipse.e4.ui.css.core.impl.dom.CSSValueImpl; +import org.eclipse.e4.ui.internal.css.swt.CSSActivator; +import org.eclipse.e4.ui.internal.css.swt.definition.IColorAndFontProvider; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.RGB; +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; + } + }); + } + + 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; + } + }); + } + + private void registerProvider(final IColorAndFontProvider provider) throws Exception { + new CSSActivator() { + @Override + public IColorAndFontProvider getColorAndFontProvider() { + return provider; + }; + }.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); + } + 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()); + } + }; + } + return null; + } + }); + } + + protected CSSValueImpl colorProperties(final String value) { + return colorProperties(value, CSSValue.CSS_PRIMITIVE_VALUE); + } + + 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 String fontDefinition(String fontDefinitionId) { + return FONT_DEFINITION_MARKER + fontDefinitionId; + } + + protected String colorDefinition(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/ColorDefinitionTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java new file mode 100644 index 00000000000..dbbe7d0acb1 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ColorDefinitionTest.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * 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.tests.css.swt; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +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.FontData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.internal.themes.ColorDefinition; + +@SuppressWarnings("restriction") +public class ColorDefinitionTest extends CSSSWTTestCase { + private Display display; + + @Override + protected void setUp() throws Exception { + display = Display.getDefault(); + } + + public void testColorDefinition() throws Exception { + //given + CSSEngine engine = createEngine("ColorDefinition#ACTIVE_HYPERLINK_COLOR{color: green}", display); + ColorDefinition definition = colorDefinition("ACTIVE_HYPERLINK_COLOR"); + + assertEquals(new RGB(0, 0, 0), definition.getValue()); + assertFalse(definition.isOverridden()); + + //when + engine.applyStyles(definition, true); + + //then + assertEquals(new RGB(0, 128, 0), definition.getValue()); + assertTrue(definition.isOverridden()); + } + + public void testColorDefinitionWhenDefinitionStylesheetNotFound() throws Exception{ + //given + CSSEngine engine = createEngine("ColorDefinition#ACTIVE_HYPERLINK_COLOR{color: green}", display); + ColorDefinition definition = colorDefinition("color definition uniqueId without matching stylesheet"); + + assertEquals(new RGB(0, 0, 0), definition.getValue()); + assertFalse(definition.isOverridden()); + + //when + engine.applyStyles(definition, true); + + //then + assertEquals(new RGB(0, 0, 0), definition.getValue()); + assertFalse(definition.isOverridden()); + } + + public void testWidgetWithColorDefinitionAsBackgroundColor() throws Exception { + //given + registerColorProviderWith("ACTIVE_HYPERLINK_COLOR", new RGB(255, 0, 0)); + + CSSEngine engine = createEngine("Label {background-color: '#ACTIVE_HYPERLINK_COLOR'}", display); + + Shell shell = new Shell(display, SWT.SHELL_TRIM); + Label label = new Label(shell, SWT.NONE); + label.setText("Some label text"); + + + //when + engine.applyStyles(label, true); + + + //then + assertEquals(new RGB(255, 0, 0), label.getBackground().getRGB()); + + shell.dispose(); + } + + private ColorDefinition colorDefinition(String uniqueId) { + return new ColorDefinition("label", uniqueId, "defaultsTo", "black", "categoryId", true, + "description", "pluginId"); + } + + private void registerColorProviderWith(final String expectedSymbolicName, final RGB expectedRgb) throws Exception { + new CSSActivator() { + @Override + public IColorAndFontProvider getColorAndFontProvider() { + return new IColorAndFontProvider() { + public FontData[] getFont(String symbolicName) { + return null; + } + public RGB getColor(String symbolicName) { + if (expectedSymbolicName.equals(symbolicName)) { + return expectedRgb; + } + return null; + } + }; + }; + }.start(null); + } +} 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 1dec2b991e2..9c88bcacce7 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 IBM Corporation and others. All rights reserved. This + * Copyright (c) 2008, 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 @@ -9,6 +9,9 @@ *******************************************************************************/ 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; @@ -24,6 +27,10 @@ public class CssSwtTestSuite extends TestSuite { * Construct the test suite. */ public CssSwtTestSuite() { + addTestSuite(CSSSWTFontHelperTest.class); + addTestSuite(CSSSWTColorHelperTest.class); + addTestSuite(FontDefinitionTest.class); + addTestSuite(ColorDefinitionTest.class); addTestSuite(CSSSWTWidgetTest.class); addTestSuite(LabelTest.class); addTestSuite(CTabFolderTest.class); @@ -46,6 +53,5 @@ public class CssSwtTestSuite extends TestSuite { 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 new file mode 100644 index 00000000000..80c38d89b44 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/FontDefinitionTest.java @@ -0,0 +1,120 @@ +/******************************************************************************* + * 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.tests.css.swt; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +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.Font; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +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"); + + assertNull(definition.getValue()); + assertFalse(definition.isOverridden()); + + //when + engine.applyStyles(definition, true); + + //then + assertNotNull(definition.getValue()); + assertEquals("Times", definition.getValue()[0].getName()); + assertEquals(12, definition.getValue()[0].getHeight()); + 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"); + + 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}", + display); + + Shell shell = new Shell(display, SWT.SHELL_TRIM); + 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); + + + //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"), + 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) { + if (expectedSymbolicName.equals(symbolicName)) { + return new FontData[]{fontData}; + } + return null; + } + public RGB getColor(String symbolicName) { + return null; + } + }; + }; + }.start(null); + } +} |
