Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de95d45f224ab4bad5bf908542b45f2307580a34 (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
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST 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 (CEA) - bug 429422
 *  Mickael ADAM (ALL4TEC) mickael.adam@ALL4TEC.net - bug 429642
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.engine;

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

import org.eclipse.e4.ui.css.core.dom.IElementProvider;
import org.eclipse.e4.ui.css.core.engine.CSSElementContext;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.DiagramHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationResource;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.ModelStyleSheets;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Element;

/**
 * An extended CSS Engine for an EMF Resource (A Model). This engine
 * is a child of the WorkspaceCSSEngine.
 *
 * It should not be used directly.
 *
 * @author Camille Letavernier
 *
 * @see DiagramCSSEngine
 */
@SuppressWarnings("restriction")
public class ModelCSSEngine extends ExtendedCSSEngineImpl {

	private final Resource model;

	/** A adapter for the model styleSheet. */
	Adapter adapter = new AdapterImpl() {

		public void notifyChanged(Notification notification) {
			ModelCSSEngine.this.reset();

			DiagramHelper.setNeedsRefresh();
			Display.getDefault().asyncExec(new Runnable() {

				public void run() {
					DiagramHelper.refreshDiagrams();
				}
			});
		}
	};

	/**
	 * Creates a ModelCSSEngine for the requested resource.
	 *
	 * @param model
	 */
	public ModelCSSEngine(Resource model) {
		super(getParentCSSEngine(model));
		this.model = model;
		initAdapter();
	}

	/**
	 * Initialize the adapter attached to model styleSheet.
	 * 
	 */
	public void initAdapter() {
		for(EObject eObject : model.getContents()) {
			if(eObject instanceof ModelStyleSheets) {
				ModelStyleSheets styleSheets = (ModelStyleSheets)eObject;
				styleSheets.eAdapters().add(adapter);
				for(StyleSheet styleSheet : styleSheets.getStylesheets()) {
					styleSheet.eAdapters().add(adapter);
				}
			}
		}
	}


	private static ExtendedCSSEngine getParentCSSEngine(Resource resource) {
		ExtendedCSSEngine result;
		if(resource instanceof CSSNotationResource) {
			result = ((CSSNotationResource)resource).getProjectEngine();
		} else {
			result = ProjectCSSEngine.createEngine(resource);
		}

		return result == null ? WorkspaceCSSEngine.instance : result;
	}

	@Override
	protected void reloadStyleSheets() {
		this.styleSheets.clear();
		for(EObject eObject : model.getContents()) {
			if(eObject instanceof ModelStyleSheets) {
				ModelStyleSheets styleSheets = (ModelStyleSheets)eObject;
				for(StyleSheet styleSheet : styleSheets.getStylesheets()) {
					this.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(model.getURI());
			path = uri.toString();
		}
		URL url = new URL(path);
		parseStyleSheet(url.openStream());
	}

	@Override
	public void dispose() {
		for(EObject eObject : model.getContents()) {
			if(eObject instanceof ModelStyleSheets) {
				ModelStyleSheets styleSheets = (ModelStyleSheets)eObject;
				styleSheets.eAdapters().remove(adapter);
				for(StyleSheet styleSheet : styleSheets.getStylesheets()) {
					styleSheet.eAdapters().remove(adapter);
				}
			}
		}
		super.dispose();
	}

	//Unsupported operations. The ModelCSSEngine should not be used directly.

	@Override
	public Element getElement(Object node) {
		throw new UnsupportedOperationException();
	}

	@Override
	public IElementProvider getElementProvider() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void setElementProvider(IElementProvider elementProvider) {
		throw new UnsupportedOperationException();
	}

	@Override
	public CSSElementContext getCSSElementContext(Object node) {
		throw new UnsupportedOperationException();
	}
}

Back to the top