KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > source > SourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jface.text.source;
12
13
14 import java.util.Arrays JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Shell;
19
20 import org.eclipse.jface.text.DefaultAutoIndentStrategy;
21 import org.eclipse.jface.text.DefaultInformationControl;
22 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy;
23 import org.eclipse.jface.text.IAutoEditStrategy;
24 import org.eclipse.jface.text.IAutoIndentStrategy;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IDocumentExtension3;
27 import org.eclipse.jface.text.IInformationControl;
28 import org.eclipse.jface.text.IInformationControlCreator;
29 import org.eclipse.jface.text.ITextDoubleClickStrategy;
30 import org.eclipse.jface.text.ITextHover;
31 import org.eclipse.jface.text.ITextViewerExtension2;
32 import org.eclipse.jface.text.IUndoManager;
33 import org.eclipse.jface.text.TextViewerUndoManager;
34 import org.eclipse.jface.text.contentassist.IContentAssistant;
35 import org.eclipse.jface.text.formatter.IContentFormatter;
36 import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter;
37 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
38 import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
39 import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
40 import org.eclipse.jface.text.information.IInformationPresenter;
41 import org.eclipse.jface.text.presentation.IPresentationReconciler;
42 import org.eclipse.jface.text.presentation.PresentationReconciler;
43 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
44 import org.eclipse.jface.text.reconciler.IReconciler;
45
46
47 /**
48  * This class bundles the configuration space of a source viewer. Instances of
49  * this class are passed to the <code>configure</code> method of
50  * <code>ISourceViewer</code>.
51  * <p>
52  * Each method in this class get as argument the source viewer for which it
53  * should provide a particular configuration setting such as a presentation
54  * reconciler. Based on its specific knowledge about the returned object, the
55  * configuration might share such objects or compute them according to some
56  * rules.</p>
57  * <p>
58  * Clients should subclass and override just those methods which must be
59  * specific to their needs.</p>
60  *
61  * @see org.eclipse.jface.text.source.ISourceViewer
62  */

63 public class SourceViewerConfiguration {
64
65
66     /**
67      * Creates a new source viewer configuration that behaves according to
68      * specification of this class' methods.
69      */

70     public SourceViewerConfiguration() {
71         super();
72     }
73
74     /**
75      * Returns the visual width of the tab character. This implementation always
76      * returns 4.
77      *
78      * @param sourceViewer the source viewer to be configured by this configuration
79      * @return the tab width
80      */

81     public int getTabWidth(ISourceViewer sourceViewer) {
82         return 4;
83     }
84
85     /**
86      * Returns the undo manager for the given source viewer. This implementation
87      * always returns a new instance of <code>DefaultUndoManager</code> whose
88      * history length is set to 25.
89      *
90      * @param sourceViewer the source viewer to be configured by this configuration
91      * @return an undo manager or <code>null</code> if no undo/redo should not be supported
92      */

93     public IUndoManager getUndoManager(ISourceViewer sourceViewer) {
94         return new TextViewerUndoManager(25);
95     }
96
97     /**
98      * Returns the reconciler ready to be used with the given source viewer.
99      * This implementation always returns <code>null</code>.
100      *
101      * @param sourceViewer the source viewer to be configured by this configuration
102      * @return a reconciler or <code>null</code> if reconciling should not be supported
103      */

104     public IReconciler getReconciler(ISourceViewer sourceViewer) {
105         return null;
106     }
107
108     /**
109      * Returns the presentation reconciler ready to be used with the given source viewer.
110      *
111      * @param sourceViewer the source viewer
112      * @return the presentation reconciler or <code>null</code> if presentation reconciling should not be supported
113      */

114     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
115         PresentationReconciler reconciler= new PresentationReconciler();
116         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
117         return reconciler;
118     }
119
120     /**
121      * Returns the content formatter ready to be used with the given source viewer.
122      * This implementation always returns <code>null</code>.
123      *
124      * @param sourceViewer the source viewer to be configured by this configuration
125      * @return a content formatter or <code>null</code> if formatting should not be supported
126      */

127     public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
128         return null;
129     }
130
131     /**
132      * Returns the content assistant ready to be used with the given source viewer.
133      * This implementation always returns <code>null</code>.
134      *
135      * @param sourceViewer the source viewer to be configured by this configuration
136      * @return a content assistant or <code>null</code> if content assist should not be supported
137      */

138     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
139         return null;
140     }
141
142     /**
143      * Returns the quick assist assistant ready to be used with the given
144      * source viewer.
145      * This implementation always returns <code>null</code>.
146      *
147      * @param sourceViewer the source viewer to be configured by this configuration
148      * @return a quick assist assistant or <code>null</code> if quick assist should not be supported
149      * @since 3.2
150      */

151     public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
152         return null;
153     }
154
155     /**
156      * Returns the auto indentation strategy ready to be used with the given source viewer
157      * when manipulating text of the given content type. This implementation always
158      * returns an new instance of <code>DefaultAutoIndentStrategy</code>.
159      *
160      * @param sourceViewer the source viewer to be configured by this configuration
161      * @param contentType the content type for which the strategy is applicable
162      * @return the auto indent strategy or <code>null</code> if automatic indentation is not to be enabled
163      * @deprecated since 3.1 use {@link #getAutoEditStrategies(ISourceViewer, String)} instead
164      */

165     public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String JavaDoc contentType) {
166         return new DefaultAutoIndentStrategy();
167     }
168
169     /**
170      * Returns the auto edit strategies ready to be used with the given source viewer
171      * when manipulating text of the given content type. For backward compatibility, this implementation always
172      * returns an array containing the result of {@link #getAutoIndentStrategy(ISourceViewer, String)}.
173      *
174      * @param sourceViewer the source viewer to be configured by this configuration
175      * @param contentType the content type for which the strategies are applicable
176      * @return the auto edit strategies or <code>null</code> if automatic editing is not to be enabled
177      * @since 3.1
178      */

179     public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String JavaDoc contentType) {
180         return new IAutoEditStrategy[] { getAutoIndentStrategy(sourceViewer, contentType) };
181     }
182
183     /**
184      * Returns the default prefixes to be used by the line-prefix operation
185      * in the given source viewer for text of the given content type. This implementation always
186      * returns <code>null</code>.
187      *
188      * @param sourceViewer the source viewer to be configured by this configuration
189      * @param contentType the content type for which the prefix is applicable
190      * @return the default prefixes or <code>null</code> if the prefix operation should not be supported
191      * @since 2.0
192      */

193     public String JavaDoc[] getDefaultPrefixes(ISourceViewer sourceViewer, String JavaDoc contentType) {
194         return null;
195     }
196
197     /**
198      * Returns the double-click strategy ready to be used in this viewer when double clicking
199      * onto text of the given content type. This implementation always returns a new instance of
200      * <code>DefaultTextDoubleClickStrategy</code>.
201      *
202      * @param sourceViewer the source viewer to be configured by this configuration
203      * @param contentType the content type for which the strategy is applicable
204      * @return a double-click strategy or <code>null</code> if double clicking should not be supported
205      */

206     public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String JavaDoc contentType) {
207         return new DefaultTextDoubleClickStrategy();
208     }
209
210     /**
211      * Returns the prefixes to be used by the line-shift operation. This implementation
212      * always returns <code>new String[] { "\t", " ", "" }</code>.
213      * <p>
214      * <strong>Note:</strong> <em>This default is incorrect but cannot be changed in order not
215      * to break any existing clients. Subclasses should overwrite this method and
216      * use {@link #getIndentPrefixesForTab(int)} if applicable.</em>
217      *
218      * @param sourceViewer the source viewer to be configured by this configuration
219      * @param contentType the content type for which the prefix is applicable
220      * @return the prefixes or <code>null</code> if the prefix operation should not be supported
221      */

222     public String JavaDoc[] getIndentPrefixes(ISourceViewer sourceViewer, String JavaDoc contentType) {
223         return new String JavaDoc[] { "\t", " ", "" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
224
}
225     
226     /**
227      * Computes and returns the indent prefixes for tab indentation
228      * which is represented as <code>tabSizeInSpaces</code>.
229      *
230      * @param tabWidth the display tab width
231      * @return the indent prefixes
232      * @see #getIndentPrefixes(ISourceViewer, String)
233      * @since 3.3
234      */

235     protected String JavaDoc[] getIndentPrefixesForTab(int tabWidth) {
236         String JavaDoc[] indentPrefixes= new String JavaDoc[tabWidth + 2];
237         for (int i= 0; i <= tabWidth; i++) {
238             char[] spaceChars= new char[i];
239             Arrays.fill(spaceChars, ' ');
240             String JavaDoc spaces= new String JavaDoc(spaceChars);
241             if (i < tabWidth)
242                 indentPrefixes[i]= spaces + '\t';
243             else
244                 indentPrefixes[i]= new String JavaDoc(spaces);
245         }
246         indentPrefixes[tabWidth + 1]= ""; //$NON-NLS-1$
247
return indentPrefixes;
248     }
249
250     /**
251      * Returns the annotation hover which will provide the information to be
252      * shown in a hover popup window when requested for the given
253      * source viewer. This implementation always returns <code>null</code>.
254      *
255      * @param sourceViewer the source viewer to be configured by this configuration
256      * @return an annotation hover or <code>null</code> if no hover support should be installed
257      */

258     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
259         return null;
260     }
261
262     /**
263      * Returns the annotation hover which will provide the information to be
264      * shown in a hover popup window when requested for the overview ruler
265      * of the given source viewer.This implementation always returns the general
266      * annotation hover returned by <code>getAnnotationHover</code>.
267      *
268      * @param sourceViewer the source viewer to be configured by this configuration
269      * @return an annotation hover or <code>null</code> if no hover support should be installed
270      * @since 3.0
271      */

272     public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
273         return getAnnotationHover(sourceViewer);
274     }
275
276     /**
277      * Returns the SWT event state masks for which text hover are configured for
278      * the given content type.
279      *
280      * @param sourceViewer the source viewer to be configured by this configuration
281      * @param contentType the content type
282      * @return an <code>int</code> array with the configured SWT event state masks
283      * or <code>null</code> if text hovers are not supported for the given content type
284      * @since 2.1
285      */

286     public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String JavaDoc contentType) {
287         return null;
288     }
289
290     /**
291      * Returns the text hover which will provide the information to be shown
292      * in a text hover popup window when requested for the given source viewer and
293      * the given content type. This implementation always returns <code>
294      * null</code>.
295      *
296      * @param sourceViewer the source viewer to be configured by this configuration
297      * @param contentType the content type
298      * @param stateMask the SWT event state mask
299      * @return a text hover or <code>null</code> if no hover support should be installed
300      * @since 2.1
301      */

302     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType, int stateMask) {
303         if (stateMask == ITextViewerExtension2.DEFAULT_HOVER_STATE_MASK)
304             return getTextHover(sourceViewer, contentType);
305         return null;
306     }
307
308     /**
309      * Returns the text hover which will provide the information to be shown
310      * in a text hover popup window when requested for the given source viewer and
311      * the given content type. This implementation always returns <code>
312      * null</code>.
313      *
314      * @param sourceViewer the source viewer to be configured by this configuration
315      * @param contentType the content type
316      * @return a text hover or <code>null</code> if no hover support should be installed
317      */

318     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType) {
319         return null;
320     }
321
322     /**
323      * Returns the information control creator. The creator is a factory creating information
324      * controls for the given source viewer. This implementation always returns a creator for
325      * <code>DefaultInformationControl</code> instances.
326      *
327      * @param sourceViewer the source viewer to be configured by this configuration
328      * @return the information control creator or <code>null</code> if no information support should be installed
329      * @since 2.0
330      */

331     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
332         return new IInformationControlCreator() {
333             public IInformationControl createInformationControl(Shell parent) {
334                 return new DefaultInformationControl(parent);
335             }
336         };
337     }
338
339     /**
340      * Returns the information presenter which will determine and shown
341      * information requested for the current cursor position. This implementation
342      * always returns <code>null</code>.
343      *
344      * @param sourceViewer the source viewer to be configured by this configuration
345      * @return an information presenter <code>null</code> if no information presenter should be installed
346      * @since 2.0
347      */

348     public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
349         return null;
350     }
351
352     /**
353      * Returns all configured content types for the given source viewer. This list
354      * tells the caller which content types must be configured for the given source
355      * viewer, i.e. for which content types the given source viewer's functionalities
356      * must be specified. This implementation always returns <code>
357      * new String[] { IDocument.DEFAULT_CONTENT_TYPE }</code>.
358      *
359      * @param sourceViewer the source viewer to be configured by this configuration
360      * @return the configured content types for the given viewer
361      */

362     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
363         return new String JavaDoc[] { IDocument.DEFAULT_CONTENT_TYPE };
364     }
365
366     /**
367      * Returns the configured partitioning for the given source viewer. The partitioning is
368      * used when the querying content types from the source viewer's input document. This
369      * implementation always returns <code>IDocumentExtension3.DEFAULT_PARTITIONING</code>.
370      *
371      * @param sourceViewer the source viewer to be configured by this configuration
372      * @return the configured partitioning
373      * @see #getConfiguredContentTypes(ISourceViewer)
374      * @since 3.0
375      */

376     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
377         return IDocumentExtension3.DEFAULT_PARTITIONING;
378     }
379
380     /**
381      * Returns the hyperlink detectors which be used to detect hyperlinks
382      * in the given source viewer. This
383      * implementation always returns an array with an URL hyperlink detector.
384      *
385      * @param sourceViewer the source viewer to be configured by this configuration
386      * @return an array with hyperlink detectors or <code>null</code> if no hyperlink support should be installed
387      * @since 3.1
388      */

389     public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
390         if (sourceViewer == null)
391             return null;
392
393         return new IHyperlinkDetector[] { new URLHyperlinkDetector() };
394     }
395
396     /**
397      * Returns the hyperlink presenter for the given source viewer.
398      * This implementation always returns the {@link DefaultHyperlinkPresenter}.
399      *
400      * @param sourceViewer the source viewer to be configured by this configuration
401      * @return the hyperlink presenter or <code>null</code> if no hyperlink support should be installed
402      * @since 3.1
403      */

404     public IHyperlinkPresenter getHyperlinkPresenter(ISourceViewer sourceViewer) {
405         return new DefaultHyperlinkPresenter(new RGB(0, 0, 255));
406     }
407
408     /**
409      * Returns the SWT event state mask which in combination
410      * with the left mouse button activates hyperlinking.
411      * This implementation always returns the {@link SWT#MOD1}.
412      *
413      * @param sourceViewer the source viewer to be configured by this configuration
414      * @return the SWT event state mask to activate hyperlink mode
415      * @since 3.1
416      */

417     public int getHyperlinkStateMask(ISourceViewer sourceViewer) {
418         return SWT.MOD1;
419     }
420 }
421
Popular Tags