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
9 * Igor Malinin - initial contribution
11 * $Id: AbstractTextTools.java,v 1.3 2006-10-21 23:13:54 pombredanne Exp $
14 package net.sourceforge.phpeclipse.ui.text;
16 import java.util.HashMap;
19 import net.sourceforge.phpeclipse.ui.ColorManager;
20 import net.sourceforge.phpeclipse.ui.preferences.ITextStylePreferences;
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;
32 * @author Igor Malinin
34 public class AbstractTextTools {
36 /** The preference store */
37 protected IPreferenceStore store;
39 /** The color manager */
40 protected ColorManager colorManager;
44 private String[] properties;
46 private String[] foregroundPropertyNames;
48 private String[] backgroundPropertyNames;
50 private String[] stylePropertyNames;
52 private IPropertyChangeListener listener;
55 * Creates a new text tools collection.
57 public AbstractTextTools(IPreferenceStore store, String[] properties) {
58 this(store, properties, new ColorManager());
62 * Creates a new text tools collection.
64 public AbstractTextTools(IPreferenceStore store, String[] properties,
65 ColorManager manager) {
67 this.properties = properties;
69 colorManager = manager;
71 tokens = new HashMap();
73 int length = properties.length;
75 foregroundPropertyNames = new String[length];
76 backgroundPropertyNames = new String[length];
77 stylePropertyNames = new String[length];
79 for (int i = 0; i < length; i++) {
80 String property = properties[i];
82 String foreground = property
83 + ITextStylePreferences.SUFFIX_FOREGROUND;
84 String background = property
85 + ITextStylePreferences.SUFFIX_BACKGROUND;
86 String style = property + ITextStylePreferences.SUFFIX_STYLE;
88 foregroundPropertyNames[i] = foreground;
89 backgroundPropertyNames[i] = background;
90 stylePropertyNames[i] = style;
94 rgb = getColor(store, foreground);
96 colorManager.bindColor(foreground, rgb);
99 rgb = getColor(store, background);
101 colorManager.bindColor(background, rgb);
104 tokens.put(property, new Token(new TextAttribute(colorManager
105 .getColor(foreground), colorManager.getColor(background),
106 getStyle(store, style))));
109 listener = new IPropertyChangeListener() {
111 public void propertyChange(PropertyChangeEvent event) {
112 adaptToPreferenceChange(event);
116 store.addPropertyChangeListener(listener);
120 * Disposes all the individual tools of this tools collection.
122 public void dispose() {
124 store.removePropertyChangeListener(listener);
130 if (colorManager != null) {
131 colorManager.dispose();
139 foregroundPropertyNames = null;
140 backgroundPropertyNames = null;
141 stylePropertyNames = null;
145 * Returns the color manager which is used to manage any XML-specific colors
146 * needed for such things like syntax highlighting.
148 * @return the color manager to be used for XML text viewers
150 // public ColorManager getColorManager() {
151 // return colorManager;
154 public Map getTokens() {
158 protected Token getToken(String key) {
159 int index = indexOf(key);
164 return (Token) tokens.get(properties[index]);
168 * Determines whether the preference change encoded by the given event
169 * changes the behavior of one its contained components.
172 * the event to be investigated
173 * @return <code>true</code> if event causes a behavioral change
175 public boolean affectsBehavior(PropertyChangeEvent event) {
176 return (indexOf(event.getProperty()) >= 0);
180 * Adapts the behavior of the contained components to the change encoded in
184 * the event to whch to adapt
186 public void adaptToPreferenceChange(PropertyChangeEvent event) {
187 String property = event.getProperty();
189 Token token = getToken(property);
191 if (property.endsWith(ITextStylePreferences.SUFFIX_FOREGROUND)
193 .endsWith(ITextStylePreferences.SUFFIX_BACKGROUND)) {
194 adaptToColorChange(token, event);
195 } else if (property.endsWith(ITextStylePreferences.SUFFIX_STYLE)) {
196 adaptToStyleChange(token, event);
201 private void adaptToColorChange(Token token, PropertyChangeEvent event) {
202 RGB rgb = getColor(event.getNewValue());
204 String property = event.getProperty();
206 colorManager.unbindColor(property);
208 colorManager.bindColor(property, rgb);
211 Object data = token.getData();
212 if (data instanceof TextAttribute) {
213 TextAttribute old = (TextAttribute) data;
215 int i = indexOf(property);
217 token.setData(new TextAttribute(colorManager
218 .getColor(foregroundPropertyNames[i]), colorManager
219 .getColor(backgroundPropertyNames[i]), old.getStyle()));
223 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
224 int style = getStyle((String) event.getNewValue());
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));
236 private int indexOf(String property) {
237 if (property != null) {
238 int length = properties.length;
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])) {
253 private RGB getColor(IPreferenceStore store, String key) {
254 return getColor(store.getString(key));
257 private RGB getColor(Object value) {
258 if (value instanceof RGB) {
262 String str = (String) value;
263 if (str.length() > 0) {
264 return StringConverter.asRGB(str);
270 private int getStyle(IPreferenceStore store, String key) {
271 return getStyle(store.getString(key));
274 private int getStyle(String value) {
275 if (value.indexOf(ITextStylePreferences.STYLE_BOLD) >= 0) {