KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > text > JavaTextTools


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 package org.eclipse.jdt.ui.text;
12
13 import org.eclipse.core.runtime.Preferences;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IDocumentExtension3;
21 import org.eclipse.jface.text.IDocumentPartitioner;
22 import org.eclipse.jface.text.rules.FastPartitioner;
23 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
24 import org.eclipse.jface.text.rules.RuleBasedScanner;
25
26 import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner;
27 import org.eclipse.jdt.internal.ui.text.JavaColorManager;
28 import org.eclipse.jdt.internal.ui.text.JavaCommentScanner;
29 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
30 import org.eclipse.jdt.internal.ui.text.java.JavaCodeScanner;
31 import org.eclipse.jdt.internal.ui.text.javadoc.JavaDocScanner;
32
33
34 /**
35  * Tools required to configure a Java text viewer.
36  * The color manager and all scanner exist only one time, i.e.
37  * the same instances are returned to all clients. Thus, clients
38  * share those tools.
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  */

43 public class JavaTextTools {
44
45     /**
46      * Array with legal content types.
47      * @since 3.0
48      */

49     private final static String JavaDoc[] LEGAL_CONTENT_TYPES= new String JavaDoc[] {
50         IJavaPartitions.JAVA_DOC,
51         IJavaPartitions.JAVA_MULTI_LINE_COMMENT,
52         IJavaPartitions.JAVA_SINGLE_LINE_COMMENT,
53         IJavaPartitions.JAVA_STRING,
54         IJavaPartitions.JAVA_CHARACTER
55     };
56
57     /**
58      * This tools' preference listener.
59      */

60     private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
61         public void propertyChange(PropertyChangeEvent event) {
62             adaptToPreferenceChange(event);
63         }
64         public void propertyChange(Preferences.PropertyChangeEvent event) {
65             adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
66         }
67     }
68
69     /** The color manager. */
70     private JavaColorManager fColorManager;
71     /** The Java source code scanner. */
72     private JavaCodeScanner fCodeScanner;
73     /** The Java multi-line comment scanner. */
74     private JavaCommentScanner fMultilineCommentScanner;
75     /** The Java single-line comment scanner. */
76     private JavaCommentScanner fSinglelineCommentScanner;
77     /** The Java string scanner. */
78     private SingleTokenJavaScanner fStringScanner;
79     /** The JavaDoc scanner. */
80     private JavaDocScanner fJavaDocScanner;
81     /** The preference store. */
82     private IPreferenceStore fPreferenceStore;
83     /**
84      * The core preference store.
85      * @since 2.1
86      */

87     private Preferences fCorePreferenceStore;
88     /** The preference change listener */
89     private PreferenceListener fPreferenceListener= new PreferenceListener();
90
91
92     /**
93      * Creates a new Java text tools collection.
94      *
95      * @param store the preference store to initialize the text tools. The text tool
96      * instance installs a listener on the passed preference store to adapt itself to
97      * changes in the preference store. In general <code>PreferenceConstants.
98      * getPreferenceStore()</code> should be used to initialize the text tools.
99      * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
100      * @since 2.0
101      */

102     public JavaTextTools(IPreferenceStore store) {
103         this(store, null, true);
104     }
105
106     /**
107      * Creates a new Java text tools collection.
108      *
109      * @param store the preference store to initialize the text tools. The text tool
110      * instance installs a listener on the passed preference store to adapt itself to
111      * changes in the preference store. In general <code>PreferenceConstants.
112      * getPreferenceStore()</code> should be used to initialize the text tools.
113      * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
114      * automatically disposes all managed colors when the current display gets disposed
115      * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
116      * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
117      * @since 2.1
118      */

119     public JavaTextTools(IPreferenceStore store, boolean autoDisposeOnDisplayDispose) {
120         this(store, null, autoDisposeOnDisplayDispose);
121     }
122
123     /**
124      * Creates a new Java text tools collection.
125      * @param store the preference store to initialize the text tools. The text tool
126      * instance installs a listener on the passed preference store to adapt itself to
127      * changes in the preference store. In general <code>PreferenceConstants.
128      * getPreferenceStore()</code> should be used to initialize the text tools.
129      * @param coreStore optional preference store to initialize the text tools. The text tool
130      * instance installs a listener on the passed preference store to adapt itself to
131      * changes in the preference store.
132      * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
133      * @since 2.1
134      */

135     public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
136         this(store, coreStore, true);
137     }
138
139     /**
140      * Creates a new Java text tools collection.
141      *
142      * @param store the preference store to initialize the text tools. The text tool
143      * instance installs a listener on the passed preference store to adapt itself to
144      * changes in the preference store. In general <code>PreferenceConstants.
145      * getPreferenceStore()</code> should be used to initialize the text tools.
146      * @param coreStore optional preference store to initialize the text tools. The text tool
147      * instance installs a listener on the passed preference store to adapt itself to
148      * changes in the preference store.
149      * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
150      * automatically disposes all managed colors when the current display gets disposed
151      * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
152      * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
153      * @since 2.1
154      */

155     public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
156         fPreferenceStore= store;
157         fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
158
159         fCorePreferenceStore= coreStore;
160         if (fCorePreferenceStore != null)
161             fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
162
163         fColorManager= new JavaColorManager(autoDisposeOnDisplayDispose);
164         fCodeScanner= new JavaCodeScanner(fColorManager, store);
165         fMultilineCommentScanner= new JavaCommentScanner(fColorManager, store, coreStore, IJavaColorConstants.JAVA_MULTI_LINE_COMMENT);
166         fSinglelineCommentScanner= new JavaCommentScanner(fColorManager, store, coreStore, IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT);
167         fStringScanner= new SingleTokenJavaScanner(fColorManager, store, IJavaColorConstants.JAVA_STRING);
168         fJavaDocScanner= new JavaDocScanner(fColorManager, store, coreStore);
169     }
170
171     /**
172      * Disposes all the individual tools of this tools collection.
173      */

174     public void dispose() {
175
176         fCodeScanner= null;
177         fMultilineCommentScanner= null;
178         fSinglelineCommentScanner= null;
179         fStringScanner= null;
180         fJavaDocScanner= null;
181
182         if (fColorManager != null) {
183             fColorManager.dispose();
184             fColorManager= null;
185         }
186
187         if (fPreferenceStore != null) {
188             fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
189             fPreferenceStore= null;
190
191             if (fCorePreferenceStore != null) {
192                 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
193                 fCorePreferenceStore= null;
194             }
195
196             fPreferenceListener= null;
197         }
198     }
199
200     /**
201      * Returns the color manager which is used to manage
202      * any Java-specific colors needed for such things like syntax highlighting.
203      * <p>
204      * Clients which are only interested in the color manager of the Java UI
205      * plug-in should use {@link org.eclipse.jdt.ui.JavaUI#getColorManager()}.
206      * </p>
207      *
208      * @return the color manager to be used for Java text viewers
209      * @see org.eclipse.jdt.ui.JavaUI#getColorManager()
210      */

211     public IColorManager getColorManager() {
212         return fColorManager;
213     }
214
215     /**
216      * Returns a scanner which is configured to scan Java source code.
217      *
218      * @return a Java source code scanner
219      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#getCodeScanner()}
220      */

221     public RuleBasedScanner getCodeScanner() {
222         return fCodeScanner;
223     }
224
225     /**
226      * Returns a scanner which is configured to scan Java multi-line comments.
227      *
228      * @return a Java multi-line comment scanner
229      * @since 2.0
230      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#getMultilineCommentScanner()}
231      */

232     public RuleBasedScanner getMultilineCommentScanner() {
233         return fMultilineCommentScanner;
234     }
235
236     /**
237      * Returns a scanner which is configured to scan Java single-line comments.
238      *
239      * @return a Java single-line comment scanner
240      * @since 2.0
241      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#getSinglelineCommentScanner()}
242      */

243     public RuleBasedScanner getSinglelineCommentScanner() {
244         return fSinglelineCommentScanner;
245     }
246
247     /**
248      * Returns a scanner which is configured to scan Java strings.
249      *
250      * @return a Java string scanner
251      * @since 2.0
252      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#getStringScanner()}
253      */

254     public RuleBasedScanner getStringScanner() {
255         return fStringScanner;
256     }
257
258     /**
259      * Returns a scanner which is configured to scan JavaDoc compliant comments.
260      * <p>
261      * Note that the start sequence "/**" and the corresponding end sequence
262      * are part of the Javadoc comment.</p>
263      *
264      * @return a Javadoc scanner
265      * @deprecated As of 3.0, replaced by {@link JavaSourceViewerConfiguration#getJavaDocScanner()}
266      */

267     public RuleBasedScanner getJavaDocScanner() {
268         return fJavaDocScanner;
269     }
270
271     /**
272      * Returns a scanner which is configured to scan
273      * Java-specific partitions, which are multi-line comments,
274      * Javadoc comments, and regular Java source code.
275      *
276      * @return a Java partition scanner
277      */

278     public IPartitionTokenScanner getPartitionScanner() {
279         return new FastJavaPartitionScanner();
280     }
281
282     /**
283      * Factory method for creating a Java-specific document partitioner
284      * using this object's partitions scanner. This method is a
285      * convenience method.
286      *
287      * @return a newly created Java document partitioner
288      */

289     public IDocumentPartitioner createDocumentPartitioner() {
290         return new FastPartitioner(getPartitionScanner(), LEGAL_CONTENT_TYPES);
291     }
292
293     /**
294      * Returns the names of the document position categories used by the document
295      * partitioners created by this object to manage their partition information.
296      * <p>
297      * If the partitioners don't use document position categories, the returned
298      * result is <code>null</code>.</p>
299      *
300      * @return the partition managing position categories or <code>null</code> if there is none
301      * @deprecated As of 3.0, replaced by {@link org.eclipse.jface.text.TextUtilities#computePartitionManagingCategories(IDocument)}
302      */

303     public String JavaDoc[] getPartitionManagingPositionCategories() {
304         return new String JavaDoc[] { org.eclipse.jface.text.rules.DefaultPartitioner.CONTENT_TYPES_CATEGORY };
305     }
306
307     /**
308      * Determines whether the preference change encoded by the given event
309      * changes the behavior of one its contained components.
310      *
311      * @param event the event to be investigated
312      * @return <code>true</code> if event causes a behavioral change
313      * @since 2.0
314      * @deprecated As of 3.0, replaced by {@link org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
315      */

316     public boolean affectsBehavior(PropertyChangeEvent event) {
317         return fCodeScanner.affectsBehavior(event) ||
318                     fMultilineCommentScanner.affectsBehavior(event) ||
319                     fSinglelineCommentScanner.affectsBehavior(event) ||
320                     fStringScanner.affectsBehavior(event) ||
321                     fJavaDocScanner.affectsBehavior(event);
322     }
323
324     /**
325      * Adapts the behavior of the contained components to the change
326      * encoded in the given event.
327      *
328      * @param event the event to which to adapt
329      * @since 2.0
330      * @deprecated As of 3.0, no replacement
331      */

332     protected void adaptToPreferenceChange(PropertyChangeEvent event) {
333         if (fCodeScanner.affectsBehavior(event))
334             fCodeScanner.adaptToPreferenceChange(event);
335         if (fMultilineCommentScanner.affectsBehavior(event))
336             fMultilineCommentScanner.adaptToPreferenceChange(event);
337         if (fSinglelineCommentScanner.affectsBehavior(event))
338             fSinglelineCommentScanner.adaptToPreferenceChange(event);
339         if (fStringScanner.affectsBehavior(event))
340             fStringScanner.adaptToPreferenceChange(event);
341         if (fJavaDocScanner.affectsBehavior(event))
342             fJavaDocScanner.adaptToPreferenceChange(event);
343     }
344
345     /**
346      * Sets up the Java document partitioner for the given document for the default partitioning.
347      *
348      * @param document the document to be set up
349      * @since 3.0
350      */

351     public void setupJavaDocumentPartitioner(IDocument document) {
352         setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING);
353     }
354
355     /**
356      * Sets up the Java document partitioner for the given document for the given partitioning.
357      *
358      * @param document the document to be set up
359      * @param partitioning the document partitioning
360      * @since 3.0
361      */

362     public void setupJavaDocumentPartitioner(IDocument document, String JavaDoc partitioning) {
363         IDocumentPartitioner partitioner= createDocumentPartitioner();
364         if (document instanceof IDocumentExtension3) {
365             IDocumentExtension3 extension3= (IDocumentExtension3) document;
366             extension3.setDocumentPartitioner(partitioning, partitioner);
367         } else {
368             document.setDocumentPartitioner(partitioner);
369         }
370         partitioner.connect(document);
371     }
372
373     /**
374      * Returns this text tool's preference store.
375      *
376      * @return the preference store
377      * @since 3.0
378      */

379     protected IPreferenceStore getPreferenceStore() {
380         return fPreferenceStore;
381     }
382
383     /**
384      * Returns this text tool's core preference store.
385      *
386      * @return the core preference store
387      * @since 3.0
388      */

389     protected Preferences getCorePreferenceStore() {
390         return fCorePreferenceStore;
391     }
392 }
393
Popular Tags