remove unnecessary extension.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / AbstractJavaScanner.java
1 package net.sourceforge.phpdt.internal.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import net.sourceforge.phpdt.ui.text.IColorManager;
13 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
14
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;
25
26 /**
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
30  * this scanner.
31  */
32 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
33
34         private IColorManager fColorManager;
35
36         private IPreferenceStore fPreferenceStore;
37
38         private Map fTokenMap = new HashMap();
39
40         private String[] fPropertyNamesColor;
41
42         private String[] fPropertyNamesStyle;
43
44         /**
45          * Returns the list of preference keys which define the tokens used in the
46          * rules of this scanner.
47          */
48         abstract protected String[] getTokenProperties();
49
50         /**
51          * Creates the list of rules controlling this scanner.
52          */
53         abstract protected List createRules();
54
55         /**
56          * Creates an abstract Java scanner.
57          */
58         public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
59                 super();
60                 fColorManager = manager;
61                 fPreferenceStore = store;
62         }
63
64         /**
65          * Must be called after the constructor has been called.
66          */
67         public final void initialize() {
68
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]);
75                 }
76
77                 initializeRules();
78         }
79
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);
86                 }
87
88                 boolean bold = fPreferenceStore.getBoolean(styleKey);
89                 fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager
90                                 .getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
91         }
92
93         protected Token getToken(String key) {
94                 return (Token) fTokenMap.get(key);
95         }
96
97         private void initializeRules() {
98                 List rules = createRules();
99                 if (rules != null) {
100                         IRule[] result = new IRule[rules.size()];
101                         rules.toArray(result);
102                         setRules(result);
103                 }
104         }
105
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]))
112                                         return i;
113                         }
114                 }
115                 return -1;
116         }
117
118         public boolean affectsBehavior(PropertyChangeEvent event) {
119                 return indexOf(event.getProperty()) >= 0;
120         }
121
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);
128                 else
129                         adaptToStyleChange(token, event);
130         }
131
132         private void adaptToColorChange(Token token, PropertyChangeEvent event) {
133                 RGB rgb = null;
134
135                 Object value = event.getNewValue();
136                 if (value instanceof RGB)
137                         rgb = (RGB) value;
138                 else if (value instanceof String)
139                         rgb = StringConverter.asRGB((String) value);
140
141                 if (rgb != null) {
142
143                         String property = event.getProperty();
144
145                         if (fColorManager instanceof IColorManagerExtension) {
146                                 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
147                                 ext.unbindColor(property);
148                                 ext.bindColor(property, rgb);
149                         }
150
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
156                                                 .getStyle()));
157                         }
158                 }
159         }
160
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))
169                                 bold = true;
170                         else if (IPreferenceStore.FALSE.equals(s))
171                                 bold = false;
172                 }
173
174                 Object data = token.getData();
175                 if (data instanceof TextAttribute) {
176                         TextAttribute oldAttr = (TextAttribute) data;
177                         boolean isBold = (oldAttr.getStyle() == SWT.BOLD);
178                         if (isBold != bold)
179                                 token.setData(new TextAttribute(oldAttr.getForeground(),
180                                                 oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));
181                 }
182         }
183 }