diff options
| author | Daniel Rolka | 2013-12-14 01:51:59 +0000 |
|---|---|---|
| committer | Paul Elder | 2014-01-14 14:38:31 +0000 |
| commit | a65bccad7395914a88571f7a889e844db51e969c (patch) | |
| tree | 7175c9c115c84faded33705a3cd2cb2e2afe49de | |
| parent | 946c6d54de1d199bb882124673f4ede122a00f89 (diff) | |
| download | eclipse.platform.ui-a65bccad7395914a88571f7a889e844db51e969c.tar.gz eclipse.platform.ui-a65bccad7395914a88571f7a889e844db51e969c.tar.xz eclipse.platform.ui-a65bccad7395914a88571f7a889e844db51e969c.zip | |
Bug 419018 - [CSS] Provide the CSS support for defining the new font and
color definitions
Change-Id: I7ecb1b7ecc48092e4b3d270f9f1296fb3e737942
Signed-off-by: Daniel Rolka <daniel.rolka@pl.ibm.com>
25 files changed, 1050 insertions, 244 deletions
diff --git a/bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme/ThemeEngine.java b/bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme/ThemeEngine.java index b4fddda09f6..55abf11182d 100644 --- a/bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme/ThemeEngine.java +++ b/bundles/org.eclipse.e4.ui.css.swt.theme/src/org/eclipse/e4/ui/css/swt/internal/theme/ThemeEngine.java @@ -441,10 +441,6 @@ public class ThemeEngine implements IThemeEngine { e.printStackTrace(); } } - - for (CSSEngine engine : cssEngines) { - engine.reapply(); - } } if (restore) { @@ -458,6 +454,10 @@ public class ThemeEngine implements IThemeEngine { } } sendThemeChangeEvent(restore); + + for (CSSEngine engine : cssEngines) { + engine.reapply(); + } } /** diff --git a/bundles/org.eclipse.e4.ui.css.swt/plugin.xml b/bundles/org.eclipse.e4.ui.css.swt/plugin.xml index 1e73eee3cd4..5653f7398c5 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/plugin.xml +++ b/bundles/org.eclipse.e4.ui.css.swt/plugin.xml @@ -42,6 +42,7 @@ 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"/> + <widget class="org.eclipse.ui.internal.themes.ThemesExtension"/> </provider> </extension> @@ -473,7 +474,14 @@ </handler> - <!-- Theme element definitions --> + <!-- Theme element definitions --> + <handler + adapter="org.eclipse.e4.ui.css.swt.dom.definition.ThemeDefinitionElement" + handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyThemeElementDefinitionHandler"> + <property-name name="category"/> + <property-name name="label"/> + <property-name name="description"/> + </handler> <handler adapter="org.eclipse.e4.ui.css.swt.dom.definition.FontDefinitionElement" handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyFontDefinitionHandler"> @@ -486,6 +494,12 @@ handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyColorDefinitionHandler"> <property-name name="color"/> </handler> + <handler + adapter="org.eclipse.e4.ui.css.swt.dom.definition.ThemesExtensionElement" + handler="org.eclipse.e4.ui.css.swt.properties.definition.CSSPropertyThemesExtensionHandler"> + <property-name name="font-definition"/> + <property-name name="color-definition"/> + </handler> </extension> 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 index 2718e1c7659..92201b8eda0 100644 --- 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 @@ -10,12 +10,12 @@ *******************************************************************************/ package org.eclipse.e4.ui.css.swt.dom.definition; +import org.eclipse.e4.ui.css.core.dom.IElementProvider; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; 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.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension; 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) { @@ -25,6 +25,9 @@ public class ThemeElementDefinitionProvider implements IElementProvider { if (element instanceof IColorDefinitionOverridable) { return new ColorDefinitionElement((IColorDefinitionOverridable) element, engine); } + if (element instanceof IThemesExtension) { + return new ThemesExtensionElement((IThemesExtension) element, engine); + } return null; } diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemesExtensionElement.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemesExtensionElement.java new file mode 100644 index 00000000000..287ba4d270c --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/dom/definition/ThemesExtensionElement.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * 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.utils.ClassUtils; + +import org.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.eclipse.e4.ui.css.core.dom.ElementAdapter; + +public class ThemesExtensionElement extends ElementAdapter { + private String localName; + + private String namespaceURI; + + public ThemesExtensionElement(IThemesExtension themesExtension, CSSEngine engine) { + super(themesExtension, 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() { + return null; + } + + 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 arg0) { + return null; + } + +} diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyThemeElementDefinitionHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyThemeElementDefinitionHandler.java new file mode 100644 index 00000000000..42587e18df0 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyThemeElementDefinitionHandler.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * 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 static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; +import org.eclipse.e4.ui.css.swt.dom.definition.ThemeDefinitionElement; + +import org.eclipse.e4.ui.css.core.dom.properties.ICSSPropertyHandler; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemeElementDefinitionOverridable; +import org.w3c.dom.css.CSSValue; + +public class CSSPropertyThemeElementDefinitionHandler implements ICSSPropertyHandler { + private final static String CATEGORY_PROP = "category"; + + private final static String LABEL_PROP = "label"; + + private final static String DESCRIPTION_PROP = "description"; + + public boolean applyCSSProperty(Object element, String property, + CSSValue value, String pseudo, CSSEngine engine) throws Exception { + if (!(element instanceof ThemeDefinitionElement<?>)) { + return false; + } + + IThemeElementDefinitionOverridable<?> definition = + (IThemeElementDefinitionOverridable<?>) ((ThemeDefinitionElement<?>) element) + .getNativeWidget(); + + if (CATEGORY_PROP.equals(property)) { + definition.setCategoryId(normalizeId(value.getCssText().substring(1))); + } else if (LABEL_PROP.equals(property)) { + definition.setName(value.getCssText()); + } else if (DESCRIPTION_PROP.equals(property)) { + definition.setDescription(value.getCssText()); + } + + return true; + } + + 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/CSSPropertyThemesExtensionHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyThemesExtensionHandler.java new file mode 100644 index 00000000000..51ba9aae806 --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyThemesExtensionHandler.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * 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 static org.eclipse.e4.ui.css.swt.helpers.ThemeElementDefinitionHelper.normalizeId; + +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.ThemesExtensionElement; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension; +import org.w3c.dom.css.CSSValue; + +public class CSSPropertyThemesExtensionHandler implements ICSSPropertyHandler { + private final static String DEFINITION_LIST_SEPARATOR = ","; + + private final static String FONT_DEFINITION_PROP = "font-definition"; + + private final static String COLOR_DEFINITION_PROP = "color-definition"; + + public boolean applyCSSProperty(Object element, String property, + CSSValue value, String pseudo, CSSEngine engine) throws Exception { + if (element instanceof ThemesExtensionElement) { + IThemesExtension themeExtension = (IThemesExtension) ((ThemesExtensionElement) element).getNativeWidget(); + if (FONT_DEFINITION_PROP.equals(property)) { + addDefinitions(themeExtension, true, parseSymbolicNames(value.getCssText())); + } else if (COLOR_DEFINITION_PROP.equals(property)) { + addDefinitions(themeExtension, false, parseSymbolicNames(value.getCssText())); + } + } + return true; + } + + public String retrieveCSSProperty(Object element, String property, + String pseudo, CSSEngine engine) throws Exception { + return null; + } + + private String[] parseSymbolicNames(String symbolicNames) { + return symbolicNames.split(DEFINITION_LIST_SEPARATOR); + } + + private void addDefinitions(IThemesExtension themeExtension, boolean fontDefinitions, String... symbolicNames) { + for (String symbolicName: symbolicNames) { + String normalizedSymbolicName = normalizeId(symbolicName.trim().substring(1)); + if (fontDefinitions) { + themeExtension.addFontDefinition(normalizedSymbolicName); + } else { + themeExtension.addColorDefinition(normalizedSymbolicName); + } + } + } +} 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 index e2212d6d462..a9d5b4bcaf1 100644 --- 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 @@ -18,4 +18,10 @@ public interface IThemeElementDefinitionOverridable<T> { T getValue(); boolean isOverridden(); + + void setCategoryId(String categoryId); + + void setName(String name); + + void setDescription(String description); } diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemesExtension.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemesExtension.java new file mode 100644 index 00000000000..31bf282045e --- /dev/null +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/internal/css/swt/definition/IThemesExtension.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * 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 IThemesExtension { + void addFontDefinition(String symbolicName); + + void addColorDefinition(String symbolicName); +} diff --git a/bundles/org.eclipse.ui.workbench/.settings/.api_filters b/bundles/org.eclipse.ui.workbench/.settings/.api_filters new file mode 100644 index 00000000000..237524dc658 --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/.settings/.api_filters @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?><component id="org.eclipse.ui.workbench" version="2"> + <resource path="Eclipse UI/org/eclipse/ui/internal/themes/WorkbenchThemeManager.java" type="org.eclipse.ui.internal.themes.WorkbenchThemeManager"> + <filter id="640712815"> + <message_arguments> + <message_argument value="IStylingEngine"/> + <message_argument value="WorkbenchThemeManager"/> + <message_argument value="style(Object)"/> + </message_arguments> + </filter> + </resource> +</component>
\ No newline at end of file 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 ba7d7657ca8..28f71402ecc 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 @@ -10,7 +10,6 @@ *******************************************************************************/ 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; @@ -26,13 +25,9 @@ import org.eclipse.ui.themes.ColorUtil; * * @since 3.0 */ -public class ColorDefinition implements IPluginContribution, - IHierarchalThemeElementDefinition, ICategorizedThemeElementDefinition, - IEditable, IColorDefinitionOverridable { - - private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Theme.class - .getName()); - +public class ColorDefinition extends ThemeElementDefinition implements IPluginContribution, + IHierarchalThemeElementDefinition, ICategorizedThemeElementDefinition, IEditable, + IColorDefinitionOverridable { /** * Default color value - black - for colors that cannot be parsed. */ @@ -40,24 +35,14 @@ public class ColorDefinition implements IPluginContribution, private String defaultsTo; - private String description; - - private String id; - - private String label; - private String pluginId; private String rawValue; - private String categoryId; - boolean isEditable; private RGB parsedValue; - private boolean overridden; - /** * Create a new instance of the receiver. * @@ -74,13 +59,9 @@ public class ColorDefinition implements IPluginContribution, public ColorDefinition(String label, String id, String defaultsTo, String value, String categoryId, boolean isEditable, String description, String pluginId) { - - this.label = label; - this.id = id; + super(id, label, description, categoryId); this.defaultsTo = defaultsTo; this.rawValue = value; - this.categoryId = categoryId; - this.description = description; this.isEditable = isEditable; this.pluginId = pluginId; } @@ -94,52 +75,20 @@ public class ColorDefinition implements IPluginContribution, * @param value the RGB value */ public ColorDefinition(ColorDefinition original, RGB value) { - - this.label = original.getName(); - this.id = original.getId(); - this.categoryId = original.getCategoryId(); - this.description = original.getDescription(); + super(original.getId(), original.getName(), original.getDescription(), original + .getCategoryId()); this.isEditable = original.isEditable(); this.pluginId = original.getPluginId(); - this.parsedValue = value; } /** - * @return the categoryId, or <code>null</code> if none was supplied. - */ - public String getCategoryId() { - return categoryId; - } - - /** * @return the defaultsTo value, or <code>null</code> if none was supplied. */ public String getDefaultsTo() { return defaultsTo; } - /** - * @return the description text, or <code>null</code> if none was supplied. - */ - public String getDescription() { - return description; - } - - /** - * @return the id of this definition. Should not be <code>null</code>. - */ - public String getId() { - return id; - } - - /** - * @return the label text. Should not be <code>null</code>. - */ - public String getName() { - return label; - } - /* (non-Javadoc) * @see org.eclipse.ui.IPluginContribution#getLocalId() */ @@ -165,13 +114,19 @@ public class ColorDefinition implements IPluginContribution, } catch (DataFormatException e) { parsedValue = DEFAULT_COLOR_VALUE; IStatus status = StatusUtil.newStatus(IStatus.WARNING, - "Could not parse value for theme color " + id, e); //$NON-NLS-1$ + "Could not parse value for theme color " + getId(), e); //$NON-NLS-1$ StatusManager.getManager().handle(status, StatusManager.LOG); } } return parsedValue; } + @Override + public void resetToDefaultValue() { + parsedValue = null; + super.resetToDefaultValue(); + } + /* * (non-Javadoc) * @@ -202,7 +157,7 @@ public class ColorDefinition implements IPluginContribution, * @see java.lang.Object#hashCode() */ public int hashCode() { - return id.hashCode(); + return getId().hashCode(); } /* @@ -214,20 +169,7 @@ public class ColorDefinition implements IPluginContribution, 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; - } + setOverridden(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 305c54a6734..0d2ff3fd102 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 @@ -367,8 +367,15 @@ public final class ColorsAndFontsPreferencePage extends PreferencePage private IPropertyChangeListener listener = new IPropertyChangeListener() { public void propertyChange(PropertyChangeEvent event) { - fireLabelProviderChanged(new LabelProviderChangedEvent( - PresentationLabelProvider.this)); + if (event.getNewValue() != null) { + fireLabelProviderChanged(new LabelProviderChangedEvent( + PresentationLabelProvider.this)); + } else { + // Some theme definition element has been modified and we + // need to refresh the viewer + tree.getViewer().setContentProvider(new ThemeContentProvider()); + tree.getViewer().collapseAll(); + } } }; 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 e2028c061e3..bd9b3007d46 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 @@ -10,7 +10,6 @@ *******************************************************************************/ 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; @@ -21,26 +20,17 @@ import org.eclipse.ui.PlatformUI; * The FontDefiniton is the representation of the fontDefinition * from the plugin.xml of a type. */ -public class FontDefinition implements IHierarchalThemeElementDefinition, - ICategorizedThemeElementDefinition, IEditable, IFontDefinitionOverridable { - private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(Theme.class - .getName()); +public class FontDefinition extends ThemeElementDefinition implements + IHierarchalThemeElementDefinition, ICategorizedThemeElementDefinition, IEditable, + IFontDefinitionOverridable { - private String label; - - private String id; - - private String defaultsTo; - - private String categoryId; - - private String description; + private String defaultsTo; private String value; - private boolean isEditable; + private String defaultValue; - private boolean overridden; + private boolean isEditable; private FontData[] parsedValue; @@ -56,12 +46,9 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, public FontDefinition(String fontName, String uniqueId, String defaultsId, String value, String categoryId, boolean isEditable, String fontDescription) { - this.label = fontName; - this.id = uniqueId; + super(uniqueId, fontName, fontDescription, categoryId); this.defaultsTo = defaultsId; this.value = value; - this.categoryId = categoryId; - this.description = fontDescription; this.isEditable = isEditable; } @@ -74,10 +61,8 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, * @param datas the FontData[] value */ public FontDefinition(FontDefinition originalFont, FontData[] datas) { - this.label = originalFont.getName(); - this.id = originalFont.getId(); - this.categoryId = originalFont.getCategoryId(); - this.description = originalFont.getDescription(); + super(originalFont.getId(), originalFont.getName(), originalFont.getDescription(), + originalFont.getCategoryId()); this.isEditable = originalFont.isEditable(); this.parsedValue = datas; } @@ -92,38 +77,6 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, } /** - * Returns the description. - * @return String or <pre>null</pre>. - */ - public String getDescription() { - return description; - } - - /** - * Returns the label. - * @return String - */ - public String getName() { - return label; - } - - /** - * Returns the id. - * @return String - */ - public String getId() { - return id; - } - - /** - * Returns the categoryId. - * @return String - */ - public String getCategoryId() { - return categoryId; - } - - /** * Returns the value. * * @return FontData [] @@ -141,6 +94,13 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, return parsedValue; } + @Override + public void resetToDefaultValue() { + value = defaultValue; + parsedValue = null; + super.resetToDefaultValue(); + } + /* (non-Javadoc) * @see org.eclipse.ui.internal.themes.IEditable#isEditable() */ @@ -162,7 +122,7 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, * @see java.lang.Object#hashCode() */ public int hashCode() { - return id.hashCode(); + return getId().hashCode(); } /* @@ -174,22 +134,12 @@ public class FontDefinition implements IHierarchalThemeElementDefinition, */ public void setValue(FontData[] data) { if (data != null && data.length > 0) { + if (defaultValue == null) { + defaultValue = value; + } value = data[0].getName(); parsedValue = data; - if (!isOverridden()) { - description += ' ' + RESOURCE_BUNDLE.getString("Overridden.by.css.label"); //$NON-NLS-1$ - overridden = true; - } + setOverridden(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 dafde16a73a..4e9438124ba 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 @@ -10,3 +10,4 @@ ############################################################################### DefaultTheme.label=Default Overridden.by.css.label=(overridden by CSS) +Added.by.css.desc=The definition has been added by CSS diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemeElementDefinition.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemeElementDefinition.java new file mode 100644 index 00000000000..3b8cf11ae9e --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemeElementDefinition.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * 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 java.util.ResourceBundle; + +/** + * @since 3.5 + * + */ +public class ThemeElementDefinition { + private String id; + + private String label; + + private String description; + + private String categoryId; + + private boolean overridden; + + private boolean addedByCss; + + private String overriddenLabel; + + public ThemeElementDefinition(String id, String label, String description, String categoryId) { + this.id = id; + this.label = label; + this.description = description; + this.categoryId = categoryId; + } + + /** + * @return the id of this definition. Should not be <code>null</code>. + */ + public String getId() { + return id; + } + + /** + * Returns the label. + * + * @return String + */ + public String getName() { + return label; + } + + public void setName(String label) { + this.label = label; + setOverridden(true); + } + + /** + * Returns the description. + * + * @return String or + * + * <pre> + * null + * </pre> + * + * . + */ + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + setOverridden(true); + } + + /** + * Returns the categoryId. + * + * @return String + */ + public String getCategoryId() { + return categoryId; + } + + public void setCategoryId(String categoryId) { + this.categoryId = categoryId; + setOverridden(true); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.e4.ui.css.swt.definition.IDefinitionOverridable#isOverriden() + */ + public boolean isOverridden() { + return overridden; + } + + protected void setOverridden(boolean overridden) { + this.overridden = overridden; + if (isAddedByCss()) { + return; + } + + boolean hasOverriddenLabel = description.endsWith(getOverriddenLabel()); + if (overridden && !hasOverriddenLabel) { + description += ' ' + getOverriddenLabel(); + } else if (!overridden && hasOverriddenLabel) { + description = description.substring(0, description.length() + - getOverriddenLabel().length() - 1); + } + } + + public boolean isAddedByCss() { + return addedByCss; + } + + public void setAddedByCss(boolean addedByCss) { + this.addedByCss = addedByCss; + } + + public String getOverriddenLabel() { + if (overriddenLabel == null) { + ResourceBundle resourceBundle = ResourceBundle.getBundle(Theme.class.getName()); + overriddenLabel = resourceBundle.getString("Overridden.by.css.label"); //$NON-NLS-1$ + } + return overriddenLabel; + } + + public void resetToDefaultValue() { + setOverridden(false); + setAddedByCss(false); + } +} diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemesExtension.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemesExtension.java new file mode 100644 index 00000000000..22637fdbf13 --- /dev/null +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/themes/ThemesExtension.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * 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 java.util.ArrayList; +import java.util.List; +import java.util.ResourceBundle; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemeElementDefinitionOverridable; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension; +import org.eclipse.ui.internal.WorkbenchPlugin; + +/** + * @since 3.5 + * + */ +public class ThemesExtension implements IThemesExtension { + public final static String DEFAULT_CATEGORY_ID = "org.eclipse.ui.themes.CssTheme"; //$NON-NLS-1$ + + private String description; + + private List<IThemeElementDefinitionOverridable<?>> definitions = new ArrayList<IThemeElementDefinitionOverridable<?>>(); + + /* + * (non-Javadoc) + * + * @see org.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension# + * addFontDefinition(java.lang.String) + */ + public void addFontDefinition(String symbolicName) { + FontDefinition definition = new FontDefinition(formatDefaultName(FontDefinition.class, + symbolicName), symbolicName, null, null, DEFAULT_CATEGORY_ID, true, + getDefaultDescription()); + definition.setAddedByCss(true); + definitions.add(definition); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.e4.ui.internal.css.swt.definition.IThemesExtension# + * addColorDefinition(java.lang.String) + */ + public void addColorDefinition(String symbolicName) { + ColorDefinition definition = new ColorDefinition(formatDefaultName(ColorDefinition.class, + symbolicName), symbolicName, null, null, DEFAULT_CATEGORY_ID, true, + getDefaultDescription(), getPluginId()); + definition.setAddedByCss(true); + definitions.add(definition); + } + + private String getPluginId() { + return WorkbenchPlugin.getDefault().getBundle().getSymbolicName(); + } + + public List<IThemeElementDefinitionOverridable<?>> getDefinitions() { + return definitions; + } + + private String formatDefaultName(Class<?> cls, String symbolicName) { + return String.format("%s #%s", cls.getSimpleName(), symbolicName); //$NON-NLS-1$ + } + + public String getDefaultDescription() { + if (description == null) { + ResourceBundle resourceBundle = ResourceBundle.getBundle(Theme.class.getName()); + description = resourceBundle.getString("Added.by.css.desc"); //$NON-NLS-1$ + } + return description; + } +} 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 c2fc15ac8ec..36d74a36f8d 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,8 @@ 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.css.swt.theme.IThemeEngine; +import org.eclipse.e4.ui.internal.css.swt.definition.IThemeElementDefinitionOverridable; import org.eclipse.e4.ui.model.application.MApplication; import org.eclipse.e4.ui.services.IStylingEngine; import org.eclipse.e4.ui.workbench.UIEvents; @@ -113,28 +115,7 @@ 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()); - } - } - } - }; + private EventHandler themeChangedHandler = new WorkbenchThemeChangedHandler(); /* * Initialize the WorkbenchThemeManager. @@ -185,6 +166,7 @@ public class WorkbenchThemeManager extends EventManager implements eventBroker = (IEventBroker) Workbench.getInstance().getService(IEventBroker.class); if (eventBroker != null) { eventBroker.subscribe(UIEvents.UILifeCycle.THEME_CHANGED, themeChangedHandler); + eventBroker.subscribe(IThemeEngine.Events.THEME_CHANGED, themeChangedHandler); } } @@ -411,4 +393,105 @@ public class WorkbenchThemeManager extends EventManager implements } } } + + public static class WorkbenchThemeChangedHandler implements EventHandler { + public void handleEvent(org.osgi.service.event.Event event) { + IStylingEngine engine = getStylingEngine(); + ThemeRegistry themeRegistry = getThemeRegistry(); + FontRegistry fontRegistry = getFontRegistry(); + ColorRegistry colorRegistry = getColorRegistry(); + + resetThemeRegistries(themeRegistry); + overrideAlreadyExistingDefinitions(engine, themeRegistry, fontRegistry, colorRegistry); + addNewDefinitions(engine, themeRegistry, fontRegistry, colorRegistry); + } + + protected IStylingEngine getStylingEngine() { + return (IStylingEngine) getContext().get(IStylingEngine.SERVICE_NAME); + } + + protected ThemeRegistry getThemeRegistry() { + return (ThemeRegistry) getContext().get(IThemeRegistry.class.getName()); + } + + protected FontRegistry getFontRegistry() { + return getCurrentTheme().getFontRegistry(); + } + + protected ColorRegistry getColorRegistry() { + return getCurrentTheme().getColorRegistry(); + } + + private ITheme getCurrentTheme() { + return WorkbenchThemeManager.getInstance().getCurrentTheme(); + } + + private IEclipseContext getContext() { + return WorkbenchThemeManager.getInstance().context; + } + + // At this moment we don't remove the definitions added by CSS since we + // don't want to modify the 3.x theme registries api + private void resetThemeRegistries(ThemeRegistry themeRegistry) { + for (FontDefinition fontDefinition : themeRegistry.getFonts()) { + if (fontDefinition.isOverridden()) { + fontDefinition.resetToDefaultValue(); + } + } + for (ColorDefinition colorDefinition : themeRegistry.getColors()) { + if (colorDefinition.isOverridden()) { + colorDefinition.resetToDefaultValue(); + } + } + } + + private void overrideAlreadyExistingDefinitions(IStylingEngine engine, + ThemeRegistry themeRegistry, FontRegistry fontRegistry, ColorRegistry colorRegistry) { + 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()); + } + } + } + + private void addNewDefinitions(IStylingEngine engine, ThemeRegistry themeRegistry, + FontRegistry fontRegistry, ColorRegistry colorRegistry) { + ThemesExtension themesExtension = createThemesExtension(); + engine.style(themesExtension); + + for (IThemeElementDefinitionOverridable<?> definition : themesExtension + .getDefinitions()) { + engine.style(definition); + if (definition.isOverridden() && definition instanceof FontDefinition) { + addFontDefinition((FontDefinition) definition, themeRegistry, fontRegistry); + } else if (definition.isOverridden() && definition instanceof ColorDefinition) { + addColorDefinition((ColorDefinition) definition, themeRegistry, colorRegistry); + } + } + } + + private void addFontDefinition(FontDefinition definition, ThemeRegistry themeRegistry, + FontRegistry fontRegistry) { + themeRegistry.add(definition); + fontRegistry.put(definition.getId(), definition.getValue()); + } + + private void addColorDefinition(ColorDefinition definition, ThemeRegistry themeRegistry, + ColorRegistry colorRegistry) { + themeRegistry.add(definition); + colorRegistry.put(definition.getId(), definition.getValue()); + } + + protected ThemesExtension createThemesExtension() { + return new ThemesExtension(); + } + } + } diff --git a/bundles/org.eclipse.ui.workbench/plugin.properties b/bundles/org.eclipse.ui.workbench/plugin.properties index bd8890cd46f..abf1985860e 100644 --- a/bundles/org.eclipse.ui.workbench/plugin.properties +++ b/bundles/org.eclipse.ui.workbench/plugin.properties @@ -19,3 +19,4 @@ bindingcontext.name.dialogAndWindows = In Dialog and Windows bindingcontext.name.windows = In Windows bindingcontext.name.bindingView = In Binding View bindingcontext.name.dialogs = In Dialogs +Other.defined.by.css.label= Other defined by CSS
\ No newline at end of file diff --git a/bundles/org.eclipse.ui.workbench/plugin.xml b/bundles/org.eclipse.ui.workbench/plugin.xml index e02b96cc313..98341c61b9a 100644 --- a/bundles/org.eclipse.ui.workbench/plugin.xml +++ b/bundles/org.eclipse.ui.workbench/plugin.xml @@ -42,4 +42,39 @@ </property-name> </handler> </extension> + <extension + point="org.eclipse.ui.handlers"> + <handler + commandId="org.eclipse.ui.window.splitEditor" + class="org.eclipse.ui.internal.SplitHandler"> + </handler> + </extension> + <extension + point="org.eclipse.ui.bindings"> + <key + commandId="org.eclipse.ui.window.splitEditor" + contextId="org.eclipse.ui.contexts.window" + sequence="M1+_" + schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"> + <parameter + id="Splitter.isHorizontal" + value="true"> + </parameter> + </key> + <key + commandId="org.eclipse.ui.window.splitEditor" + contextId="org.eclipse.ui.contexts.window" + sequence="M1+{" + schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"> + <parameter + id="Splitter.isHorizontal" + value="false"> + </parameter> + </key> + </extension> + <extension point="org.eclipse.ui.themes"> + <themeElementCategory + id="org.eclipse.ui.themes.CssTheme" + label="%Other.defined.by.css.label"/> + </extension> </plugin> 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 index dbbe7d0acb1..27ee7a878f0 100644 --- 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 @@ -10,11 +10,13 @@ *******************************************************************************/ package org.eclipse.e4.ui.tests.css.swt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + 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; @@ -24,85 +26,103 @@ 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"); - + CSSEngine engine = createEngine("ColorDefinition#ACTIVE_HYPERLINK_COLOR{color: green}", display); + ColorDefinition definition = colorDefinition("ACTIVE_HYPERLINK_COLOR", "name", "categoryId", "description"); + assertEquals(new RGB(0, 0, 0), definition.getValue()); - assertFalse(definition.isOverridden()); - + assertFalse(definition.isOverridden()); + //when engine.applyStyles(definition, true); - + //then assertEquals(new RGB(0, 128, 0), definition.getValue()); + assertEquals("name", definition.getName()); + assertEquals("categoryId", definition.getCategoryId()); + assertTrue(definition.getDescription().startsWith("description")); + assertTrue(definition.getDescription().endsWith(definition.getOverriddenLabel())); + assertTrue(definition.isOverridden()); + } + + public void testColorDefinitionWhenNameCategoryIdAndDescriptionOverridden() + throws Exception { + // given + CSSEngine engine = createEngine("ColorDefinition#ACTIVE_HYPERLINK_COLOR{color: green;" + + "label:'nameOverridden'; category:'#categoryIdOverridden'; description: 'descriptionOverridden'}", display); + ColorDefinition definition = colorDefinition("ACTIVE_HYPERLINK_COLOR","name", "categoryId", "description"); + + 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()); + assertEquals("nameOverridden", definition.getName()); + assertEquals("categoryIdOverridden", definition.getCategoryId()); + assertTrue(definition.getDescription().startsWith("descriptionOverridden")); + assertTrue(definition.getDescription().endsWith(definition.getOverriddenLabel())); 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"); - + CSSEngine engine = createEngine("ColorDefinition#ACTIVE_HYPERLINK_COLOR{color: green}", display); + ColorDefinition definition = colorDefinition("color definition uniqueId without matching stylesheet", + "name", "categoryId", "description"); + assertEquals(new RGB(0, 0, 0), definition.getValue()); - assertFalse(definition.isOverridden()); - + assertFalse(definition.isOverridden()); + //when - engine.applyStyles(definition, true); - + engine.applyStyles(definition, true); + //then assertEquals(new RGB(0, 0, 0), definition.getValue()); - assertFalse(definition.isOverridden()); + 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); - - + engine.applyStyles(label, true); + //then assertEquals(new RGB(255, 0, 0), label.getBackground().getRGB()); - - shell.dispose(); + + shell.dispose(); } - - private ColorDefinition colorDefinition(String uniqueId) { - return new ColorDefinition("label", uniqueId, "defaultsTo", "black", "categoryId", true, - "description", "pluginId"); + + private ColorDefinition colorDefinition(String uniqueId, String name, + String categoryId, String description) { + return new ColorDefinition(name, uniqueId, "defaultsTo", "black", + categoryId, true, description, "pluginId"); } - - private void registerColorProviderWith(final String expectedSymbolicName, final RGB expectedRgb) throws Exception { + + private void registerColorProviderWith(final String symbolicName, final RGB rgb) 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; - } - }; + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(rgb).when(provider).getColor(symbolicName); + return provider; }; }.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 791f9362e6c..2dea086e719 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 @@ -37,6 +37,7 @@ public class CssSwtTestSuite extends TestSuite { addTestSuite(SWTResourcesRegistryTest.class); addTestSuite(FontDefinitionTest.class); addTestSuite(ColorDefinitionTest.class); + addTestSuite(ThemesExtensionTest.class); addTestSuite(CSSSWTWidgetTest.class); addTestSuite(LabelTest.class); addTestSuite(CTabFolderTest.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 ef215b297c1..5413a58b975 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 @@ -10,13 +10,15 @@ *******************************************************************************/ package org.eclipse.e4.ui.tests.css.swt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + 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; @@ -34,7 +36,7 @@ public class FontDefinitionTest extends CSSSWTTestCase { 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"); + FontDefinition definition = fontDefinition("org.eclipse.jface.bannerfont", "name", "categoryId","description"); assertNull(definition.getValue()); assertFalse(definition.isOverridden()); @@ -47,13 +49,42 @@ public class FontDefinitionTest extends CSSSWTTestCase { assertEquals("Times", definition.getValue()[0].getName()); assertEquals(12, definition.getValue()[0].getHeight()); assertEquals(SWT.ITALIC, definition.getValue()[0].getStyle()); + assertEquals("categoryId", definition.getCategoryId()); + assertEquals("name", definition.getName()); + assertTrue(definition.getDescription().startsWith("description")); + assertTrue(definition.getDescription().endsWith(definition.getOverriddenLabel())); + assertTrue(definition.isOverridden()); + } + + public void testFontDefinitionWhenNameCategoryIdAndDescriptionOverridden() + throws Exception { + // given + CSSEngine engine = createEngine("FontDefinition#org-eclipse-jface-bannerfont {font-family: 'Times';font-size: 12;font-style: italic;" + + " label:'nameOverridden'; category: '#categoryIdOverridden'; description: 'descriptionOverridden'}", display); + FontDefinition definition = fontDefinition("org.eclipse.jface.bannerfont", "name", "categoryId", "description"); + + 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()); + assertEquals("categoryIdOverridden", definition.getCategoryId()); + assertEquals("nameOverridden", definition.getName()); + assertTrue(definition.getDescription().startsWith("descriptionOverridden")); + assertTrue(definition.getDescription().endsWith(definition.getOverriddenLabel())); 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"); + FontDefinition definition = fontDefinition("font definition uniqueId without matching stylesheet", "name", "categoryId", "description"); assertNull(definition.getValue()); assertFalse(definition.isOverridden()); @@ -70,9 +101,7 @@ public class FontDefinitionTest extends CSSSWTTestCase { //given registerFontProviderWith("org.eclipse.jface.bannerfont", new FontData("Times", 12, SWT.ITALIC)); - CSSEngine engine = createEngine( - "Label {font-family: '#org-eclipse-jface-bannerfont'}", - display); + 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); @@ -94,27 +123,20 @@ public class FontDefinitionTest extends CSSSWTTestCase { font.dispose(); } - private FontDefinition fontDefinition(String uniqueId) { - return new FontDefinition(new FontDefinition("fontName", uniqueId, "defaultsId", - "value", "categoryId", true, "fontDescription"), + private FontDefinition fontDefinition(String uniqueId, String name, + String categoryId, String description) { + return new FontDefinition(new FontDefinition(name, uniqueId, + "defaultsId", "value", categoryId, true, description), new FontData[] {new FontData("Arial", 10, SWT.NORMAL)}); } - private void registerFontProviderWith(final String expectedSymbolicName, final FontData fontData) throws Exception { + private void registerFontProviderWith(final String symbolicName, 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; - } - }; + IColorAndFontProvider provider = mock(IColorAndFontProvider.class); + doReturn(new FontData[] { fontData }).when(provider).getFont(symbolicName); + return provider; }; }.start(null); } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ThemesExtensionTest.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ThemesExtensionTest.java new file mode 100644 index 00000000000..7d994e39e05 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/ThemesExtensionTest.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.tests.css.swt; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.internal.themes.ColorDefinition; +import org.eclipse.ui.internal.themes.FontDefinition; +import org.eclipse.ui.internal.themes.ThemesExtension; + +@SuppressWarnings("restriction") +public class ThemesExtensionTest extends CSSSWTTestCase { + private Display display; + + @Override + protected void setUp() throws Exception { + display = Display.getDefault(); + } + + public void testThemesExtension() throws Exception { + //given + CSSEngine engine = createEngine("ThemesExtension {font-definition: '#org-eclipse-ui-workbench-FONT_DEF_1'," + + "'#org-eclipse-ui-workbench-FONT_DEF_2'; color-definition: '#org-eclipse-ui-workbench-COLOR_DEF_1';}", display); + ThemesExtension themesExtention = new ThemesExtension(); + + //when + engine.applyStyles(themesExtention, true); + + //then + assertEquals(3, themesExtention.getDefinitions().size()); + + assertTrue(themesExtention.getDefinitions().get(0) instanceof FontDefinition); + FontDefinition fontDefinition1 = (FontDefinition) themesExtention.getDefinitions().get(0); + assertTrue(fontDefinition1.isAddedByCss()); + assertFalse(fontDefinition1.isOverridden()); + assertEquals("org.eclipse.ui.workbench.FONT_DEF_1",fontDefinition1.getId()); + assertEquals(ThemesExtension.DEFAULT_CATEGORY_ID,fontDefinition1.getCategoryId()); + assertTrue(fontDefinition1.getName().startsWith(FontDefinition.class.getSimpleName())); + assertTrue(fontDefinition1.getName().endsWith(fontDefinition1.getId())); + assertFalse(fontDefinition1.getDescription().contains(fontDefinition1.getOverriddenLabel())); + assertEquals(themesExtention.getDefaultDescription(),fontDefinition1.getDescription()); + + assertTrue(themesExtention.getDefinitions().get(1) instanceof FontDefinition); + FontDefinition fontDefinition2 = (FontDefinition) themesExtention.getDefinitions().get(1); + assertTrue(fontDefinition2.isAddedByCss()); + assertFalse(fontDefinition2.isOverridden()); + assertEquals("org.eclipse.ui.workbench.FONT_DEF_2",fontDefinition2.getId()); + assertEquals(ThemesExtension.DEFAULT_CATEGORY_ID,fontDefinition1.getCategoryId()); + assertTrue(fontDefinition2.getName().startsWith(FontDefinition.class.getSimpleName())); + assertTrue(fontDefinition2.getName().endsWith(fontDefinition2.getId())); + assertFalse(fontDefinition2.getDescription().contains(fontDefinition2.getOverriddenLabel())); + assertEquals(themesExtention.getDefaultDescription(),fontDefinition2.getDescription()); + + assertTrue(themesExtention.getDefinitions().get(2) instanceof ColorDefinition); + ColorDefinition colorDefinition1 = (ColorDefinition) themesExtention.getDefinitions().get(2); + assertTrue(colorDefinition1.isAddedByCss()); + assertFalse(colorDefinition1.isOverridden()); + assertEquals("org.eclipse.ui.workbench.COLOR_DEF_1",colorDefinition1.getId()); + assertFalse(colorDefinition1.getDescription().contains(colorDefinition1.getOverriddenLabel())); + assertEquals(themesExtention.getDefaultDescription(),colorDefinition1.getDescription()); + assertEquals(ThemesExtension.DEFAULT_CATEGORY_ID,colorDefinition1.getCategoryId()); + assertTrue(colorDefinition1.getName().startsWith(ColorDefinition.class.getSimpleName())); + assertTrue(colorDefinition1.getName().endsWith(colorDefinition1.getId())); + assertFalse(colorDefinition1.getDescription().contains(colorDefinition1.getOverriddenLabel())); + assertEquals(themesExtention.getDefaultDescription(),colorDefinition1.getDescription()); + } +} diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/ThemesTestSuite.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/ThemesTestSuite.java index 171baca2f8e..7998601cc64 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/ThemesTestSuite.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/ThemesTestSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 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 @@ -25,5 +25,6 @@ public class ThemesTestSuite extends TestSuite { public ThemesTestSuite() { addTest(new TestSuite(ThemeAPITest.class)); addTest(new TestSuite(JFaceThemeTest.class)); + addTest(new TestSuite(WorkbenchThemeChangedHandlerTest.class)); } } diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/WorkbenchThemeChangedHandlerTest.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/WorkbenchThemeChangedHandlerTest.java new file mode 100644 index 00000000000..7702e81954b --- /dev/null +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/themes/WorkbenchThemeChangedHandlerTest.java @@ -0,0 +1,210 @@ +/******************************************************************************* + * 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.tests.themes; + +import java.util.Arrays; + +import org.eclipse.e4.ui.services.IStylingEngine; +import org.eclipse.jface.resource.ColorRegistry; +import org.eclipse.jface.resource.FontRegistry; +import org.eclipse.swt.graphics.FontData; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.ui.internal.themes.ColorDefinition; +import org.eclipse.ui.internal.themes.FontDefinition; +import org.eclipse.ui.internal.themes.ThemeRegistry; +import org.eclipse.ui.internal.themes.ThemesExtension; +import org.eclipse.ui.internal.themes.WorkbenchThemeManager.WorkbenchThemeChangedHandler; +import org.osgi.service.event.Event; + +import junit.framework.TestCase; +import static org.mockito.Mockito.*; + +/** + * @since 3.5 + * + */ +public class WorkbenchThemeChangedHandlerTest extends TestCase { + public void testOverrideThemeDefinitions() throws Exception { + //given + IStylingEngine stylingEngine = mock(IStylingEngine.class); + + FontDefinition fontDefinition1 = mock(FontDefinition.class); + doReturn("fontDefinition1").when(fontDefinition1).getId(); + doReturn(true).when(fontDefinition1).isOverridden(); + + FontDefinition fontDefinition2 = mock(FontDefinition.class); + doReturn("fontDefinition2").when(fontDefinition2).getId(); + doReturn(false).when(fontDefinition2).isOverridden(); + + ColorDefinition colorDefinition = mock(ColorDefinition.class); + doReturn("colorDefinition").when(colorDefinition).getId(); + doReturn(true).when(colorDefinition).isOverridden(); + + ThemeRegistry themeRegistry = spy(new ThemeRegistry()); + doReturn(new FontDefinition[]{fontDefinition1, fontDefinition2}).when(themeRegistry).getFonts(); + doReturn(new ColorDefinition[] {colorDefinition}).when(themeRegistry).getColors(); + + FontRegistry fontRegistry = mock(FontRegistry.class); + + ColorRegistry colorRegistry = mock(ColorRegistry.class); + + ThemesExtension themesExtension = mock(ThemesExtension.class); + + WorkbenchThemeChangedHandlerTestable handler = spy(new WorkbenchThemeChangedHandlerTestable()); + doReturn(stylingEngine).when(handler).getStylingEngine(); + doReturn(themeRegistry).when(handler).getThemeRegistry(); + doReturn(fontRegistry).when(handler).getFontRegistry(); + doReturn(colorRegistry).when(handler).getColorRegistry(); + doReturn(themesExtension).when(handler).createThemesExtension(); + + + //when + handler.handleEvent(mock(Event.class)); + + //then + verify(stylingEngine, times(4)).style(anyObject()); + verify(stylingEngine, times(1)).style(themesExtension); + verify(stylingEngine, times(1)).style(fontDefinition1); + verify(stylingEngine, times(1)).style(fontDefinition2); + verify(stylingEngine, times(1)).style(colorDefinition); + + verify(fontRegistry, times(1)).put(eq("fontDefinition1"), any(FontData[].class)); + verify(fontRegistry, never()).put(eq("fontDefinition2"), any(FontData[].class)); + verify(colorRegistry, times(1)).put(eq("colorDefinition"), any(RGB.class)); + } + + public void testAddThemeDefinitions() throws Exception { + //given + IStylingEngine stylingEngine = mock(IStylingEngine.class); + + FontDefinition fontDefinition = mock(FontDefinition.class); + doReturn("fontDefinition").when(fontDefinition).getId(); + doReturn(true).when(fontDefinition).isOverridden(); + + ColorDefinition colorDefinition = mock(ColorDefinition.class); + doReturn("colorDefinition").when(colorDefinition).getId(); + doReturn(true).when(colorDefinition).isOverridden(); + + ThemeRegistry themeRegistry = spy(new ThemeRegistry()); + + FontRegistry fontRegistry = mock(FontRegistry.class); + + ColorRegistry colorRegistry = mock(ColorRegistry.class); + + ThemesExtension themesExtension = mock(ThemesExtension.class); + doReturn(Arrays.asList(fontDefinition, colorDefinition)).when(themesExtension).getDefinitions(); + + WorkbenchThemeChangedHandlerTestable handler = spy(new WorkbenchThemeChangedHandlerTestable()); + doReturn(stylingEngine).when(handler).getStylingEngine(); + doReturn(themeRegistry).when(handler).getThemeRegistry(); + doReturn(fontRegistry).when(handler).getFontRegistry(); + doReturn(colorRegistry).when(handler).getColorRegistry(); + doReturn(themesExtension).when(handler).createThemesExtension(); + + + //when + handler.handleEvent(mock(Event.class)); + + //then + verify(stylingEngine, times(3)).style(anyObject()); + verify(stylingEngine, times(1)).style(themesExtension); + verify(stylingEngine, times(1)).style(fontDefinition); + verify(stylingEngine, times(1)).style(colorDefinition); + + verify(fontRegistry, times(1)).put(eq("fontDefinition"), any(FontData[].class)); + assertEquals(1, themeRegistry.getFonts().length); + verify(colorRegistry, times(1)).put(eq("colorDefinition"), any(RGB.class)); + assertEquals(1, themeRegistry.getColors().length); + } + + public void testOverrideAndAddThemeDefinitions() throws Exception { + //given + IStylingEngine stylingEngine = mock(IStylingEngine.class); + + FontDefinition fontDefinition1 = mock(FontDefinition.class); + doReturn("fontDefinition1").when(fontDefinition1).getId(); + doReturn(true).when(fontDefinition1).isOverridden(); + + FontDefinition fontDefinition2 = mock(FontDefinition.class); + doReturn("fontDefinition2").when(fontDefinition2).getId(); + doReturn(true).when(fontDefinition2).isOverridden(); + + ColorDefinition colorDefinition1 = mock(ColorDefinition.class); + doReturn("colorDefinition1").when(colorDefinition1).getId(); + doReturn(true).when(colorDefinition1).isOverridden(); + + ColorDefinition colorDefinition2 = mock(ColorDefinition.class); + doReturn("colorDefinition2").when(colorDefinition2).getId(); + doReturn(true).when(colorDefinition2).isOverridden(); + + ThemeRegistry themeRegistry = spy(new ThemeRegistry()); + doReturn(new FontDefinition[]{fontDefinition1, fontDefinition2}).when(themeRegistry).getFonts(); + doReturn(new ColorDefinition[] {colorDefinition1, colorDefinition2}).when(themeRegistry).getColors(); + + FontRegistry fontRegistry = mock(FontRegistry.class); + + ColorRegistry colorRegistry = mock(ColorRegistry.class); + + ThemesExtension themesExtension = mock(ThemesExtension.class); + + WorkbenchThemeChangedHandlerTestable handler = spy(new WorkbenchThemeChangedHandlerTestable()); + doReturn(stylingEngine).when(handler).getStylingEngine(); + doReturn(themeRegistry).when(handler).getThemeRegistry(); + doReturn(fontRegistry).when(handler).getFontRegistry(); + doReturn(colorRegistry).when(handler).getColorRegistry(); + doReturn(themesExtension).when(handler).createThemesExtension(); + + + //when + handler.handleEvent(mock(Event.class)); + + //then + verify(stylingEngine, times(5)).style(anyObject()); + verify(stylingEngine, times(1)).style(themesExtension); + verify(stylingEngine, times(1)).style(fontDefinition1); + verify(stylingEngine, times(1)).style(fontDefinition2); + verify(stylingEngine, times(1)).style(colorDefinition1); + verify(stylingEngine, times(1)).style(colorDefinition2); + + verify(fontRegistry, times(1)).put(eq("fontDefinition1"), any(FontData[].class)); + verify(fontRegistry, times(1)).put(eq("fontDefinition2"), any(FontData[].class)); + verify(colorRegistry, times(1)).put(eq("colorDefinition1"), any(RGB.class)); + verify(colorRegistry, times(1)).put(eq("colorDefinition2"), any(RGB.class)); + } + + public static class WorkbenchThemeChangedHandlerTestable extends WorkbenchThemeChangedHandler { + @Override + public IStylingEngine getStylingEngine() { + return super.getStylingEngine(); + } + + @Override + public ThemeRegistry getThemeRegistry() { + return super.getThemeRegistry(); + } + + @Override + public FontRegistry getFontRegistry() { + return super.getFontRegistry(); + } + + @Override + public ColorRegistry getColorRegistry() { + return super.getColorRegistry(); + } + + @Override + public ThemesExtension createThemesExtension() { + return super.createThemesExtension(); + } + } +} diff --git a/tests/org.eclipse.ui.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.ui.tests/META-INF/MANIFEST.MF index adf67e69ac5..698913b34f9 100644 --- a/tests/org.eclipse.ui.tests/META-INF/MANIFEST.MF +++ b/tests/org.eclipse.ui.tests/META-INF/MANIFEST.MF @@ -42,7 +42,10 @@ Require-Bundle: org.eclipse.core.runtime.compatibility, org.eclipse.e4.ui.services;bundle-version="0.10.0", org.eclipse.e4.ui.workbench.addons.swt;bundle-version="0.10.0", org.eclipse.e4.ui.workbench.renderers.swt;bundle-version="0.10.0", - org.eclipse.osgi.services;bundle-version="3.3.100" + org.eclipse.osgi.services;bundle-version="3.3.100", + org.hamcrest;bundle-version="1.1.0", + org.objenesis;bundle-version="1.0.0", + org.mockito;bundle-version="1.8.4" Eclipse-AutoStart: true Export-Package: org.eclipse.ui.tests.api, org.eclipse.ui.tests.menus |
