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.2 2004-09-22 18:51:51 jsurfer 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());
61 * Creates a new text tools collection.
63 public AbstractTextTools(IPreferenceStore store, String[] properties, ColorManager manager) {
65 this.properties = properties;
67 colorManager = manager;
69 tokens = new HashMap();
71 int length = properties.length;
73 foregroundPropertyNames = new String[length];
74 backgroundPropertyNames = new String[length];
75 stylePropertyNames = new String[length];
77 for (int i = 0; i < length; i++) {
78 String property = properties[i];
80 String foreground = property
81 + ITextStylePreferences.SUFFIX_FOREGROUND;
82 String background = property
83 + ITextStylePreferences.SUFFIX_BACKGROUND;
84 String style = property + ITextStylePreferences.SUFFIX_STYLE;
86 foregroundPropertyNames[i] = foreground;
87 backgroundPropertyNames[i] = background;
88 stylePropertyNames[i] = style;
92 rgb = getColor(store, foreground);
94 colorManager.bindColor(foreground, rgb);
97 rgb = getColor(store, background);
99 colorManager.bindColor(background, rgb);
102 tokens.put(property, new Token(new TextAttribute(colorManager
103 .getColor(foreground), colorManager.getColor(background),
104 getStyle(store, style))));
107 listener = new IPropertyChangeListener() {
109 public void propertyChange(PropertyChangeEvent event) {
110 adaptToPreferenceChange(event);
114 store.addPropertyChangeListener(listener);
118 * Disposes all the individual tools of this tools collection.
120 public void dispose() {
122 store.removePropertyChangeListener(listener);
128 if (colorManager != null) {
129 colorManager.dispose();
137 foregroundPropertyNames = null;
138 backgroundPropertyNames = null;
139 stylePropertyNames = null;
143 * Returns the color manager which is used to manage any XML-specific
144 * colors needed for such things like syntax highlighting.
146 * @return the color manager to be used for XML text viewers
148 public ColorManager getColorManager() {
152 public Map getTokens() {
156 protected Token getToken(String key) {
157 int index = indexOf(key);
162 return (Token) tokens.get(properties[index]);
166 * Determines whether the preference change encoded by the given event
167 * changes the behavior of one its contained components.
170 * the event to be investigated
171 * @return <code>true</code> if event causes a behavioral change
173 public boolean affectsBehavior(PropertyChangeEvent event) {
174 return (indexOf(event.getProperty()) >= 0);
178 * Adapts the behavior of the contained components to the change encoded in
182 * the event to whch to adapt
184 public void adaptToPreferenceChange(PropertyChangeEvent event) {
185 String property = event.getProperty();
187 Token token = getToken(property);
189 if (property.endsWith(ITextStylePreferences.SUFFIX_FOREGROUND)
191 .endsWith(ITextStylePreferences.SUFFIX_BACKGROUND)) {
192 adaptToColorChange(token, event);
193 } else if (property.endsWith(ITextStylePreferences.SUFFIX_STYLE)) {
194 adaptToStyleChange(token, event);
199 private void adaptToColorChange(Token token, PropertyChangeEvent event) {
200 RGB rgb = getColor(event.getNewValue());
202 String property = event.getProperty();
204 colorManager.unbindColor(property);
206 colorManager.bindColor(property, rgb);
209 Object data = token.getData();
210 if (data instanceof TextAttribute) {
211 TextAttribute old = (TextAttribute) data;
213 int i = indexOf(property);
215 token.setData(new TextAttribute(colorManager
216 .getColor(foregroundPropertyNames[i]), colorManager
217 .getColor(backgroundPropertyNames[i]), old.getStyle()));
221 private void adaptToStyleChange(Token token, PropertyChangeEvent event) {
222 int style = getStyle((String) event.getNewValue());
224 Object data = token.getData();
225 if (data instanceof TextAttribute) {
226 TextAttribute old = (TextAttribute) data;
227 if (old.getStyle() != style) {
228 token.setData(new TextAttribute(old.getForeground(), old
229 .getBackground(), style));
234 private int indexOf(String property) {
235 if (property != null) {
236 int length = properties.length;
238 for (int i = 0; i < length; i++) {
239 if (property.equals(properties[i])
240 || property.equals(foregroundPropertyNames[i])
241 || property.equals(backgroundPropertyNames[i])
242 || property.equals(stylePropertyNames[i])) {
251 private RGB getColor(IPreferenceStore store, String key) {
252 return getColor(store.getString(key));
255 private RGB getColor(Object value) {
256 if (value instanceof RGB) {
260 String str = (String) value;
261 if (str.length() > 0) {
262 return StringConverter.asRGB(str);
268 private int getStyle(IPreferenceStore store, String key) {
269 return getStyle(store.getString(key));
272 private int getStyle(String value) {
273 if (value.indexOf(ITextStylePreferences.STYLE_BOLD) >= 0) {