129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.hyperlink.DefaultHyperlinkPresenter;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21 import dwt.DWT;
|
|
22 import dwt.custom.StyleRange;
|
|
23 import dwt.custom.StyledText;
|
|
24 import dwt.graphics.Color;
|
|
25 import dwt.graphics.Cursor;
|
|
26 import dwt.graphics.RGB;
|
|
27 import dwt.widgets.Display;
|
|
28 import dwtx.core.runtime.Assert;
|
|
29 import dwtx.jface.preference.IPreferenceStore;
|
|
30 import dwtx.jface.preference.PreferenceConverter;
|
|
31 import dwtx.jface.text.BadLocationException;
|
|
32 import dwtx.jface.text.DocumentEvent;
|
|
33 import dwtx.jface.text.IDocument;
|
|
34 import dwtx.jface.text.IDocumentListener;
|
|
35 import dwtx.jface.text.IRegion;
|
|
36 import dwtx.jface.text.ITextInputListener;
|
|
37 import dwtx.jface.text.ITextPresentationListener;
|
|
38 import dwtx.jface.text.ITextViewer;
|
|
39 import dwtx.jface.text.ITextViewerExtension2;
|
|
40 import dwtx.jface.text.ITextViewerExtension4;
|
|
41 import dwtx.jface.text.Position;
|
|
42 import dwtx.jface.text.Region;
|
|
43 import dwtx.jface.text.TextPresentation;
|
|
44 import dwtx.jface.util.IPropertyChangeListener;
|
|
45 import dwtx.jface.util.PropertyChangeEvent;
|
|
46
|
|
47
|
|
48 /**
|
|
49 * The default hyperlink presenter underlines the
|
|
50 * link and colors the line and the text with
|
|
51 * the given color.
|
|
52 * <p>
|
|
53 * It can only be used together with the {@link HyperlinkManager#FIRST}
|
|
54 * or the {@link HyperlinkManager#LONGEST_REGION_FIRST} hyperlink strategy.
|
|
55 * </p>
|
|
56 *
|
|
57 * @since 3.1
|
|
58 */
|
|
59 public class DefaultHyperlinkPresenter : IHyperlinkPresenter, IHyperlinkPresenterExtension, ITextPresentationListener, ITextInputListener, IDocumentListener, IPropertyChangeListener {
|
|
60
|
|
61 /**
|
|
62 * A named preference that holds the color used for hyperlinks.
|
|
63 * <p>
|
|
64 * Value is of type <code>String</code>. A RGB color value encoded as a string
|
|
65 * using class <code>PreferenceConverter</code>
|
|
66 * </p>
|
|
67 *
|
|
68 * @see dwtx.jface.resource.StringConverter
|
|
69 * @see dwtx.jface.preference.PreferenceConverter
|
|
70 */
|
|
71 public final static String HYPERLINK_COLOR= "hyperlinkColor"; //$NON-NLS-1$
|
|
72
|
|
73
|
|
74 /** The text viewer. */
|
|
75 private ITextViewer fTextViewer;
|
|
76 /** The hand cursor. */
|
|
77 private Cursor fCursor;
|
|
78 /** The link color. */
|
|
79 private Color fColor;
|
|
80 /** The link color specification. May be <code>null</code>. */
|
|
81 private RGB fRGB;
|
|
82 /** Tells whether to dispose the color on uninstall. */
|
|
83 private bool fDisposeColor;
|
|
84 /** The currently active region. */
|
|
85 private IRegion fActiveRegion;
|
|
86 /** The currently active style range as position. */
|
|
87 private Position fRememberedPosition;
|
|
88 /** The optional preference store. May be <code>null</code>. */
|
|
89 private IPreferenceStore fPreferenceStore;
|
|
90
|
|
91
|
|
92 /**
|
|
93 * Creates a new default hyperlink presenter which uses
|
|
94 * {@link #HYPERLINK_COLOR} to read the color from the given preference store.
|
|
95 *
|
|
96 * @param store the preference store
|
|
97 */
|
|
98 public DefaultHyperlinkPresenter(IPreferenceStore store) {
|
|
99 fPreferenceStore= store;
|
|
100 fDisposeColor= true;
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * Creates a new default hyperlink presenter.
|
|
105 *
|
|
106 * @param color the hyperlink color, to be disposed by the caller
|
|
107 */
|
|
108 public DefaultHyperlinkPresenter(Color color) {
|
|
109 fDisposeColor= false;
|
|
110 fColor= color;
|
|
111 }
|
|
112
|
|
113 /**
|
|
114 * Creates a new default hyperlink presenter.
|
|
115 *
|
|
116 * @param color the hyperlink color
|
|
117 */
|
|
118 public DefaultHyperlinkPresenter(RGB color) {
|
|
119 fRGB= color;
|
|
120 fDisposeColor= true;
|
|
121 }
|
|
122
|
|
123 /*
|
|
124 * @see dwtx.jdt.internal.ui.javaeditor.IHyperlinkControl#canShowMultipleHyperlinks()
|
|
125 */
|
|
126 public bool canShowMultipleHyperlinks() {
|
|
127 return false;
|
|
128 }
|
|
129
|
|
130 /*
|
|
131 * @see dwtx.jdt.internal.ui.javaeditor.IHyperlinkControl#activate(dwtx.jdt.internal.ui.javaeditor.IHyperlink[])
|
|
132 */
|
|
133 public void showHyperlinks(IHyperlink[] hyperlinks) {
|
|
134 Assert.isLegal(hyperlinks !is null && hyperlinks.length is 1);
|
|
135 highlightRegion(hyperlinks[0].getHyperlinkRegion());
|
|
136 activateCursor();
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * {@inheritDoc}
|
|
141 *
|
|
142 * @since 3.4
|
|
143 */
|
|
144 public bool canHideHyperlinks() {
|
|
145 return true;
|
|
146 }
|
|
147
|
|
148 /*
|
|
149 * @see dwtx.jdt.internal.ui.javaeditor.IHyperlinkControl#deactivate()
|
|
150 */
|
|
151 public void hideHyperlinks() {
|
|
152 repairRepresentation();
|
|
153 fRememberedPosition= null;
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * @see dwtx.jdt.internal.ui.javaeditor.IHyperlinkControl#install(dwtx.jface.text.ITextViewer)
|
|
158 */
|
|
159 public void install(ITextViewer textViewer) {
|
|
160 Assert.isNotNull(textViewer);
|
|
161 fTextViewer= textViewer;
|
|
162 fTextViewer.addTextInputListener(this);
|
|
163 if (fTextViewer instanceof ITextViewerExtension4)
|
|
164 ((ITextViewerExtension4)fTextViewer).addTextPresentationListener(this);
|
|
165
|
|
166 StyledText text= fTextViewer.getTextWidget();
|
|
167 if (text !is null && !text.isDisposed()) {
|
|
168 if (fPreferenceStore !is null)
|
|
169 fColor= createColor(fPreferenceStore, HYPERLINK_COLOR, text.getDisplay());
|
|
170 else if (fRGB !is null)
|
|
171 fColor= new Color(text.getDisplay(), fRGB);
|
|
172 }
|
|
173
|
|
174 if (fPreferenceStore !is null)
|
|
175 fPreferenceStore.addPropertyChangeListener(this);
|
|
176 }
|
|
177
|
|
178 /*
|
|
179 * @see dwtx.jdt.internal.ui.javaeditor.IHyperlinkControl#uninstall()
|
|
180 */
|
|
181 public void uninstall() {
|
|
182 fTextViewer.removeTextInputListener(this);
|
|
183 IDocument document= fTextViewer.getDocument();
|
|
184 if (document !is null)
|
|
185 document.removeDocumentListener(this);
|
|
186
|
|
187 if (fColor !is null) {
|
|
188 if (fDisposeColor)
|
|
189 fColor.dispose();
|
|
190 fColor= null;
|
|
191 }
|
|
192
|
|
193 if (fCursor !is null) {
|
|
194 fCursor.dispose();
|
|
195 fCursor= null;
|
|
196 }
|
|
197
|
|
198 if (fTextViewer instanceof ITextViewerExtension4)
|
|
199 ((ITextViewerExtension4)fTextViewer).removeTextPresentationListener(this);
|
|
200 fTextViewer= null;
|
|
201
|
|
202 if (fPreferenceStore !is null)
|
|
203 fPreferenceStore.removePropertyChangeListener(this);
|
|
204 }
|
|
205
|
|
206 public void setColor(Color color) {
|
|
207 Assert.isNotNull(fTextViewer);
|
|
208 fColor= color;
|
|
209 }
|
|
210
|
|
211 /*
|
|
212 * @see dwtx.jface.text.ITextPresentationListener#applyTextPresentation(dwtx.jface.text.TextPresentation)
|
|
213 */
|
|
214 public void applyTextPresentation(TextPresentation textPresentation) {
|
|
215 if (fActiveRegion is null)
|
|
216 return;
|
|
217 IRegion region= textPresentation.getExtent();
|
|
218 if (fActiveRegion.getOffset() + fActiveRegion.getLength() >= region.getOffset() && region.getOffset() + region.getLength() > fActiveRegion.getOffset()) {
|
|
219 StyleRange styleRange= new StyleRange(fActiveRegion.getOffset(), fActiveRegion.getLength(), fColor, null);
|
|
220 styleRange.underline= true;
|
|
221 textPresentation.mergeStyleRange(styleRange);
|
|
222 }
|
|
223 }
|
|
224
|
|
225 private void highlightRegion(IRegion region) {
|
|
226
|
|
227 if (region.equals(fActiveRegion))
|
|
228 return;
|
|
229
|
|
230 repairRepresentation();
|
|
231
|
|
232 StyledText text= fTextViewer.getTextWidget();
|
|
233 if (text is null || text.isDisposed())
|
|
234 return;
|
|
235
|
|
236 // Invalidate region is> apply text presentation
|
|
237 fActiveRegion= region;
|
|
238 if (fTextViewer instanceof ITextViewerExtension2)
|
|
239 ((ITextViewerExtension2)fTextViewer).invalidateTextPresentation(region.getOffset(), region.getLength());
|
|
240 else
|
|
241 fTextViewer.invalidateTextPresentation();
|
|
242 }
|
|
243
|
|
244 private void activateCursor() {
|
|
245 StyledText text= fTextViewer.getTextWidget();
|
|
246 if (text is null || text.isDisposed())
|
|
247 return;
|
|
248 Display display= text.getDisplay();
|
|
249 if (fCursor is null)
|
|
250 fCursor= new Cursor(display, DWT.CURSOR_HAND);
|
|
251 text.setCursor(fCursor);
|
|
252 }
|
|
253
|
|
254 private void resetCursor() {
|
|
255 StyledText text= fTextViewer.getTextWidget();
|
|
256 if (text !is null && !text.isDisposed())
|
|
257 text.setCursor(null);
|
|
258
|
|
259 if (fCursor !is null) {
|
|
260 fCursor.dispose();
|
|
261 fCursor= null;
|
|
262 }
|
|
263 }
|
|
264
|
|
265 private void repairRepresentation() {
|
|
266
|
|
267 if (fActiveRegion is null)
|
|
268 return;
|
|
269
|
|
270 int offset= fActiveRegion.getOffset();
|
|
271 int length= fActiveRegion.getLength();
|
|
272 fActiveRegion= null;
|
|
273
|
|
274 resetCursor();
|
|
275
|
|
276 // Invalidate is> remove applied text presentation
|
|
277 if (fTextViewer instanceof ITextViewerExtension2)
|
|
278 ((ITextViewerExtension2) fTextViewer).invalidateTextPresentation(offset, length);
|
|
279 else
|
|
280 fTextViewer.invalidateTextPresentation();
|
|
281
|
|
282 }
|
|
283
|
|
284 /*
|
|
285 * @see dwtx.jface.text.IDocumentListener#documentAboutToBeChanged(dwtx.jface.text.DocumentEvent)
|
|
286 */
|
|
287 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
288 if (fActiveRegion !is null) {
|
|
289 fRememberedPosition= new Position(fActiveRegion.getOffset(), fActiveRegion.getLength());
|
|
290 try {
|
|
291 event.getDocument().addPosition(fRememberedPosition);
|
|
292 } catch (BadLocationException x) {
|
|
293 fRememberedPosition= null;
|
|
294 }
|
|
295 }
|
|
296 }
|
|
297
|
|
298 /*
|
|
299 * @see dwtx.jface.text.IDocumentListener#documentChanged(dwtx.jface.text.DocumentEvent)
|
|
300 */
|
|
301 public void documentChanged(DocumentEvent event) {
|
|
302 if (fRememberedPosition !is null) {
|
|
303 if (!fRememberedPosition.isDeleted()) {
|
|
304 event.getDocument().removePosition(fRememberedPosition);
|
|
305 fActiveRegion= new Region(fRememberedPosition.getOffset(), fRememberedPosition.getLength());
|
|
306 } else {
|
|
307 fActiveRegion= new Region(event.getOffset(), event.getLength());
|
|
308 }
|
|
309 fRememberedPosition= null;
|
|
310
|
|
311 StyledText widget= fTextViewer.getTextWidget();
|
|
312 if (widget !is null && !widget.isDisposed()) {
|
|
313 widget.getDisplay().asyncExec(new Runnable() {
|
|
314 public void run() {
|
|
315 hideHyperlinks();
|
|
316 }
|
|
317 });
|
|
318 }
|
|
319 }
|
|
320 }
|
|
321
|
|
322 /*
|
|
323 * @see dwtx.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument)
|
|
324 */
|
|
325 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
|
|
326 if (oldInput is null)
|
|
327 return;
|
|
328 hideHyperlinks();
|
|
329 oldInput.removeDocumentListener(this);
|
|
330 }
|
|
331
|
|
332 /*
|
|
333 * @see dwtx.jface.text.ITextInputListener#inputDocumentChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument)
|
|
334 */
|
|
335 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
|
|
336 if (newInput is null)
|
|
337 return;
|
|
338 newInput.addDocumentListener(this);
|
|
339 }
|
|
340
|
|
341 /**
|
|
342 * Creates a color from the information stored in the given preference store.
|
|
343 *
|
|
344 * @param store the preference store
|
|
345 * @param key the key
|
|
346 * @param display the display
|
|
347 * @return the color or <code>null</code> if there is no such information available
|
|
348 */
|
|
349 private Color createColor(IPreferenceStore store, String key, Display display) {
|
|
350
|
|
351 RGB rgb= null;
|
|
352
|
|
353 if (store.contains(key)) {
|
|
354
|
|
355 if (store.isDefault(key))
|
|
356 rgb= PreferenceConverter.getDefaultColor(store, key);
|
|
357 else
|
|
358 rgb= PreferenceConverter.getColor(store, key);
|
|
359
|
|
360 if (rgb !is null)
|
|
361 return new Color(display, rgb);
|
|
362 }
|
|
363
|
|
364 return null;
|
|
365 }
|
|
366
|
|
367 /*
|
|
368 * @see dwtx.jface.util.IPropertyChangeListener#propertyChange(dwtx.jface.util.PropertyChangeEvent)
|
|
369 */
|
|
370 public void propertyChange(PropertyChangeEvent event) {
|
|
371 if (!HYPERLINK_COLOR.equals(event.getProperty()))
|
|
372 return;
|
|
373
|
|
374 if (fDisposeColor && fColor !is null && !fColor.isDisposed())
|
|
375 fColor.dispose();
|
|
376 fColor= null;
|
|
377
|
|
378 StyledText textWidget= fTextViewer.getTextWidget();
|
|
379 if (textWidget !is null && !textWidget.isDisposed())
|
|
380 fColor= createColor(fPreferenceStore, HYPERLINK_COLOR, textWidget.getDisplay());
|
|
381 }
|
|
382 }
|