Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 27bf5378cd2edc53a89ec1ccde9328eea5dcb9af (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.css.ui.style;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.wst.common.encoding.content.IContentTypeIdentifier;
import org.eclipse.wst.css.core.parser.CSSRegionContexts;
import org.eclipse.wst.sse.core.text.ITextRegion;
import org.eclipse.wst.sse.ui.EditorPlugin;
import org.eclipse.wst.sse.ui.preferences.PreferenceKeyGenerator;
import org.eclipse.wst.sse.ui.style.AbstractLineStyleProvider;
import org.eclipse.wst.sse.ui.style.LineStyleProvider;

/**
 */
public class LineStyleProviderForCSS extends AbstractLineStyleProvider implements LineStyleProvider {
	/** Contains region to style mapping */
	private Map fColorTypes;

	/**
	 * LineStyleProviderForEmbeddedCSS constructor comment.
	 */
	public LineStyleProviderForCSS() {
		super();
		initAttributes();
		loadColors();
	}

	protected TextAttribute getAttributeFor(ITextRegion region) {
		if (region != null) {
			String type = region.getType();
			if (type != null) {
				return getAttributeFor(type);
			}
		}
		return (TextAttribute) getTextAttributes().get(IStyleConstantsCSS.NORMAL);
	}

	protected TextAttribute getAttributeFor(String type) {
		return (TextAttribute) getTextAttributes().get(fColorTypes.get(type));
	}

	private void initAttributes() {
		if (fColorTypes == null) {
			fColorTypes = new HashMap();
		}
		fColorTypes.put(CSSRegionContexts.CSS_COMMENT, IStyleConstantsCSS.COMMENT);
		fColorTypes.put(CSSRegionContexts.CSS_CDO, IStyleConstantsCSS.COMMENT);
		fColorTypes.put(CSSRegionContexts.CSS_CDC, IStyleConstantsCSS.COMMENT);
		fColorTypes.put(CSSRegionContexts.CSS_S, IStyleConstantsCSS.NORMAL);

		fColorTypes.put(CSSRegionContexts.CSS_DELIMITER, IStyleConstantsCSS.SEMI_COLON);
		fColorTypes.put(CSSRegionContexts.CSS_LBRACE, IStyleConstantsCSS.CURLY_BRACE);
		fColorTypes.put(CSSRegionContexts.CSS_RBRACE, IStyleConstantsCSS.CURLY_BRACE);

		fColorTypes.put(CSSRegionContexts.CSS_IMPORT, IStyleConstantsCSS.ATMARK_RULE);
		fColorTypes.put(CSSRegionContexts.CSS_PAGE, IStyleConstantsCSS.ATMARK_RULE);
		fColorTypes.put(CSSRegionContexts.CSS_MEDIA, IStyleConstantsCSS.ATMARK_RULE);
		fColorTypes.put(CSSRegionContexts.CSS_FONT_FACE, IStyleConstantsCSS.ATMARK_RULE);
		fColorTypes.put(CSSRegionContexts.CSS_CHARSET, IStyleConstantsCSS.ATMARK_RULE);
		fColorTypes.put(CSSRegionContexts.CSS_ATKEYWORD, IStyleConstantsCSS.ATMARK_RULE);

		fColorTypes.put(CSSRegionContexts.CSS_STRING, IStyleConstantsCSS.STRING);
		fColorTypes.put(CSSRegionContexts.CSS_URI, IStyleConstantsCSS.URI);
		fColorTypes.put(CSSRegionContexts.CSS_MEDIUM, IStyleConstantsCSS.MEDIA);
		fColorTypes.put(CSSRegionContexts.CSS_MEDIA_SEPARATOR, IStyleConstantsCSS.MEDIA);

		fColorTypes.put(CSSRegionContexts.CSS_CHARSET_NAME, IStyleConstantsCSS.STRING);

		fColorTypes.put(CSSRegionContexts.CSS_PAGE_SELECTOR, IStyleConstantsCSS.MEDIA);

		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ELEMENT_NAME, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_UNIVERSAL, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_PSEUDO, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_CLASS, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ID, IStyleConstantsCSS.SELECTOR);

		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_COMBINATOR, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_SEPARATOR, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_START, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_END, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_NAME, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_VALUE, IStyleConstantsCSS.SELECTOR);
		fColorTypes.put(CSSRegionContexts.CSS_SELECTOR_ATTRIBUTE_OPERATOR, IStyleConstantsCSS.SELECTOR);

		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_PROPERTY, IStyleConstantsCSS.PROPERTY_NAME);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_IDENT, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_DIMENSION, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_PERCENTAGE, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_NUMBER, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_FUNCTION, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_PARENTHESIS_CLOSE, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_STRING, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_URI, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_HASH, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_UNICODE_RANGE, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_IMPORTANT, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_OPERATOR, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_VALUE_S, IStyleConstantsCSS.PROPERTY_VALUE);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_SEPARATOR, IStyleConstantsCSS.COLON);
		fColorTypes.put(CSSRegionContexts.CSS_DECLARATION_DELIMITER, IStyleConstantsCSS.SEMI_COLON);

		fColorTypes.put(CSSRegionContexts.CSS_UNKNOWN, IStyleConstantsCSS.NORMAL);
	}

	protected void handlePropertyChange(PropertyChangeEvent event) {
		String styleKey = null;

		if (event != null) {
			String prefKey = event.getProperty();
			// check if preference changed is a style preference
			if (getPreferenceKey(IStyleConstantsCSS.ATMARK_RULE).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.ATMARK_RULE;
			} else if (getPreferenceKey(IStyleConstantsCSS.COLON).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.COLON;
			} else if (getPreferenceKey(IStyleConstantsCSS.COMMENT).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.COMMENT;
			} else if (getPreferenceKey(IStyleConstantsCSS.CURLY_BRACE).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.CURLY_BRACE;
			} else if (getPreferenceKey(IStyleConstantsCSS.ERROR).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.ERROR;
			} else if (getPreferenceKey(IStyleConstantsCSS.MEDIA).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.MEDIA;
			} else if (getPreferenceKey(IStyleConstantsCSS.NORMAL).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.NORMAL;
			} else if (getPreferenceKey(IStyleConstantsCSS.PROPERTY_NAME).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.PROPERTY_NAME;
			} else if (getPreferenceKey(IStyleConstantsCSS.PROPERTY_VALUE).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.PROPERTY_VALUE;
			} else if (getPreferenceKey(IStyleConstantsCSS.SELECTOR).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.SELECTOR;
			} else if (getPreferenceKey(IStyleConstantsCSS.SEMI_COLON).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.SEMI_COLON;
			} else if (getPreferenceKey(IStyleConstantsCSS.STRING).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.STRING;
			} else if (getPreferenceKey(IStyleConstantsCSS.URI).equals(prefKey)) {
				styleKey = IStyleConstantsCSS.URI;
			}
		} else {
			// this is around for old deprecated preferencesChanged() method
			// TODO remove when preferencesChanged() is removed
			loadColors();
			super.handlePropertyChange(event);
		}

		if (styleKey != null) {
			// overwrite style preference with new value
			addTextAttribute(styleKey);
			super.handlePropertyChange(event);
		}
	}

	public void release() {
		if (fColorTypes != null) {
			fColorTypes.clear();
		}
		super.release();
	}

	public void loadColors() {
		clearColors();
		addTextAttribute(IStyleConstantsCSS.ATMARK_RULE);
		addTextAttribute(IStyleConstantsCSS.COLON);
		addTextAttribute(IStyleConstantsCSS.COMMENT);
		addTextAttribute(IStyleConstantsCSS.CURLY_BRACE);
		addTextAttribute(IStyleConstantsCSS.ERROR);
		addTextAttribute(IStyleConstantsCSS.MEDIA);
		addTextAttribute(IStyleConstantsCSS.NORMAL);
		addTextAttribute(IStyleConstantsCSS.PROPERTY_NAME);
		addTextAttribute(IStyleConstantsCSS.PROPERTY_VALUE);
		addTextAttribute(IStyleConstantsCSS.SELECTOR);
		addTextAttribute(IStyleConstantsCSS.SEMI_COLON);
		addTextAttribute(IStyleConstantsCSS.STRING);
		addTextAttribute(IStyleConstantsCSS.URI);
	}

	protected void clearColors() {
		getTextAttributes().clear();
	}

	protected String getPreferenceKey(String key) {
		String contentTypeId = IContentTypeIdentifier.ContentTypeID_CSS;
		return PreferenceKeyGenerator.generateKey(key, contentTypeId);
	}

	protected IPreferenceStore getColorPreferences() {
		return EditorPlugin.getDefault().getPreferenceStore();
	}
}

Back to the top