KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > text > AbstractAntEditorScanner


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.editor.text;
13
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.ColorManager;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.resource.StringConverter;
18 import org.eclipse.jface.text.TextAttribute;
19 import org.eclipse.jface.text.rules.RuleBasedScanner;
20 import org.eclipse.jface.text.rules.Token;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.graphics.Color;
24 import org.eclipse.swt.graphics.RGB;
25
26 public abstract class AbstractAntEditorScanner extends RuleBasedScanner {
27
28     protected void adaptToColorChange(PropertyChangeEvent event, Token token) {
29         RGB rgb= null;
30         
31         Object JavaDoc value= event.getNewValue();
32         if (value instanceof RGB) {
33             rgb= (RGB) value;
34         } else if (value instanceof String JavaDoc) {
35             rgb= StringConverter.asRGB((String JavaDoc) value);
36         }
37             
38         if (rgb != null) {
39             TextAttribute attr= (TextAttribute) token.getData();
40             token.setData(new TextAttribute(ColorManager.getDefault().getColor(rgb), attr.getBackground(), attr.getStyle()));
41         }
42     }
43
44     protected void adaptToStyleChange(PropertyChangeEvent event, Token token, int styleAttribute) {
45         if (token == null) {
46             return;
47         }
48         boolean eventValue= false;
49         Object JavaDoc value= event.getNewValue();
50         if (value instanceof Boolean JavaDoc) {
51             eventValue= ((Boolean JavaDoc) value).booleanValue();
52         } else if (IPreferenceStore.TRUE.equals(value)) {
53             eventValue= true;
54         }
55         
56         TextAttribute attr= (TextAttribute) token.getData();
57         boolean activeValue= (attr.getStyle() & styleAttribute) == styleAttribute;
58         if (activeValue != eventValue) {
59             token.setData(new TextAttribute(attr.getForeground(), attr.getBackground(), eventValue ? attr.getStyle() | styleAttribute : attr.getStyle() & ~styleAttribute));
60         }
61     }
62
63     protected TextAttribute createTextAttribute(String JavaDoc colorID, String JavaDoc boldKey, String JavaDoc italicKey) {
64         Color color= null;
65         if (colorID != null) {
66             color= AntUIPlugin.getPreferenceColor(colorID);
67         }
68         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
69         int style= store.getBoolean(boldKey) ? SWT.BOLD : SWT.NORMAL;
70         if (store.getBoolean(italicKey)) {
71             style |= SWT.ITALIC;
72         }
73         
74         return new TextAttribute(color, null, style);
75     }
76 }
77
Popular Tags