KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > BuildSourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.pde.internal.ui.editor.build;
13
14 import org.eclipse.jdt.ui.PreferenceConstants;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18 import org.eclipse.jface.text.IRegion;
19 import org.eclipse.jface.text.TextAttribute;
20 import org.eclipse.jface.text.presentation.IPresentationReconciler;
21 import org.eclipse.jface.text.presentation.PresentationReconciler;
22 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
23 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
24 import org.eclipse.jface.text.rules.IRule;
25 import org.eclipse.jface.text.rules.IWhitespaceDetector;
26 import org.eclipse.jface.text.rules.IWordDetector;
27 import org.eclipse.jface.text.rules.Token;
28 import org.eclipse.jface.text.rules.WhitespaceRule;
29 import org.eclipse.jface.text.rules.WordRule;
30 import org.eclipse.jface.text.source.ISourceViewer;
31 import org.eclipse.jface.util.PropertyChangeEvent;
32 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
33 import org.eclipse.pde.internal.ui.editor.text.ArgumentRule;
34 import org.eclipse.pde.internal.ui.editor.text.BasePDEScanner;
35 import org.eclipse.pde.internal.ui.editor.text.ChangeAwareSourceViewerConfiguration;
36 import org.eclipse.pde.internal.ui.editor.text.IColorManager;
37 import org.eclipse.pde.internal.ui.editor.text.PDEQuickAssistAssistant;
38 import org.eclipse.swt.SWT;
39 import org.eclipse.swt.graphics.Color;
40
41
42 public class BuildSourceViewerConfiguration extends ChangeAwareSourceViewerConfiguration {
43
44     protected static String JavaDoc PROPERTIES_FILE_PARTITIONING = "___pf_partitioning"; //$NON-NLS-1$
45
protected static String JavaDoc COMMENT = "__pf_comment"; //$NON-NLS-1$
46
protected static String JavaDoc PROPERTY_VALUE = "__pf_roperty_value"; //$NON-NLS-1$
47
protected static String JavaDoc[] PARTITIONS = new String JavaDoc[] { COMMENT, PROPERTY_VALUE };
48
49     private BasePDEScanner fPropertyKeyScanner;
50     private BasePDEScanner fCommentScanner;
51     private BasePDEScanner fPropertyValueScanner;
52     
53     private PDEQuickAssistAssistant fQuickAssistant;
54     
55     private abstract class AbstractJavaScanner extends BasePDEScanner {
56         
57         public void adaptToPreferenceChange(PropertyChangeEvent event) {
58             String JavaDoc property= event.getProperty();
59             if (affectsTextPresentation(property)) {
60                 Token token = getTokenAffected(event);
61                 if (property.endsWith(PreferenceConstants.EDITOR_BOLD_SUFFIX))
62                     adaptToStyleChange(event, token, SWT.BOLD);
63                 else if (property.endsWith(PreferenceConstants.EDITOR_ITALIC_SUFFIX))
64                     adaptToStyleChange(event, token, SWT.ITALIC);
65                 else if (property.endsWith(PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX))
66                     adaptToStyleChange(event, token, TextAttribute.STRIKETHROUGH);
67                 else if (property.endsWith(PreferenceConstants.EDITOR_UNDERLINE_SUFFIX))
68                     adaptToStyleChange(event, token, TextAttribute.UNDERLINE);
69                 else
70                     adaptToColorChange(event, token);
71             }
72         }
73         
74         protected TextAttribute createTextAttribute(String JavaDoc property) {
75             Color color = fColorManager.getColor(property);
76             int style = SWT.NORMAL;
77             if (fPreferenceStore.getBoolean(property + PreferenceConstants.EDITOR_BOLD_SUFFIX))
78                 style |= SWT.BOLD;
79             if (fPreferenceStore.getBoolean(property + PreferenceConstants.EDITOR_ITALIC_SUFFIX))
80                 style |= SWT.ITALIC;
81             if (fPreferenceStore.getBoolean(property + PreferenceConstants.EDITOR_STRIKETHROUGH_SUFFIX))
82                 style |= TextAttribute.STRIKETHROUGH;
83             if (fPreferenceStore.getBoolean(property + PreferenceConstants.EDITOR_UNDERLINE_SUFFIX))
84                 style |= TextAttribute.UNDERLINE;
85             return new TextAttribute(color, null, style);
86         }
87     }
88
89     private class SingleTokenJavaScanner extends AbstractJavaScanner {
90
91         private String JavaDoc fProperty;
92         public SingleTokenJavaScanner(String JavaDoc property) {
93             fProperty = property;
94             setColorManager(fColorManager);
95             initialize();
96         }
97
98         public boolean affectsTextPresentation(String JavaDoc property) {
99             return property.startsWith(fProperty);
100         }
101
102         protected Token getTokenAffected(PropertyChangeEvent event) {
103             return (Token)fDefaultReturnToken;
104         }
105
106         protected void initialize() {
107             setDefaultReturnToken(new Token(createTextAttribute(fProperty)));
108         }
109     }
110
111     public class PropertyValueScanner extends AbstractJavaScanner {
112
113         public class AssignmentDetector implements IWordDetector {
114
115             public boolean isWordStart(char c) {
116                 if ('=' != c && ':' != c || fDocument == null)
117                     return false;
118
119                 try {
120                     // check whether it is the first '='
121
IRegion lineInfo = fDocument.getLineInformationOfOffset(fOffset);
122                     int offset = lineInfo.getOffset();
123                     String JavaDoc line = fDocument.get(offset, lineInfo.getLength());
124                     int i = line.indexOf(c);
125                     return i != -1 && i + lineInfo.getOffset() + 1 == fOffset;
126                 } catch (BadLocationException ex) {
127                     return false;
128                 }
129             }
130
131             public boolean isWordPart(char c) {
132                 return false;
133             }
134         }
135
136         private Token fArgumentToken;
137         private Token fAssignmentToken;
138
139         public PropertyValueScanner() {
140             setColorManager(fColorManager);
141             initialize();
142         }
143         
144         public boolean affectsTextPresentation(String JavaDoc property) {
145             return property.startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)
146                     || property.startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT)
147                     || property.startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT);
148         }
149
150         protected Token getTokenAffected(PropertyChangeEvent event) {
151             String JavaDoc property = event.getProperty();
152             if (property.startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT))
153                 return fArgumentToken;
154             if (property.startsWith(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT))
155                 return fAssignmentToken;
156             return (Token)fDefaultReturnToken;
157         }
158
159         protected void initialize() {
160             IRule[] rules = new IRule[3];
161             fArgumentToken = new Token(createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT));
162             rules[0] = new ArgumentRule(fArgumentToken);
163             
164             fAssignmentToken = new Token(createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT));
165             rules[1] = new WordRule(new AssignmentDetector(), fAssignmentToken);
166             
167             rules[2] = new WhitespaceRule(new IWhitespaceDetector() {
168                 public boolean isWhitespace(char c) {
169                     return Character.isWhitespace(c);
170                 }
171             });
172             setRules(rules);
173             setDefaultReturnToken(new Token(createTextAttribute(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)));
174         }
175     }
176     
177     public BuildSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore store, PDESourcePage sourcePage) {
178         super(sourcePage, colorManager, store);
179         initializeScanners();
180     }
181
182
183     private void initializeScanners() {
184         fPropertyKeyScanner = new SingleTokenJavaScanner(PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
185         fPropertyValueScanner = new PropertyValueScanner();
186         fCommentScanner = new SingleTokenJavaScanner(PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
187     }
188
189     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
190         PresentationReconciler reconciler = new PresentationReconciler();
191         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
192
193         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(fPropertyKeyScanner);
194         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
195         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
196
197         dr = new DefaultDamagerRepairer(fCommentScanner);
198         reconciler.setDamager(dr, COMMENT);
199         reconciler.setRepairer(dr, COMMENT);
200
201         dr = new DefaultDamagerRepairer(fPropertyValueScanner);
202         reconciler.setDamager(dr, PROPERTY_VALUE);
203         reconciler.setRepairer(dr, PROPERTY_VALUE);
204
205         return reconciler;
206     }
207     
208     public void adaptToPreferenceChange(PropertyChangeEvent event) {
209         if (affectsColorPresentation(event))
210             fColorManager.handlePropertyChangeEvent(event);
211         fPropertyKeyScanner.adaptToPreferenceChange(event);
212         fCommentScanner.adaptToPreferenceChange(event);
213         fPropertyValueScanner.adaptToPreferenceChange(event);
214     }
215     
216     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
217         int length = PARTITIONS.length;
218         String JavaDoc[] contentTypes = new String JavaDoc[length + 1];
219         contentTypes[0] = IDocument.DEFAULT_CONTENT_TYPE;
220         for (int i = 0; i < length; i++)
221             contentTypes[i+1] = PARTITIONS[i];
222
223         return contentTypes;
224     }
225
226     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
227         return PROPERTIES_FILE_PARTITIONING;
228     }
229     
230     public boolean affectsTextPresentation(PropertyChangeEvent event) {
231         String JavaDoc property = event.getProperty();
232         return fCommentScanner.affectsTextPresentation(property)
233                 || fPropertyKeyScanner.affectsTextPresentation(property)
234                 || fPropertyValueScanner.affectsTextPresentation(property);
235     }
236     
237     public boolean affectsColorPresentation(PropertyChangeEvent event) {
238         String JavaDoc property = event.getProperty();
239         return property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_VALUE)
240                 || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ARGUMENT)
241                 || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_ASSIGNMENT)
242                 || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_KEY)
243                 || property.equals(PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
244     }
245     
246     public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
247         if (sourceViewer.isEditable()) {
248             if (fQuickAssistant == null)
249                 fQuickAssistant = new PDEQuickAssistAssistant();
250             return fQuickAssistant;
251         }
252         return null;
253     }
254     
255     public void dispose() {
256         if (fQuickAssistant != null)
257             fQuickAssistant.dispose();
258     }
259 }
260
261
Popular Tags