1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / net.sourceforge.phpeclipse / 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         private String[] fPropertyNamesBold;
42         private String[] fPropertyNamesItalic;
43         private String[] fPropertyNamesUnderline;
44         private String[] fPropertyNamesStrikeThrough;
45
46         /**
47          * Returns the list of preference keys which define the tokens used in the
48          * rules of this scanner.
49          */
50         abstract protected String[] getTokenProperties();
51
52         /**
53          * Creates the list of rules controlling this scanner.
54          */
55         abstract protected List createRules();
56
57         /**
58          * Creates an abstract Java scanner.
59          */
60         public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
61                 super();
62                 fColorManager = manager;
63                 fPreferenceStore = store;
64         }
65
66         /**
67          * Must be called after the constructor has been called.
68          */
69         public final void initialize() {
70                 fPropertyNamesColor         = getTokenProperties ();
71                 int length                  = fPropertyNamesColor.length;
72                 fPropertyNamesBold          = new String[length];
73                 fPropertyNamesItalic        = new String[length];
74                 fPropertyNamesUnderline     = new String[length];
75                 fPropertyNamesStrikeThrough = new String[length];
76
77                 for (int i = 0; i < length; i++) {
78                         fPropertyNamesBold[i]          = fPropertyNamesColor[i] + "_bold";          //$NON-NLS-1$
79                         fPropertyNamesItalic[i]        = fPropertyNamesColor[i] + "_italic";        //$NON-NLS-1$
80                         fPropertyNamesUnderline[i]     = fPropertyNamesColor[i] + "_underline";     //$NON-NLS-1$
81                         fPropertyNamesStrikeThrough[i] = fPropertyNamesColor[i] + "_strikethrough"; //$NON-NLS-1$
82
83                         addToken (fPropertyNamesColor[i],
84                                   fPropertyNamesBold[i],                 // bold
85                                   fPropertyNamesItalic[i],               // italic
86                                   fPropertyNamesUnderline[i],            // underline
87                                           fPropertyNamesStrikeThrough[i]);       // strike through
88                 }
89
90                 initializeRules ();
91         }
92
93         /**
94          *
95          */
96
97         private void addToken (String colorKey, 
98                                        String styleKeyBold, String styleKeyItalic, 
99                                        String styleKeyUnderline, String styleKeyStrikeThrough) {
100                 RGB rgb = PreferenceConverter.getColor (fPreferenceStore, colorKey);
101
102                 if (fColorManager instanceof IColorManagerExtension) {
103                         IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
104                         ext.unbindColor(colorKey);
105                         ext.bindColor(colorKey, rgb);
106                 }
107
108                 boolean bold          = fPreferenceStore.getBoolean (styleKeyBold);
109                 boolean italic        = fPreferenceStore.getBoolean (styleKeyItalic);
110                 boolean underline     = fPreferenceStore.getBoolean (styleKeyUnderline);
111                 boolean strikethrough = fPreferenceStore.getBoolean (styleKeyStrikeThrough);
112
113                 int     style     = SWT.NORMAL;
114
115                 style                    |= (bold ? SWT.BOLD : SWT.NORMAL) |
116                                                         (italic ? SWT.ITALIC : SWT.NORMAL) |
117                                                         (underline ? (1 << 30) : SWT.NORMAL) |
118                                                         (strikethrough ? (1 << 29) : SWT.NORMAL);
119
120                 fTokenMap.put (colorKey, new Token (new TextAttribute (fColorManager.getColor(colorKey), null, style)));
121         }
122
123         /**
124          *
125          */
126
127         protected Token getToken (String key) {
128                 return (Token) fTokenMap.get(key);
129         }
130
131         /**
132          *
133          */
134
135         private void initializeRules() {
136                 List rules = createRules();
137                 if (rules != null) {
138                         IRule[] result = new IRule[rules.size()];
139                         rules.toArray(result);
140                         setRules(result);
141                 }
142         }
143
144         /**
145          *
146          */
147
148         private int indexOfNamesColor (String property) {
149                 if (property != null) {
150                         int length = fPropertyNamesColor.length;
151
152                         for (int i = 0; i < length; i++) {
153                                 if (property.equals (fPropertyNamesColor[i])) {
154                                         return i;
155                                 }
156                         }
157                 }
158
159                 return -1;
160         }
161
162         /**
163          *
164          */
165
166         private int indexOfNamesBold (String property) {
167                 if (property != null) {
168                         int length = fPropertyNamesBold.length;
169
170                         for (int i = 0; i < length; i++) {
171                                 if (property.equals (fPropertyNamesBold[i])) {
172                                         return i;
173                                 }
174                         }
175                 }
176
177                 return -1;
178         }
179
180         /**
181          *
182          */
183
184         private int indexOfNamesItalic (String property) {
185                 if (property != null) {
186                         int length = fPropertyNamesItalic.length;
187
188                         for (int i = 0; i < length; i++) {
189                                 if (property.equals (fPropertyNamesItalic[i])) {
190                                         return i;
191                                 }
192                         }
193                 }
194
195                 return -1;
196         }
197
198         /**
199          *
200          */
201
202         private int indexOfNamesUnderline (String property) {
203                 if (property != null) {
204                         int length = fPropertyNamesUnderline.length;
205
206                         for (int i = 0; i < length; i++) {
207                                 if (property.equals (fPropertyNamesUnderline[i])) {
208                                         return i;
209                                 }
210                         }
211                 }
212
213                 return -1;
214         }
215
216         /**
217          *
218          */
219
220         private int indexOfNamesStrikeThrough (String property) {
221                 if (property != null) {
222                         int length = fPropertyNamesStrikeThrough.length;
223
224                         for (int i = 0; i < length; i++) {
225                                 if (property.equals (fPropertyNamesStrikeThrough[i])) {
226                                         return i;
227                                 }
228                         }
229                 }
230
231                 return -1;
232         }
233
234
235         /**
236          *
237          */
238
239         public boolean affectsBehavior (PropertyChangeEvent event) {
240                 return (indexOfNamesColor         (event.getProperty ()) >= 0) ||
241                        (indexOfNamesBold          (event.getProperty ()) >= 0) ||
242                            (indexOfNamesItalic        (event.getProperty ()) >= 0) ||
243                            (indexOfNamesUnderline     (event.getProperty ()) >= 0) ||
244                            (indexOfNamesStrikeThrough (event.getProperty ()) >= 0);
245         }
246
247         /**
248          *
249          */
250
251         public void adaptToPreferenceChange (PropertyChangeEvent event) {
252                 String p = event.getProperty ();
253                 int index = indexOfNamesColor (p);
254
255                 if (index < 0) {
256                         index = indexOfNamesBold (p);
257                 }
258
259                 if (index < 0) {
260                         index = indexOfNamesItalic (p);
261                 }
262
263                 if (index < 0) {
264                         index = indexOfNamesUnderline (p);
265                 }
266
267                 if (index < 0) {
268                         index = indexOfNamesStrikeThrough (p);
269                 }
270
271                 Token token = getToken (fPropertyNamesColor[index]);
272
273                 if (fPropertyNamesColor[index].equals (p)) {
274                         adaptToColorChange (token, event);
275                 }
276                 else {
277                         adaptToStyleChange (token, event);
278                 }
279         }
280
281         /**
282          *
283          */
284         private void adaptToColorChange (Token token, PropertyChangeEvent event) {
285                 RGB rgb = null;
286
287                 Object value = event.getNewValue();
288
289                 if (value instanceof RGB) {
290                         rgb = (RGB) value;
291                 }
292                 else if (value instanceof String) {
293                         rgb = StringConverter.asRGB((String) value);
294                 }
295
296                 if (rgb != null) {
297                         String property = event.getProperty ();
298
299                         if (fColorManager instanceof IColorManagerExtension) {
300                                 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
301                                 ext.unbindColor (property);
302                                 ext.bindColor (property, rgb);
303                         }
304
305                         Object data = token.getData ();
306
307                         if (data instanceof TextAttribute) {
308                                 TextAttribute oldAttr = (TextAttribute) data;
309                                 token.setData (new TextAttribute (fColorManager.getColor (property), oldAttr.getBackground (), oldAttr.getStyle ()));
310                         }
311                 }
312         }
313
314         /**
315          *
316          */
317
318         private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
319                 int       index = -1;
320                 boolean   newValue = false;
321                 Object    value = event.getNewValue ();
322
323                 if (value instanceof Boolean) {
324                         newValue = ((Boolean) value).booleanValue();
325                 }
326                 else if (value instanceof String) {
327                         String s = (String) value;
328
329                         if (IPreferenceStore.TRUE.equals(s)) {
330                                 newValue = true;
331                         }
332                         else if (IPreferenceStore.FALSE.equals(s)) {
333                                 newValue = false;
334                         }
335                 }
336
337                 Object data = token.getData ();
338
339                 if (data instanceof TextAttribute) {
340                         TextAttribute oldAttr = (TextAttribute) data;
341
342                         int style = oldAttr.getStyle ();
343
344                         index = indexOfNamesBold (event.getProperty ());
345
346                         if (index >= 0) {
347                                 if (newValue) {
348                                         style |= SWT.BOLD;
349                                 }
350                                 else {
351                                         style &= ~SWT.BOLD;
352                                 }
353                         }
354
355                         index = indexOfNamesItalic (event.getProperty ());
356
357                         if (index >= 0) {
358                                 if (newValue) {
359                                         style |= SWT.ITALIC;
360                                 }
361                                 else {
362                                         style &= ~SWT.ITALIC;
363                                 }
364                         }
365
366                         index = indexOfNamesUnderline (event.getProperty ());
367
368                         if (index >= 0) {
369                                 if (newValue) {
370                                         style |= (1 << 30);
371                                 }
372                                 else {
373                                         style &= ~(1 << 30);
374                                 }
375                         }
376
377                         index = indexOfNamesStrikeThrough (event.getProperty ());
378
379                         if (index >= 0) {
380                                 if (newValue) {
381                                         style |= (1 << 29);
382                                 }
383                                 else {
384                                         style &= ~(1 << 29);
385                                 }
386                         }
387
388
389                         token.setData (new TextAttribute (oldAttr.getForeground (), oldAttr.getBackground (), style));
390                 }
391         }
392 }