KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > BasePDEScanner


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.text;
12
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.jface.text.TextAttribute;
15 import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
16 import org.eclipse.jface.text.rules.Token;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.pde.internal.ui.PDEPlugin;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21
22 public abstract class BasePDEScanner extends BufferedRuleBasedScanner {
23     
24     private IColorManager fColorManager;
25     
26     protected BasePDEScanner() {
27     }
28     
29     public void setColorManager(IColorManager manager) {
30         fColorManager = manager;
31     }
32
33     public BasePDEScanner(IColorManager manager) {
34         fColorManager = manager;
35         initialize();
36     }
37     
38     public void adaptToPreferenceChange(PropertyChangeEvent event) {
39         String JavaDoc property= event.getProperty();
40         if (affectsTextPresentation(property)) {
41             Token token = getTokenAffected(event);
42             if (property.endsWith(IPDEColorConstants.P_BOLD_SUFFIX))
43                 adaptToStyleChange(event, token, SWT.BOLD);
44             else if (property.endsWith(IPDEColorConstants.P_ITALIC_SUFFIX))
45                 adaptToStyleChange(event, token, SWT.ITALIC);
46             else
47                 adaptToColorChange(event, token);
48         }
49     }
50     
51     public abstract boolean affectsTextPresentation(String JavaDoc property);
52     
53     protected abstract Token getTokenAffected(PropertyChangeEvent event);
54     
55     protected abstract void initialize();
56
57     protected void adaptToStyleChange(PropertyChangeEvent event, Token token, int styleAttribute) {
58         if (token == null)
59             return;
60         
61         boolean eventValue = false;
62         Object JavaDoc value = event.getNewValue();
63         if (value instanceof Boolean JavaDoc)
64             eventValue = ((Boolean JavaDoc)value).booleanValue();
65         
66         TextAttribute attr = (TextAttribute) token.getData();
67         boolean activeValue = (attr.getStyle() & styleAttribute) == styleAttribute;
68         if (activeValue != eventValue) {
69             Color foreground = attr.getForeground();
70             Color background = attr.getBackground();
71             int style = eventValue ? attr.getStyle() | styleAttribute : attr.getStyle() & ~styleAttribute;
72             token.setData(new TextAttribute(foreground, background, style));
73         }
74     }
75     
76     protected void adaptToColorChange(PropertyChangeEvent event, Token token) {
77         TextAttribute attr= (TextAttribute) token.getData();
78         token.setData(new TextAttribute(fColorManager.getColor(event.getProperty()), attr.getBackground(), attr.getStyle()));
79     }
80     
81     protected TextAttribute createTextAttribute(String JavaDoc property) {
82         return createTextAttribute(fColorManager, property);
83     }
84
85     protected static TextAttribute createTextAttribute(IColorManager manager, String JavaDoc property) {
86         Color color = manager.getColor(property);
87         int style = SWT.NORMAL;
88         IPreferenceStore store = PDEPlugin.getDefault().getPreferenceStore();
89         if (store.getBoolean(property + IPDEColorConstants.P_BOLD_SUFFIX))
90             style |= SWT.BOLD;
91         if (store.getBoolean(property + IPDEColorConstants.P_ITALIC_SUFFIX))
92             style |= SWT.ITALIC;
93         return new TextAttribute(color, null, style);
94     }
95
96 }
97
Popular Tags