Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 408fc665e1eda1a803b34d761b14f199d3852ed2 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*****************************************************************************
 * Copyright (c) 2014 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:
 *	Gabriel Pascual (ALL4TEC) gabriel.pascual@all4tec.net - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.property;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.papyrus.infra.emf.providers.EMFContentProvider;
import org.eclipse.papyrus.infra.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.Activator;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.ModelStyleSheets;
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.StylesheetsPackage;
import org.eclipse.papyrus.infra.widgets.editors.MultipleValueSelectorDialog;
import org.eclipse.papyrus.infra.widgets.providers.AbstractStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.selectors.ReferenceSelector;
import org.eclipse.papyrus.views.properties.creation.EcorePropertyEditorFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbenchPropertyPage;
import org.eclipse.ui.dialogs.PropertyPage;



/**
 * Preference page to associate set of style sheets to Papyrus project.
 * <p>
 * The preference is stored in a file with EMF format. The target is file .settings/stylesheets.xmi
 * </p>
 *
 * @author gpascual
 */
public class StyleSheetsPropertyPage extends PropertyPage implements IWorkbenchPropertyPage {

	/** Preference resource of project to load and save. */
	private Resource resource = null;

	/** Style sheets model used to manage style sheets list as container. */
	private ModelStyleSheets modelStyleSheets = null;

	/** Content provider which used for different displays. */
	private IContentProvider contentProvider = null;

	/** Label provider which used for different displays. */
	private ILabelProvider labelProvider = null;

	/** Viewer for style sheets list. */
	private TreeViewer styleSheetsViewer = null;

	/** Button to delete style sheets in project preferences. */
	private Button removeButton = null;


	/**
	 * Default Constructor.
	 *
	 */
	public StyleSheetsPropertyPage() {
		//This preference is stored with EMF format : .settings/stylesheets.xmi
		setPreferenceStore(null);
		noDefaultAndApplyButton();
	}




	/**
	 * Get Style sheets model file to manage preference.
	 * 
	 * @return a new style sheets list or the existing one
	 */
	private EList<EObject> getProjectStylesheets() {
		//Get selected project
		Object pageElement = getElement().getAdapter(IProject.class);

		//Initialize preferences data
		ResourceSet resourceSet = new ResourceSetImpl();
		EList<EObject> styleSheetsList = null;

		//Check if element is a project
		if(pageElement instanceof IProject) {
			// Build path of preference file
			ProjectScope pageProject = new ProjectScope((IProject)pageElement);
			IPath preferencePath = pageProject.getLocation().append("stylesheets.xmi");

			// Check path is valid
			if(preferencePath != null) {

				// Verify presence of file in project
				IPath projectRelativePath = preferencePath.makeRelativeTo(((IProject)pageElement).getLocation());
				IFile preferenceFile = ((IProject)pageElement).getFile(projectRelativePath);
				if(preferenceFile.exists()) {

					//Load existing preference file
					resource = resourceSet.getResource(URI.createPlatformResourceURI(preferenceFile.getFullPath().toOSString(), true), true);
					styleSheetsList = resource.getContents();


				} else {

					//Create default preference file
					resource = resourceSet.createResource(URI.createPlatformResourceURI(preferenceFile.getFullPath().toOSString(), true));
					styleSheetsList = resource.getContents();
				}
			}
		}

		return styleSheetsList;
	}

	/**
	 * 
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createContents(Composite parent) {

		// Create the container composite
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout containerLayout = new GridLayout(2, false);
		container.setLayout(containerLayout);

		// Initialize data and providers fro page
		initializeDataPage();
		initializeProviders();

		// Create different components
		createStyleSheetsPageViewer(container);
		createStyleSheetsPageButtons(container);

		//Update state buttons
		updateButton();

		return container;
	}




	/**
	 * Initialize preference page.
	 */
	private void initializeDataPage() {

		EList<EObject> stylesheetsModel = getProjectStylesheets();
		modelStyleSheets = (ModelStyleSheets)StylesheetsPackage.eINSTANCE.getEFactoryInstance().create(StylesheetsPackage.Literals.MODEL_STYLE_SHEETS);
		modelStyleSheets.getStylesheets();
		List<EObject> mirrorList = new ArrayList<EObject>();
		mirrorList.addAll(stylesheetsModel);

		for(EObject object : mirrorList) {

			if(object instanceof StyleSheet) {
				modelStyleSheets.getStylesheets().add((StyleSheet)object);
			}
		}



	}




	/**
	 * Initialize the different providers to display style sheets list.
	 */
	private void initializeProviders() {

		contentProvider = new EMFContentProvider(modelStyleSheets, StylesheetsPackage.eINSTANCE.getModelStyleSheets_Stylesheets()) {

			@Override
			protected IStructuredContentProvider getSemanticProvider(final EObject editedEObject, final EStructuralFeature feature) {

				// Use a standard content provider
				return new AbstractStaticContentProvider() {

					public Object[] getElements() {
						List<Object> result = new LinkedList<Object>();
						if(editedEObject instanceof ModelStyleSheets) {
							result.addAll(modelStyleSheets.getStylesheets());
						}
						return result.toArray();
					}
				};
			}
		};

		labelProvider = new StylesheetsModelLabelProvider();
	}




	/**
	 * Create viewer to display content of style sheets model.
	 * 
	 * @param parent
	 *        Parent composite where compound will added
	 */
	private void createStyleSheetsPageViewer(Composite parent) {


		// Set layout and behavior to tree viewer
		styleSheetsViewer = new TreeViewer(parent);
		styleSheetsViewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		styleSheetsViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				updateButton();

			}


		});
		styleSheetsViewer.setContentProvider(contentProvider);
		styleSheetsViewer.setLabelProvider(labelProvider);
		styleSheetsViewer.setInput(modelStyleSheets);
	}




	/**
	 * Create buttons which are associated with viewer.
	 * 
	 * @param parent
	 *        Parent composite where compound will added
	 */
	private void createStyleSheetsPageButtons(Composite parent) {

		//Build composite which contains buttons
		Composite buttonsComposite = new Composite(parent, SWT.NONE);
		buttonsComposite.setLayout(new GridLayout());
		buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));


		//Add button
		Button addButton = new Button(buttonsComposite, SWT.PUSH);
		addButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		addButton.setImage(Activator.imageDescriptorFromPlugin("org.eclipse.papyrus.infra.widgets", "/icons/Add_12x12.gif").createImage());
		addButton.addSelectionListener(new SelectionListener() {

			/**
			 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 *
			 * @param e
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {

				ReferenceSelector selector = new ReferenceSelector(true);
				selector.setContentProvider((IStaticContentProvider)contentProvider);
				selector.setLabelProvider(labelProvider);

				// Use common component for add dialog and parameterize it
				MultipleValueSelectorDialog vDialog = new MultipleValueSelectorDialog(getShell(), selector, "CSS Style Sheet");
				vDialog.setContextElement(modelStyleSheets);
				vDialog.setLabelProvider(labelProvider);
				vDialog.setFactory(new EcorePropertyEditorFactory(StylesheetsPackage.Literals.MODEL_STYLE_SHEETS__STYLESHEETS));

				// Handle dialog result
				int result = vDialog.open();
				if(result == Dialog.OK) {

					Object[] resultArray = vDialog.getResult();
					if(resultArray != null) {
						refreshStyleSheets(resultArray);
					}
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				//Nothing to do
			}
		});

		removeButton = new Button(buttonsComposite, SWT.PUSH);
		removeButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		removeButton.setImage(Activator.imageDescriptorFromPlugin("org.eclipse.papyrus.infra.widgets", "/icons/Delete_12x12.gif").createImage());
		removeButton.addSelectionListener(new SelectionListener() {

			/**
			 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 *
			 * @param e
			 */
			@Override
			public void widgetSelected(SelectionEvent e) {
				ISelection selection = styleSheetsViewer.getSelection();
				if(selection instanceof IStructuredSelection) {

					// Handle selection
					Object firstElement = ((IStructuredSelection)selection).getFirstElement();
					if(firstElement instanceof StyleSheet) {
						modelStyleSheets.getStylesheets().remove(firstElement);
					}

					// Refresh viewer
					styleSheetsViewer.setInput(modelStyleSheets);

				}

			}

			/**
			 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
			 *
			 * @param e
			 */
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// Nothing to do

			}
		});
	}

	/**
	 * Fill style sheets viewer with selected style sheets.
	 * 
	 * @param result
	 *        Result from dialog selection
	 */
	protected void refreshStyleSheets(Object[] result) {

		// Add selected CSS style sheets
		for(Object object : result) {

			//Check if object is a style sheet
			if(object instanceof StyleSheet) {
				modelStyleSheets.getStylesheets().add((StyleSheet)object);

			}
		}

		// Refresh viewer
		styleSheetsViewer.setInput(modelStyleSheets);
	}




	/**
	 * Update state buttons.
	 */
	protected void updateButton() {
		ISelection selection = styleSheetsViewer.getSelection();

		removeButton.setEnabled(!selection.isEmpty());


	}

	/**
	 * 
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
	 *
	 * @return
	 */
	@Override
	public boolean performOk() {
		boolean performOK = false;

		try {
			resource.getContents().addAll(modelStyleSheets.getStylesheets());

			// Save preference file
			resource.save(Collections.EMPTY_MAP);
			performOK = true;
		} catch (IOException e) {
			Activator.log.error(e);
		}

		return performOK;
	}



	/**
	 * 
	 * Label provider for style sheets list in different displays.
	 * 
	 * @author gpascual
	 *
	 */
	public class StylesheetsModelLabelProvider extends EMFLabelProvider {

		/**
		 * Default constructor.
		 *
		 */
		public StylesheetsModelLabelProvider() {
			super();
		}

		/**
		 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
		 *
		 * @param element
		 * @return
		 */
		@Override
		public String getText(Object element) {
			String text = "";

			if(element instanceof StyleSheetReference) {
				text = ((StyleSheetReference)element).getPath();
			} else if(element instanceof EmbeddedStyleSheet) {
				text = ((EmbeddedStyleSheet)element).getLabel();
			}

			return text;
		}



	}



}

Back to the top