Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0bb5486c9ff3aceb91adf15f08fab07a985c7e47 (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
/*****************************************************************************
 * Copyright (c) 2012 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.engine;

import java.io.IOException;
import java.net.URL;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.helper.SemanticElementHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.w3c.dom.Element;

/**
 * A CSS Engine associated to a Diagram.
 * 
 * This Engine is a child of a ModelCSSEngine.
 * 
 * @author Camille Letavernier
 */
@SuppressWarnings("restriction")
public class DiagramCSSEngine extends ExtendedCSSEngineImpl implements IChangeListener {

	private CSSDiagram diagram;

	/**
	 * Constructor
	 * 
	 * Builds a DiagramCSSEngine for a given CSSDiagram
	 * 
	 * @param parent
	 *        The diagram's parent CSSEngine. Its stylesheets will be inherited
	 * @param diagram
	 *        This engine's diagram
	 */
	public DiagramCSSEngine(ExtendedCSSEngine parent, CSSDiagram diagram) {
		super(parent);
		this.diagram = diagram;

		setElementProvider(new GMFElementProvider());
	}

	@Override
	protected void reloadStyleSheets() {
		styleSheets.clear();
		for(StyleSheet styleSheet : diagram.getStyleSheets()) {
			//Do not call super#addStyleSheet(styleSheet) to avoid a StackOverFlow
			styleSheets.add(styleSheet);
		}
	}

	@Override
	protected void parseStyleSheet(StyleSheetReference styleSheet) throws IOException {
		String path = styleSheet.getPath();
		if(path.startsWith("/")) { //Either plug-in or workspace
			path = "platform:/resource" + path;
			URL url = new URL(path);
			try {
				url.openConnection();
			} catch (IOException ex) {
				path = "platform:/plugin" + styleSheet.getPath();
			}
		} else {
			URI uri = URI.createURI(styleSheet.getPath());
			uri = uri.resolve(diagram.eResource().getURI());
			path = uri.toString();
		}
		URL url = new URL(path);
		parseStyleSheet(url.openStream());
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Returns the GMF Notation EObject
	 */
	@Override
	public EObject getNativeWidget(Object element) {
		element = super.getNativeWidget(element);

		if(element == null) {
			return null;
		}

		if(!(element instanceof EObject)) {
			throw new IllegalArgumentException("Unknown element : " + element);
		}

		return (EObject)element; //GMFElement
	}

	public void handleChange(ChangeEvent event) {
		resetCache();
		DiagramHelper.refreshDiagrams(); //FIXME: Should be contextual. We should only refresh the editor(s) containing this Diagram
	}

	@Override
	public Element getElement(Object node) {
		if(node == null) {
			return null;
		}

		EObject notationElement = getNativeWidget(node);
		View canonicalNotationElement = SemanticElementHelper.findPrimaryView(notationElement);

		//A View and a Compartment associated to the same Semantic Element
		//must have the same XML Element. They share the same children.
		//This is required to map the Semantic model (Used by the CSS selectors) 
		//to the Notation model (Used by the CSS properties)
		return super.getElement(canonicalNotationElement);
	}

}

Back to the top