Refactory: remove dependance to org.eclipse.debug.ui from net.sourceforge.phpeclipse.ui
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / text / AbstractTextTools.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://solareclipse.sourceforge.net/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Igor Malinin - initial contribution
10  * 
11  * $Id: AbstractTextTools.java,v 1.3 2006-10-21 23:13:54 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui.text;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import net.sourceforge.phpeclipse.ui.ColorManager;
20 import net.sourceforge.phpeclipse.ui.preferences.ITextStylePreferences;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.resource.StringConverter;
24 import org.eclipse.jface.text.TextAttribute;
25 import org.eclipse.jface.text.rules.Token;
26 import org.eclipse.jface.util.IPropertyChangeListener;
27 import org.eclipse.jface.util.PropertyChangeEvent;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.RGB;
30
31 /**
32  * @author Igor Malinin
33  */
34 public class AbstractTextTools {
35
36         /** The preference store */
37         protected IPreferenceStore store;
38
39         /** The color manager */
40         protected ColorManager colorManager;
41
42         private Map tokens;
43
44         private String[] properties;
45
46         private String[] foregroundPropertyNames;
47
48         private String[] backgroundPropertyNames;
49
50         private String[] stylePropertyNames;
51
52         private IPropertyChangeListener listener;
53
54         /**
55          * Creates a new text tools collection.
56          */
57         public AbstractTextTools(IPreferenceStore store, String[] properties) {
58                 this(store, properties, new ColorManager());
59         }
60
61         /**
62          * Creates a new text tools collection.
63          */
64         public AbstractTextTools(IPreferenceStore store, String[] properties,
65                         ColorManager manager) {
66                 this.store = store;
67                 this.properties = properties;
68
69                 colorManager = manager;
70
71                 tokens = new HashMap();
72
73                 int length = properties.length;
74
75                 foregroundPropertyNames = new String[length];
76                 backgroundPropertyNames = new String[length];
77                 stylePropertyNames = new String[length];
78
79                 for (int i = 0; i < length; i++) {
80                         String property = properties[i];
81
82                         String foreground = property
83                                         + ITextStylePreferences.SUFFIX_FOREGROUND;
84                         String background = property
85                                         + ITextStylePreferences.SUFFIX_BACKGROUND;
86                         String style = property + ITextStylePreferences.SUFFIX_STYLE;
87
88                         foregroundPropertyNames[i] = foreground;
89                         backgroundPropertyNames[i] = background;
90                         stylePropertyNames[i] = style;
91
92                         RGB rgb;
93
94                         rgb = getColor(store, foreground);
95                         if (rgb != null) {
96                                 colorManager.bindColor(foreground, rgb);
97                         }
98
99                         rgb = getColor(store, background);
100                         if (rgb != null) {
101                                 colorManager.bindColor(background, rgb);
102                         }
103
104                         tokens.put(property, new Token(new TextAttribute(colorManager
105                                         .getColor(foreground), colorManager.getColor(background),
106                                         getStyle(store, style))));
107                 }
108
109                 listener = new IPropertyChangeListener() {
110
111                         public void propertyChange(PropertyChangeEvent event) {
112                                 adaptToPreferenceChange(event);
113                         }
114                 };
115
116                 store.addPropertyChangeListener(listener);
117         }
118
119         /**
120          * Disposes all the individual tools of this tools collection.
121          */
122         public void dispose() {
123                 if (store != null) {
124                         store.removePropertyChangeListener(listener);
125
126                         store = null;
127                         listener = null;
128                 }
129
130                 if (colorManager != null) {
131                         colorManager.dispose();
132
133                         colorManager = null;
134                 }
135
136                 tokens = null;
137
138                 properties = null;
139                 foregroundPropertyNames = null;
140                 backgroundPropertyNames = null;
141                 stylePropertyNames = null;
142         }
143
144         /**
145          * Returns the color manager which is used to manage any XML-specific colors
146          * needed for such things like syntax highlighting.
147          * 
148          * @return the color manager to be used for XML text viewers
149          */
150         public ColorManager getColorManager() {
151                 return colorManager;
152         }
153
154         public Map getTokens() {
155                 return tokens;
156         }
157
158         protected Token getToken(String key) {
159                 int index = indexOf(key);
160                 if (index < 0) {
161                         return null;
162                 }
163
164                 return (Token) tokens.get(properties[index]);
165         }
166
167         /**
168          * Determines whether the preference change encoded by the given event
169          * changes the behavior of one its contained components.
170          * 
171          * @param event
172          *            the event to be investigated
173          * @return <code>true</code> if event causes a behavioral change
174          */
175         public boolean affectsBehavior(PropertyChangeEvent event) {
176                 return (indexOf(event.getProperty()) >= 0);
177         }
178
179         /**
180          * Adapts the behavior of the contained components to the change encoded in
181          * the given event.
182          * 
183          * @param event
184          *            the event to whch to adapt
185          */
186         public void adaptToPreferenceChange(PropertyChangeEvent event) {
187                 String property = event.getProperty();
188
189                 Token token = getToken(property);
190                 if (token != null) {
191                         if (property.endsWith(ITextStylePreferences.SUFFIX_FOREGROUND)
192                                         || property
193                                                         .endsWith(ITextStylePreferences.SUFFIX_BACKGROUND)) {
194                                 adaptToColorChange(token, event);
195                         } else if (property.endsWith(ITextStylePreferences.SUFFIX_STYLE)) {
196                                 adaptToStyleChange(token, event);
197                         }
198                 }
199         }
200
201         private void adaptToColorChange(Token token, PropertyChangeEvent event) {
202                 RGB rgb = getColor(event.getNewValue());
203
204                 String property = event.getProperty();
205
206                 colorManager.unbindColor(property);
207                 if (rgb != null) {
208                         colorManager.bindColor(property, rgb);
209                 }
210
211                 Object data = token.getData();
212                 if (data instanceof TextAttribute) {
213                         TextAttribute old = (TextAttribute) data;
214
215                         int i = indexOf(property);
216
217                         token.setData(new TextAttribute(colorManager
218                                         .getColor(foregroundPropertyNames[i]), colorManager
219                                         .getColor(backgroundPropertyNames[i]), old.getStyle()));
220                 }
221         }
222
223         private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
224                 int style = getStyle((String) event.getNewValue());
225
226                 Object data = token.getData();
227                 if (data instanceof TextAttribute) {
228                         TextAttribute old = (TextAttribute) data;
229                         if (old.getStyle() != style) {
230                                 token.setData(new TextAttribute(old.getForeground(), old
231                                                 .getBackground(), style));
232                         }
233                 }
234         }
235
236         private int indexOf(String property) {
237                 if (property != null) {
238                         int length = properties.length;
239
240                         for (int i = 0; i < length; i++) {
241                                 if (property.equals(properties[i])
242                                                 || property.equals(foregroundPropertyNames[i])
243                                                 || property.equals(backgroundPropertyNames[i])
244                                                 || property.equals(stylePropertyNames[i])) {
245                                         return i;
246                                 }
247                         }
248                 }
249
250                 return -1;
251         }
252
253         private RGB getColor(IPreferenceStore store, String key) {
254                 return getColor(store.getString(key));
255         }
256
257         private RGB getColor(Object value) {
258                 if (value instanceof RGB) {
259                         return (RGB) value;
260                 }
261
262                 String str = (String) value;
263                 if (str.length() > 0) {
264                         return StringConverter.asRGB(str);
265                 }
266
267                 return null;
268         }
269
270         private int getStyle(IPreferenceStore store, String key) {
271                 return getStyle(store.getString(key));
272         }
273
274         private int getStyle(String value) {
275                 if (value.indexOf(ITextStylePreferences.STYLE_BOLD) >= 0) {
276                         return SWT.BOLD;
277                 }
278
279                 return SWT.NORMAL;
280         }
281 }