Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f24c94bed635de312270dff12b6e686da814e31 (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
/*******************************************************************************
 * Copyright (c) 2001, 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.dtd.ui.style;

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.dtd.core.parser.DTDRegionTypes;
import org.eclipse.wst.sse.core.text.ITextRegion;
import org.eclipse.wst.sse.ui.internal.SSEUIPlugin;
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 LineStyleProviderForDTD extends AbstractLineStyleProvider implements LineStyleProvider {
	public LineStyleProviderForDTD() {
		super();
		loadColors();
	}

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

	protected TextAttribute getAttributeFor(ITextRegion region) {
		/**
		 * a method to centralize all the "format rules" for regions
		 * specifically associated for how to "open" the region.
		 */
		// not sure why this is coming through null, but just to catch it
		if (region == null) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_DEFAULT);
		}
		String type = region.getType();
		if (type == DTDRegionTypes.CONTENT_EMPTY || type == DTDRegionTypes.CONTENT_ANY || type == DTDRegionTypes.CONTENT_PCDATA) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_DATA);
		} else if (type == DTDRegionTypes.ELEMENT_TAG || type == DTDRegionTypes.ENTITY_TAG || type == DTDRegionTypes.ATTLIST_TAG || type == DTDRegionTypes.NOTATION_TAG) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_TAGNAME);
		} else if (type == DTDRegionTypes.CONNECTOR || type == DTDRegionTypes.OCCUR_TYPE) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_SYMBOL);
		} else if (type == DTDRegionTypes.NDATA_VALUE) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_DATA);
		} else if (type == DTDRegionTypes.START_TAG || type == DTDRegionTypes.END_TAG || type == DTDRegionTypes.EXCLAMATION) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_TAG);
		} else if (type == DTDRegionTypes.COMMENT_START || type == DTDRegionTypes.COMMENT_CONTENT || type == DTDRegionTypes.COMMENT_END) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_COMMENT);
		} else if (type == DTDRegionTypes.SINGLEQUOTED_LITERAL || type == DTDRegionTypes.DOUBLEQUOTED_LITERAL) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_STRING);
		} else if (type == DTDRegionTypes.SYSTEM_KEYWORD || type == DTDRegionTypes.PUBLIC_KEYWORD || type == DTDRegionTypes.NDATA_KEYWORD || type == DTDRegionTypes.CDATA_KEYWORD || type == DTDRegionTypes.ID_KEYWORD || type == DTDRegionTypes.IDREF_KEYWORD || type == DTDRegionTypes.IDREFS_KEYWORD || type == DTDRegionTypes.ENTITY_KEYWORD || type == DTDRegionTypes.ENTITIES_KEYWORD || type == DTDRegionTypes.NMTOKEN_KEYWORD || type == DTDRegionTypes.NMTOKENS_KEYWORD || type == DTDRegionTypes.NOTATION_KEYWORD || type == DTDRegionTypes.REQUIRED_KEYWORD || type == DTDRegionTypes.IMPLIED_KEYWORD || type == DTDRegionTypes.FIXED_KEYWORD) {
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_KEYWORD);
		} else if (type == DTDRegionTypes.NAME || type == DTDRegionTypes.ENTITY_PARM) {
			//			if (region instanceof DTDRegion) {
			//				DTDRegion dtdRegion = (DTDRegion) region;
			//				IStructuredDocumentRegion flatNode = dtdRegion.getParent();
			//				String regionText = flatNode.getText(dtdRegion);
			//				if (regionText.equals("ANY") || regionText.equals("EMPTY")) {
			//					return new TextAttribute(DTDColors.DTD_KEYWORD);
			//				}
			//			}
			return (TextAttribute) getTextAttributes().get(IStyleConstantsDTD.DTD_DATA);
		}

		// default, return null to signal "not handled"
		// in which case, other factories should be tried
		return null;
	}

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

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

	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(IStyleConstantsDTD.DTD_DEFAULT).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_DEFAULT;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_TAG).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_TAG;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_TAGNAME).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_TAGNAME;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_COMMENT).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_COMMENT;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_KEYWORD).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_KEYWORD;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_STRING).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_STRING;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_DATA).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_DATA;
			} else if (getPreferenceKey(IStyleConstantsDTD.DTD_SYMBOL).equals(prefKey)) {
				styleKey = IStyleConstantsDTD.DTD_SYMBOL;
			}
		}

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

	protected void loadColors() {
		clearColors();

		addTextAttribute(IStyleConstantsDTD.DTD_DEFAULT);
		addTextAttribute(IStyleConstantsDTD.DTD_TAG);
		addTextAttribute(IStyleConstantsDTD.DTD_TAGNAME);
		addTextAttribute(IStyleConstantsDTD.DTD_COMMENT);
		addTextAttribute(IStyleConstantsDTD.DTD_KEYWORD);
		addTextAttribute(IStyleConstantsDTD.DTD_STRING);
		addTextAttribute(IStyleConstantsDTD.DTD_DATA);
		addTextAttribute(IStyleConstantsDTD.DTD_SYMBOL);
	}
}

Back to the top