1 package net.sourceforge.phpdt.internal.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import java.util.HashMap;
12 import org.eclipse.swt.SWT;
13 import org.eclipse.swt.graphics.RGB;
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;
24 import net.sourceforge.phpdt.ui.text.IColorManager;
25 import net.sourceforge.phpdt.ui.text.IColorManagerExtension;
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.
33 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
36 private IColorManager fColorManager;
37 private IPreferenceStore fPreferenceStore;
39 private Map fTokenMap= new HashMap();
40 private String[] fPropertyNamesColor;
41 private String[] fPropertyNamesStyle;
45 * Returns the list of preference keys which define the tokens
46 * used in the rules of this scanner.
48 abstract protected String[] getTokenProperties();
51 * Creates the list of rules controlling this scanner.
53 abstract protected List createRules();
57 * Creates an abstract Java scanner.
59 public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
61 fColorManager= manager;
62 fPreferenceStore= store;
66 * Must be called after the constructor has been called.
68 public final void initialize() {
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]);
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);
89 boolean bold= fPreferenceStore.getBoolean(styleKey);
90 fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager.getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
93 protected Token getToken(String key) {
94 return (Token) fTokenMap.get(key);
97 private void initializeRules() {
98 List rules= createRules();
100 IRule[] result= new IRule[rules.size()];
101 rules.toArray(result);
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]))
117 public boolean affectsBehavior(PropertyChangeEvent event) {
118 return indexOf(event.getProperty()) >= 0;
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);
128 adaptToStyleChange(token, event);
131 private void adaptToColorChange(Token token, PropertyChangeEvent event) {
134 Object value= event.getNewValue();
135 if (value instanceof RGB)
137 else if (value instanceof String)
138 rgb= StringConverter.asRGB((String) value);
142 String property= event.getProperty();
144 if (fColorManager instanceof IColorManagerExtension) {
145 IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
146 ext.unbindColor(property);
147 ext.bindColor(property, rgb);
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()));
158 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
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))
167 else if (IPreferenceStore.FALSE.equals(s))
171 Object data= token.getData();
172 if (data instanceof TextAttribute) {
173 TextAttribute oldAttr= (TextAttribute) data;
174 boolean isBold= (oldAttr.getStyle() == SWT.BOLD);
176 token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));