Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93ab1e8c70dcdb2afc9092d2fc1f2b01e6588e24 (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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*****************************************************************************
 * Copyright (c) 2013 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.debug.views;

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.runtime.notation.datatype.GradientData;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.papyrus.infra.emf.Activator;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
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.StringValue;
import org.eclipse.papyrus.infra.gmfdiag.css.Subterm;
import org.eclipse.papyrus.infra.gmfdiag.css.Term;
import org.eclipse.papyrus.infra.gmfdiag.css.configuration.handler.GMFToCSSConverter;
import org.eclipse.papyrus.infra.gmfdiag.css.dom.GMFElementAdapter;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.ForceValueHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.util.CssSwitch;
import org.eclipse.papyrus.infra.services.labelprovider.service.impl.LabelProviderServiceImpl;
import org.eclipse.papyrus.infra.widgets.providers.CollectionContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.w3c.dom.Element;

/**
 * A View to help debugging CSS elements. The view updates itself according to the current selection
 * (when the selection is css-compatible). The following features are currently supported:
 * 
 * - Filters the selected items and displays the css-compatible ones
 * - For each css-compatible item, lists its styleable properties
 * - For each styleable property, indicates whether the value has been forced by the user or computed by the CSS engine
 * 
 * 
 * 
 * @author Camille Letavernier
 */
/*
 * TODO:
 * - Display pseudo selectors of the current element
 * - When the style is computed, display the CSS rule which is used to compute the property
 */
public class CSSDebugView extends ViewPart implements ISelectionListener, ISelectionChangedListener {

	private Composite viewParent;

	private TreeViewer treeViewer;

	private Composite viewComposite;

	private Composite viewPropertiesComposite;

	private Composite viewDescriptionComposite;

	private TableViewer semanticProperties;

	private TableViewer notationProperties;

	public CSSDebugView() {
		//Nothing
	}

	@Override
	public void createPartControl(Composite parent) {
		viewParent = new Composite(parent, SWT.NONE);
		viewParent.setLayout(new GridLayout(2, false));

		treeViewer = new TreeViewer(viewParent, SWT.BORDER);
		treeViewer.setContentProvider(CollectionContentProvider.instance);
		treeViewer.setLabelProvider(new LabelProviderServiceImpl().getLabelProvider());
		GridData treeLayoutData = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
		treeLayoutData.minimumWidth = 250;
		treeLayoutData.widthHint = 250;
		treeViewer.getTree().setLayoutData(treeLayoutData);
		treeViewer.addSelectionChangedListener(this);

		viewComposite = new Composite(viewParent, SWT.BORDER);
		viewComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		viewComposite.setLayout(new GridLayout(2, true));

		viewDescriptionComposite = new Composite(viewComposite, SWT.NONE);
		GridLayout viewDescriptionCompositeLayout = new GridLayout(2, false);
		viewDescriptionCompositeLayout.marginHeight = 0;
		viewDescriptionCompositeLayout.marginWidth = 0;
		viewDescriptionComposite.setLayout(viewDescriptionCompositeLayout);
		viewDescriptionComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		viewPropertiesComposite = new Composite(viewComposite, SWT.NONE);
		GridLayout viewPropertiesCompositeLayout = new GridLayout(2, false);
		viewPropertiesCompositeLayout.marginHeight = 0;
		viewPropertiesCompositeLayout.marginWidth = 0;
		viewPropertiesComposite.setLayout(viewPropertiesCompositeLayout);
		viewPropertiesComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));

		Label notationPropertiesHeader = new Label(viewComposite, SWT.NONE);
		notationPropertiesHeader.setText("Notation properties:");
		notationPropertiesHeader.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));

		notationProperties = new TableViewer(viewComposite, SWT.BORDER);
		notationProperties.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		notationProperties.setContentProvider(CollectionContentProvider.instance);

		createColumn(notationProperties, "Property");
		createColumn(notationProperties, "Value source");
		createColumn(notationProperties, "Value");

		Label semanticPropertiesHeader = new Label(viewComposite, SWT.NONE);
		semanticPropertiesHeader.setText("Semantic properties:");
		semanticPropertiesHeader.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));

		semanticProperties = new TableViewer(viewComposite, SWT.BORDER);
		semanticProperties.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		semanticProperties.setContentProvider(CollectionContentProvider.instance);

		createColumn(semanticProperties, "Property");
		createColumn(semanticProperties, "Value");

		getViewSite().getPage().addSelectionListener(this);
	}

	private void createColumn(TableViewer viewer, String label) {
		viewer.getTable().setHeaderVisible(true);
		TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
		column.setText(label);
		column.setWidth(150);
	}

	@Override
	public void setFocus() {
		//Nothing
	}

	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;

			List<Object> supportedElements = getElementsWithCSSSupport(structuredSelection);
			if(supportedElements.isEmpty()) {
				return;
			}

			clean();
			debug(supportedElements);
		}
	}

	@Override
	public void dispose() {
		getViewSite().getPage().removeSelectionListener(this);
		super.dispose();
	}

	private List<Object> getElementsWithCSSSupport(IStructuredSelection fromSelection) {
		Iterator<?> selectionIterator = fromSelection.iterator();

		List<Object> result = new LinkedList<Object>();

		while(selectionIterator.hasNext()) {
			Object selectedElement = selectionIterator.next();
			if(supportsCSS(selectedElement)) {
				result.add(selectedElement);
			}
		}

		return result;
	}

	private boolean supportsCSS(Object element) {
		View view = getView(element);

		return view != null && supportsCSS(view);
	}

	private boolean supportsCSS(View view) {
		return view.getDiagram() instanceof CSSDiagram;
	}

	private void debug(List<Object> supportedElements) {
		if(supportedElements.isEmpty()) {
			System.out.println("Ignored");
			return; //Do not change the state of the debug view if the selection isn't interesting
		}

		treeViewer.setInput(supportedElements);
		treeViewer.setSelection(new StructuredSelection(supportedElements.get(0)));
	}

	private View getView(Object fromElement) {
		View view = null;

		if(fromElement instanceof View) {
			view = (View)fromElement;
		}

		if(fromElement instanceof IAdaptable) {
			view = (View)((IAdaptable)fromElement).getAdapter(View.class);
		}

		return view;
	}

	public void selectionChanged(SelectionChangedEvent event) {
		ISelection selection = event.getSelection();
		if(selection.isEmpty()) {
			clean();
			return;
		}

		if(selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			Object selectedElement = structuredSelection.getFirstElement();
			View view = getView(selectedElement);
			if(view != null) {
				debugView(view);
			}
		}
	}

	private void clean() {
		clean(viewDescriptionComposite);
		clean(viewPropertiesComposite);
		notationProperties.setInput("");
		semanticProperties.setInput("");
		viewComposite.layout();
		viewDescriptionComposite.layout();
	}

	private void clean(Composite composite) {
		for(Control control : composite.getChildren()) {
			control.dispose();
		}
	}

	private void debugView(View view) {
		clean();

		Diagram diagram = view.getDiagram();
		ExtendedCSSEngine engine;
		if(diagram instanceof CSSDiagram) {
			engine = ((CSSDiagram)diagram).getEngine();
		} else {
			return;
		}

		@SuppressWarnings("restriction")
		Element cssElement = engine.getElement(view);
		GMFElementAdapter gmfCssElement;
		if(cssElement instanceof GMFElementAdapter) {
			gmfCssElement = (GMFElementAdapter)cssElement;
		} else {
			return;
		}

		fillDescription(view, gmfCssElement);

		fillProperties(view, gmfCssElement);

		fillNotationProperties(view, gmfCssElement);

		fillSemanticProperties(view, gmfCssElement);

		viewComposite.layout();
	}

	private void fillDescription(View view, GMFElementAdapter cssElement) {
		setProperty(viewDescriptionComposite, "Name", cssElement.getLocalName());
	}

	private void fillProperties(View view, GMFElementAdapter cssElement) {
		setProperty(viewPropertiesComposite, "Classes", cssElement.getCSSClass().replaceAll("\\s+", ", "));
		setProperty(viewPropertiesComposite, "Events", "<Not yet implemented>");
	}

	private void setProperty(Composite parent, String label, String value) {
		Label labelLabel = new Label(parent, SWT.NONE);
		labelLabel.setText(label + ": ");

		Label valueLabel = new Label(parent, SWT.NONE);
		valueLabel.setText(value);
	}

	private void fillNotationProperties(View view, GMFElementAdapter cssElement) {
		List<EStructuralFeature> styleFeatures = getStyleFeatures(view);
		notationProperties.setInput(styleFeatures);
		notationProperties.setLabelProvider(new NotationPropertyLabelProvider(cssElement));
	}

	private void fillSemanticProperties(View view, GMFElementAdapter cssElement) {
		EObject semanticElement = cssElement.getSemanticElement();

		if(semanticElement == null) {
			semanticProperties.setInput("");
			return;
		}

		Set<EStructuralFeature> semanticFeatures = new HashSet<EStructuralFeature>();

		for(EStructuralFeature semanticFeature : semanticElement.eClass().getEAllStructuralFeatures()) {
			if(isPrimitive(semanticFeature)) {
				semanticFeatures.add(semanticFeature);
			}
		}

		semanticProperties.setLabelProvider(new SemanticPropertyLabelProvider(cssElement));
		semanticProperties.setInput(semanticFeatures);
	}

	private boolean isPrimitive(EStructuralFeature feature) {
		Set<String> values = new HashSet<String>();
		values.add("boolean");
		values.add("java.lang.Boolean");
		values.add("java.lang.String");
		values.add("int");
		values.add("java.lang.Integer");
		return values.contains(feature.getEType().getInstanceTypeName());
	}

	private List<EStructuralFeature> getStyleFeatures(View view) {
		Set<EStructuralFeature> allFeatures = new HashSet<EStructuralFeature>();
		allFeatures.addAll(view.eClass().getEAllStructuralFeatures());

		List<EStructuralFeature> styleFeatures = new LinkedList<EStructuralFeature>();
		for(EStructuralFeature feature : allFeatures) {
			if(EMFHelper.isSubclass(feature.getEContainingClass(), NotationPackage.eINSTANCE.getStyle())) {
				styleFeatures.add(feature);
			}
		}

		Collections.sort(styleFeatures, FeatureComparator.instance);

		return styleFeatures;
	}

	private static class FeatureComparator implements Comparator<EStructuralFeature> {

		public static FeatureComparator instance = new FeatureComparator();

		public int compare(EStructuralFeature o1, EStructuralFeature o2) {
			return Collator.getInstance().compare(o1.getName(), o2.getName());
		}

	}

	private class NotationPropertyLabelProvider extends CellLabelProvider {

		private GMFElementAdapter cssElement;

		public NotationPropertyLabelProvider(GMFElementAdapter cssElement) {
			this.cssElement = cssElement;
		}

		@Override
		public void update(ViewerCell cell) {
			Object element = cell.getElement();
			if(!(element instanceof EStructuralFeature)) {
				return;
			}

			EStructuralFeature feature = (EStructuralFeature)element;

			try {

				switch(cell.getColumnIndex()) {
				case 0:
					updateProperty(cell, feature);
					break;
				case 1:
					updatePropertySource(cell, feature);
					break;
				case 2:
					updateValue(cell, feature);
					break;
				}
			} catch (Exception ex) {
				Activator.log.error(ex);
				cell.setText("!! Error !!");
			}
		}

		private void updateProperty(ViewerCell cell, EStructuralFeature feature) {
			cell.setText(feature.getName());
		}

		private void updatePropertySource(ViewerCell cell, EStructuralFeature feature) {
			Object value = cssElement.getNotationElement().eGet(feature);

			boolean forced;

			if(equals(value, feature.getDefaultValue())) {
				boolean isSet = ForceValueHelper.isSet(cssElement.getNotationElement(), feature, value);
				forced = isSet;
				//				cell.setText(isSet ? "Forced value" : "Computed value");
				//				cell.setText("Default value " + isSet);
			} else {
				boolean isSet = cssElement.getNotationElement().eIsSet(feature);
				forced = isSet;
				//				cell.setText(isSet ? "Forced value" : "Computed value");
			}

			cell.setText(forced ? "Forced value" : "Computed value");
			int systemColor = forced ? SWT.COLOR_RED : SWT.COLOR_BLACK;
			cell.setForeground(Display.getDefault().getSystemColor(systemColor));
		}

		private boolean equals(Object value1, Object value2) {
			if(value1 == value2) {
				return true;
			}

			if(value1 == null) {
				return value2.equals(value1);
			}

			return value1.equals(value2);
		}

		private void updateValue(ViewerCell cell, EStructuralFeature feature) {
			Object value = cssElement.getNotationElement().eGet(feature);
			String text = getValueAsText(feature, value);
			cell.setText(text);
			if(feature.getName().endsWith("Color") && value instanceof Integer) {
				Color color = FigureUtilities.integerToColor((Integer)value);
				cell.setBackground(color);
				if(((Integer)value) == 0) {
					//Use a white font when the color is black. TODO: Do the same for all dark colors...
					cell.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
				}
			}
		}

		private String getValueAsText(EStructuralFeature feature, Object value) {
			if(value instanceof GradientData) {
				GradientData gradient = (GradientData)value;
				return getLabel(GMFToCSSConverter.instance.convert(gradient));
			}

			if(feature.getName().endsWith("Color") && value instanceof Integer) {
				Color color = FigureUtilities.integerToColor((Integer)value);
				String result = getLabel(GMFToCSSConverter.instance.convert(color));
				color.dispose();
				return result;
			}

			return value == null ? "" : value.toString();
		}
	}

	protected String getLabel(Expression expression) {
		if(expression == null) {
			return "";
		}
		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);
	}

	private class SemanticPropertyLabelProvider extends CellLabelProvider {

		private GMFElementAdapter cssElement;

		public SemanticPropertyLabelProvider(GMFElementAdapter cssElement) {
			this.cssElement = cssElement;
		}

		@Override
		public void update(ViewerCell cell) {
			Object element = cell.getElement();
			if(!(element instanceof EStructuralFeature)) {
				return;
			}

			EStructuralFeature feature = (EStructuralFeature)element;

			try {

				switch(cell.getColumnIndex()) {
				case 0:
					updateProperty(cell, feature);
					break;
				case 1:
					updateValue(cell, feature);
					break;
				}

			} catch (Exception ex) {
				Activator.log.error(ex);
				cell.setText("!! Error !!");
			}
		}

		private void updateProperty(ViewerCell cell, EStructuralFeature feature) {
			cell.setText(feature.getName());
		}

		private void updateValue(ViewerCell cell, EStructuralFeature feature) {
			cell.setText(cssElement.getSemanticElement().eGet(feature).toString());
		}

	}

}

Back to the top