1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.util.HashMap;
12 import net.sourceforge.phpdt.ui.text.IColorManager;
13 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.PreferenceConverter;
17 import org.eclipse.jface.resource.StringConverter;
18 import org.eclipse.jface.text.TextAttribute;
19 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
20 import org.eclipse.jface.text.rules.IRule;
21 import org.eclipse.jface.text.rules.Token;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.RGB;
27 * Initialized with a color manager and a preference store, its subclasses are
28 * only responsible for providing a list of preference keys based on which
29 * tokens are generated and to use this tokens to define the rules controlling
32 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
34 private IColorManager fColorManager;
36 private IPreferenceStore fPreferenceStore;
38 private Map fTokenMap = new HashMap();
40 private String[] fPropertyNamesColor;
42 private String[] fPropertyNamesStyle;
45 * Returns the list of preference keys which define the tokens used in the
46 * rules of this scanner.
48 abstract protected String[] getTokenProperties();
51 * Creates the list of rules controlling this scanner.
53 abstract protected List createRules();
56 * Creates an abstract Java scanner.
58 public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
60 fColorManager = manager;
61 fPreferenceStore = store;
65 * Must be called after the constructor has been called.
67 public final void initialize() {
69 fPropertyNamesColor = getTokenProperties();
70 int length = fPropertyNamesColor.length;
71 fPropertyNamesStyle = new String[length];
72 for (int i = 0; i < length; i++) {
73 fPropertyNamesStyle[i] = fPropertyNamesColor[i] + "_bold"; //$NON-NLS-1$
74 addToken(fPropertyNamesColor[i], fPropertyNamesStyle[i]);
80 private void addToken(String colorKey, String styleKey) {
81 RGB rgb = PreferenceConverter.getColor(fPreferenceStore, colorKey);
82 if (fColorManager instanceof IColorManagerExtension) {
83 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
84 ext.unbindColor(colorKey);
85 ext.bindColor(colorKey, rgb);
88 boolean bold = fPreferenceStore.getBoolean(styleKey);
89 fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager
90 .getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
93 protected Token getToken(String key) {
94 return (Token) fTokenMap.get(key);
97 private void initializeRules() {
98 List rules = createRules();
100 IRule[] result = new IRule[rules.size()];
101 rules.toArray(result);
106 private int indexOf(String property) {
107 if (property != null) {
108 int length = fPropertyNamesColor.length;
109 for (int i = 0; i < length; i++) {
110 if (property.equals(fPropertyNamesColor[i])
111 || property.equals(fPropertyNamesStyle[i]))
118 public boolean affectsBehavior(PropertyChangeEvent event) {
119 return indexOf(event.getProperty()) >= 0;
122 public void adaptToPreferenceChange(PropertyChangeEvent event) {
123 String p = event.getProperty();
124 int index = indexOf(p);
125 Token token = getToken(fPropertyNamesColor[index]);
126 if (fPropertyNamesColor[index].equals(p))
127 adaptToColorChange(token, event);
129 adaptToStyleChange(token, event);
132 private void adaptToColorChange(Token token, PropertyChangeEvent event) {
135 Object value = event.getNewValue();
136 if (value instanceof RGB)
138 else if (value instanceof String)
139 rgb = StringConverter.asRGB((String) value);
143 String property = event.getProperty();
145 if (fColorManager instanceof IColorManagerExtension) {
146 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
147 ext.unbindColor(property);
148 ext.bindColor(property, rgb);
151 Object data = token.getData();
152 if (data instanceof TextAttribute) {
153 TextAttribute oldAttr = (TextAttribute) data;
154 token.setData(new TextAttribute(fColorManager
155 .getColor(property), oldAttr.getBackground(), oldAttr
161 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
162 boolean bold = false;
163 Object value = event.getNewValue();
164 if (value instanceof Boolean)
165 bold = ((Boolean) value).booleanValue();
166 else if (value instanceof String) {
167 String s = (String) value;
168 if (IPreferenceStore.TRUE.equals(s))
170 else if (IPreferenceStore.FALSE.equals(s))
174 Object data = token.getData();
175 if (data instanceof TextAttribute) {
176 TextAttribute oldAttr = (TextAttribute) data;
177 boolean isBold = (oldAttr.getStyle() == SWT.BOLD);
179 token.setData(new TextAttribute(oldAttr.getForeground(),
180 oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));