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;
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.
32 public abstract class AbstractJavaScanner extends BufferedRuleBasedScanner {
35 private IColorManager fColorManager;
36 private IPreferenceStore fPreferenceStore;
38 private Map fTokenMap= new HashMap();
39 private String[] fPropertyNamesColor;
40 private String[] fPropertyNamesStyle;
44 * Returns the list of preference keys which define the tokens
45 * used in the rules of this scanner.
47 abstract protected String[] getTokenProperties();
50 * Creates the list of rules controlling this scanner.
52 abstract protected List createRules();
56 * Creates an abstract Java scanner.
58 public AbstractJavaScanner(IColorManager manager, IPreferenceStore store) {
60 fColorManager= manager;
61 fPreferenceStore= store;
65 * Must be called after the constructor has been called.
67 public final void initialize() {
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]);
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);
88 boolean bold= fPreferenceStore.getBoolean(styleKey);
89 fTokenMap.put(colorKey, new Token(new TextAttribute(fColorManager.getColor(colorKey), null, bold ? SWT.BOLD : SWT.NORMAL)));
92 protected Token getToken(String key) {
93 return (Token) fTokenMap.get(key);
96 private void initializeRules() {
97 List rules= createRules();
99 IRule[] result= new IRule[rules.size()];
100 rules.toArray(result);
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]))
116 public boolean affectsBehavior(PropertyChangeEvent event) {
117 return indexOf(event.getProperty()) >= 0;
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);
127 adaptToStyleChange(token, event);
130 private void adaptToColorChange(Token token, PropertyChangeEvent event) {
133 Object value= event.getNewValue();
134 if (value instanceof RGB)
136 else if (value instanceof String)
137 rgb= StringConverter.asRGB((String) value);
141 String property= event.getProperty();
143 if (fColorManager instanceof IColorManagerExtension) {
144 IColorManagerExtension ext= (IColorManagerExtension) fColorManager;
145 ext.unbindColor(property);
146 ext.bindColor(property, rgb);
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()));
157 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
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))
166 else if (IPreferenceStore.FALSE.equals(s))
170 Object data= token.getData();
171 if (data instanceof TextAttribute) {
172 TextAttribute oldAttr= (TextAttribute) data;
173 boolean isBold= (oldAttr.getStyle() == SWT.BOLD);
175 token.setData(new TextAttribute(oldAttr.getForeground(), oldAttr.getBackground(), bold ? SWT.BOLD : SWT.NORMAL));