Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 017a8d46c46006bb73743b55e2174a503ed8360d (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
/*****************************************************************************
 * 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.papyrus.infra.gmfdiag.css.Attribute;
import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
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.widgets.editors.AbstractEditor;
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.swt.SWT;
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 AbstractStyleDialog {

	private StringFileSelector externalStylesheetEditor;

	private StringEditor embeddedStylesheetEditor;

	private ReferenceDialog appliedStylesheetEditor;

	private boolean ignoreEvents = false;

	private AbstractEditor stylesheetSource;

	private StyleSheet stylesheet;

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

	@Override
	public void create() {
		super.create();

		Composite parent = getDialogArea();

		CTabItem stylesheetTab = new CTabItem(tabFolder, SWT.NONE);
		stylesheetTab.setText("Stylesheet");

		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);
		createStylesheet(stylesheetContainer);

		getShell().setText("Create a new style");
	}

	@Override
	protected boolean isValid() {
		boolean result = true;

		// There must be a stylesheet
		if (getStyleSheet() == null) {
			setError("You must select a Stylesheet");
			result = false;
		}

		// EmbeddedStyleSheets are not yet supported
		if (getStyleSheet() instanceof EmbeddedStyleSheet) {
			setError("Edition of embedded stylesheets is not yet supported. Please select an external stylesheet");
			result = false;
		}

		return super.isValid() && result;
	}

	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
		externalStylesheetEditor = new StringFileSelector(parent, SWT.NONE);
		externalStylesheetEditor.setFilters(new String[] { "*.css", "*" }, new String[] { "CSS Stylesheets (*.css)", "All (*)" });
		externalStylesheetEditor.setAllowFileSystem(false);
		externalStylesheetEditor.setLabel("External stylesheet:");
		externalStylesheetEditor.setToolTipText("Create or use an existing external CSS Stylesheet");
		externalStylesheetEditor.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		externalStylesheetEditor.addCommitListener(new ICommitListener() {

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


				if ((path != null && !"".equals(path)) || stylesheetSource == externalStylesheetEditor) {
					StyleSheetReference stylesheetReference = null;
					if (path != null && !"".equals(path)) {
						stylesheetReference = StylesheetsFactory.eINSTANCE.createStyleSheetReference();
						stylesheetReference.setPath(path);
					}

					stylesheet = stylesheetReference;
					resetStylesheetEditors(externalStylesheetEditor);
					updateButtons();
				}
			}
		});

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

		// Create a new Embedded Stylesheet
		embeddedStylesheetEditor = new StringEditor(parent, SWT.NONE);
		embeddedStylesheetEditor.setLabel("New local stylesheet:");

		// FIXME: Change the tooltip text when the X-Text editor can edit local stylesheets
		embeddedStylesheetEditor.setToolTipText("Enter the new local stylesheet's name");
		// embeddedStylesheetEditor.setToolTipText("Create a new local stylesheet. The stylesheet will be embedded in the current model. Unsupported yet");

		// FIXME: Set read only to false when the X-Text editor can edit local stylesheets
		embeddedStylesheetEditor.setReadOnly(true);
		embeddedStylesheetEditor.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		embeddedStylesheetEditor.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				// TODO: Check empty names & set the stylesheet to null when the name is null/empty
				String name = (String) ((StringEditor) editor).getValue();
				EmbeddedStyleSheet embeddedStylesheet = StylesheetsFactory.eINSTANCE.createEmbeddedStyleSheet();
				embeddedStylesheet.setLabel(name);
				stylesheet = embeddedStylesheet;
				resetStylesheetEditors(embeddedStylesheetEditor);
				updateButtons();
			}
		});

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

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

			public void commit(AbstractEditor editor) {
				StyleSheet value = (StyleSheet) ((ReferenceDialog) editor).getValue();
				if (!(ignoreEvents && value == null)) {
					stylesheet = value;
					resetStylesheetEditors(appliedStylesheetEditor);
				}
				updateButtons();
			}
		});

	}

	private void resetStylesheetEditors(AbstractEditor stylesheetSource) {
		if (ignoreEvents) {
			return;
		}

		ignoreEvents = true;
		if (stylesheetSource != externalStylesheetEditor) {
			externalStylesheetEditor.setValue(null);
		}

		if (stylesheetSource != embeddedStylesheetEditor) {
			embeddedStylesheetEditor.setValue(null);
		}

		if (stylesheetSource != appliedStylesheetEditor) {
			appliedStylesheetEditor.setValue(null);
		}

		this.stylesheetSource = stylesheetSource;
		ignoreEvents = false;
	}

	public StyleSheet getStyleSheet() {
		return stylesheet;
	}

}

Back to the top