comparison dwtx/jface/text/TextViewerHoverManager.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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.TextViewerHoverManager;
14
15 import dwt.dwthelper.utils;
16
17
18
19
20 import dwt.custom.StyledText;
21 import dwt.events.MouseEvent;
22 import dwt.events.MouseMoveListener;
23 import dwt.graphics.Point;
24 import dwt.graphics.Rectangle;
25 import dwt.widgets.Display;
26 import dwtx.core.runtime.ILog;
27 import dwtx.core.runtime.IStatus;
28 import dwtx.core.runtime.Platform;
29 import dwtx.core.runtime.Status;
30
31
32 /**
33 * This manager controls the layout, content, and visibility of an information
34 * control in reaction to mouse hover events issued by the text widget of a
35 * text viewer. It overrides <code>computeInformation</code>, so that the
36 * computation is performed in a dedicated background thread. This implies
37 * that the used <code>ITextHover</code> objects must be capable of
38 * operating in a non-UI thread.
39 *
40 * @since 2.0
41 */
42 class TextViewerHoverManager : AbstractHoverInformationControlManager , IWidgetTokenKeeper, IWidgetTokenKeeperExtension {
43
44
45 /**
46 * Priority of the hovers managed by this manager.
47 * Default value: <code>0</code>;
48 * @since 3.0
49 */
50 public final static int WIDGET_PRIORITY= 0;
51
52
53 /** The text viewer */
54 private TextViewer fTextViewer;
55 /** The hover information computation thread */
56 private Thread fThread;
57 /** The stopper of the computation thread */
58 private ITextListener fStopper;
59 /** Internal monitor */
60 private Object fMutex= new Object();
61 /** The currently shown text hover. */
62 private volatile ITextHover fTextHover;
63 /**
64 * Tells whether the next mouse hover event
65 * should be processed.
66 * @since 3.0
67 */
68 private bool fProcessMouseHoverEvent= true;
69 /**
70 * Internal mouse move listener.
71 * @since 3.0
72 */
73 private MouseMoveListener fMouseMoveListener;
74 /**
75 * Internal view port listener.
76 * @since 3.0
77 */
78 private IViewportListener fViewportListener;
79
80
81 /**
82 * Creates a new text viewer hover manager specific for the given text viewer.
83 * The manager uses the given information control creator.
84 *
85 * @param textViewer the viewer for which the controller is created
86 * @param creator the information control creator
87 */
88 public TextViewerHoverManager(TextViewer textViewer, IInformationControlCreator creator) {
89 super(creator);
90 fTextViewer= textViewer;
91 fStopper= new ITextListener() {
92 public void textChanged(TextEvent event) {
93 synchronized (fMutex) {
94 if (fThread !is null) {
95 fThread.interrupt();
96 fThread= null;
97 }
98 }
99 }
100 };
101 fViewportListener= new IViewportListener() {
102 /*
103 * @see dwtx.jface.text.IViewportListener#viewportChanged(int)
104 */
105 public void viewportChanged(int verticalOffset) {
106 fProcessMouseHoverEvent= false;
107 }
108 };
109 fTextViewer.addViewportListener(fViewportListener);
110 fMouseMoveListener= new MouseMoveListener() {
111 /*
112 * @see MouseMoveListener#mouseMove(MouseEvent)
113 */
114 public void mouseMove(MouseEvent event) {
115 fProcessMouseHoverEvent= true;
116 }
117 };
118 fTextViewer.getTextWidget().addMouseMoveListener(fMouseMoveListener);
119 }
120
121 /**
122 * Determines all necessary details and delegates the computation into
123 * a background thread.
124 */
125 protected void computeInformation() {
126
127 if (!fProcessMouseHoverEvent) {
128 setInformation(null, null);
129 return;
130 }
131
132 Point location= getHoverEventLocation();
133 int offset= computeOffsetAtLocation(location.x, location.y);
134 if (offset is -1) {
135 setInformation(null, null);
136 return;
137 }
138
139 final ITextHover hover= fTextViewer.getTextHover(offset, getHoverEventStateMask());
140 if (hover is null) {
141 setInformation(null, null);
142 return;
143 }
144
145 final IRegion region= hover.getHoverRegion(fTextViewer, offset);
146 if (region is null) {
147 setInformation(null, null);
148 return;
149 }
150
151 final Rectangle area= JFaceTextUtil.computeArea(region, fTextViewer);
152 if (area is null || area.isEmpty()) {
153 setInformation(null, null);
154 return;
155 }
156
157 if (fThread !is null) {
158 setInformation(null, null);
159 return;
160 }
161
162 fThread= new Thread("Text Viewer Hover Presenter") { //$NON-NLS-1$
163 public void run() {
164 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=17693
165 bool hasFinished= false;
166 try {
167 if (fThread !is null) {
168 Object information;
169 try {
170 if (hover instanceof ITextHoverExtension2)
171 information= ((ITextHoverExtension2)hover).getHoverInfo2(fTextViewer, region);
172 else
173 information= hover.getHoverInfo(fTextViewer, region);
174 } catch (ArrayIndexOutOfBoundsException x) {
175 /*
176 * This code runs in a separate thread which can
177 * lead to text offsets being out of bounds when
178 * computing the hover info (see bug 32848).
179 */
180 information= null;
181 }
182
183 if (hover instanceof ITextHoverExtension)
184 setCustomInformationControlCreator(((ITextHoverExtension) hover).getHoverControlCreator());
185 else
186 setCustomInformationControlCreator(null);
187
188 setInformation(information, area);
189 if (information !is null)
190 fTextHover= hover;
191 } else {
192 setInformation(null, null);
193 }
194 hasFinished= true;
195 } catch (RuntimeException ex) {
196 String PLUGIN_ID= "dwtx.jface.text"; //$NON-NLS-1$
197 ILog log= Platform.getLog(Platform.getBundle(PLUGIN_ID));
198 log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Unexpected runtime error while computing a text hover", ex)); //$NON-NLS-1$
199 } finally {
200 synchronized (fMutex) {
201 if (fTextViewer !is null)
202 fTextViewer.removeTextListener(fStopper);
203 fThread= null;
204 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=44756
205 if (!hasFinished)
206 setInformation(null, null);
207 }
208 }
209 }
210 };
211
212 fThread.setDaemon(true);
213 fThread.setPriority(Thread.MIN_PRIORITY);
214 synchronized (fMutex) {
215 fTextViewer.addTextListener(fStopper);
216 fThread.start();
217 }
218 }
219
220 /**
221 * As computation is done in the background, this method is
222 * also called in the background thread. Delegates the control
223 * flow back into the UI thread, in order to allow displaying the
224 * information in the information control.
225 */
226 protected void presentInformation() {
227 if (fTextViewer is null)
228 return;
229
230 StyledText textWidget= fTextViewer.getTextWidget();
231 if (textWidget !is null && !textWidget.isDisposed()) {
232 Display display= textWidget.getDisplay();
233 if (display is null)
234 return;
235
236 display.asyncExec(new Runnable() {
237 public void run() {
238 doPresentInformation();
239 }
240 });
241 }
242 }
243
244 /*
245 * @see AbstractInformationControlManager#presentInformation()
246 */
247 protected void doPresentInformation() {
248 super.presentInformation();
249 }
250
251 /**
252 * Computes the document offset underlying the given text widget coordinates.
253 * This method uses a linear search as it cannot make any assumption about
254 * how the document is actually presented in the widget. (Covers cases such
255 * as bidirectional text.)
256 *
257 * @param x the horizontal coordinate inside the text widget
258 * @param y the vertical coordinate inside the text widget
259 * @return the document offset corresponding to the given point
260 */
261 private int computeOffsetAtLocation(int x, int y) {
262
263 try {
264
265 StyledText styledText= fTextViewer.getTextWidget();
266 int widgetOffset= styledText.getOffsetAtLocation(new Point(x, y));
267 Point p= styledText.getLocationAtOffset(widgetOffset);
268 if (p.x > x)
269 widgetOffset--;
270
271 if (fTextViewer instanceof ITextViewerExtension5) {
272 ITextViewerExtension5 extension= (ITextViewerExtension5) fTextViewer;
273 return extension.widgetOffset2ModelOffset(widgetOffset);
274 }
275
276 return widgetOffset + fTextViewer._getVisibleRegionOffset();
277
278 } catch (IllegalArgumentException e) {
279 return -1;
280 }
281 }
282
283 /*
284 * @see dwtx.jface.text.AbstractInformationControlManager#showInformationControl(dwt.graphics.Rectangle)
285 */
286 protected void showInformationControl(Rectangle subjectArea) {
287 if (fTextViewer !is null && fTextViewer.requestWidgetToken(this, WIDGET_PRIORITY))
288 super.showInformationControl(subjectArea);
289 else
290 if (DEBUG)
291 System.out.println("TextViewerHoverManager#showInformationControl(..) did not get widget token"); //$NON-NLS-1$
292 }
293
294 /*
295 * @see dwtx.jface.text.AbstractInformationControlManager#hideInformationControl()
296 */
297 protected void hideInformationControl() {
298 try {
299 fTextHover= null;
300 super.hideInformationControl();
301 } finally {
302 if (fTextViewer !is null)
303 fTextViewer.releaseWidgetToken(this);
304 }
305 }
306
307 /*
308 * @see dwtx.jface.text.AbstractInformationControlManager#replaceInformationControl(bool)
309 * @since 3.4
310 */
311 void replaceInformationControl(bool takeFocus) {
312 if (fTextViewer !is null)
313 fTextViewer.releaseWidgetToken(this);
314 super.replaceInformationControl(takeFocus);
315 }
316
317 /*
318 * @see dwtx.jface.text.AbstractInformationControlManager#handleInformationControlDisposed()
319 */
320 protected void handleInformationControlDisposed() {
321 try {
322 super.handleInformationControlDisposed();
323 } finally {
324 if (fTextViewer !is null)
325 fTextViewer.releaseWidgetToken(this);
326 }
327 }
328
329 /*
330 * @see dwtx.jface.text.IWidgetTokenKeeper#requestWidgetToken(dwtx.jface.text.IWidgetTokenOwner)
331 */
332 public bool requestWidgetToken(IWidgetTokenOwner owner) {
333 fTextHover= null;
334 super.hideInformationControl();
335 return true;
336 }
337
338 /*
339 * @see dwtx.jface.text.IWidgetTokenKeeperExtension#requestWidgetToken(dwtx.jface.text.IWidgetTokenOwner, int)
340 * @since 3.0
341 */
342 public bool requestWidgetToken(IWidgetTokenOwner owner, int priority) {
343 if (priority > WIDGET_PRIORITY) {
344 fTextHover= null;
345 super.hideInformationControl();
346 return true;
347 }
348 return false;
349 }
350
351 /*
352 * @see dwtx.jface.text.IWidgetTokenKeeperExtension#setFocus(dwtx.jface.text.IWidgetTokenOwner)
353 * @since 3.0
354 */
355 public bool setFocus(IWidgetTokenOwner owner) {
356 if (! hasInformationControlReplacer())
357 return false;
358
359 IInformationControl iControl= getCurrentInformationControl();
360 if (canReplace(iControl)) {
361 if (cancelReplacingDelay())
362 replaceInformationControl(true);
363
364 return true;
365 }
366
367 return false;
368 }
369
370 /**
371 * Returns the currently shown text hover or <code>null</code> if no text
372 * hover is shown.
373 *
374 * @return the currently shown text hover or <code>null</code>
375 */
376 protected ITextHover getCurrentTextHover() {
377 return fTextHover;
378 }
379
380 /*
381 * @see dwtx.jface.text.AbstractHoverInformationControlManager#dispose()
382 * @since 3.0
383 */
384 public void dispose() {
385 if (fTextViewer !is null) {
386 fTextViewer.removeViewportListener(fViewportListener);
387 fViewportListener= null;
388
389 StyledText st= fTextViewer.getTextWidget();
390 if (st !is null && !st.isDisposed())
391 st.removeMouseMoveListener(fMouseMoveListener);
392 fMouseMoveListener= null;
393 }
394 super.dispose();
395 }
396 }