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;
41 private String[] fPropertyNamesBold;
42 private String[] fPropertyNamesItalic;
43 private String[] fPropertyNamesUnderline;
44 private String[] fPropertyNamesStrikeThrough;
47 * Returns the list of preference keys which define the tokens used in the
48 * rules of this scanner.
50 abstract protected String[] getTokenProperties();
53 * Creates the list of rules controlling this scanner.
55 abstract protected List createRules();
58 * Creates an abstract Java scanner.
60 public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
62 fColorManager = manager;
63 fPreferenceStore = store;
67 * Must be called after the constructor has been called.
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];
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$
83 addToken (fPropertyNamesColor[i],
84 fPropertyNamesBold[i], // bold
85 fPropertyNamesItalic[i], // italic
86 fPropertyNamesUnderline[i], // underline
87 fPropertyNamesStrikeThrough[i]); // strike through
97 private void addToken (String colorKey,
98 String styleKeyBold, String styleKeyItalic,
99 String styleKeyUnderline, String styleKeyStrikeThrough) {
100 RGB rgb = PreferenceConverter.getColor (fPreferenceStore, colorKey);
102 if (fColorManager instanceof IColorManagerExtension) {
103 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
104 ext.unbindColor(colorKey);
105 ext.bindColor(colorKey, rgb);
108 boolean bold = fPreferenceStore.getBoolean (styleKeyBold);
109 boolean italic = fPreferenceStore.getBoolean (styleKeyItalic);
110 boolean underline = fPreferenceStore.getBoolean (styleKeyUnderline);
111 boolean strikethrough = fPreferenceStore.getBoolean (styleKeyStrikeThrough);
113 int style = SWT.NORMAL;
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);
120 fTokenMap.put (colorKey, new Token (new TextAttribute (fColorManager.getColor(colorKey), null, style)));
127 protected Token getToken (String key) {
128 return (Token) fTokenMap.get(key);
135 private void initializeRules() {
136 List rules = createRules();
138 IRule[] result = new IRule[rules.size()];
139 rules.toArray(result);
148 private int indexOfNamesColor (String property) {
149 if (property != null) {
150 int length = fPropertyNamesColor.length;
152 for (int i = 0; i < length; i++) {
153 if (property.equals (fPropertyNamesColor[i])) {
166 private int indexOfNamesBold (String property) {
167 if (property != null) {
168 int length = fPropertyNamesBold.length;
170 for (int i = 0; i < length; i++) {
171 if (property.equals (fPropertyNamesBold[i])) {
184 private int indexOfNamesItalic (String property) {
185 if (property != null) {
186 int length = fPropertyNamesItalic.length;
188 for (int i = 0; i < length; i++) {
189 if (property.equals (fPropertyNamesItalic[i])) {
202 private int indexOfNamesUnderline (String property) {
203 if (property != null) {
204 int length = fPropertyNamesUnderline.length;
206 for (int i = 0; i < length; i++) {
207 if (property.equals (fPropertyNamesUnderline[i])) {
220 private int indexOfNamesStrikeThrough (String property) {
221 if (property != null) {
222 int length = fPropertyNamesStrikeThrough.length;
224 for (int i = 0; i < length; i++) {
225 if (property.equals (fPropertyNamesStrikeThrough[i])) {
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);
251 public void adaptToPreferenceChange (PropertyChangeEvent event) {
252 String p = event.getProperty ();
253 int index = indexOfNamesColor (p);
256 index = indexOfNamesBold (p);
260 index = indexOfNamesItalic (p);
264 index = indexOfNamesUnderline (p);
268 index = indexOfNamesStrikeThrough (p);
271 Token token = getToken (fPropertyNamesColor[index]);
273 if (fPropertyNamesColor[index].equals (p)) {
274 adaptToColorChange (token, event);
277 adaptToStyleChange (token, event);
284 private void adaptToColorChange (Token token, PropertyChangeEvent event) {
287 Object value = event.getNewValue();
289 if (value instanceof RGB) {
292 else if (value instanceof String) {
293 rgb = StringConverter.asRGB((String) value);
297 String property = event.getProperty ();
299 if (fColorManager instanceof IColorManagerExtension) {
300 IColorManagerExtension ext = (IColorManagerExtension) fColorManager;
301 ext.unbindColor (property);
302 ext.bindColor (property, rgb);
305 Object data = token.getData ();
307 if (data instanceof TextAttribute) {
308 TextAttribute oldAttr = (TextAttribute) data;
309 token.setData (new TextAttribute (fColorManager.getColor (property), oldAttr.getBackground (), oldAttr.getStyle ()));
318 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
320 boolean newValue = false;
321 Object value = event.getNewValue ();
323 if (value instanceof Boolean) {
324 newValue = ((Boolean) value).booleanValue();
326 else if (value instanceof String) {
327 String s = (String) value;
329 if (IPreferenceStore.TRUE.equals(s)) {
332 else if (IPreferenceStore.FALSE.equals(s)) {
337 Object data = token.getData ();
339 if (data instanceof TextAttribute) {
340 TextAttribute oldAttr = (TextAttribute) data;
342 int style = oldAttr.getStyle ();
344 index = indexOfNamesBold (event.getProperty ());
355 index = indexOfNamesItalic (event.getProperty ());
362 style &= ~SWT.ITALIC;
366 index = indexOfNamesUnderline (event.getProperty ());
377 index = indexOfNamesStrikeThrough (event.getProperty ());
389 token.setData (new TextAttribute (oldAttr.getForeground (), oldAttr.getBackground (), style));