fixed NPE
[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 /**
28  * Initialized with a color manager and a preference store, its subclasses are
29  * only responsible for providing a list of preference keys based on which tokens
30  * are generated and to use this tokens to define the rules controlling this scanner.
31  */
32 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
33                         
34         
35         private IColorManager fColorManager;
36         private IPreferenceStore fPreferenceStore;
37         
38         private Map fTokenMap= new HashMap();
39         private String[] fPropertyNamesColor;
40         private String[] fPropertyNamesStyle;
41         
42         
43         /** 
44          * Returns the list of preference keys which define the tokens
45          * used in the rules of this scanner.
46          */
47         abstract protected String[] getTokenProperties();
48                 
49         /**
50          * Creates the list of rules controlling this scanner.
51          */
52         abstract protected List createRules();
53                 
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.getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
90         }
91         
92         protected Token getToken(String key) {
93                 return (Token) fTokenMap.get(key);
94         }
95                 
96         private void initializeRules() {
97                 List rules= createRules();
98                 if (rules != null) {
99                         IRule[] result= new IRule[rules.size()];
100                         rules.toArray(result);
101                         setRules(result);
102                 }
103         }
104         
105         private int indexOf(String property) {
106                 if (property != null) {
107                         int length= fPropertyNamesColor.length;
108                         for (int i= 0; i < length; i++) {
109                                 if (property.equals(fPropertyNamesColor[i]) || property.equals(fPropertyNamesStyle[i]))
110                                         return i;
111                         }
112                 }
113                 return -1;
114         }
115         
116         public boolean affectsBehavior(PropertyChangeEvent event) {
117                 return indexOf(event.getProperty()) >= 0;
118         }
119         
120         public void adaptToPreferenceChange(PropertyChangeEvent event) {
121                 String p= event.getProperty();
122                 int index= indexOf(p);
123                 Token token= getToken(fPropertyNamesColor[index]);
124                 if (fPropertyNamesColor[index].equals(p))
125                         adaptToColorChange(token, event);
126                 else
127                         adaptToStyleChange(token, event);
128         }
129         
130         private void adaptToColorChange(Token token, PropertyChangeEvent event) {
131                 RGB rgb= null;
132                 
133                 Object value= event.getNewValue();
134                 if (value instanceof RGB)
135                         rgb= (RGB) value;
136                 else if (value instanceof String)
137                         rgb= StringConverter.asRGB((String) value);
138                         
139                 if (rgb != null) {
140                         
141                         String property= event.getProperty();
142                         
143                         if (fColorManager instanceof IColorManagerExtension) {
144                                 IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
145                                 ext.unbindColor(property);
146                                 ext.bindColor(property, rgb);
147                         }
148                         
149                         Object data= token.getData();
150                         if (data instanceof TextAttribute) {
151                                 TextAttribute oldAttr= (TextAttribute) data;
152                                 token.setData(new TextAttribute(fColorManager.getColor(property), oldAttr.getBackground(), oldAttr.getStyle()));
153                         }
154                 }
155         }
156         
157         private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
158                 boolean bold= false;
159                 Object value= event.getNewValue();
160                 if (value instanceof Boolean)
161                         bold= ((Boolean) value).booleanValue();
162                 else if (value instanceof String) {
163                         String s= (String) value;
164                         if (IPreferenceStore.TRUE.equals(s))
165                                 bold= true;
166                         else if (IPreferenceStore.FALSE.equals(s))
167                                 bold= false;
168                 }
169                 
170                 Object data= token.getData();
171                 if (data instanceof TextAttribute) {
172                         TextAttribute oldAttr= (TextAttribute) data;
173                         boolean isBold= (oldAttr.getStyle() == SWT.BOLD);
174                         if (isBold != bold) 
175                                 token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));
176                 }
177         }
178 }