Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e794b18a870ad66e0a6d794a445ea0ad42e12963 (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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*****************************************************************************
 * Copyright (c) 2012, 2015 CEA LIST, Christian W. Damus, and others.
 *
 * 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
 *  Christian W. Damus - bug 461629
 *  Mickael ADAM (ALL4TEC) mickael.adam@all4tec.net - bug 462381
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.dom;

import static org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles.CSS_DIAGRAM_STYLESHEETS_KEY;
import static org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles.CSS_GMF_CLASS_KEY;
import static org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles.CSS_GMF_ID_KEY;
import static org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles.CSS_GMF_STYLE_KEY;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.e4.ui.css.core.dom.ElementAdapter;
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.gmf.runtime.notation.BasicCompartment;
import org.eclipse.gmf.runtime.notation.DecorationNode;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NamedStyle;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.StringListValueStyle;
import org.eclipse.gmf.runtime.notation.StringValueStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.SemanticElementHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.listener.CustomStyleListener;
import org.eclipse.papyrus.infra.gmfdiag.common.types.NotationTypesMap;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.helper.CSSDOMSemanticElementHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.StatefulView;
import org.eclipse.papyrus.infra.tools.util.ListHelper;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * An Adapter for providing a CSS support to GMF Objects
 *
 * @author Camille Letavernier
 */
@SuppressWarnings("restriction")
public class GMFElementAdapter extends ElementAdapter implements NodeList, IChangeListener, StatefulView, IAdaptable {

	/**
	 * 
	 */
	protected static final String KIND = "kind"; //$NON-NLS-1$

	public static final String CSS_VALUES_SEPARATOR = " "; //$NON-NLS-1$

	public CSSDOMSemanticElementHelper helper;

	/**
	 * The Semantic Model Element associated to the current styled element
	 * Might also be a GMF Diagram
	 */
	protected EObject semanticElement;

	/**
	 * The current GMF Element
	 */
	protected View notationElement;

	/**
	 * The namespace URI of the semanticElement
	 */
	protected String namespaceURI;

	/**
	 * The unqualified name of the semantic Element
	 */
	protected String localName;

	/**
	 * The list of child nodes for this element
	 */
	protected Node[] children;

	/**
	 * The parent node of this element
	 */
	protected Node parentNode;

	/**
	 * A Listener for standard Style properties
	 */
	protected Adapter styleListener;

	/**
	 * A Listener for custom Style properties
	 */
	protected CustomStyleListener cssStyleListener;

	/**
	 * Returns the CSS ID of the selected element.
	 *
	 * @param sourceElement
	 *            The source element must be a GMF notation object (View, Style, ...)
	 * @return
	 * 		The CSS ID associated to the source element, or null if it cannot be found
	 */
	public static String getCSSID(EObject sourceElement) {
		return getCSSValue(sourceElement, CSS_GMF_ID_KEY);
	}

	/**
	 * Returns the CSS Class of the selected element. If more than one CSS
	 * class is associated to the source elements, returns a String containing
	 * all classes, separated with spaces.
	 *
	 * @param sourceElement
	 *            The source element must be a GMF notation object (View, Style, ...)
	 * @return
	 * 		The CSS Class associated to the source element, or null if it cannot be found
	 */
	public static String getCSSClass(EObject sourceElement) {
		List<String> allClasses = getCSSValues(sourceElement, CSS_GMF_CLASS_KEY);
		return ListHelper.deepToString(allClasses, " "); //$NON-NLS-1$
	}

	/**
	 * Returns the source element's local CSS style.
	 *
	 * @param sourceElement
	 *            The source element must be a GMF notation object (View, Style, ...)
	 * @return
	 * 		the source element's local CSS style.
	 */
	public static String getCSSStyle(EObject sourceElement) {
		return getCSSValue(sourceElement, CSS_GMF_STYLE_KEY);
	}

	private static String getCSSValue(EObject sourceElement, String key) {
		StringValueStyle style = (StringValueStyle) findStyle(sourceElement, key, NotationPackage.eINSTANCE.getStringValueStyle());
		if (style == null) {
			return null;
		}

		return style.getStringValue();
	}

	@SuppressWarnings("unchecked")
	private static List<String> getCSSValues(EObject sourceElement, String key) {
		StringListValueStyle style = (StringListValueStyle) findStyle(sourceElement, key, NotationPackage.eINSTANCE.getStringListValueStyle());
		if (style == null) {
			return Collections.emptyList();
		}


		return style.getStringListValue();
	}

	private static NamedStyle findStyle(EObject sourceElement, String key, EClass type) {
		View view = findView(sourceElement);
		if (view == null) {
			return null;
		}

		return view.getNamedStyle(type, key);
	}

	private static View findView(EObject sourceElement) {
		if (sourceElement == null) {
			return null;
		}

		if (sourceElement instanceof View) {
			return (View) sourceElement;
		}

		return findView(sourceElement.eContainer());
	}

	/**
	 * Creates a new GMF Element Adapter for the requested view.
	 *
	 * @param view
	 *            The view to be adapted
	 * @param engine
	 *            The associated CSS Engine
	 */
	public GMFElementAdapter(View view, ExtendedCSSEngine engine) {
		super(view, engine);

		// Bug 431694: Don't instantiate an ElementAdapter for an Orphaned view
		if (view == null || view.getDiagram() == null) {
			throw new IllegalArgumentException("Cannot handle orphaned view : " + view);
		}

		notationElement = view;
		helper = CSSDOMSemanticElementHelper.getInstance();
		listenNotationElement();

	}

	/**
	 * I provide adapters for
	 * 
	 * <ul>
	 * <li>{@link Diagram} - the diagram containing my {@linkplain #getNotationElement() notation element}</li>
	 * </ul>
	 */
	@Override
	public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
		if (adapter == Diagram.class) {
			return getNotationElement().getDiagram();
		}
		return null;
	}

	/**
	 * Returns the semantic element associated to this adapter. In case of a diagram,
	 * the diagram is itself the semantic element.
	 *
	 * @return
	 * 		The semantic element associated to this adapter
	 */
	public EObject getSemanticElement() {
		if (semanticElement == null) {
			semanticElement = helper.findSemanticElement(notationElement);
			computePseudoInstances();
			listenSemanticElement();
		}
		return semanticElement;
	}

	private void computePseudoInstances() {
		if (helper.isFloatingLabel(notationElement)) {
			String humanType = NotationTypesMap.instance.getHumanReadableType(notationElement);
			if (humanType == null) {
				humanType = notationElement.getType();
			}

			pseudoInstances.add(new StringIgnoreCase(humanType));
		}
	}

	private Adapter getStyleListener() {
		if (styleListener == null) {
			styleListener = new StyleListener(this);
		}
		return styleListener;
	}

	private void listenNotationElement() {
		notationElement.eAdapters().add(getStyleListener());

		Collection<String> cssStyles = Arrays.asList(new String[] { CSS_GMF_CLASS_KEY, CSS_GMF_ID_KEY, CSS_GMF_STYLE_KEY, CSS_DIAGRAM_STYLESHEETS_KEY });

		notationElement.eAdapters().add(cssStyleListener = new CustomStyleListener(notationElement, this, cssStyles));
	}

	private ExtendedCSSEngine getEngine() {
		return (ExtendedCSSEngine) engine;
	}

	private void listenSemanticElement() {
		// FIXME: The semantic hierarchy is never refresh (children & parentNode)
		if (semanticElement != null) {
			semanticElement.eAdapters().add(getStyleListener());
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * Returns the first parent Notation Element representing a different semantic object
	 * than self.
	 */
	@Override
	public Node getParentNode() {
		if (parentNode == null) {
			View gmfElement = notationElement;
			while (gmfElement != null) {
				EObject semanticElement = helper.findSemanticElement(gmfElement);

				if (semanticElement != this.getSemanticElement()) {
					break;
				}

				EObject container = gmfElement.eContainer();

				if (gmfElement == container || !(container instanceof View)) {
					gmfElement = null;
					break;
				}

				gmfElement = (View) container;
			}

			parentNode = engine.getElement(gmfElement);
			if (parentNode == this) {
				return parentNode = null;
			}
		}

		return parentNode;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public NodeList getChildNodes() {
		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getNamespaceURI() {
		if (namespaceURI == null) {
			namespaceURI = EMFHelper.getQualifiedName(getSemanticElement().eClass().getEPackage(), ".");//$NON-NLS-1$
		}
		return namespaceURI;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getCSSId() {
		return getCSSID(notationElement);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getCSSClass() {
		return getCSSClass(notationElement);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getCSSStyle() {
		return getCSSStyle(notationElement);
	}

	/**
	 * {@inheritDoc}
	 *
	 * The local name is either the diagram type (For a Diagram) or the name of the semantic
	 * element's metaclass
	 */
	@Override
	public String getLocalName() {
		if (localName == null) {
			if (getSemanticElement() instanceof Diagram) {
				Diagram diagram = (Diagram) getSemanticElement();
				String type = diagram.getType();

				String humanType = NotationTypesMap.instance.getHumanReadableType(diagram);
				if (humanType != null) {
					localName = humanType;
				} else {
					localName = type;
				}
			} else if (getNotationElement() instanceof BasicCompartment) {
				return "Compartment";
			} else if (helper.isFloatingLabel(getNotationElement())) {
				return "Label";
			} else {
				localName = getSemanticElement().eClass().getName();
			}
		}

		return localName;
	}

	/**
	 * {@inheritDoc}
	 *
	 * The attributes are the semantic's element features
	 */
	@Override
	public final String getAttribute(String attr) {
		String value = doGetAttribute(attr);
		if (value != null) {
			return value;
		}

		return "";
	}

	/**
	 * {@inheritDoc}
	 *
	 * The attributes are the semantic's element features
	 *
	 * Returns null if the attribute is not known
	 */
	protected String doGetAttribute(String attr) {

		if (notationElement instanceof DecorationNode) {
			// DecorationNode can be filtered by type (notation::View::type), or by title (From GmfGen model)
			// We add the "kind" attribute which is specific to the CSS (More user-friendly)
			if (KIND.equals(attr)) {

				DecorationNode node = (DecorationNode) notationElement;
				String humanType = NotationTypesMap.instance.getHumanReadableType(node);
				if (humanType == null) {
					return node.getType();
				}
				return humanType; // 7017, 7018, 7019 for Attribute/Operation/Classifier compartments
				// TODO: Create a mapping list between GMF ID (Type) and user-readable labels
			}
		}

		EStructuralFeature feature = getSemanticElement().eClass().getEStructuralFeature(attr);

		if (feature != null) {
			if (feature.isMany()) {
				List<?> values = (List<?>) semanticElement.eGet(feature);
				List<String> cssValues = new LinkedList<String>();
				for (Object value : values) {
					if (value != null) {
						cssValues.add(getCSSValue(feature, value));
					}
				}
				return ListHelper.deepToString(cssValues, CSS_VALUES_SEPARATOR);
			} else {
				Object value = semanticElement.eGet(feature);
				if (value != null) {
					return getCSSValue(feature, value);
				}
			}
		}

		return null;
	}

	/**
	 * Returns the value of the given feature as a formatted String (Valid w.r.t CSS Syntax)
	 * Used by {@link #doGetAttribute(String)}
	 *
	 * Return null if the feature is not supported or the value cannot be converted to a String
	 *
	 * @param feature
	 * @param value
	 * @return
	 */
	protected String getCSSValue(EStructuralFeature feature, Object value) {
		if (value == null) {
			return null;
		}

		if (feature instanceof EReference && value instanceof ENamedElement) {
			return ((ENamedElement) value).getName();
		}

		// Standard case. For EObject values, it might be better to return null than a random label...
		return value.toString();
	}

	/**
	 * {@inheritDoc}
	 *
	 * Returns the GMF EObject handled by this adapter
	 */
	@Override
	public EObject getNativeWidget() {
		return notationElement;
	}



	// ///////////
	// Node List//
	// ///////////



	/**
	 * {@inheritDoc}
	 */
	@Override
	public Node item(int index) {
		return getChildren()[index];
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int getLength() {
		return getChildren().length;
	}

	protected Node[] getChildren() {
		if (children == null) {
			children = computeChildren(notationElement, engine);
		}

		return children;
	}

	/**
	 * Returns the list of notation child elements attached to semantic
	 * elements different than self's semantic element.
	 *
	 * If a notation child element represents the same semantic element
	 * than self, returns its own children (Recursively).
	 */
	protected Node[] computeChildren(View notationElement, CSSEngine engine) {
		EObject semanticElement = helper.findSemanticElement(notationElement);
		List<Node> childList = new LinkedList<Node>();
		for (EObject child : notationElement.eContents()) {
			if (child instanceof View) {
				View notationChild = (View) child;
				if (SemanticElementHelper.findSemanticElement(notationChild) != semanticElement) {
					childList.add(engine.getElement(notationChild));
				} else {
					// This is the same semantic element (e.g. label). Compute indirect children, if any
					childList.addAll(Arrays.asList(computeChildren(notationChild, engine)));
				}
			}
		}

		/*
		 * <--------------------
		 * 
		 * //Allows both notations Class > Property and Class > Compartment > Property
		 * 
		 * //FIXME: The Tree is computed through "getParentNode". "getChildren" is barely used. Moreover,
		 * //there is a mapping between Notation element and DOM element, which makes it impossible to associate the same
		 * //notation element to different DOM elements.
		 * 
		 * // for(EObject child : notationElement.eContents()) {
		 * // if(child instanceof BasicCompartment) {
		 * // //Add the Compartment's children to this' children
		 * // childList.addAll(Arrays.asList(computeChildren((View)child, engine)));
		 * // }
		 * // }
		 * 
		 * -------------------->
		 */

		return childList.toArray(new Node[childList.size()]);
	}

	// ////////////////////
	// Handle events //
	// ////////////////////


	/**
	 * The GMF View element associated to this adapter
	 *
	 * @return
	 * 		The GMF View element associated to this adapter
	 */
	public View getNotationElement() {
		return notationElement;
	}

	/**
	 * Handles a notification for semantic element change.
	 * Source: styleListener
	 */
	public void semanticElementChanged() {
		if (semanticElement != null && semanticElement != notationElement) {
			semanticElement.eAdapters().remove(styleListener);
			semanticElement = null;
		}

		localName = null;
		parentNode = null;
		namespaceURI = null;
		children = null;
		getEngine().notifyChange(this);
	}

	/**
	 * Handles a notification for Custom style change.
	 * Source: cssStyleListener
	 */
	// Change incoming from one of the cssCustomStyles (class, id, local style or diagram stylesheets)
	@Override
	public void handleChange(ChangeEvent event) {
		if (notationElement instanceof CSSDiagram) {
			// TODO: Use a finer grained event (We should reset only when the
			// change occurs on a DiagramStyleSheet)
			getEngine().reset();
		}

		// Notify the CSS Engine
		getEngine().notifyChange(this);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void dispose() {
		super.dispose();
		notationElement.eAdapters().remove(cssStyleListener);
		cssStyleListener.dispose();

		notationElement.eAdapters().remove(styleListener);

		if (semanticElement != null) {
			semanticElement.eAdapters().remove(styleListener);
		}
	}

	/**
	 * Handles a notification for notation property change
	 * Source : styleListener
	 */
	public void notationPropertyChanged() {
		// Notify the CSSEngine
		getEngine().notifyChange(this);
	}

	/**
	 * Handles a notification for semantic property change
	 * Source : styleListener
	 */
	public void semanticPropertyChanged() {
		// Notify the CSSEngine
		getEngine().notifyChange(this);
	}

	/**
	 * Handles a notification for notation element disposed
	 * Source : styleListener
	 */
	public void notationElementDisposed() {
		dispose();
		// Notify the CSSEngine
		getEngine().handleDispose(notationElement);
	}

	private final Set<StringIgnoreCase> pseudoInstances = new HashSet<StringIgnoreCase>();

	private final static class StringIgnoreCase {

		private final String sourceString;

		private final String sourceStringToLower;

		public StringIgnoreCase(String source) {
			this.sourceString = source;
			this.sourceStringToLower = source == null ? null : source.toLowerCase();
		}

		@Override
		public boolean equals(Object other) {
			if (other instanceof String) {
				return ((String) other).equalsIgnoreCase(sourceString);
			}

			if (other instanceof StringIgnoreCase) {
				return equals(((StringIgnoreCase) other).sourceString);
			}

			return false;
		}

		@Override
		public int hashCode() {
			if (sourceStringToLower == null) {
				return 0;
			}

			return sourceStringToLower.hashCode();
		}

		@Override
		public String toString() {
			return sourceStringToLower;
		}

	}

	@Override
	public boolean isPseudoInstanceOf(String pseudo) {
		return pseudoInstances.contains(new StringIgnoreCase(pseudo));
	}


	// The following methods (Static pseudo instances) are not supported.

	@Override
	public void addStaticPseudoInstance(String pseudo) {
		// Disable the super implementation. Static pseudo instances are not supported.
		throw new UnsupportedOperationException();
	}

	@Override
	public String[] getStaticPseudoInstances() {
		return new String[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.css.notation.StatefulView#addStates(java.util.Set)
	 */
	@Override
	public void addStates(Set<String> states) {
		for (String state : states) {
			this.pseudoInstances.add(new StringIgnoreCase(state));
		}
		getEngine().notifyChange(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.infra.gmfdiag.css.notation.StatefulView#removeStates(java.util.Set)
	 */
	@Override
	public void removeStates(Set<String> states) {
		for (String state : states) {
			this.pseudoInstances.remove(new StringIgnoreCase(state));
		}
		getEngine().notifyChange(this);
	}

	@Override
	public Set<String> getStates() {
		Set<String> result = new HashSet<String>();
		for (StringIgnoreCase element : pseudoInstances) {
			result.add(element.toString());
		}
		return result;
	}
}

Back to the top