Started implementation of label based CSS style registration Change-Id: Iba2bbf6ce87316139826497cb40436b816551c47 Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF b/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF index 9d7beb6..58ee93b 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/META-INF/MANIFEST.MF
@@ -1,6 +1,14 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: NatTable extension for Eclipse 4 -Bundle-SymbolicName: org.eclipse.nebula.widgets.nattable.extension.e4 +Bundle-SymbolicName: org.eclipse.nebula.widgets.nattable.extension.e4;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Require-Bundle: org.eclipse.swt;bundle-version="3.103.0", + org.eclipse.e4.ui.css.core;bundle-version="0.10.100", + org.eclipse.e4.ui.css.swt;bundle-version="0.11.101", + org.w3c.css.sac;bundle-version="1.3.1" +Import-Package: org.eclipse.nebula.widgets.nattable;version="1.3.0", + org.eclipse.nebula.widgets.nattable.config;version="1.4.0", + org.eclipse.nebula.widgets.nattable.style;version="1.4.0" +Bundle-ActivationPolicy: lazy
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/build.properties b/org.eclipse.nebula.widgets.nattable.extension.e4/build.properties index 34d2e4d..e9863e2 100644 --- a/org.eclipse.nebula.widgets.nattable.extension.e4/build.properties +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/build.properties
@@ -1,4 +1,5 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + plugin.xml
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml b/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml new file mode 100644 index 0000000..679613c --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/plugin.xml
@@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + <extension + point="org.eclipse.e4.ui.css.core.propertyHandler"> + <handler + adapter="org.eclipse.nebula.widgets.nattable.extension.e4.NatTableElementAdapter" + handler="org.eclipse.nebula.widgets.nattable.extension.e4.NatTableCSSHandler"> + <property-name + name="background-color"> + </property-name> + <property-name + name="cell-background-color"> + </property-name> + <property-name + name="color"> + </property-name> + <property-name + name="horizontal-align"> + </property-name> + <property-name + name="vertical-align"> + </property-name> + <property-name + name="font"> + </property-name> + <property-name + name="image"> + </property-name> + <property-name + name="border"> + </property-name> + <property-name + name="password-echo-char"> + </property-name> + <property-name + name="text-decoration"> + </property-name> + </handler> + </extension> + <extension + point="org.eclipse.e4.ui.css.core.elementProvider"> + <provider + class="org.eclipse.nebula.widgets.nattable.extension.e4.NatTableElementProvider"> + <widget + class="org.eclipse.nebula.widgets.nattable.NatTable"></widget> + </provider> + </extension> + <extension + point="org.eclipse.e4.ui.css.core.elementProvider"> + <provider + class="org.eclipse.nebula.widgets.nattable.extension.e4.NatTableElementProvider"> + <widget + class="org.eclipse.nebula.widgets.nattable.extension.e4.NatTableWrapper"> + </widget> + </provider> + </extension> + +</plugin>
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSEngineImpl.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSEngineImpl.java new file mode 100644 index 0000000..12e893d --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSEngineImpl.java
@@ -0,0 +1,114 @@ +package org.eclipse.nebula.widgets.nattable.extension.e4; + +import org.eclipse.e4.ui.css.core.dom.ChildVisibilityAwareElement; +import org.eclipse.e4.ui.css.core.dom.ExtendedCSSRule; +import org.eclipse.e4.ui.css.swt.engine.CSSSWTEngineImpl; +import org.eclipse.swt.widgets.Display; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.w3c.dom.css.CSSRule; +import org.w3c.dom.css.CSSStyleDeclaration; + +//TODO I think this can be removed after the child stuff works + +@SuppressWarnings("restriction") +public class NatTableCSSEngineImpl extends CSSSWTEngineImpl { + + public NatTableCSSEngineImpl(Display display) { + super(display); + } + + public NatTableCSSEngineImpl(Display display, boolean lazyApplyingStyles) { + super(display, lazyApplyingStyles); + } + + @Override + public void applyStyles(Object element, boolean applyStylesToChildNodes, + boolean computeDefaultStyle) { + Element elt = getElement(element); + if (elt != null) { + if (!isVisible(elt)) { + return; + } + + /* + * Compute new Style to apply. + */ + // TODO custom viewCSS + CSSStyleDeclaration style = null;// viewCSS.getComputedStyle(elt, + // null); + if (computeDefaultStyle) { + if (applyStylesToChildNodes) { + this.computeDefaultStyle = computeDefaultStyle; + } + /* + * Apply default style. + */ + applyDefaultStyleDeclaration(element, false, style, null); + } + + /* + * Manage static pseudo instances + */ + String[] pseudoInstances = getStaticPseudoInstances(elt); + if (pseudoInstances != null) { + // there are static pseudo instances definied, loop for it and + // apply styles for each pseudo instance. + for (String pseudoInstance : pseudoInstances) { + // TODO custom viewCSS + CSSStyleDeclaration styleWithPseudoInstance = null;// viewCSS.getComputedStyle(elt, + // pseudoInstance); + if (computeDefaultStyle) { + /* + * Apply default style for the current pseudo instance. + */ + applyDefaultStyleDeclaration(element, false, + styleWithPseudoInstance, pseudoInstance); + } + + if (styleWithPseudoInstance != null) { + CSSRule parentRule = styleWithPseudoInstance.getParentRule(); + if (parentRule instanceof ExtendedCSSRule) { + // TODO inspect and handle pseudo + // applyConditionalPseudoStyle((ExtendedCSSRule) + // parentRule, pseudoInstance, element, + // styleWithPseudoInstance); + } else { + // applyStyleDeclaration(element, + // styleWithPseudoInstance, + // pseudoInstance); + applyStyleDeclaration(elt, styleWithPseudoInstance, pseudoInstance); + } + } + } + } + + if (style != null) { + // applyStyleDeclaration(element, style, null); + applyStyleDeclaration(elt, style, null); + } + try { + // Apply inline style + applyInlineStyle(elt, false); + } catch (Exception e) { + handleExceptions(e); + } + + if (applyStylesToChildNodes) { + /* + * Style all children recursive. + */ + NodeList nodes = elt instanceof ChildVisibilityAwareElement ? ((ChildVisibilityAwareElement) elt) + .getVisibleChildNodes() : elt.getChildNodes(); + if (nodes != null) { + for (int k = 0; k < nodes.getLength(); k++) { + applyStyles(nodes.item(k), applyStylesToChildNodes); + } + onStylesAppliedToChildNodes(elt, nodes); + } + } + } + + } + +}
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSHandler.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSHandler.java new file mode 100644 index 0000000..b0f4d02 --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableCSSHandler.java
@@ -0,0 +1,261 @@ +/***************************************************************************** + * Copyright (c) 2015 CEA LIST. + * + * 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: + * Dirk Fauth <dirk.fauth@googlemail.com> - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.nebula.widgets.nattable.extension.e4; + +import java.util.List; + +import org.eclipse.e4.ui.css.core.dom.properties.ICSSPropertyHandler2; +import org.eclipse.e4.ui.css.core.dom.properties.converters.ICSSValueConverter; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.swt.properties.AbstractCSSPropertySWTHandler; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes; +import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry; +import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes; +import org.eclipse.nebula.widgets.nattable.style.CellStyleProxy; +import org.eclipse.nebula.widgets.nattable.style.ConfigAttribute; +import org.eclipse.nebula.widgets.nattable.style.DisplayMode; +import org.eclipse.nebula.widgets.nattable.style.IStyle; +import org.eclipse.nebula.widgets.nattable.style.Style; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.widgets.Control; +import org.w3c.dom.css.CSSValue; + +@SuppressWarnings("restriction") +public class NatTableCSSHandler extends AbstractCSSPropertySWTHandler implements ICSSPropertyHandler2 { + + /** + * CSS property for the NatTable background color. This has effect on the + * area that does not show cells. + */ + public static final String BACKGROUND_COLOR = "background-color"; + + /** + * CSS property for {@link CellStyleAttributes#BACKGROUND_COLOR}. + */ + public static final String CELL_BACKGROUND_COLOR = "cell-background-color"; + + /** + * CSS property for {@link CellStyleAttributes#FOREGROUND_COLOR}. + */ + public static final String FOREGROUND_COLOR = "color"; + + // TODO instead of additional properties, distinguish via value + // http://www.vogella.com/tutorials/Eclipse4CSS/article.html#css_colors + + /** + * CSS property for {@link CellStyleAttributes#GRADIENT_BACKGROUND_COLOR}. + * Triggers usage of the GradientBackgroundPainter. + */ + public static final String GRADIENT_BACKGROUND_COLOR = "gradient-background-color"; + + /** + * CSS property for {@link CellStyleAttributes#GRADIENT_FOREGROUND_COLOR}. + * Triggers usage of the GradientBackgroundPainter. + */ + public static final String GRADIENT__FOREGROUND_COLOR = "gradient-foreground-color"; + + /** + * CSS property for {@link CellStyleAttributes#HORIZONTAL_ALIGNMENT}. + */ + public static final String HORIZONTAL_ALIGNMENT = "horizontal-align"; + + /** + * CSS property for {@link CellStyleAttributes#VERTICAL_ALIGNMENT}. + */ + public static final String VERTICAL_ALIGNMENT = "vertical-align"; + + /** + * CSS property for {@link CellStyleAttributes#FONT}. + */ + public static final String FONT = "font"; + + /** + * CSS property for {@link CellStyleAttributes#IMAGE}. Triggers the usage of + * the ImagePainter. + */ + public static final String IMAGE = "image"; + + /** + * CSS property for {@link CellStyleAttributes#BORDER_STYLE}. Triggers the + * usage of the LineBorderDecorator. + */ + public static final String BORDER = "border"; + + /** + * CSS property for {@link CellStyleAttributes#PASSWORD_ECHO_CHAR}. Triggers + * the usage of the PasswordTextPainter. + */ + public static final String PASSWORD_ECHO_CHAR = "password-echo-char"; + + /** + * CSS property for {@link CellStyleAttributes#TEXT_DECORATION}. + * <p> + * Available values: none, underline, line-through + * </p> + * <p> + * Combinations are possible via space separated list. + * </p> + */ + public static final String TEXT_DECORATION = "text-decoration"; + + // TODO add convenience for font attributes + // font-family, font-size, font-style, font-weight + + // TODO add support for decorations + // image, side, padding + + // TODO add support for border side definition via CustomLineBorderDecorator + // possibly extend CustomLineBorderDecorator to specify side at creation + // time (border-left, border-right ...) + + // TODO add convenience for border attributes + // border-color, border-width, border-style + + // TODO word wrapping + + // TODO width/height calculation + + // TODO padding + + // TODO text direction + + // TODO background image (on NatTable or cell) + // trigger BackgroundImagePainter (add separator color) + // public static final String BACKGROUND_IMAGE = "background-image"; + + @Override + protected void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine) + throws Exception { + + if (control instanceof NatTable) { + NatTable natTable = (NatTable) control; + + // check property + if (BACKGROUND_COLOR.equalsIgnoreCase(property) + && (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE)) { + Color newColor = (Color) engine.convert(value, Color.class, control.getDisplay()); + // apply style + natTable.setBackground(newColor); + } else if (CELL_BACKGROUND_COLOR.equalsIgnoreCase(property) + && (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE)) { + Color newColor = (Color) engine.convert(value, Color.class, control.getDisplay()); + // apply style + applyNatTableStyle( + natTable, + CellStyleAttributes.BACKGROUND_COLOR, + newColor, + getDisplayMode(pseudo)); + } + + } + } + + @Override + protected String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine) + throws Exception { + + if (control instanceof NatTable) { + NatTable natTable = (NatTable) control; + + // check property + if (BACKGROUND_COLOR.equalsIgnoreCase(property)) { + ICSSValueConverter cssValueConverter = engine.getCSSValueConverter(String.class); + return cssValueConverter.convert( + natTable.getBackground(), engine, null); + } else if (CELL_BACKGROUND_COLOR.equalsIgnoreCase(property)) { + ICSSValueConverter cssValueConverter = engine.getCSSValueConverter(String.class); + return cssValueConverter.convert( + getNatTableStyle( + natTable, + CellStyleAttributes.BACKGROUND_COLOR, + getDisplayMode(pseudo)), + engine, + null); + } + + } + return null; + } + + protected <T> void applyNatTableStyle( + NatTable natTable, + ConfigAttribute<T> styleConfig, + T value, + String displayMode) { + + IConfigRegistry configRegistry = natTable.getConfigRegistry(); + + // TODO retrieve labels from selector (pseudo?) + String[] configLabels = new String[0]; + + // retrieve the style object for the given selector + IStyle style = configRegistry.getConfigAttribute( + CellConfigAttributes.CELL_STYLE, + displayMode, + configLabels); + + if (style == null) { + style = new Style(); + if (configLabels != null && configLabels.length > 0) { + configRegistry.registerConfigAttribute( + CellConfigAttributes.CELL_STYLE, + style, + displayMode, + configLabels[0]); + } else { + configRegistry.registerConfigAttribute( + CellConfigAttributes.CELL_STYLE, + style, + displayMode); + } + } + + // set the value to the style object + style.setAttributeValue( + styleConfig, + value); + } + + protected <T> T getNatTableStyle(NatTable natTable, ConfigAttribute<T> styleConfig, String displayMode) { + IConfigRegistry configRegistry = natTable.getConfigRegistry(); + + // TODO retrieve labels from selector (pseudo?) + List<String> configLabels = null; + + IStyle style = new CellStyleProxy(configRegistry, displayMode, configLabels); + + return style.getAttributeValue(styleConfig); + } + + protected String getDisplayMode(String pseudo) { + if (pseudo != null) { + if ("select".equals(pseudo)) { + return DisplayMode.SELECT; + } else if ("edit".equals(pseudo)) { + return DisplayMode.EDIT; + } else if ("hover".equals(pseudo)) { + return DisplayMode.HOVER; + } else if ("select-hover".equals(pseudo)) { + return DisplayMode.SELECT_HOVER; + } + } + return DisplayMode.NORMAL; + } + + @Override + public void onAllCSSPropertiesApplyed(Object element, CSSEngine engine) throws Exception { + // TODO build and apply painters + + } +}
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementAdapter.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementAdapter.java new file mode 100644 index 0000000..7fbe0cd --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementAdapter.java
@@ -0,0 +1,82 @@ +/***************************************************************************** + * Copyright (c) 2015 CEA LIST. + * + * 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: + * Dirk Fauth <dirk.fauth@googlemail.com> - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.nebula.widgets.nattable.extension.e4; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.e4.ui.css.swt.dom.WidgetElement; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +@SuppressWarnings("restriction") +public class NatTableElementAdapter extends WidgetElement { + + List<String> labels = new ArrayList<String>(); + + { + this.labels.add("COLUMN_HEADER"); + } + + public NatTableElementAdapter(NatTable natTable, CSSEngine engine) { + super(natTable, engine); + + addStaticPseudoInstance("normal"); + addStaticPseudoInstance("select"); + addStaticPseudoInstance("edit"); + addStaticPseudoInstance("hover"); + addStaticPseudoInstance("select-hover"); + } + + // @Override + // public Node getParentNode() { + // return this; + // } + + @Override + public Node getParentNode() { + Control control = getControl(); + Composite parent = control.getParent(); + if (parent != null) { + Element element = getElement(parent); + return element; + } + return null; + } + + protected Control getControl() { + return (Control) getNativeWidget(); + } + + @Override + public NodeList getChildNodes() { + // only need to return a non-null value + // strange implementation + return this; + } + + @Override + public int getLength() { + return this.labels.size(); + } + + @Override + public Node item(int index) { + return new NatTableWrapper((NatTable) getNativeWidget(), this.labels.get(index), this.engine, this); + } +}
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementProvider.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementProvider.java new file mode 100644 index 0000000..1ce8ac2 --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableElementProvider.java
@@ -0,0 +1,33 @@ +/***************************************************************************** + * Copyright (c) 2015 CEA LIST. + * + * 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: + * Dirk Fauth <dirk.fauth@googlemail.com> - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.nebula.widgets.nattable.extension.e4; + +import org.eclipse.e4.ui.css.core.dom.IElementProvider; +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.w3c.dom.Element; + +@SuppressWarnings("restriction") +public class NatTableElementProvider implements IElementProvider { + + @Override + public Element getElement(Object element, CSSEngine engine) { + if (element instanceof NatTable + || element instanceof NatTableWrapper) { + NatTable natTable = (NatTable) element; + return new NatTableElementAdapter(natTable, engine); + } + return null; + } + +}
diff --git a/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableWrapper.java b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableWrapper.java new file mode 100644 index 0000000..ddd8bc1 --- /dev/null +++ b/org.eclipse.nebula.widgets.nattable.extension.e4/src/org/eclipse/nebula/widgets/nattable/extension/e4/NatTableWrapper.java
@@ -0,0 +1,37 @@ +package org.eclipse.nebula.widgets.nattable.extension.e4; + +import org.eclipse.e4.ui.css.core.engine.CSSEngine; +import org.eclipse.nebula.widgets.nattable.NatTable; +import org.w3c.dom.Node; + +public class NatTableWrapper extends NatTableElementAdapter { + + public final NatTableElementAdapter parent; + public final String styleLabel; + + public NatTableWrapper(NatTable natTable, String styleLabel, CSSEngine engine, NatTableElementAdapter parent) { + super(natTable, engine); + + this.styleLabel = styleLabel; + this.parent = parent; + } + + public String getStyleLabel() { + return this.styleLabel; + } + + @Override + public Node getParentNode() { + return this.parent; + } + + @Override + public int getLength() { + return 0; + } + + @Override + public Node item(int index) { + return null; + } +}