Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fe51ef704fce8b58de4525ab582f647f45e19995 (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 Mia-Software.
 * 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:
 *     Nicolas Bros (Mia-Software) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.facet.custom.metamodel.v0_2_0.custom.presentation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.naming.event.ObjectChangeListener;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EParameter;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.ETypedElementCase;
import org.eclipse.papyrus.emf.facet.custom.metamodel.v0_2_0.custom.ETypedElementSwitchQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.DerivedTypedElement;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.Facet;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetAttribute;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetElement;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetOperation;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.FacetReference;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.ParameterValue;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.extensible.Query;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.EObjectLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.FalseLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.FloatLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.IntegerLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.IsOneOfQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.NavigationQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.NullLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.OperationCallQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.StringLiteralQuery;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.query.TrueLiteralQuery;
import org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2;
import org.eclipse.papyrus.emf.facet.query.java.metamodel.v0_2_0.javaquery.JavaQuery;
import org.eclipse.papyrus.emf.facet.query.ocl.metamodel.oclquery.OclQuery;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Displays the current customizations for a given {@link CustomView}, which is
 * the input of the viewer
 */
public class CustomViewer {

	private static final int VALUE_COLUMN_WIDTH = 200;
	private static final int TYPE_COLUMN_WIDTH = 200;
	private static final int FEATURE_COLUMN_WIDTH = 200;
	private final TreeViewer treeViewer;
	
	//Customization
	private static final String TOP_LEFT_OVERLAY_ICON_PATH = "topLeftOverlay";
	private static final String TOP_MIDDLE_OVERLAY_ICON_PATH = "topMiddleOverlay";
	private static final String TOP_RIGHT_OVERLAY_ICON_PATH = "topRightOverlay";
	private static final String MIDDLE_LEFT_OVERLAY_ICON_PATH = "middleLeftOverlay";
	private static final String MIDDLE_RIGHT_OVERLAY_ICON_PATH = "middleRightOverlay";
	private static final String BOTTOM_LEFT_OVERLAY_ICON_PATH = "bottomLeftOverlay";
	private static final String BOTTOM_MIDDLE_OVERLAY_ICON_PATH = "bottomMiddleOverlay";
	private static final String BOTTOM_RIGHT_OVERLAY_ICON_PATH = "bottomRightOverlay";
	private static final String CUSTOMIZATION_LABEL = "label";
	private static final String CUSTOMIZATION_IMAGE = "image";
	private static final String CUSTOMIZATION_FOREGROUND = "foreground";
	private static final String CUSTOMIZATION_BACKGROUND = "background";
	private static final String FONT_NAME = "fontName";
	private static final String FONT_SIZE = "fontSize";
	private static final String FONT_BOLD = "isBold";
	private static final String FONT_ITALIC = "isItalic";
	private static final String FONT_UNDERLINE = "isUnderlined";
	private static final String FONT_STRUKETHROUGH = "isStruckthrough";

	public CustomViewer(final Composite parent) {
		this.treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER
				| SWT.SINGLE | SWT.FULL_SELECTION);
		this.treeViewer.getTree().setHeaderVisible(true);

		final TreeViewerColumn featureColumn = new TreeViewerColumn(this.treeViewer, SWT.LEFT);
		featureColumn.getColumn().setText("feature column");
		featureColumn.getColumn().setWidth(CustomViewer.FEATURE_COLUMN_WIDTH);
		final TreeViewerColumn typeColumn = new TreeViewerColumn(this.treeViewer, SWT.LEFT);
		typeColumn.getColumn().setText("feature type");
		typeColumn.getColumn().setWidth(CustomViewer.TYPE_COLUMN_WIDTH);
		final TreeViewerColumn valueColumn = new TreeViewerColumn(this.treeViewer, SWT.LEFT);
		valueColumn.getColumn().setText("feature value");
		valueColumn.getColumn().setWidth(CustomViewer.VALUE_COLUMN_WIDTH);

		this.treeViewer.setContentProvider(new CustomViewerContentProvider());

		// for sorting purposes
		this.treeViewer.setLabelProvider(new FeatureLabelProvider());
		this.treeViewer.setComparator(new ViewerComparator() {
			@Override
			public int compare(final Viewer viewer, final Object e1, final Object e2) {
				// do not sort value cases
				if (e1 instanceof FacetOperation) {
					return 0;
				}
				return super.compare(viewer, e1, e2);
			}
		});

		featureColumn.setLabelProvider(new FeatureLabelProvider());
		typeColumn.setLabelProvider(new TypeLabelProvider());
		valueColumn.setLabelProvider(new ValueLabelProvider());

		this.treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(final SelectionChangedEvent event) {
				// for selection text color
				CustomViewer.this.treeViewer.refresh();
			}
		});
	}

	public void setInput(final Object input) {
		this.treeViewer.setInput(input);
	}

	protected class CustomViewerContentProvider implements ITreeContentProvider {
		public Object[] getElements(final Object inputElement) {
			final List facetOperations = new ArrayList<FacetOperation>();
			if (inputElement instanceof FacetOperation) {
				final FacetOperation facetOperation = (FacetOperation) inputElement;
				facetOperations.add(facetOperation);
			}
			if(inputElement instanceof FacetElement){
				final FacetElement facetElement = (FacetElement)inputElement;
				facetOperations.add(facetElement);
			}
			if(inputElement instanceof List){
				for (Object object : (List)inputElement) {
					if(object instanceof DerivedTypedElement){
						facetOperations.add(object);
					}
				}
			}
			return facetOperations.toArray();
		}

		public Object[] getChildren(final Object parentElement) {

			if (parentElement instanceof FacetOperation) {
				final FacetOperation facetOperation = (FacetOperation) parentElement;
				final EList<EParameter> parameters = new BasicEList<EParameter>();
				parameters.addAll(facetOperation.getEParameters());
				return parameters.toArray();
			}

			return new Object[0];
		}

		public Object getParent(final Object element) {
			if (element instanceof EParameter) {
				final EParameter eParameter = (EParameter) element;
				return eParameter.getEOperation();
			}
			return null;
		}

		public boolean hasChildren(final Object element) {
			if (element instanceof FacetOperation) {
				final FacetOperation facetOperation = (FacetOperation) element;
				return !facetOperation.getEParameters().isEmpty();
			}
			return false;
		}

		public void dispose() {
			// nothing
		}

		public void inputChanged(final Viewer viewer, final Object oldInput, final Object newInput) {
			// nothing
		}
	}

	protected class FeatureLabelProvider extends ColumnLabelProvider {

		@Override
		public String getText(final Object element) {
			if (element instanceof DerivedTypedElement) {
				final DerivedTypedElement facetElement = (DerivedTypedElement) element;
				return facetElement.getName() ;
			}else if (element instanceof ParameterValue){
				final ParameterValue parameterValue = (ParameterValue) element;
				return parameterValue.getParameter().getName();
			}else if (element instanceof EParameter){
				final EParameter parameterValue = (EParameter) element;
				return parameterValue.getName();
			}
			return element.toString();
		}

		@Override
		public Font getFont(final Object element) {
			return null;
		}

	}

	protected class ValueLabelProvider extends ColumnLabelProvider {

		/** Images in use */
		private final HashMap<String, Image> images = new HashMap<String, Image>();

		public ValueLabelProvider() {
			super();
		}

		public String getText(final Object element) {
			
			if(element instanceof DerivedTypedElement){
				DerivedTypedElement derivedTypedElement = (DerivedTypedElement)element;
				Query query = derivedTypedElement.getQuery();
				if(query != null){
					if(query instanceof StringLiteralQuery){
						StringLiteralQuery stringQuery = (StringLiteralQuery)query;
						return stringQuery.getValue();
					}else if(query instanceof TrueLiteralQuery){
						return "true";
					}else if(query instanceof FalseLiteralQuery){
						return "false";
					}else if(query instanceof NullLiteralQuery){
						return "null";
					}else if(query instanceof OperationCallQuery){
						OperationCallQuery operationCallQuery = (OperationCallQuery)query;
						return operationCallQuery.toString();
					}else if(query instanceof JavaQuery){
						JavaQuery javaQuery = (JavaQuery)query;
						String qualifiedName = javaQuery.getImplementationClassName();
						String[] path = qualifiedName.split("\\.");
						return path[path.length -1];
					}else if (query instanceof OclQuery){
						OclQuery oclQuery = (OclQuery)query;
						return oclQuery.getOclExpression();
					}else if (query instanceof NavigationQuery){
						NavigationQuery navigationQuery = (NavigationQuery)query;
						return navigationQuery.getPath().toString();
					}else if (query instanceof IsOneOfQuery){
						IsOneOfQuery isOneOfQuery = (IsOneOfQuery)query;
						return isOneOfQuery.getExpectedEObjects().toString();
					}else if (query instanceof IntegerLiteralQuery){
						IntegerLiteralQuery integerQuery = (IntegerLiteralQuery)query;
						return String.valueOf(integerQuery.getValue());
					}else if (query instanceof FloatLiteralQuery){
						FloatLiteralQuery floatQuery = (FloatLiteralQuery)query;
						return String.valueOf(floatQuery.getValue());
					}else if (query instanceof EObjectLiteralQuery){
						EObjectLiteralQuery eObjectQuery = (EObjectLiteralQuery)query;
						return eObjectQuery.getElement().eClass().getName();
					}else if(query instanceof ETypedElementSwitchQuery){
						ETypedElementSwitchQuery switchQuery = (ETypedElementSwitchQuery)query;
						EList<ETypedElementCase> list = switchQuery.getCases();
						List listResult = new ArrayList<String>();
						for (ETypedElementCase object : list) {
							if(object.getCase() != null){
								listResult.add(object.getCase().getName());
							}
						}
						return listResult.toString();
					}
				}
			}else if(element instanceof EParameter){
				EParameter parameterValue = (EParameter)element;
				return "";
				//return parameterValue.getName();
			}
			return "N/A";
		}

		public Image getImage(final Object element) {
			return null;
		}

		@Override
		public void dispose() {
			for (final Image image : this.images.values()) {
				image.dispose();
			}
			super.dispose();
		}

		@Override
		public void update(final ViewerCell cell) {
			// for a color static value => display color
			final Object element = cell.getElement();

			// gray if not customized
			boolean gray = false;
			if (element instanceof FacetOperation) {
				final FacetOperation customViewFeature = (FacetOperation) element;
				//if (customViewFeature.getDefaultValue() == null) {
				//	gray = true;
				//}
			}

			// red if unresolved query
			boolean unresolved = false;
			if (element instanceof DerivedTypedElement) {
				final DerivedTypedElement derivedTypedElement = (DerivedTypedElement) element;
				final Query query = derivedTypedElement.getQuery();
				if (query == null || !EMFUtil.tryResolve(query)) {
					unresolved = true;
				}
			}

			if (unresolved) {
				final String text = getText(element);
				cell.setText(text);
				cell.setImage(ImageProvider.getInstance().getErrorIcon());
				super.update(cell);
			} else {
				cell.setImage(getImage(element));
				super.update(cell);
			}
		}
	}
	
	protected class TypeLabelProvider extends ColumnLabelProvider{
		
		@Override
		public String getText(final Object element) {
			if (element instanceof DerivedTypedElement) {
				final DerivedTypedElement facetElement = (DerivedTypedElement) element;
				if(facetElement instanceof FacetAttribute || facetElement instanceof FacetReference){
					return facetElement.getEType().getName();
				}else{
					DerivedTypedElement override = facetElement.getOverride();
					if( override != null){
						return override.getName();
					}else{
						return ("N/A");
					}
				}
			} else if( element instanceof EParameter){
				final EParameter eParameter = (EParameter) element;
				return eParameter.getEType().getName();
			}

			return element.toString();
		}
		
		@Override
		public Image getImage(Object element) {
			if (element instanceof DerivedTypedElement) {
				final DerivedTypedElement facetElement = (DerivedTypedElement) element;
				if(facetElement instanceof FacetAttribute || facetElement instanceof FacetReference){
					return ImageProvider.getInstance().getAttributeIcon();
				}else{
					DerivedTypedElement override = facetElement.getOverride();
					if( override != null){
						String overrideName = override.getName();
						if(CustomViewer.TOP_LEFT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getTopLeftOverlayIcon();
						}else if(CustomViewer.TOP_MIDDLE_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getTopMiddleOverlayIcon();
						}else if(CustomViewer.TOP_RIGHT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getTopRightOverlayIcon();
						}else if(CustomViewer.MIDDLE_LEFT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getMiddleLeftOverlayIcon();
						}else if(CustomViewer.MIDDLE_RIGHT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getMiddleRightOverlayIcon();
						}else if(CustomViewer.BOTTOM_LEFT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getBottomLeftOverlayIcon();
						}else if(CustomViewer.BOTTOM_MIDDLE_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getBottomMiddleOverlayIcon();
						}else if(CustomViewer.BOTTOM_RIGHT_OVERLAY_ICON_PATH.equals(overrideName)){
							return ImageProvider.getInstance().getBottomRightOverlayIcon();
						}else if(CustomViewer.CUSTOMIZATION_LABEL.equals(overrideName)){
							return ImageProvider.getInstance().getCustomizationLabelIcon();
						}else if(CustomViewer.CUSTOMIZATION_IMAGE.equals(overrideName)){
							return ImageProvider.getInstance().getCustomizationImageIcon();
						}else if(CustomViewer.CUSTOMIZATION_FOREGROUND.equals(overrideName)){
							return ImageProvider.getInstance().getCustomizationColorIcon();
						}else if(CustomViewer.CUSTOMIZATION_BACKGROUND.equals(overrideName)){
							return ImageProvider.getInstance().getCustomizationColorIcon();
						}else if(CustomViewer.FONT_NAME.equals(overrideName)){
							return ImageProvider.getInstance().getFontNameIcon();
						}else if(CustomViewer.FONT_SIZE.equals(overrideName)){
							return ImageProvider.getInstance().getFontSizeIcon();
						}else if(CustomViewer.FONT_BOLD.equals(overrideName)){
							return ImageProvider.getInstance().getIsBoldIcon();
						}else if(CustomViewer.FONT_ITALIC.equals(overrideName)){
							return ImageProvider.getInstance().getIsItalicIcon();
						}else if(CustomViewer.FONT_UNDERLINE.equals(overrideName)){
							return ImageProvider.getInstance().getIsUnderlinedIcon();
						}else if(CustomViewer.FONT_STRUKETHROUGH.equals(overrideName)){
							return ImageProvider.getInstance().getIsStrukethroughIcon();
						}
					}
				}
			}
			return super.getImage(element);
		}
	}

	public void refresh() {
		if (!this.treeViewer.getTree().isDisposed()) {
			try {
				this.treeViewer.getTree().setRedraw(false);
				this.treeViewer.refresh();
			} finally {
				this.treeViewer.getTree().setRedraw(true);
			}
		}
	}

	public void addSelectionChangedListener(final ISelectionChangedListener selectionChangedListener) {
		this.treeViewer.addSelectionChangedListener(selectionChangedListener);
	}

	public ISelection getSelection() {
		return this.treeViewer.getSelection();
	}

	public TreeViewer getViewer() {
		return this.treeViewer;
	}
}

Back to the top