1 /*******************************************************************************
2  * Copyright (c) 2015 Fabio Zadrozny and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Fabio Zadrozny <fabiofz@gmail.com> - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.ui.internal.forms.css.properties.css2;
15 
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 
21 import org.eclipse.e4.ui.css.core.dom.properties.Gradient;
22 import org.eclipse.e4.ui.css.core.engine.CSSEngine;
23 import org.eclipse.e4.ui.css.swt.properties.AbstractCSSPropertySWTHandler;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.ui.forms.IFormColors;
27 import org.eclipse.ui.forms.widgets.Form;
28 import org.w3c.dom.css.CSSPrimitiveValue;
29 import org.w3c.dom.css.CSSValue;
30 
31 public class CSSPropertyFormHandler extends AbstractCSSPropertySWTHandler {
32 
33 	// Constants to customize the Form (IFormColors constants).
34 	public static final String TB_TOGGLE_HOVER = "tb-toggle-hover-color"; //$NON-NLS-1$
35 	public static final String TB_TOGGLE = "tb-toggle-color"; //$NON-NLS-1$
36 	public static final String H_HOVER_FULL = "h-hover-full-color"; //$NON-NLS-1$
37 	public static final String H_HOVER_LIGHT = "h-hover-light-color"; //$NON-NLS-1$
38 	public static final String H_BOTTOM_KEYLINE_2 = "h-bottom-keyline-2-color"; //$NON-NLS-1$
39 	public static final String H_BOTTOM_KEYLINE_1 = "h-bottom-keyline-1-color"; //$NON-NLS-1$
40 
41 	// Constant to customize:
42 	// org.eclipse.ui.forms.widgets.Form.setTextBackground(Color[], int[],
43 	// boolean)
44 	public static final String TEXT_BACKGROUND_COLOR = "text-background-color"; //$NON-NLS-1$
45 
46 	private static final Map<String, String> propertyToHeadProperty = new HashMap<>();
47 
48 	static {
propertyToHeadProperty.put(H_BOTTOM_KEYLINE_1, IFormColors.H_BOTTOM_KEYLINE1)49 		propertyToHeadProperty.put(H_BOTTOM_KEYLINE_1, IFormColors.H_BOTTOM_KEYLINE1);
propertyToHeadProperty.put(H_BOTTOM_KEYLINE_2, IFormColors.H_BOTTOM_KEYLINE2)50 		propertyToHeadProperty.put(H_BOTTOM_KEYLINE_2, IFormColors.H_BOTTOM_KEYLINE2);
propertyToHeadProperty.put(H_HOVER_LIGHT, IFormColors.H_HOVER_LIGHT)51 		propertyToHeadProperty.put(H_HOVER_LIGHT, IFormColors.H_HOVER_LIGHT);
propertyToHeadProperty.put(H_HOVER_FULL, IFormColors.H_HOVER_FULL)52 		propertyToHeadProperty.put(H_HOVER_FULL, IFormColors.H_HOVER_FULL);
propertyToHeadProperty.put(TB_TOGGLE, IFormColors.TB_TOGGLE)53 		propertyToHeadProperty.put(TB_TOGGLE, IFormColors.TB_TOGGLE);
propertyToHeadProperty.put(TB_TOGGLE_HOVER, IFormColors.TB_TOGGLE_HOVER)54 		propertyToHeadProperty.put(TB_TOGGLE_HOVER, IFormColors.TB_TOGGLE_HOVER);
55 	}
56 
57 	@Override
applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine)58 	protected void applyCSSProperty(Control control, String property, CSSValue value, String pseudo, CSSEngine engine)
59 			throws Exception {
60 		if (!(control instanceof Form)) {
61 			return;
62 		}
63 
64 		Form form = (Form) control;
65 		if (TEXT_BACKGROUND_COLOR.equals(property)) {
66 			if (value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
67 				Color color = (Color) engine.convert(value, Color.class, form.getDisplay());
68 				// When a single color is received, make it 100% with that
69 				// single color.
70 				form.setTextBackground(new Color[] { color }, new int[] { 100 }, true);
71 
72 			} else if (value.getCssValueType() == CSSValue.CSS_VALUE_LIST) {
73 				Gradient grad = (Gradient) engine.convert(value, Gradient.class, form.getDisplay());
74 				if (grad == null) {
75 					return;
76 				}
77 				List<CSSPrimitiveValue> values = grad.getValues();
78 				List<Color> colors = new ArrayList<>(values.size());
79 				for (CSSPrimitiveValue cssValue : values) {
80 					if (cssValue != null && cssValue.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
81 						Color color = (Color) engine.convert(cssValue, Color.class, form.getDisplay());
82 						colors.add(color);
83 					}
84 				}
85 
86 				if (colors.size() > 0) {
87 					List<Integer> list = grad.getPercents();
88 					int[] percents = new int[list.size()];
89 					for (int i = 0; i < percents.length; i++) {
90 						percents[i] = list.get(i).intValue();
91 					}
92 					form.setTextBackground(colors.toArray(new Color[0]), percents, grad.getVerticalGradient());
93 				}
94 			}
95 
96 		} else {
97 			String headProperty = propertyToHeadProperty.get(property);
98 			if (headProperty != null && value.getCssValueType() == CSSValue.CSS_PRIMITIVE_VALUE) {
99 				Color color = (Color) engine.convert(value, Color.class, form.getDisplay());
100 				form.setHeadColor(headProperty, color);
101 			}
102 		}
103 	}
104 
105 	@Override
retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine)106 	protected String retrieveCSSProperty(Control control, String property, String pseudo, CSSEngine engine)
107 			throws Exception {
108 		return null;
109 	}
110 
111 }
112