KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > AbstractAntSourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation 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://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.editor;
13
14 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant;
15 import org.eclipse.ant.internal.ui.editor.text.AntEditorPartitionScanner;
16 import org.eclipse.ant.internal.ui.editor.text.AntEditorProcInstrScanner;
17 import org.eclipse.ant.internal.ui.editor.text.AntEditorTagScanner;
18 import org.eclipse.ant.internal.ui.editor.text.IAntEditorColorConstants;
19 import org.eclipse.ant.internal.ui.editor.text.MultilineDamagerRepairer;
20 import org.eclipse.ant.internal.ui.model.AntUIPlugin;
21 import org.eclipse.ant.internal.ui.model.ColorManager;
22 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.resource.StringConverter;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.TextAttribute;
27 import org.eclipse.jface.text.presentation.IPresentationReconciler;
28 import org.eclipse.jface.text.presentation.PresentationReconciler;
29 import org.eclipse.jface.text.source.ISourceViewer;
30 import org.eclipse.jface.text.source.SourceViewerConfiguration;
31 import org.eclipse.jface.util.PropertyChangeEvent;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.graphics.RGB;
34 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
35
36 public abstract class AbstractAntSourceViewerConfiguration extends SourceViewerConfiguration {
37
38     private AntEditorTagScanner tagScanner;
39     private AntEditorProcInstrScanner instructionScanner;
40     private MultilineDamagerRepairer damageRepairer;
41     private TextAttribute xmlCommentAttribute;
42     
43     private AntEditorProcInstrScanner getDefaultScanner() {
44         if (instructionScanner == null) {
45             instructionScanner = new AntEditorProcInstrScanner();
46         }
47         return instructionScanner;
48     }
49
50     private AntEditorTagScanner getTagScanner() {
51         if (tagScanner == null) {
52             tagScanner = new AntEditorTagScanner();
53         }
54         return tagScanner;
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
59      */

60     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
61         PresentationReconciler reconciler = new PresentationReconciler();
62         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
63         
64         MultilineDamagerRepairer dr = new MultilineDamagerRepairer(getDefaultScanner(), null);
65         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
66         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
67     
68         dr = new MultilineDamagerRepairer(getTagScanner(), null);
69         reconciler.setDamager(dr, AntEditorPartitionScanner.XML_TAG);
70         reconciler.setRepairer(dr, AntEditorPartitionScanner.XML_TAG);
71     
72         IPreferenceStore store= AntUIPlugin.getDefault().getPreferenceStore();
73         int style= SWT.NORMAL;
74         if (store.getBoolean(IAntEditorColorConstants.XML_COMMENT_COLOR + AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) {
75             style |= SWT.BOLD;
76         }
77         if (store.getBoolean(IAntEditorColorConstants.XML_COMMENT_COLOR + AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) {
78             style |= SWT.ITALIC;
79         }
80         
81         xmlCommentAttribute= new TextAttribute(AntUIPlugin.getPreferenceColor(IAntEditorColorConstants.XML_COMMENT_COLOR), null, style);
82         damageRepairer= new MultilineDamagerRepairer(null, xmlCommentAttribute);
83         reconciler.setDamager(damageRepairer, AntEditorPartitionScanner.XML_COMMENT);
84         reconciler.setRepairer(damageRepairer, AntEditorPartitionScanner.XML_COMMENT);
85     
86         return reconciler;
87     }
88
89     /**
90      * Preference colors have changed.
91      * Update the default tokens of the scanners.
92      */

93     public void adaptToPreferenceChange(PropertyChangeEvent event) {
94         if (tagScanner == null) {
95             return; //property change before the editor is fully created
96
}
97         tagScanner.adaptToPreferenceChange(event);
98         instructionScanner.adaptToPreferenceChange(event);
99         String JavaDoc property= event.getProperty();
100         if (property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR)) {
101             if (property.endsWith(AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) {
102                 adaptToStyleChange(event, SWT.BOLD);
103             } else if (property.endsWith(AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) {
104                 adaptToStyleChange(event, SWT.ITALIC);
105             } else {
106                 adaptToColorChange(event);
107             }
108             damageRepairer.setDefaultTextAttribute(xmlCommentAttribute);
109         }
110     }
111     
112     private void adaptToStyleChange(PropertyChangeEvent event, int styleAttribute) {
113         boolean eventValue= false;
114         Object JavaDoc value= event.getNewValue();
115         if (value instanceof Boolean JavaDoc) {
116             eventValue= ((Boolean JavaDoc) value).booleanValue();
117         } else if (IPreferenceStore.TRUE.equals(value)) {
118             eventValue= true;
119         }
120         
121         boolean activeValue= (xmlCommentAttribute.getStyle() & styleAttribute) == styleAttribute;
122         if (activeValue != eventValue) {
123             xmlCommentAttribute= new TextAttribute(xmlCommentAttribute.getForeground(), xmlCommentAttribute.getBackground(), eventValue ? xmlCommentAttribute.getStyle() | styleAttribute : xmlCommentAttribute.getStyle() & ~styleAttribute);
124         }
125     }
126     
127      /**
128      * Update the text attributes associated with the tokens of this scanner as a color preference has been changed.
129      */

130     private void adaptToColorChange(PropertyChangeEvent event) {
131         RGB rgb= null;
132         
133         Object JavaDoc value= event.getNewValue();
134         if (value instanceof RGB) {
135             rgb= (RGB) value;
136         } else if (value instanceof String JavaDoc) {
137             rgb= StringConverter.asRGB((String JavaDoc) value);
138         }
139             
140         if (rgb != null) {
141             xmlCommentAttribute= new TextAttribute(ColorManager.getDefault().getColor(rgb), xmlCommentAttribute.getBackground(), xmlCommentAttribute.getStyle());
142         }
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(org.eclipse.jface.text.source.ISourceViewer)
147      */

148     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
149         return new String JavaDoc[] {
150             IDocument.DEFAULT_CONTENT_TYPE,
151             AntEditorPartitionScanner.XML_COMMENT,
152             AntEditorPartitionScanner.XML_TAG,
153             AntEditorPartitionScanner.XML_CDATA};
154     }
155
156     /* (non-Javadoc)
157      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTabWidth(org.eclipse.jface.text.source.ISourceViewer)
158      */

159     public int getTabWidth(ISourceViewer sourceViewer) {
160         return AntUIPlugin.getDefault().getPreferenceStore().getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
161     }
162     
163     public boolean affectsTextPresentation(PropertyChangeEvent event) {
164         String JavaDoc property= event.getProperty();
165         return property.startsWith(IAntEditorColorConstants.TEXT_COLOR) ||
166             property.startsWith(IAntEditorColorConstants.PROCESSING_INSTRUCTIONS_COLOR) ||
167             property.startsWith(IAntEditorColorConstants.STRING_COLOR) ||
168             property.startsWith(IAntEditorColorConstants.TAG_COLOR) ||
169             property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR);
170     }
171     
172     /* (non-Javadoc)
173      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
174      */

175     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
176         return AntDocumentSetupParticipant.ANT_PARTITIONING;
177     }
178 }
Popular Tags