Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a0eda76c54ad890b9a50b7113d8590fce42308b (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*****************************************************************************
 * Copyright (c) 2012 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.configuration.handler;

import java.util.Map;

import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
import org.eclipse.papyrus.infra.gmfdiag.css.Expression;
import org.eclipse.papyrus.infra.gmfdiag.css.HexColor;
import org.eclipse.papyrus.infra.gmfdiag.css.Name;
import org.eclipse.papyrus.infra.gmfdiag.css.Number;
import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
import org.eclipse.papyrus.infra.gmfdiag.css.StringValue;
import org.eclipse.papyrus.infra.gmfdiag.css.Subterm;
import org.eclipse.papyrus.infra.gmfdiag.css.Term;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetContentProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetLabelProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsFactory;
import org.eclipse.papyrus.infra.gmfdiag.css.util.CssSwitch;
import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;
import org.eclipse.papyrus.infra.widgets.editors.BooleanCheckbox;
import org.eclipse.papyrus.infra.widgets.editors.EnumRadio;
import org.eclipse.papyrus.infra.widgets.editors.ICommitListener;
import org.eclipse.papyrus.infra.widgets.editors.ReferenceDialog;
import org.eclipse.papyrus.infra.widgets.editors.StringEditor;
import org.eclipse.papyrus.infra.widgets.editors.StringFileSelector;
import org.eclipse.papyrus.infra.widgets.providers.StaticContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;


public class StyleCreationDialog extends TrayDialog {

	private final Map<SelectorCondition, Boolean> conditions;

	private final Map<Declaration, Boolean> declarations;

	private final String selectorName;

	private boolean useSelectorName = true;

	private boolean diagramRestriction = false;

	private String cssClass;

	private View contextView;

	private StyleSheet stylesheet;

	/**
	 * 
	 * @param shell
	 * @param conditions
	 *        inout
	 * @param declarations
	 *        inout
	 * @param selectorName
	 */
	public StyleCreationDialog(Shell shell, Map<SelectorCondition, Boolean> conditions, Map<Declaration, Boolean> declarations, String selectorName, View context) {
		super(shell);
		this.conditions = conditions;
		this.declarations = declarations;
		this.selectorName = selectorName;
		this.contextView = context;
	}

	@Override
	public void create() {
		super.create();
		Composite parent = getDialogArea();

		CTabFolder tabFolder = new CTabFolder(parent, SWT.BORDER);
		tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));

		CTabItem conditionsTab = new CTabItem(tabFolder, SWT.NONE);
		CTabItem declarationsTab = new CTabItem(tabFolder, SWT.NONE);
		CTabItem stylesheetTab = new CTabItem(tabFolder, SWT.NONE);

		conditionsTab.setText("Conditions");
		declarationsTab.setText("Properties");
		stylesheetTab.setText("Stylesheet");

		Composite conditionsContainer = new Composite(tabFolder, SWT.NONE);
		conditionsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
		conditionsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		conditionsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);

		conditionsTab.setControl(conditionsContainer);

		Composite declarationsContainer = new Composite(tabFolder, SWT.NONE);
		declarationsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
		declarationsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		declarationsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);

		declarationsTab.setControl(declarationsContainer);

		Composite stylesheetContainer = new Composite(tabFolder, SWT.NONE);
		stylesheetContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
		stylesheetContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		stylesheetContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);

		stylesheetTab.setControl(stylesheetContainer);

		createConditions(conditionsContainer);
		createDeclarations(declarationsContainer);
		createStylesheet(stylesheetContainer);

		getShell().setText("New Style");
		getShell().pack();
	}

	protected void createConditions(Composite parent) {
		parent.setLayout(new GridLayout(3, false));

		Label conditionsLabel = new Label(parent, SWT.WRAP);
		conditionsLabel.setText("Select the condition(s) under which the style will be applied.");
		conditionsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		createElementNameSection(parent);
		createAttributeSelectorsSection(parent);
		createStyleNameSection(parent);
	}

	protected void createElementNameSection(Composite parent) {
		EnumRadio diagramRestrictionWidget = new EnumRadio(parent, SWT.NONE, "Diagram:");
		diagramRestrictionWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		ILabelProvider labelProvider = new LabelProvider() {

			@Override
			public String getText(Object element) {
				Boolean value = (Boolean)element;
				return value ? contextView.getDiagram().getType() + " only" : "Any diagram";
			}
		};

		diagramRestrictionWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
		diagramRestrictionWidget.setValue(this.diagramRestriction);
		diagramRestrictionWidget.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				diagramRestriction = (Boolean)((EnumRadio)editor).getValue();
			}
		});

		EnumRadio selectorNameWidget = new EnumRadio(parent, SWT.NONE, "Applies to:");
		selectorNameWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		labelProvider = new LabelProvider() {

			@Override
			public String getText(Object element) {
				Boolean value = (Boolean)element;
				return value ? selectorName + " only" : "Any kind of element";
			}
		};

		selectorNameWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
		selectorNameWidget.setValue(this.useSelectorName);
		selectorNameWidget.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				useSelectorName = (Boolean)((EnumRadio)editor).getValue();
			}
		});
	}

	protected void createAttributeSelectorsSection(Composite parent) {
		if(conditions.isEmpty()) {
			return;
		}

		Label detailLabel = new Label(parent, SWT.NONE);
		detailLabel.setText("If the following properties are matched:");
		detailLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		for(SelectorCondition condition : conditions.keySet()) {
			final Attribute currentCondition = (Attribute)condition;

			String attributeLabel = currentCondition.getName();
			if(currentCondition.getValue() != null) {
				attributeLabel += " " + currentCondition.getValue().getOperator() + " " + currentCondition.getValue().getValue();
			}

			BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, attributeLabel);
			checkbox.setValue(conditions.get(currentCondition));

			checkbox.addCommitListener(new ICommitListener() {

				public void commit(AbstractEditor editor) {
					conditions.put(currentCondition, ((BooleanCheckbox)editor).getValue());
				}
			});
		}
	}

	protected void createStyleNameSection(Composite parent) {
		Label styleNameLabel = new Label(parent, SWT.WRAP);

		String label = "If a name is used for this style, it will have to be applied manually:";

		styleNameLabel.setText(label);
		styleNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		StringEditor styleNameEditor = new StringEditor(parent, SWT.NONE, "Style name:");
		styleNameEditor.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
		//		styleNameEditor.setToolTipText(label);
		styleNameEditor.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				cssClass = (String)((StringEditor)editor).getValue();
			}

		});
	}

	protected void createDeclarations(Composite parent) {
		parent.setLayout(new GridLayout(3, false));
		//		parent.setLayout(new FillLayout());

		Label declarationsLabel = new Label(parent, SWT.WRAP);
		declarationsLabel.setText("Select the properties you want to set. Unchecked properties will keep their default value (Which might be inherited from another style).");
		declarationsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));

		for(Declaration declaration : declarations.keySet()) {
			String label = declaration.getProperty() + ": " + getLabel(declaration.getExpression());
			BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, label);
			checkbox.setValue(declarations.get(declaration));

			final Declaration currentDeclaration = declaration;
			checkbox.addCommitListener(new ICommitListener() {

				public void commit(AbstractEditor editor) {
					declarations.put(currentDeclaration, ((BooleanCheckbox)editor).getValue());
				}
			});
		}
	}

	protected void createStylesheet(Composite parent) {
		//TODO: Use a preference to remember the last edited Stylesheet (Per model? Diagram? Workspace? With user choice?)

		parent.setLayout(new GridLayout(1, false));

		//Create or use an existing External Stylesheet
		StringFileSelector externalStylesheet = new StringFileSelector(parent, SWT.NONE);
		externalStylesheet.setAllowFileSystem(false);
		externalStylesheet.setLabel("External stylesheet:");
		externalStylesheet.setToolTipText("Create or use an existing external CSS Stylesheet");
		externalStylesheet.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		externalStylesheet.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				String path = (String)((StringEditor)editor).getValue();
				StyleSheetReference stylesheetReference = StylesheetsFactory.eINSTANCE.createStyleSheetReference();
				stylesheetReference.setPath(path);
				stylesheet = stylesheetReference;
			}
		});

		Label orLabel = new Label(parent, SWT.NONE);
		orLabel.setText("-- OR --");

		//Create a new Embedded Stylesheet
		StringEditor embeddedStylesheet = new StringEditor(parent, SWT.NONE);
		embeddedStylesheet.setLabel("New local stylesheet:");
		embeddedStylesheet.setToolTipText("Create a new local stylesheet. The stylesheet will be embedded in the current model. Unsupported yet");
		embeddedStylesheet.setReadOnly(true);
		embeddedStylesheet.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		embeddedStylesheet.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				String name = (String)((StringEditor)editor).getValue();
				EmbeddedStyleSheet embeddedStylesheet = StylesheetsFactory.eINSTANCE.createEmbeddedStyleSheet();
				embeddedStylesheet.setLabel(name);
				stylesheet = embeddedStylesheet;
			}
		});

		orLabel = new Label(parent, SWT.NONE);
		orLabel.setText("-- OR --");

		//Use an existing applied stylesheet (Either Reference or Embedded)
		ReferenceDialog appliedStylesheet = new ReferenceDialog(parent, SWT.NONE);
		appliedStylesheet.setLabel("Applied stylesheet:");
		appliedStylesheet.setToolTipText("Use an existing stylesheet, from the stylesheets applied to the current model");
		appliedStylesheet.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		appliedStylesheet.setContentProvider(new CSSStyleSheetContentProvider(contextView));
		appliedStylesheet.setLabelProvider(new CSSStyleSheetLabelProvider());
		appliedStylesheet.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				StyleSheet value = (StyleSheet)((ReferenceDialog)editor).getValue();
				stylesheet = value;
			}
		});

	}

	public String getCSSClass() {
		if(cssClass != null) {
			return cssClass.trim().equals("") ? null : cssClass.trim();
		}
		return null;
	}

	protected String getLabel(Expression expression) {
		String label = getLabel(expression.getTerms());
		for(Subterm subTerm : expression.getSubterms()) {
			if(subTerm.getOperator() != null) {
				label += subTerm.getOperator();
			}
			label += " " + getLabel(subTerm.getTerm());
		}
		return label;
	}

	protected String getLabel(Term term) {
		return (new CssSwitch<String>() {

			@Override
			public String caseHexColor(HexColor term) {
				return '#' + term.getValue();
			}

			@Override
			public String caseName(Name term) {
				return term.getValue();
			}

			@Override
			public String caseStringValue(StringValue term) {
				return "\"" + term.getValue() + "\"";
			}

			@Override
			public String caseNumber(Number term) {
				String label = "";
				if(term.getOp() != null) {
					label += term.getOp().getOperator();
				}
				label += term.getValue();
				return label;
			}
		}).doSwitch(term);
	}

	public StyleSheet getStylesheet() {
		return stylesheet;
	}

	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	public Composite getDialogArea() {
		return (Composite)super.getDialogArea();
	}

	public boolean useSelectorName() {
		return useSelectorName;
	}

	public boolean getDiagramRestriction() {
		return diagramRestriction;
	}

}

Back to the top