KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > AntSourceViewerConfiguration


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 API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui;
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.preferences.AntEditorPreferenceConstants;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.resource.StringConverter;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextDoubleClickStrategy;
25 import org.eclipse.jface.text.TextAttribute;
26 import org.eclipse.jface.text.presentation.IPresentationReconciler;
27 import org.eclipse.jface.text.presentation.PresentationReconciler;
28 import org.eclipse.jface.text.source.ISourceViewer;
29 import org.eclipse.jface.util.PropertyChangeEvent;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
33 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
34
35 public class AntSourceViewerConfiguration extends TextSourceViewerConfiguration {
36
37     private AntEditorTagScanner tagScanner;
38     private AntEditorProcInstrScanner instructionScanner;
39     private MultilineDamagerRepairer damageRepairer;
40     private MultilineDamagerRepairer dtdDamageRepairer;
41     private TextAttribute xmlCommentAttribute;
42     private TextAttribute xmlDtdAttribute;
43     
44     public AntSourceViewerConfiguration() {
45         super(AntUIPlugin.getDefault().getCombinedPreferenceStore());
46     }
47
48     private AntEditorProcInstrScanner getDefaultScanner() {
49         if (instructionScanner == null) {
50             instructionScanner = new AntEditorProcInstrScanner();
51         }
52         return instructionScanner;
53     }
54
55     private AntEditorTagScanner getTagScanner() {
56         if (tagScanner == null) {
57             tagScanner = new AntEditorTagScanner();
58         }
59         return tagScanner;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
64      */

65     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
66         PresentationReconciler reconciler = new PresentationReconciler();
67         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
68         
69         MultilineDamagerRepairer dr = new MultilineDamagerRepairer(getDefaultScanner());
70         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
71         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
72     
73         dr = new MultilineDamagerRepairer(getTagScanner());
74         reconciler.setDamager(dr, AntEditorPartitionScanner.XML_TAG);
75         reconciler.setRepairer(dr, AntEditorPartitionScanner.XML_TAG);
76     
77         int style= getStyle(IAntEditorColorConstants.XML_COMMENT_COLOR);
78         xmlCommentAttribute= new TextAttribute(AntUIPlugin.getPreferenceColor(IAntEditorColorConstants.XML_COMMENT_COLOR), null, style);
79         damageRepairer= new MultilineDamagerRepairer(null, xmlCommentAttribute);
80         reconciler.setDamager(damageRepairer, AntEditorPartitionScanner.XML_COMMENT);
81         reconciler.setRepairer(damageRepairer, AntEditorPartitionScanner.XML_COMMENT);
82         
83         style= getStyle(IAntEditorColorConstants.XML_DTD_COLOR);
84         xmlDtdAttribute= new TextAttribute(AntUIPlugin.getPreferenceColor(IAntEditorColorConstants.XML_DTD_COLOR), null, style);
85         dtdDamageRepairer= new MultilineDamagerRepairer(null, xmlDtdAttribute);
86         reconciler.setDamager(dtdDamageRepairer, AntEditorPartitionScanner.XML_DTD);
87         reconciler.setRepairer(dtdDamageRepairer, AntEditorPartitionScanner.XML_DTD);
88     
89         return reconciler;
90     }
91
92     private int getStyle(String JavaDoc pref) {
93         int style= SWT.NORMAL;
94         if (fPreferenceStore.getBoolean(pref + AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) {
95             style |= SWT.BOLD;
96         }
97         if (fPreferenceStore.getBoolean(pref + AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) {
98             style |= SWT.ITALIC;
99         }
100         return style;
101     }
102
103     /**
104      * Preference colors have changed.
105      * Update the default tokens of the scanners.
106      */

107     public void adaptToPreferenceChange(PropertyChangeEvent event) {
108         if (tagScanner == null) {
109             return; //property change before the editor is fully created
110
}
111         tagScanner.adaptToPreferenceChange(event);
112         instructionScanner.adaptToPreferenceChange(event);
113         String JavaDoc property= event.getProperty();
114         if (property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR)) {
115             xmlCommentAttribute= adaptTextAttribute(event, property, xmlCommentAttribute, damageRepairer);
116         } else if (property.startsWith(IAntEditorColorConstants.XML_DTD_COLOR)) {
117             xmlDtdAttribute= adaptTextAttribute(event, property, xmlDtdAttribute, dtdDamageRepairer);
118         }
119     }
120     
121     private TextAttribute adaptTextAttribute(PropertyChangeEvent event, String JavaDoc property, TextAttribute textAttribute, MultilineDamagerRepairer repairer) {
122         if (property.endsWith(AntEditorPreferenceConstants.EDITOR_BOLD_SUFFIX)) {
123             textAttribute= adaptToStyleChange(event, SWT.BOLD, textAttribute);
124         } else if (property.endsWith(AntEditorPreferenceConstants.EDITOR_ITALIC_SUFFIX)) {
125             textAttribute= adaptToStyleChange(event, SWT.ITALIC, textAttribute);
126         } else {
127             textAttribute= adaptToColorChange(event, textAttribute);
128         }
129         repairer.setDefaultTextAttribute(textAttribute);
130         return textAttribute;
131     }
132
133     private TextAttribute adaptToStyleChange(PropertyChangeEvent event, int styleAttribute, TextAttribute textAttribute) {
134         boolean eventValue= false;
135         Object JavaDoc value= event.getNewValue();
136         if (value instanceof Boolean JavaDoc) {
137             eventValue= ((Boolean JavaDoc) value).booleanValue();
138         } else if (IPreferenceStore.TRUE.equals(value)) {
139             eventValue= true;
140         }
141         
142         boolean activeValue= (textAttribute.getStyle() & styleAttribute) == styleAttribute;
143         if (activeValue != eventValue) {
144             textAttribute= new TextAttribute(textAttribute.getForeground(), textAttribute.getBackground(), eventValue ? textAttribute.getStyle() | styleAttribute : textAttribute.getStyle() & ~styleAttribute);
145         }
146         return textAttribute;
147     }
148     
149      /**
150      * Update the text attributes associated with the tokens of this scanner as a color preference has been changed.
151      */

152     private TextAttribute adaptToColorChange(PropertyChangeEvent event, TextAttribute textAttribute) {
153         RGB rgb= null;
154         
155         Object JavaDoc value= event.getNewValue();
156         if (value instanceof RGB) {
157             rgb= (RGB) value;
158         } else if (value instanceof String JavaDoc) {
159             rgb= StringConverter.asRGB((String JavaDoc) value);
160         }
161             
162         if (rgb != null) {
163             textAttribute= new TextAttribute(ColorManager.getDefault().getColor(rgb), textAttribute.getBackground(), textAttribute.getStyle());
164         }
165         return textAttribute;
166     }
167     
168     /* (non-Javadoc)
169      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(org.eclipse.jface.text.source.ISourceViewer)
170      */

171     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
172         return new String JavaDoc[] {
173             IDocument.DEFAULT_CONTENT_TYPE,
174             AntEditorPartitionScanner.XML_COMMENT,
175             AntEditorPartitionScanner.XML_TAG,
176             AntEditorPartitionScanner.XML_CDATA,
177             AntEditorPartitionScanner.XML_DTD};
178     }
179
180     /* (non-Javadoc)
181      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTabWidth(org.eclipse.jface.text.source.ISourceViewer)
182      */

183     public int getTabWidth(ISourceViewer sourceViewer) {
184         return fPreferenceStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH);
185     }
186     
187     public boolean affectsTextPresentation(PropertyChangeEvent event) {
188         String JavaDoc property= event.getProperty();
189         return property.startsWith(IAntEditorColorConstants.TEXT_COLOR) ||
190             property.startsWith(IAntEditorColorConstants.PROCESSING_INSTRUCTIONS_COLOR) ||
191             property.startsWith(IAntEditorColorConstants.STRING_COLOR) ||
192             property.startsWith(IAntEditorColorConstants.TAG_COLOR) ||
193             property.startsWith(IAntEditorColorConstants.XML_COMMENT_COLOR) ||
194             property.startsWith(IAntEditorColorConstants.XML_DTD_COLOR);
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
199      */

200     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
201         return AntDocumentSetupParticipant.ANT_PARTITIONING;
202     }
203
204     /* (non-Javadoc)
205      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getDoubleClickStrategy(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
206      */

207     public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String JavaDoc contentType) {
208         if (AntEditorPartitionScanner.XML_TAG.equals(contentType)) {
209             return new AntDoubleClickStrategy();
210         }
211         return super.getDoubleClickStrategy(sourceViewer, contentType);
212     }
213 }
214
Popular Tags