Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28183752898a3e92ea96111eb6069b950a161ba8 (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
/*****************************************************************************
 * Copyright (c) 2012, 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
 *  Christian W. Damus (CEA) - support adapter instead of custom resource impl for CSS (CDO)
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.notation;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

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.ecore.resource.Resource;
import org.eclipse.gmf.runtime.notation.EObjectListValueStyle;
import org.eclipse.gmf.runtime.notation.NamedStyle;
import org.eclipse.gmf.runtime.notation.impl.DiagramImpl;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.DiagramCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.resource.CSSNotationResource;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;

/**
 * Default implementation for CSSDiagram
 * 
 * @author Camille letavernier
 */
@SuppressWarnings("restriction")
public class CSSDiagramImpl extends DiagramImpl implements CSSDiagram {

	protected ExtendedCSSEngine engine;

	private Adapter disposeListener;

	public ExtendedCSSEngine getEngine() {
		if(engine == null) {
			engine = new DiagramCSSEngine(getModelEngine(), this);
			eResource().eAdapters().add(disposeListener = new DiagramDisposeListener());
		}
		return engine;
	}

	private void disposeEngine(Object notifier) {
		if(engine != null) {
			engine.dispose();
			engine = null;
			((Resource)notifier).eAdapters().remove(disposeListener);
		}
	}

	protected ExtendedCSSEngine getModelEngine() {
		return CSSNotationResource.getEngine(eResource());
	}

	public List<StyleSheet> getStyleSheets() {
		List<StyleSheet> result = new LinkedList<StyleSheet>();

		for(Object styleObject : getStyles()) {
			if(styleObject instanceof NamedStyle) {

				NamedStyle style = (NamedStyle)styleObject;

				if(CSSStyles.CSS_DIAGRAM_STYLESHEETS_KEY.equals(style.getName())) {
					if(style instanceof EObjectListValueStyle) {

						EObjectListValueStyle stylesheetsStyle = (EObjectListValueStyle)style;

						for(Object eObjectValue : stylesheetsStyle.getEObjectListValue()) {
							if(eObjectValue instanceof StyleSheet) {
								result.add((StyleSheet)eObjectValue);
							}
						}
					}
				}
			}
		}

		return result;
	}

	private class DiagramDisposeListener extends AdapterImpl {

		@Override
		public void notifyChanged(Notification notification) {
			switch(notification.getEventType()) {
			case Notification.REMOVE:
				if(notification.getOldValue() == CSSDiagramImpl.this) {
					disposeEngine(notification.getNotifier());
				}
				break;
			case Notification.REMOVE_MANY:
				for(Object object : (Collection<?>)notification.getOldValue()) {
					if(object == CSSDiagramImpl.this) {
						disposeEngine(notification.getNotifier());
					}
				}
				break;
			}
		}
	}

}

Back to the top