Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0aa6acf6f598e74923a058f675b57b413df8e4ca (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
/*****************************************************************************
 * 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.enginecopy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.e4.ui.css.core.dom.CSSStylableElement;
import org.eclipse.e4.ui.css.core.dom.ExtendedCSSRule;
import org.eclipse.e4.ui.css.core.impl.dom.ViewCSSImpl;
import org.eclipse.e4.ui.css.core.impl.sac.ExtendedSelector;
import org.eclipse.papyrus.infra.gmfdiag.css.engine.ExtendedCSSEngine;
import org.eclipse.papyrus.infra.gmfdiag.css.lists.ExtendedStyleSheetList;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SelectorList;
import org.w3c.dom.Element;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleRule;
import org.w3c.dom.css.CSSStyleSheet;
import org.w3c.dom.css.ViewCSS;
import org.w3c.dom.stylesheets.StyleSheet;
import org.w3c.dom.views.DocumentView;


@SuppressWarnings("restriction")
public class ExtendedViewCSSImpl implements ViewCSS {

	protected ExtendedCSSEngine engine;

	public ExtendedViewCSSImpl(ExtendedCSSEngine extendedCSSEngine) {
		this.engine = extendedCSSEngine;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Implementation based on {@link ViewCSSImpl#getComputedStyle(Element, String)} The base implementations returns the first CSSStyleDeclaration.
	 * 
	 * When a StyleDeclaration is found in a StyleSheet, it is added to a list of declarations.
	 * Then, these declarations are merged
	 */
	public CSSStyleDeclaration getComputedStyle(Element elt, String pseudo) {

		ExtendedStyleSheetList styleSheetList = engine.getAllStylesheets();

		List<StyleWrapper> declarations = new ArrayList<StyleWrapper>();

		//Stylesheets
		for(StyleSheet styleSheet : styleSheetList) {
			List<StyleWrapper> styleWrappers = getStyleWrappers((CSSStyleSheet)styleSheet, elt, pseudo);
			declarations.addAll(styleWrappers);
		}

		//Local styles
		String localStyles = ((CSSStylableElement)elt).getCSSStyle();
		if(localStyles != null) {
			StyleWrapper wrapper = getStyleWrapper(localStyles);
			if(wrapper != null) {
				declarations.add(wrapper);
			}
		}

		return new CSSComputedStyleImpl(declarations);
	}

	private StyleWrapper getStyleWrapper(String localStyles) {
		CSSStyleDeclaration style;
		try {
			style = engine.parseStyleDeclaration(localStyles);
			StyleWrapper wrapper = new StyleWrapper(style, Integer.MAX_VALUE, 0);
			return wrapper;
		} catch (IOException ex) {
			engine.handleExceptions(ex);
		}
		return null;
	}

	/**
	 * Implementation based on {@link ViewCSSImpl#getComputedStyle(CSSStyleSheet, Element, String)}
	 * 
	 * Returns the StyleWrappers instead of a StyleDeclaration
	 */
	private List<StyleWrapper> getStyleWrappers(CSSStyleSheet styleSheet, Element elt, String pseudoElt) {
		List<StyleWrapper> styleDeclarations = new ArrayList<StyleWrapper>();
		CSSRuleList ruleList = styleSheet.getCssRules();
		int length = ruleList.getLength();
		int position = 0;
		for(int i = 0; i < length; i++) {
			CSSRule rule = ruleList.item(i);
			switch(rule.getType()) {
			case CSSRule.STYLE_RULE:
			{
				CSSStyleRule styleRule = (CSSStyleRule)rule;
				if(rule instanceof ExtendedCSSRule) {
					ExtendedCSSRule r = (ExtendedCSSRule)rule;
					SelectorList selectorList = r.getSelectorList();
					// Loop for SelectorList
					int l = selectorList.getLength();
					for(int j = 0; j < l; j++) {
						Selector selector = selectorList.item(j);
						if(selector instanceof ExtendedSelector) {
							ExtendedSelector extendedSelector = (ExtendedSelector)selector;
							if(extendedSelector.match(elt, pseudoElt)) {
								CSSStyleDeclaration style = styleRule.getStyle();
								int specificity = extendedSelector.getSpecificity();
								StyleWrapper wrapper = new StyleWrapper(style, specificity, position++);
								styleDeclarations.add(wrapper);
							}
						} else {
							// TODO : selector is not batik ExtendedSelector,
							// Manage this case...
						}
					}
				} else {
					// TODO : CSS rule is not ExtendedCSSRule,
					// Manage this case...
				}
			}
			}
		}

		return styleDeclarations;
	}

	/**
	 * {@inheritDoc}
	 * 
	 * Unsupported
	 * TODO : Do we need an implementation ?
	 */
	public DocumentView getDocument() {
		return null;
	}
}

Back to the top