Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b7f9e830e905aa72ffb7e1995a5334a533cfef20 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*****************************************************************************
 * 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.properties.dialog;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.PapyrusImageUtils;
import org.eclipse.papyrus.infra.emf.providers.EMFContentProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.Activator;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsPackage;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.Theme;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.infra.widgets.editors.TreeSelectorDialog;
import org.eclipse.papyrus.infra.widgets.providers.AbstractStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.WorkspaceContentProvider;
import org.eclipse.papyrus.infra.widgets.util.FileUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
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.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;


/**
 * Dialog to define a local theme from a selected CSS style.
 * 
 * @author gpascual
 */
public class CSSThemeCreationDialog extends Dialog {

	/** Title for icon selection dialog. */
	private static final String ICON_SELECTION_DIALOG_TITLE = "Icon selection";

	/** Label for workspace menu. */
	private static final String WORKSPACE_MENU_LABEL = "Workspace";

	/** Label for file system menu. */
	private static final String FILE_SYSTEM_MENU_LABEL = "File System";

	/** Id for file system menu item. */
	private static final int FILESYSTEM_MENU_ID = 15;

	/** Id for workspace menu item. */
	private static final int WORKSPACE_MENU_ID = 12;

	/** Label for browse button to select theme icon. */
	private static final String BROWSE_BUTTON_LABEL = "Browse...";

	/** List of valid extensions for an icon. */
	private static List<String> filterExtensions = Arrays.asList(new String[]{ "*.gif;*.png;*.jpeg", "*.gif", "*.png", "*.jpeg" });

	/** List of name associated to valid extensions. */
	private List<String> filterNames = Arrays.asList(new String[]{ "All images", "GIF Icon", "PNG Icon", "JPEG Icon" });

	/** Text for dialog title. */
	private static final String DIALOG_TITLE = "CSS Theme Definition";

	/** Text for theme icon field. */
	private static final String ICON_PATH_LABEL = "Icon";

	/** Text for theme label field. */
	private static final String THEME_NAME_LABEL = "Label";

	/** Id of browse button. */
	private static final int BROWSE_BUTTON_ID = 13;

	/** Theme to definr with dialog. */
	private Theme theme = null;

	/** Theme label field. */
	private Text themeLabelField = null;

	/** Theme icon path field. */
	private Text iconPathField = null;

	/** Theme style sheets viewer. */
	private TreeViewer styleSheetsViewer = null;

	/** Menu of browse button to select theme icon. */
	private Menu browseMenu = null;

	/**
	 * Default constructor.
	 *
	 * @param parentShell
	 *        shell of parent widget
	 * @param theme
	 *        theme to edit
	 */
	public CSSThemeCreationDialog(Shell parentShell, Theme theme) {
		super(parentShell);

		// Set theme to edit
		this.theme = theme;
		initialiseFilterLabels(filterNames, filterExtensions);
	}

	/**
	 * Gets the filter labels.
	 *
	 * @param filterNames
	 *        the filter names
	 * @param filterExtensions
	 *        the filter extensions
	 */
	private void initialiseFilterLabels(List<String> filterNames, List<String> filterExtensions) {
		int size = Math.min(filterNames.size(), filterExtensions.size());
		String[] filters = new String[size];
		for(int i = 0; i < size; i++) {
			filters[i] = filterNames.get(i) + " (" + filterExtensions.get(i) + ")";
		}

		this.filterNames = Arrays.asList(filters);
	}

	/**
	 * @see org.eclipse.jface.dialogs.Dialog#getInitialSize()
	 *
	 * @return
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(320, 360);
	}

	/**
	 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
	 *
	 * @return
	 */
	@Override
	protected boolean isResizable() {

		// Set dialog resizable
		return true;
	}

	/**
	 * 
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 *
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);

		// Add title and icon to dialog
		newShell.setText(DIALOG_TITLE);
		newShell.setImage(PapyrusImageUtils.getDefaultIcon());
	}


	/**
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 *
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {

		// Create main container to dialog
		Composite mainComposite = (Composite)super.createDialogArea(parent);
		mainComposite.setLayout(new GridLayout(2, false));

		// Add different parts to dialog
		createThemeLabelPart(mainComposite);
		createThemeIconPart(mainComposite);
		createStyleSheetsTreeComposite(mainComposite);

		// Initialise tree viewer 
		initialiseTree();

		return mainComposite;
	}

	/**
	 * Create all components for theme label.
	 * 
	 * @param parent
	 *        Composite where components will be added
	 */
	private void createThemeLabelPart(Composite parent) {

		// Label for theme name field
		Label themeNameLabel = new Label(parent, SWT.NONE);
		themeNameLabel.setText(THEME_NAME_LABEL);

		// Field to edit theme label
		themeLabelField = new Text(parent, SWT.BORDER);
		GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		themeLabelField.setLayoutData(layoutData);

		// Add behavior to text field
		themeLabelField.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				theme.setLabel(themeLabelField.getText());
			}
		});
	}

	/**
	 * Create all components for theme icon.
	 * 
	 * @param parent
	 *        Composite where components will be added
	 */
	private void createThemeIconPart(Composite parent) {

		//Label for theme icon field
		Label themeIconLabel = new Label(parent, SWT.NONE);
		themeIconLabel.setText(ICON_PATH_LABEL);

		// Field to edit icon path for theme
		iconPathField = new Text(parent, SWT.BORDER);
		iconPathField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		// Add behavior to text field
		iconPathField.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				theme.setIcon(iconPathField.getText());
			}
		});


		// Add browse button to help user
		Button browseButton = createButton(parent, BROWSE_BUTTON_ID, BROWSE_BUTTON_LABEL, false);

		browseMenu = new Menu(browseButton);

		createMenuItem(browseMenu, FILE_SYSTEM_MENU_LABEL, FILESYSTEM_MENU_ID);
		createMenuItem(browseMenu, WORKSPACE_MENU_LABEL, WORKSPACE_MENU_ID);

	}

	/**
	 * Create menu item.
	 * 
	 * @param parentMenu
	 *        Menu where it will be added
	 * @param label
	 *        Label of menu item
	 * @param menuId
	 */
	private void createMenuItem(Menu parentMenu, String label, int menuId) {

		MenuItem menuItem = new MenuItem(parentMenu, SWT.NONE);
		menuItem.setText(label);
		menuItem.setData(new Integer(menuId));
		menuItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				menuSelected(((Integer)e.widget.getData()).intValue());
			}
		});


	}

	/**
	 * Action to run when a menu is slected.
	 * 
	 * @param menuId
	 *        ID of selected menu
	 */
	private void menuSelected(int menuId) {
		switch(menuId) {
		case WORKSPACE_MENU_ID:
			browseWorkspace();
			break;
		case FILESYSTEM_MENU_ID:
			browseFileSytem();
			break;
		default:
			// Nothing to do 
			break;
		}
	}

	/**
	 * Browse file in file system.
	 */
	private void browseFileSytem() {
		File file = getFile(iconPathField.getText());

		FileDialog dialog = new FileDialog(getShell());
		dialog.setText(ICON_SELECTION_DIALOG_TITLE);

		dialog.setFileName(file.getAbsolutePath());
		dialog.setFilterExtensions(filterExtensions.toArray(new String[filterExtensions.size()]));
		dialog.setFilterNames(filterNames.toArray(new String[filterNames.size()]));

		String result = dialog.open();
		if(result == null) { //Cancel
			return;
		}
		setResult(result);
	}

	/**
	 * Browse file in workspace.
	 */
	private void browseWorkspace() {
		LabelProviderService labelProviderService = new LabelProviderServiceImpl();
		try {
			labelProviderService.startService();
		} catch (ServiceException ex) {
			Activator.log.error(ex);
		}

		ILabelProvider labelProvider = labelProviderService.getLabelProvider();

		IFile currentFile = getIFile(iconPathField.getText());

		TreeSelectorDialog dialog = new TreeSelectorDialog(getShell());
		dialog.setTitle(ICON_SELECTION_DIALOG_TITLE);


		WorkspaceContentProvider contentProvider = new WorkspaceContentProvider();


		if(!(filterExtensions.isEmpty() || filterNames.isEmpty())) {
			//The filters have been defined 
			contentProvider.setExtensionFilters(new LinkedHashMap<String, String>()); //Reset the default filters

			//Use our own filters
			for(int i = 0; i < Math.min(filterNames.size(), filterExtensions.size()); i++) {
				contentProvider.addExtensionFilter(filterExtensions.get(i), filterNames.get(i));
			}
		}

		dialog.setContentProvider(contentProvider);
		dialog.setLabelProvider(labelProvider);


		if(currentFile != null && currentFile.exists()) {
			dialog.setInitialSelections(new IFile[]{ currentFile });
		}

		int code = dialog.open();
		if(code == Window.OK) {
			Object[] result = dialog.getResult();
			if(result.length > 0) {
				Object file = result[0];
				if(file instanceof IFile) {
					setResult((IFile)file);
				}
			}
		}
	}

	/**
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 *
	 * @param buttonId
	 */
	@Override
	protected void buttonPressed(int buttonId) {

		switch(buttonId) {
		case BROWSE_BUTTON_ID:
			browseMenu.setVisible(true);
			break;
		default:
			super.buttonPressed(buttonId);
			break;
		}

	}



	/**
	 * Sets the result.
	 *
	 * @param file
	 *        the new result
	 */
	protected void setResult(IFile file) {
		iconPathField.setText(file.getFullPath().toString());
	}

	/**
	 * Sets the result.
	 *
	 * @param file
	 *        the new result
	 */
	protected void setResult(File file) {
		iconPathField.setText(file.getAbsolutePath());

	}

	/**
	 * Sets the result.
	 *
	 * @param path
	 *        the new result
	 */
	protected void setResult(String path) {
		iconPathField.setText(path);

	}

	/**
	 * Gets the file.
	 *
	 * @param path
	 *        the path
	 * @return the i file
	 */
	protected IFile getIFile(String path) {
		return FileUtil.getIFile(path);
	}

	/**
	 * Gets the file.
	 *
	 * @param path
	 *        the path
	 * @return the file
	 */
	protected File getFile(String path) {
		return FileUtil.getFile(path);
	}



	/**
	 * Create composite containing style sheets viewer.
	 * 
	 * @param parent
	 *        Composite where components will be added
	 */
	private void createStyleSheetsTreeComposite(Composite parent) {

		styleSheetsViewer = new TreeViewer(parent, SWT.BORDER);
		Tree tree = styleSheetsViewer.getTree();
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
	}

	/**
	 * Initialise display of style sheets tree viewer.
	 */
	private void initialiseTree() {
		styleSheetsViewer.setContentProvider(new EMFContentProvider(theme, StylesheetsPackage.eINSTANCE.getTheme_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 Theme) {
							result.addAll(theme.getStylesheets());
						}
						return result.toArray();
					}
				};

			}
		});

		styleSheetsViewer.setLabelProvider(new StyleSheetLabelProvider());
		styleSheetsViewer.setInput(theme.getStylesheets());
	}


	/**
	 * Label provider for style sheets tree viwer.
	 * 
	 * @author gpascual
	 *
	 */
	private class StyleSheetLabelProvider extends BaseLabelProvider implements ILabelProvider {

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

		/**
		 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
		 *
		 * @param element
		 * @return
		 */
		public Image getImage(Object element) {
			return null;
		}

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

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

			return text;
		}

	}

}

Back to the top