Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 901d4d45588e8e8fbc24d9a7b689266edec8e39a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (c) 2016 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.ide.ui.api.widgets;

import java.util.Iterator;
import java.util.List;

import org.eclipse.eef.EEFConditionalStyle;
import org.eclipse.eef.EEFWidgetDescription;
import org.eclipse.eef.EEFWidgetStyle;
import org.eclipse.eef.common.api.utils.Util;
import org.eclipse.eef.core.api.utils.EvalFactory;
import org.eclipse.eef.ide.ui.internal.widgets.styles.EEFColor;
import org.eclipse.eef.ide.ui.internal.widgets.styles.EEFFont;
import org.eclipse.eef.util.EEFConditionalStyleToWidgetStyleSwitch;
import org.eclipse.eef.util.EEFDescriptionToConditionalStylesSwitch;
import org.eclipse.eef.util.EEFDescriptionToWidgetStyleSwitch;
import org.eclipse.emf.ecore.util.Switch;
import org.eclipse.sirius.common.interpreter.api.IInterpreter;
import org.eclipse.sirius.common.interpreter.api.IVariableManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

/**
 * This utility class contains various helper method to manipulate styles.
 *
 * @author sbegaudeau
 */
public class EEFStyleHelper {
	/**
	 * The interpreter.
	 */
	private IInterpreter interpreter;

	/**
	 * The variable manager.
	 */
	private IVariableManager variableManager;

	/**
	 * The constructor.
	 *
	 * @param interpreter
	 *            The interpreter
	 * @param variableManager
	 *            The variable manager
	 */
	public EEFStyleHelper(IInterpreter interpreter, IVariableManager variableManager) {
		this.interpreter = interpreter;
		this.variableManager = variableManager;
	}

	/**
	 * Returns the widget style to use from the given widget description.
	 *
	 * @param widgetDescription
	 *            The widget description
	 * @return The widget style to use
	 */
	public EEFWidgetStyle getWidgetStyle(EEFWidgetDescription widgetDescription) {
		EEFWidgetStyle widgetStyle = null;
		List<EEFConditionalStyle> conditionalStyles = this.getDescriptionToConditionalStylesSwitch().doSwitch(widgetDescription);
		Iterator<EEFConditionalStyle> iterator = conditionalStyles.iterator();
		while (widgetStyle == null && iterator.hasNext()) {
			EEFConditionalStyle conditionalStyle = iterator.next();
			String preconditionExpression = conditionalStyle.getPreconditionExpression();
			Boolean preconditionValid = EvalFactory.of(interpreter, variableManager).logIfInvalidType(Boolean.class).evaluate(preconditionExpression);
			if (preconditionValid != null && preconditionValid.booleanValue()) {
				widgetStyle = this.getConditionalStyleToWidgetStyleSwitch().doSwitch(conditionalStyle);
			}
		}

		if (widgetStyle == null) {
			widgetStyle = this.getDescriptionToWidgetStyleSwitch().doSwitch(widgetDescription);
		}

		return widgetStyle;
	}

	/**
	 * Returns the switch to use to find the conditional styles of a widget description. The switch returned should be
	 * able to handle the default use case, as a result it is highly recommended to return a composed switch with the
	 * default one and new switches. Example:<br>
	 * <code>
	 * return new ComposedSwitch<>(Arrays.asList(new EEFDescriptionToConditionalStylesSwitch(), new
	 * CustomDescriptionToConditionalStylesSwitch()));
	 * </code>
	 *
	 * @return The switch to use to find the conditional styles of a widget description
	 */
	protected Switch<List<EEFConditionalStyle>> getDescriptionToConditionalStylesSwitch() {
		return new EEFDescriptionToConditionalStylesSwitch();
	}

	/**
	 * Returns the switch to use to find the style of a conditional style. The switch returned should be able to handle
	 * the default use case, as a result it is highly recommended to return a composed switch with the default one and
	 * new switches. Example:<br>
	 * <code>
	 * return new ComposedSwitch<>(Arrays.asList(new EEFConditionalStyleToWidgetStyleSwitch(), new
	 * CustomConditionalStyleToWidgetStyleSwitch()));
	 * </code>
	 *
	 * @return The switch to use to find the style of a conditional style
	 */
	protected Switch<EEFWidgetStyle> getConditionalStyleToWidgetStyleSwitch() {
		return new EEFConditionalStyleToWidgetStyleSwitch();
	}

	/**
	 * Returns the switch to use to find the style of a description. The switch returned should be able to handle the
	 * default use case, as a result it is highly recommended to return a composed switch with the default one and new
	 * switches. Example:<br>
	 * <code>
	 * return new ComposedSwitch<>(Arrays.asList(new EEFDescriptionToWidgetStyleSwitch(), new
	 * EEFDescriptionToWidgetStyleSwitch()));
	 * </code>
	 *
	 * @return The switch to use to find the style of a description.
	 */
	protected Switch<EEFWidgetStyle> getDescriptionToWidgetStyleSwitch() {
		return new EEFDescriptionToWidgetStyleSwitch();
	}

	/**
	 * Uses the given expressions to update the style using the given callback.
	 *
	 * @param fontNameExpression
	 *            The expression used to compute the name of the font
	 * @param fontSizeExpression
	 *            The expression used to compute the size of the font
	 * @param fontStyleExpression
	 *            The expression used to compute the style of the font
	 * @param defaultFont
	 *            The default font
	 * @param backgroundColorExpression
	 *            The expression used to compute the background color of the font
	 * @param foregroundColorExpression
	 *            The expression used to compute the foreground color of the font
	 * @param callback
	 *            The callback which will be called when a property of the style has been computed
	 */
	public void applyTextStyle(String fontNameExpression, String fontSizeExpression, String fontStyleExpression, Font defaultFont,
			String backgroundColorExpression, String foregroundColorExpression, IEEFTextStyleCallback callback) {
		if (!Util.isBlank(foregroundColorExpression)) {
			this.applyForegroundColor(foregroundColorExpression, callback);
		} else {
			callback.applyForegroundColor(new EEFColor((Color) null));
		}
		if (!Util.isBlank(backgroundColorExpression)) {
			this.applyBackgroundColor(backgroundColorExpression, callback);
		} else {
			callback.applyBackgroundColor(new EEFColor((Color) null));
		}

		String fontStyleValue = EvalFactory.of(interpreter, variableManager).logIfInvalidType(String.class).evaluate(fontStyleExpression);
		this.applyFont(fontNameExpression, fontSizeExpression, defaultFont, callback, fontStyleValue);
		this.applyFontStyle(callback, fontStyleValue);
	}

	/**
	 * Applies the foreground color computed using the given foreground color expression.
	 *
	 * @param foregroundColorExpression
	 *            The expression
	 * @param callback
	 *            The callback
	 */
	private void applyForegroundColor(String foregroundColorExpression, IEEFTextStyleCallback callback) {
		String foregroundColorCode = EvalFactory.of(interpreter, variableManager).logIfInvalidType(String.class).evaluate(foregroundColorExpression);
		if (!Util.isBlank(foregroundColorCode)) {
			callback.applyForegroundColor(new EEFColor(foregroundColorCode));
		}
	}

	/**
	 * Applies the background color computed using the given background color expression.
	 *
	 * @param backgroundColorExpression
	 *            The expression
	 * @param callback
	 *            The callback
	 */
	private void applyBackgroundColor(String backgroundColorExpression, IEEFTextStyleCallback callback) {
		String backgroundColorCode = EvalFactory.of(interpreter, variableManager).logIfInvalidType(String.class).evaluate(backgroundColorExpression);
		if (!Util.isBlank(backgroundColorCode)) {
			callback.applyBackgroundColor(new EEFColor(backgroundColorCode));
		}
	}

	/**
	 * Applies the font computed using the given expressions.
	 *
	 * @param fontNameExpression
	 *            The expression used to compute the name of the font
	 * @param fontSizeExpression
	 *            The expression used to compute the size of the font
	 * @param defaultFont
	 *            The default font
	 * @param callback
	 *            The callback
	 * @param fontStyleValue
	 *            The value of the font style (strikeout and/or underline)
	 */
	private void applyFont(String fontNameExpression, String fontSizeExpression, Font defaultFont, IEEFTextStyleCallback callback,
			String fontStyleValue) {
		FontData defaultFontData = defaultFont.getFontData()[0];
		String fontName = EvalFactory.of(interpreter, variableManager).logIfInvalidType(String.class).defaultValue(defaultFontData.getName())
				.evaluate(fontNameExpression);
		int fontSize = EvalFactory.of(interpreter, variableManager).logIfInvalidType(Integer.class)
				.defaultValue(Integer.valueOf(defaultFontData.getHeight())).evaluate(fontSizeExpression).intValue();

		int fontStyle = defaultFontData.getStyle();
		if (fontStyleValue == null) {
			fontStyle = 0;
		} else if (fontStyleValue.contains("bold")) { //$NON-NLS-1$
			fontStyle = fontStyle | SWT.BOLD;
		} else if (fontStyleValue.contains("italic")) { //$NON-NLS-1$
			fontStyle = fontStyle | SWT.ITALIC;
		}
		EEFFont font = new EEFFont(fontName, fontSize, fontStyle);
		callback.applyFont(font);
	}

	/**
	 * Applies the font style (underline and/or strikeout).
	 *
	 * @param callback
	 *            The callback
	 * @param fontStyleValue
	 *            The font style value
	 */
	private void applyFontStyle(IEEFTextStyleCallback callback, String fontStyleValue) {
		boolean strikeout = false;
		boolean underline = false;
		if (fontStyleValue != null && fontStyleValue.contains("underline")) { //$NON-NLS-1$
			underline = true;
		}
		if (fontStyleValue != null && fontStyleValue.contains("strike_through")) { //$NON-NLS-1$
			strikeout = true;
		}
		callback.applyFontStyle(strikeout, underline);
	}

	/**
	 * This class will be called to update the style of the text.
	 *
	 * @author sbegaudeau
	 */
	public interface IEEFTextStyleCallback {
		/**
		 * Applies the given font to the widget.
		 *
		 * @param font
		 *            The font
		 */
		void applyFont(EEFFont font);

		/**
		 * Applies the strikeout and/or underline styles.
		 *
		 * @param strikeout
		 *            <code>true</code> if the widget should be striked out, <code>false</code> otherwise
		 * @param underline
		 *            <code>true</code> if the widget should be underlined, <code>false</code> otherwise
		 */
		void applyFontStyle(boolean strikeout, boolean underline);

		/**
		 * Applies the given foreground color to the widget.
		 *
		 * @param color
		 *            The foreground color
		 */
		void applyForegroundColor(EEFColor color);

		/**
		 * Applies the given background color to the widget.
		 *
		 * @param color
		 *            The background color
		 */
		void applyBackgroundColor(EEFColor color);
	}
}

Back to the top