KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaCommentScanner


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  * Michel Ishizuka <cqw10305@nifty.com> - Bug 113266 [syntax highlighting] javadoc tag names including period is not highlighting correctly
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.text;
13
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.Preferences;
23
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26
27 import org.eclipse.jface.text.rules.ICharacterScanner;
28 import org.eclipse.jface.text.rules.IToken;
29 import org.eclipse.jface.text.rules.IWordDetector;
30 import org.eclipse.jface.text.rules.Token;
31
32 import org.eclipse.jdt.core.JavaCore;
33
34 import org.eclipse.jdt.ui.text.IColorManager;
35 import org.eclipse.jdt.ui.text.IJavaColorConstants;
36
37 import org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher;
38
39 /**
40  * AbstractJavaCommentScanner.java
41  */

42 public class JavaCommentScanner extends AbstractJavaScanner{
43
44     private static class AtJavaIdentifierDetector implements IWordDetector {
45
46         public boolean isWordStart(char c) {
47             return c == '@' || Character.isJavaIdentifierStart(c);
48         }
49
50         public boolean isWordPart(char c) {
51             return c == '.' || Character.isJavaIdentifierPart(c);
52         }
53     }
54
55     private class TaskTagMatcher extends CombinedWordRule.WordMatcher {
56
57         private IToken fToken;
58         /**
59          * Uppercase words
60          * @since 3.0
61          */

62         private Map JavaDoc fUppercaseWords= new HashMap JavaDoc();
63         /**
64          * <code>true</code> if task tag detection is case-sensitive.
65          * @since 3.0
66          */

67         private boolean fCaseSensitive= true;
68         /**
69          * Buffer for uppercase word
70          * @since 3.0
71          */

72         private CombinedWordRule.CharacterBuffer fBuffer= new CombinedWordRule.CharacterBuffer(16);
73
74         public TaskTagMatcher(IToken token) {
75             fToken= token;
76         }
77
78         /*
79          * @see org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher#clearWords()
80          * @since 3.0
81          */

82         public synchronized void clearWords() {
83             super.clearWords();
84             fUppercaseWords.clear();
85         }
86
87         public synchronized void addTaskTags(String JavaDoc value) {
88             String JavaDoc[] tasks= split(value, ","); //$NON-NLS-1$
89
for (int i= 0; i < tasks.length; i++) {
90                 if (tasks[i].length() > 0) {
91                     addWord(tasks[i], fToken);
92                 }
93             }
94         }
95
96         private String JavaDoc[] split(String JavaDoc value, String JavaDoc delimiters) {
97             StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(value, delimiters);
98             int size= tokenizer.countTokens();
99             String JavaDoc[] tokens= new String JavaDoc[size];
100             int i= 0;
101             while (i < size)
102                 tokens[i++]= tokenizer.nextToken();
103             return tokens;
104         }
105
106         /*
107          * @see org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher#addWord(java.lang.String, org.eclipse.jface.text.rules.IToken)
108          * @since 3.0
109          */

110         public synchronized void addWord(String JavaDoc word, IToken token) {
111             Assert.isNotNull(word);
112             Assert.isNotNull(token);
113
114             super.addWord(word, token);
115             fUppercaseWords.put(new CombinedWordRule.CharacterBuffer(word.toUpperCase()), token);
116         }
117
118         /*
119          * @see org.eclipse.jdt.internal.ui.text.CombinedWordRule.WordMatcher#evaluate(org.eclipse.jface.text.rules.ICharacterScanner, org.eclipse.jdt.internal.ui.text.CombinedWordRule.CharacterBuffer)
120          * @since 3.0
121          */

122         public synchronized IToken evaluate(ICharacterScanner scanner, CombinedWordRule.CharacterBuffer word) {
123             if (fCaseSensitive)
124                 return super.evaluate(scanner, word);
125
126             fBuffer.clear();
127             for (int i= 0, n= word.length(); i < n; i++)
128                 fBuffer.append(Character.toUpperCase(word.charAt(i)));
129
130             IToken token= (IToken) fUppercaseWords.get(fBuffer);
131             if (token != null)
132                 return token;
133             return Token.UNDEFINED;
134         }
135
136         /**
137          * Is task tag detection case-senstive?
138          *
139          * @return <code>true</code> iff task tag detection is case-sensitive
140          * @since 3.0
141          */

142         public boolean isCaseSensitive() {
143             return fCaseSensitive;
144         }
145
146         /**
147          * Enables/disables the case-sensitivity of the task tag detection.
148          *
149          * @param caseSensitive <code>true</code> iff case-sensitivity should be enabled
150          * @since 3.0
151          */

152         public void setCaseSensitive(boolean caseSensitive) {
153             fCaseSensitive= caseSensitive;
154         }
155     }
156
157     private static final String JavaDoc COMPILER_TASK_TAGS= JavaCore.COMPILER_TASK_TAGS;
158     protected static final String JavaDoc TASK_TAG= IJavaColorConstants.TASK_TAG;
159     /**
160      * Preference key of a string preference, specifying if task tag detection is case-sensitive.
161      * @since 3.0
162      */

163     private static final String JavaDoc COMPILER_TASK_CASE_SENSITIVE= JavaCore.COMPILER_TASK_CASE_SENSITIVE;
164     /**
165      * Preference value of enabled preferences.
166      * @since 3.0
167      */

168     private static final String JavaDoc ENABLED= JavaCore.ENABLED;
169
170     private TaskTagMatcher fTaskTagMatcher;
171     private Preferences fCorePreferenceStore;
172     private String JavaDoc fDefaultTokenProperty;
173     private String JavaDoc[] fTokenProperties;
174
175     public JavaCommentScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore, String JavaDoc defaultTokenProperty) {
176         this(manager, store, coreStore, defaultTokenProperty, new String JavaDoc[] { defaultTokenProperty, TASK_TAG });
177     }
178
179     public JavaCommentScanner(IColorManager manager, IPreferenceStore store, Preferences coreStore, String JavaDoc defaultTokenProperty, String JavaDoc[] tokenProperties) {
180         super(manager, store);
181
182         fCorePreferenceStore= coreStore;
183         fDefaultTokenProperty= defaultTokenProperty;
184         fTokenProperties= tokenProperties;
185
186         initialize();
187     }
188
189     /**
190      * Initialize with the given arguments.
191      *
192      * @param manager Color manager
193      * @param store Preference store
194      * @param defaultTokenProperty Default token property
195      *
196      * @since 3.0
197      */

198     public JavaCommentScanner(IColorManager manager, IPreferenceStore store, String JavaDoc defaultTokenProperty) {
199         this(manager, store, null, defaultTokenProperty, new String JavaDoc[] { defaultTokenProperty, TASK_TAG });
200     }
201
202     /**
203      * Initialize with the given arguments.
204      *
205      * @param manager Color manager
206      * @param store Preference store
207      * @param defaultTokenProperty Default token property
208      * @param tokenProperties Token properties
209      *
210      * @since 3.0
211      */

212     public JavaCommentScanner(IColorManager manager, IPreferenceStore store, String JavaDoc defaultTokenProperty, String JavaDoc[] tokenProperties) {
213         this(manager, store, null, defaultTokenProperty, tokenProperties);
214     }
215
216     /*
217      * @see AbstractJavaScanner#createRules()
218      */

219     protected List JavaDoc createRules() {
220         List JavaDoc list= new ArrayList JavaDoc();
221         Token defaultToken= getToken(fDefaultTokenProperty);
222
223         List JavaDoc matchers= createMatchers();
224         if (matchers.size() > 0) {
225             CombinedWordRule combinedWordRule= new CombinedWordRule(new AtJavaIdentifierDetector(), defaultToken);
226             for (int i= 0, n= matchers.size(); i < n; i++)
227                 combinedWordRule.addWordMatcher((WordMatcher) matchers.get(i));
228             list.add(combinedWordRule);
229         }
230
231         setDefaultReturnToken(defaultToken);
232
233         return list;
234     }
235
236     /**
237      * Creates a list of word matchers.
238      *
239      * @return the list of word matchers
240      */

241     protected List JavaDoc createMatchers() {
242         List JavaDoc list= new ArrayList JavaDoc();
243
244         // Add rule for Task Tags.
245
boolean isCaseSensitive= true;
246         String JavaDoc tasks= null;
247         if (getPreferenceStore().contains(COMPILER_TASK_TAGS)) {
248             tasks= getPreferenceStore().getString(COMPILER_TASK_TAGS);
249             isCaseSensitive= ENABLED.equals(getPreferenceStore().getString(COMPILER_TASK_CASE_SENSITIVE));
250         } else if (fCorePreferenceStore != null) {
251             tasks= fCorePreferenceStore.getString(COMPILER_TASK_TAGS);
252             isCaseSensitive= ENABLED.equals(fCorePreferenceStore.getString(COMPILER_TASK_CASE_SENSITIVE));
253         }
254         if (tasks != null) {
255             fTaskTagMatcher= new TaskTagMatcher(getToken(TASK_TAG));
256             fTaskTagMatcher.addTaskTags(tasks);
257             fTaskTagMatcher.setCaseSensitive(isCaseSensitive);
258             list.add(fTaskTagMatcher);
259         }
260
261         return list;
262     }
263
264     /*
265      * @see org.eclipse.jdt.internal.ui.text.AbstractJavaScanner#affectsBehavior(org.eclipse.jface.util.PropertyChangeEvent)
266      */

267     public boolean affectsBehavior(PropertyChangeEvent event) {
268         return event.getProperty().equals(COMPILER_TASK_TAGS) || event.getProperty().equals(COMPILER_TASK_CASE_SENSITIVE) || super.affectsBehavior(event);
269     }
270
271     /*
272      * @see org.eclipse.jdt.internal.ui.text.AbstractJavaScanner#adaptToPreferenceChange(org.eclipse.jface.util.PropertyChangeEvent)
273      */

274     public void adaptToPreferenceChange(PropertyChangeEvent event) {
275         if (fTaskTagMatcher != null && event.getProperty().equals(COMPILER_TASK_TAGS)) {
276             Object JavaDoc value= event.getNewValue();
277             if (value instanceof String JavaDoc) {
278                 synchronized (fTaskTagMatcher) {
279                     fTaskTagMatcher.clearWords();
280                     fTaskTagMatcher.addTaskTags((String JavaDoc) value);
281                 }
282             }
283         } else if (fTaskTagMatcher != null && event.getProperty().equals(COMPILER_TASK_CASE_SENSITIVE)) {
284             Object JavaDoc value= event.getNewValue();
285             if (value instanceof String JavaDoc)
286                 fTaskTagMatcher.setCaseSensitive(ENABLED.equals(value));
287         } else if (super.affectsBehavior(event))
288             super.adaptToPreferenceChange(event);
289     }
290
291     /*
292      * @see org.eclipse.jdt.internal.ui.text.AbstractJavaScanner#getTokenProperties()
293      */

294     protected String JavaDoc[] getTokenProperties() {
295         return fTokenProperties;
296     }
297
298 }
299
300
Popular Tags