Skip to main content
summaryrefslogtreecommitdiffstats
blob: 00c932c6475b148d8ecf5753119b7eb55529e55c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.ui;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.IUndoManager;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.formatter.IContentFormatter;
import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter;
import org.eclipse.jface.text.information.IInformationPresenter;
import org.eclipse.jface.text.information.IInformationProvider;
import org.eclipse.jface.text.information.InformationPresenter;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
import org.eclipse.ui.texteditor.ChainedPreferenceStore;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredPartitioning;
import org.eclipse.wst.sse.ui.internal.SSEUIPlugin;
import org.eclipse.wst.sse.ui.internal.StructuredTextAnnotationHover;
import org.eclipse.wst.sse.ui.internal.contentassist.StructuredContentAssistant;
import org.eclipse.wst.sse.ui.internal.derived.HTMLTextPresenter;
import org.eclipse.wst.sse.ui.internal.hyperlink.HighlighterHyperlinkPresenter;
import org.eclipse.wst.sse.ui.internal.provisional.preferences.CommonEditorPreferenceNames;
import org.eclipse.wst.sse.ui.internal.provisional.style.LineStyleProvider;
import org.eclipse.wst.sse.ui.internal.reconcile.StructuredRegionProcessor;
import org.eclipse.wst.sse.ui.internal.taginfo.AnnotationHoverProcessor;
import org.eclipse.wst.sse.ui.internal.taginfo.BestMatchHover;
import org.eclipse.wst.sse.ui.internal.taginfo.ProblemAnnotationHoverProcessor;
import org.eclipse.wst.sse.ui.internal.taginfo.TextHoverManager;


/**
 * Configuration for the source viewer used by StructuredTextEditor.<br />
 * Note: While ISourceViewer is passed in for each get configuration, clients
 * should create a new viewer configuration instance for each instance of
 * source viewer as some methods return the same instance of an object,
 * regardless of the sourceviewer.
 * <p>
 * Clients should subclass and override just those methods which must be
 * specific to their needs.
 * </p>
 * 
 * @see org.eclipse.wst.sse.ui.StructuredTextEditor
 * @see org.eclipse.wst.sse.ui.internal.StructuredTextViewer
 * @since 1.0
 */
public class StructuredTextViewerConfiguration extends TextSourceViewerConfiguration {
	/*
	 * One instance per configuration because creating a second assistant that
	 * is added to a viewer can cause odd key-eating by the wrong one.
	 */
	private StructuredContentAssistant fContentAssistant = null;
	/*
	 * One instance per configuration
	 */
	private IReconciler fReconciler;


	/**
	 * Creates a structured text viewer configuration.
	 */
	public StructuredTextViewerConfiguration() {
		super();
		// initialize fPreferenceStore with same preference store used in
		// StructuredTextEditor
		fPreferenceStore = createCombinedPreferenceStore();
	}

	/**
	 * Create a preference store that combines the source editor preferences
	 * with the base editor's preferences.
	 * 
	 * @return IPreferenceStore
	 */
	private IPreferenceStore createCombinedPreferenceStore() {
		IPreferenceStore sseEditorPrefs = SSEUIPlugin.getDefault().getPreferenceStore();
		IPreferenceStore baseEditorPrefs = EditorsUI.getPreferenceStore();
		return new ChainedPreferenceStore(new IPreferenceStore[]{sseEditorPrefs, baseEditorPrefs});
	}

	/**
	 * Returns the annotation hover which will provide the information to be
	 * shown in a hover popup window when requested for the given source
	 * viewer.<br />
	 * Note: Clients cannot override this method because this method returns a
	 * specially configured Annotation Hover for the StructuredTextViewer.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return an annotation hover specially configured for
	 *         StructuredTextViewer
	 */
	final public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
		/*
		 * This implmentation returns an annotation hover that works with
		 * StructuredTextViewer and breakpoints. Important! must remember to
		 * release it when done with it (during viewer.unconfigure)
		 */
		return new StructuredTextAnnotationHover();
	}

	/**
	 * Returns the configured partitioning for the given source viewer. The
	 * partitioning is used when the querying content types from the source
	 * viewer's input document.<br />
	 * Note: Clients cannot override this method at this time.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return the configured partitioning
	 * @see #getConfiguredContentTypes(ISourceViewer)
	 */
	final public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
		/*
		 * This implementation returns default structured text partitioning
		 */
		return IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING;
	}

	public int[] getConfiguredTextHoverStateMasks(ISourceViewer sourceViewer, String contentType) {
		/*
		 * This implementation returns configured text hover state masks for
		 * StructuredTextViewers
		 */
		TextHoverManager.TextHoverDescriptor[] hoverDescs = SSEUIPlugin.getDefault().getTextHoverManager().getTextHovers();
		int stateMasks[] = new int[hoverDescs.length];
		int stateMasksLength = 0;
		for (int i = 0; i < hoverDescs.length; i++) {
			if (hoverDescs[i].isEnabled()) {
				int j = 0;
				int stateMask = computeStateMask(hoverDescs[i].getModifierString());
				while (j < stateMasksLength) {
					if (stateMasks[j] == stateMask)
						break;
					j++;
				}
				if (j == stateMasksLength)
					stateMasks[stateMasksLength++] = stateMask;
			}
		}
		if (stateMasksLength == hoverDescs.length)
			return stateMasks;

		int[] shortenedStateMasks = new int[stateMasksLength];
		System.arraycopy(stateMasks, 0, shortenedStateMasks, 0, stateMasksLength);
		return shortenedStateMasks;
	}

	/**
	 * Returns the content assistant ready to be used with the given source
	 * viewer.<br />
	 * Note: The same instance of IContentAssistant is returned regardless of
	 * the source viewer passed in.
	 * <p>
	 * Clients cannot override this method. Instead, clients wanting to add
	 * their own processors should override
	 * <code>getContentAssistProcessors(ISourceViewer, String)</code>
	 * </p>
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return a content assistant
	 * @see #getContentAssistProcessors(ISourceViewer, String)
	 */
	final public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
		/*
		 * Note: This method was made final so that StructuredContentAssist is
		 * always used and content assist extension point always works.
		 */
		if (fContentAssistant == null) {
			fContentAssistant = new StructuredContentAssistant();

			// content assistant configurations
			fContentAssistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
			fContentAssistant.enableAutoActivation(true);
			fContentAssistant.setAutoActivationDelay(500);
			fContentAssistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
			fContentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
			fContentAssistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));

			// add content assist processors for each partition type
			String[] types = getConfiguredContentTypes(sourceViewer);
			for (int i = 0; i < types.length; i++) {
				String type = types[i];

				// add all content assist processors for current partiton type
				IContentAssistProcessor[] processors = getContentAssistProcessors(sourceViewer, type);
				if (processors != null) {
					for (int j = 0; j < processors.length; j++) {
						fContentAssistant.setContentAssistProcessor(processors[j], type);
					}
				}
			}
		}
		return fContentAssistant;
	}

	/**
	 * Returns the content assist processors that will be used for content
	 * assist in the given source viewer and for the given partition type.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @param partitionType
	 *            the partition type for which the content assist processors
	 *            are applicable
	 * @return IContentAssistProcessors or null if should not be supported
	 */
	protected IContentAssistProcessor[] getContentAssistProcessors(ISourceViewer sourceViewer, String partitionType) {
		return null;
	}

	/**
	 * Returns the content formatter ready to be used with the given source
	 * viewer.
	 * <p>
	 * It is not recommended that clients override this method as it may
	 * become <code>final</code> in the future and replaced by an extensible
	 * framework.
	 * </p>
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return a content formatter or <code>null</code> if formatting should
	 *         not be supported
	 */
	public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
		return null;
	}

	/**
	 * Returns the hyperlink presenter for the given source viewer.<br />
	 * Note: Clients cannot override this method because this method returns a
	 * specially configured hyperlink presenter for the StructuredTextViewer.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return a hyperlink presenter specially configured for
	 *         StructuredTextViewer
	 */
	final public IHyperlinkPresenter getHyperlinkPresenter(ISourceViewer sourceViewer) {
		/*
		 * This implementation returns a hyperlink presenter that uses
		 * Highlither instead of PresentationReconciler
		 */
		if (fPreferenceStore == null) {
			return super.getHyperlinkPresenter(sourceViewer);
		}
		return new HighlighterHyperlinkPresenter(fPreferenceStore);
	}

	/**
	 * Returns the information control creator. The creator is a factory
	 * creating information controls for the given source viewer.<br />
	 * Note: Clients cannot override this method at this time.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return the information control creator
	 */
	final public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
		// used by hover help
		return new IInformationControlCreator() {
			public IInformationControl createInformationControl(Shell parent) {
				return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(false));
			}
		};
	}

	/**
	 * Returns the information presenter ready to be used with the given
	 * source viewer.
	 * <p>
	 * Clients cannot override this method. Instead, clients wanting to add
	 * their own information providers should override
	 * <code>getInformationProvider(ISourceViewer, String)</code>
	 * </p>
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return a content assistant
	 * @see #getInformationProvider(ISourceViewer, String)
	 */
	final public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
		InformationPresenter presenter = new InformationPresenter(getInformationPresenterControlCreator(sourceViewer));

		// information presenter configurations
		presenter.setSizeConstraints(60, 10, true, true);
		presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));

		// add information providers for each partition type
		String[] types = getConfiguredContentTypes(sourceViewer);
		for (int i = 0; i < types.length; i++) {
			String type = types[i];

			IInformationProvider provider = getInformationProvider(sourceViewer, type);
			if (provider != null) {
				presenter.setInformationProvider(provider, type);
			}
		}

		return presenter;
	}

	/**
	 * Returns the information provider that will be used for information
	 * presentation in the given source viewer and for the given partition
	 * type.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @param partitionType
	 *            the partition type for which the information provider is
	 *            applicable
	 * @return IInformationProvider or null if should not be supported
	 */
	protected IInformationProvider getInformationProvider(ISourceViewer sourceViewer, String partitionType) {
		return null;
	}

	/**
	 * Returns the information presenter control creator. The creator is a
	 * factory creating the presenter controls for the given source viewer.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return an information control creator
	 */
	private IInformationControlCreator getInformationPresenterControlCreator(ISourceViewer sourceViewer) {
		return new IInformationControlCreator() {
			public IInformationControl createInformationControl(Shell parent) {
				int shellStyle = SWT.RESIZE | SWT.TOOL;
				int style = SWT.V_SCROLL | SWT.H_SCROLL;
				return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
			}
		};
	}

	/**
	 * Returns the line style providers that will be used for syntax
	 * highlighting in the given source viewer.
	 * <p>
	 * Not fully API since return type LineStyleProvider is not API.
	 * </p>
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @param partitionType
	 *            the partition type for which the lineStyleProviders are
	 *            applicable
	 * @return LineStyleProvders or null if should not be supported
	 */
	public LineStyleProvider[] getLineStyleProviders(ISourceViewer sourceViewer, String partitionType) {
		return null;
	}

	/**
	 * StructuredTextViewer currently does not support presentation
	 * reconciler, so clients cannot override this method to provide their own
	 * presentation reconciler. <br />
	 * See <code>getLineStyleProviders(ISourceViewer, String)</code> for
	 * alternative way to provide highlighting information.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return always returns null
	 * @see #getLineStyleProviders(ISourceViewer, String)
	 */
	final public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		/*
		 * This implementation returns null because StructuredTextViewer does
		 * not use presentation reconciler
		 */
		return null;
	}

	/**
	 * Returns the reconciler ready to be used with the given source viewer.<br />
	 * Note: The same instance of IReconciler is returned regardless of the
	 * source viewer passed in.
	 * <p>
	 * Clients cannot override this method. Instead, clients wanting to add
	 * their own reconciling strategy should use the
	 * <code>org.eclipse.wst.sse.ui.extensions.sourcevalidation</code>
	 * extension point.
	 * </p>
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return a reconciler
	 */
	final public IReconciler getReconciler(ISourceViewer sourceViewer) {
		boolean reconcilingEnabled = fPreferenceStore.getBoolean(CommonEditorPreferenceNames.EVALUATE_TEMPORARY_PROBLEMS);
		if (sourceViewer == null || !reconcilingEnabled)
			return null;

		/*
		 * Only create reconciler if sourceviewer is present
		 */
		if (fReconciler == null && sourceViewer != null) {
			StructuredRegionProcessor reconciler = new StructuredRegionProcessor();

			// reconciler configurations
			reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));

			fReconciler = reconciler;
		}
		return fReconciler;
	}

	public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
		ITextHover textHover = null;

		/*
		 * Returns a default problem, annotation, and best match hover
		 * depending on stateMask
		 */
		TextHoverManager.TextHoverDescriptor[] hoverDescs = SSEUIPlugin.getDefault().getTextHoverManager().getTextHovers();
		int i = 0;
		while (i < hoverDescs.length && textHover == null) {
			if (hoverDescs[i].isEnabled() && computeStateMask(hoverDescs[i].getModifierString()) == stateMask) {
				String hoverType = hoverDescs[i].getId();
				if (TextHoverManager.PROBLEM_HOVER.equalsIgnoreCase(hoverType))
					textHover = new ProblemAnnotationHoverProcessor();
				else if (TextHoverManager.ANNOTATION_HOVER.equalsIgnoreCase(hoverType))
					textHover = new AnnotationHoverProcessor();
				else if (TextHoverManager.COMBINATION_HOVER.equalsIgnoreCase(hoverType))
					textHover = new BestMatchHover(null);
			}
			i++;
		}
		return textHover;
	}

	/**
	 * Returns the undo manager for the given source viewer.<br />
	 * Note: Clients cannot override this method because this method returns a
	 * specially configured undo manager for the StructuredTextViewer.
	 * 
	 * @param sourceViewer
	 *            the source viewer to be configured by this configuration
	 * @return an undo manager specially configured for StructuredTextViewer
	 */
	final public IUndoManager getUndoManager(ISourceViewer sourceViewer) {
		/*
		 * This implementation returns an UndoManager that is used exclusively
		 * in StructuredTextViewer
		 */
		return new StructuredTextViewerUndoManager();
	}
}

Back to the top