Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a5b76514954d7123f860f193cd59fd3d9f9eeb8c (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
/*****************************************************************************
 * 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.configuration.providers;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.papyrus.infra.gmfdiag.css.Content;
import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
import org.eclipse.papyrus.infra.gmfdiag.css.configuration.helper.XtextStylesheetHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider;


public class ExistingStyleContentProvider implements IHierarchicContentProvider {

	protected Map<StyleSheet, Stylesheet> stylesheets;

	protected final View context;

	public ExistingStyleContentProvider(View context) {
		this.context = context;
	}

	public Object[] getElements(Object inputElement) {
		Collection<StyleSheet> stylesheets = getStyleSheets();
		if(stylesheets.isEmpty()) {
			//Display a message to let the user know why he cannot edit a stylesheet
			return new Object[]{ "No stylesheet available" };
		}
		return stylesheets.toArray();
	}

	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof StyleSheet) {
			Stylesheet xtextStylesheet = stylesheets.get(parentElement);

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

			for(Content stylesheetElement : xtextStylesheet.getContents()) {
				if(stylesheetElement instanceof Ruleset) {
					result.add((Ruleset)stylesheetElement);
				}
			}

			return result.toArray();
		} else {
			return new Object[0];
		}
	}

	public Object getParent(Object element) {
		if(element instanceof Stylesheet) {
			return null;
		}

		if(element instanceof Ruleset) {
			return ((Ruleset)element).eContainer();
		}

		return null;
	}

	public boolean hasChildren(Object element) {
		return getChildren(element).length > 0;
	}

	public void dispose() {

	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

	}

	public boolean isValidValue(Object element) {
		return element instanceof Ruleset;
	}

	protected Collection<StyleSheet> getStyleSheets() {
		if(stylesheets == null) {
			stylesheets = new LinkedHashMap<StyleSheet, Stylesheet>();
			if(context.getDiagram() instanceof CSSDiagram) {
				CSSDiagram diagram = (CSSDiagram)context.getDiagram();
				parseStyleSheets(diagram.getStyleSheets());
			}
		}
		return stylesheets.keySet();
	}

	protected void parseStyleSheets(List<StyleSheet> appliedStylesheets) {
		for(StyleSheet stylesheet : appliedStylesheets) {
			if(stylesheet instanceof StyleSheetReference) {
				parseStyleSheet((StyleSheetReference)stylesheet);
			} else if(stylesheet instanceof EmbeddedStyleSheet) {
				//Unsupported yet
			}
		}
	}

	protected void parseStyleSheet(StyleSheetReference stylesheet) {
		Resource resource = XtextStylesheetHelper.loadStylesheet(stylesheet, null, context, null);
		if(resource.getContents().isEmpty()) {
			return;
		}

		for(EObject rootElement : resource.getContents()) {
			if(rootElement instanceof Stylesheet) {
				stylesheets.put(stylesheet, (Stylesheet)rootElement);
			}
		}
	}

}

Back to the top