deleted refs to org.eclipse.update.internal.ui.UpdateUIPlugin
[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 org.eclipse.swt.SWT;
13 import org.eclipse.swt.graphics.RGB;
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
24 import net.sourceforge.phpdt.ui.text.IColorManager;
25 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
26
27
28 /**
29  * Initialized with a color manager and a preference store, its subclasses are
30  * only responsible for providing a list of preference keys based on which tokens
31  * are generated and to use this tokens to define the rules controlling this scanner.
32  */
33 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
34                         
35         
36         private IColorManager fColorManager;
37         private IPreferenceStore fPreferenceStore;
38         
39         private Map fTokenMap= new HashMap();
40         private String[] fPropertyNamesColor;
41         private String[] fPropertyNamesStyle;
42         
43         
44         /** 
45          * Returns the list of preference keys which define the tokens
46          * used in the 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         /**
57          * Creates an abstract Java scanner.
58          */
59         public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
60                 super();
61                 fColorManager= manager;
62                 fPreferenceStore= store;
63         }
64         
65         /**
66          * Must be called after the constructor has been called.
67          */
68         public final void initialize() {
69                 
70                 fPropertyNamesColor= getTokenProperties();
71                 int length= fPropertyNamesColor.length;
72                 fPropertyNamesStyle= new String[length];
73                 for (int i= 0; i < length; i++) {
74                         fPropertyNamesStyle[i]= fPropertyNamesColor[i] + "_bold"; //$NON-NLS-1$
75                         addToken(fPropertyNamesColor[i], fPropertyNamesStyle[i]);
76                 }
77                 
78                 initializeRules();
79         }
80                 
81         private void addToken(String colorKey, String styleKey) {
82                 RGB rgb= PreferenceConverter.getColor(fPreferenceStore, colorKey);
83                 if (fColorManager instanceof IColorManagerExtension) {
84                         IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
85                         ext.unbindColor(colorKey);
86                         ext.bindColor(colorKey, rgb);
87                 }
88                 
89                 boolean bold= fPreferenceStore.getBoolean(styleKey);
90                 fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager.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]) || property.equals(fPropertyNamesStyle[i]))
111                                         return i;
112                         }
113                 }
114                 return -1;
115         }
116         
117         public boolean affectsBehavior(PropertyChangeEvent event) {
118                 return indexOf(event.getProperty()) >= 0;
119         }
120         
121         public void adaptToPreferenceChange(PropertyChangeEvent event) {
122                 String p= event.getProperty();
123                 int index= indexOf(p);
124                 Token token= getToken(fPropertyNamesColor[index]);
125                 if (fPropertyNamesColor[index].equals(p))
126                         adaptToColorChange(token, event);
127                 else
128                         adaptToStyleChange(token, event);
129         }
130         
131         private void adaptToColorChange(Token token, PropertyChangeEvent event) {
132                 RGB rgb= null;
133                 
134                 Object value= event.getNewValue();
135                 if (value instanceof RGB)
136                         rgb= (RGB) value;
137                 else if (value instanceof String)
138                         rgb= StringConverter.asRGB((String) value);
139                         
140                 if (rgb != null) {
141                         
142                         String property= event.getProperty();
143                         
144                         if (fColorManager instanceof IColorManagerExtension) {
145                                 IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
146                                 ext.unbindColor(property);
147                                 ext.bindColor(property, rgb);
148                         }
149                         
150                         Object data= token.getData();
151                         if (data instanceof TextAttribute) {
152                                 TextAttribute oldAttr= (TextAttribute) data;
153                                 token.setData(new TextAttribute(fColorManager.getColor(property), oldAttr.getBackground(), oldAttr.getStyle()));
154                         }
155                 }
156         }
157         
158         private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
159                 boolean bold= false;
160                 Object value= event.getNewValue();
161                 if (value instanceof Boolean)
162                         bold= ((Boolean) value).booleanValue();
163                 else if (value instanceof String) {
164                         String s= (String) value;
165                         if (IPreferenceStore.TRUE.equals(s))
166                                 bold= true;
167                         else if (IPreferenceStore.FALSE.equals(s))
168                                 bold= false;
169                 }
170                 
171                 Object data= token.getData();
172                 if (data instanceof TextAttribute) {
173                         TextAttribute oldAttr= (TextAttribute) data;
174                         boolean isBold= (oldAttr.getStyle() == SWT.BOLD);
175                         if (isBold != bold) 
176                                 token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));
177                 }
178         }
179 }