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.contentassist.AdditionalInfoController;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20 import dwt.events.SelectionEvent;
|
|
21 import dwt.events.SelectionListener;
|
|
22 import dwt.graphics.Point;
|
|
23 import dwt.graphics.Rectangle;
|
|
24 import dwt.widgets.Control;
|
|
25 import dwt.widgets.Display;
|
|
26 import dwt.widgets.Shell;
|
|
27 import dwt.widgets.Table;
|
|
28 import dwt.widgets.TableItem;
|
|
29 import dwtx.core.runtime.Assert;
|
|
30 import dwtx.core.runtime.IProgressMonitor;
|
|
31 import dwtx.core.runtime.IStatus;
|
|
32 import dwtx.core.runtime.Status;
|
|
33 import dwtx.core.runtime.jobs.Job;
|
|
34 import dwtx.jface.internal.text.InformationControlReplacer;
|
|
35 import dwtx.jface.text.AbstractInformationControlManager;
|
|
36 import dwtx.jface.text.AbstractReusableInformationControlCreator;
|
|
37 import dwtx.jface.text.DefaultInformationControl;
|
|
38 import dwtx.jface.text.IInformationControl;
|
|
39 import dwtx.jface.text.IInformationControlCreator;
|
|
40 import dwtx.jface.text.IInformationControlExtension3;
|
|
41
|
|
42
|
|
43 /**
|
|
44 * Displays the additional information available for a completion proposal.
|
|
45 *
|
|
46 * @since 2.0
|
|
47 */
|
|
48 class AdditionalInfoController : AbstractInformationControlManager {
|
|
49
|
|
50 /**
|
|
51 * A timer thread.
|
|
52 *
|
|
53 * @since 3.2
|
|
54 */
|
|
55 private static abstract class Timer {
|
|
56 private static final int DELAY_UNTIL_JOB_IS_SCHEDULED= 50;
|
|
57
|
|
58 /**
|
|
59 * A <code>Task</code> is {@link Task#run() run} when {@link #delay()} milliseconds have
|
|
60 * elapsed after it was scheduled without a {@link #reset(ICompletionProposal) reset}
|
|
61 * to occur.
|
|
62 */
|
|
63 private abstract class Task : Runnable {
|
|
64 /**
|
|
65 * @return the delay in milliseconds before this task should be run
|
|
66 */
|
|
67 public abstract long delay();
|
|
68 /**
|
|
69 * Runs this task.
|
|
70 */
|
|
71 public abstract void run();
|
|
72 /**
|
|
73 * @return the task to be scheduled after this task has been run
|
|
74 */
|
|
75 public abstract Task nextTask();
|
|
76 }
|
|
77
|
|
78 /**
|
|
79 * IDLE: the initial task, and active whenever the info has been shown. It cannot be run,
|
|
80 * but specifies an infinite delay.
|
|
81 */
|
|
82 private final Task IDLE= new Task() {
|
|
83 public void run() {
|
|
84 Assert.isTrue(false);
|
|
85 }
|
|
86
|
|
87 public Task nextTask() {
|
|
88 Assert.isTrue(false);
|
|
89 return null;
|
|
90 }
|
|
91
|
|
92 public long delay() {
|
|
93 return Long.MAX_VALUE;
|
|
94 }
|
|
95
|
|
96 public String toString() {
|
|
97 return "IDLE"; //$NON-NLS-1$
|
|
98 }
|
|
99 };
|
|
100 /**
|
|
101 * FIRST_WAIT: Schedules a platform {@link Job} to fetch additional info from an {@link ICompletionProposalExtension5}.
|
|
102 */
|
|
103 private final Task FIRST_WAIT= new Task() {
|
|
104 public void run() {
|
|
105 final ICompletionProposalExtension5 proposal= getCurrentProposalEx();
|
|
106 Job job= new Job(JFaceTextMessages.getString("AdditionalInfoController.job_name")) { //$NON-NLS-1$
|
|
107 protected IStatus run(IProgressMonitor monitor) {
|
|
108 Object info;
|
|
109 try {
|
|
110 info= proposal.getAdditionalProposalInfo(monitor);
|
|
111 } catch (RuntimeException x) {
|
|
112 /*
|
|
113 * XXX: This is the safest fix at this point so close to end of 3.2.
|
|
114 * Will be revisited when fixing https://bugs.eclipse.org/bugs/show_bug.cgi?id=101033
|
|
115 */
|
|
116 return new Status(IStatus.WARNING, "dwtx.jface.text", IStatus.OK, "", x); //$NON-NLS-1$ //$NON-NLS-2$
|
|
117 }
|
|
118 setInfo((ICompletionProposal) proposal, info);
|
|
119 return new Status(IStatus.OK, "dwtx.jface.text", IStatus.OK, "", null); //$NON-NLS-1$ //$NON-NLS-2$
|
|
120 }
|
|
121 };
|
|
122 job.schedule();
|
|
123 }
|
|
124
|
|
125 public Task nextTask() {
|
|
126 return SECOND_WAIT;
|
|
127 }
|
|
128
|
|
129 public long delay() {
|
|
130 return DELAY_UNTIL_JOB_IS_SCHEDULED;
|
|
131 }
|
|
132
|
|
133 public String toString() {
|
|
134 return "FIRST_WAIT"; //$NON-NLS-1$
|
|
135 }
|
|
136 };
|
|
137 /**
|
|
138 * SECOND_WAIT: Allows display of additional info obtained from an
|
|
139 * {@link ICompletionProposalExtension5}.
|
|
140 */
|
|
141 private final Task SECOND_WAIT= new Task() {
|
|
142 public void run() {
|
|
143 // show the info
|
|
144 allowShowing();
|
|
145 }
|
|
146
|
|
147 public Task nextTask() {
|
|
148 return IDLE;
|
|
149 }
|
|
150
|
|
151 public long delay() {
|
|
152 return fDelay - DELAY_UNTIL_JOB_IS_SCHEDULED;
|
|
153 }
|
|
154
|
|
155 public String toString() {
|
|
156 return "SECOND_WAIT"; //$NON-NLS-1$
|
|
157 }
|
|
158 };
|
|
159 /**
|
|
160 * LEGACY_WAIT: Posts a runnable into the display thread to fetch additional info from non-{@link ICompletionProposalExtension5}s.
|
|
161 */
|
|
162 private final Task LEGACY_WAIT= new Task() {
|
|
163 public void run() {
|
|
164 final ICompletionProposal proposal= getCurrentProposal();
|
|
165 if (!fDisplay.isDisposed()) {
|
|
166 fDisplay.asyncExec(new Runnable() {
|
|
167 public void run() {
|
|
168 synchronized (Timer.this) {
|
|
169 if (proposal is getCurrentProposal()) {
|
|
170 Object info= proposal.getAdditionalProposalInfo();
|
|
171 showInformation(proposal, info);
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175 });
|
|
176 }
|
|
177 }
|
|
178
|
|
179 public Task nextTask() {
|
|
180 return IDLE;
|
|
181 }
|
|
182
|
|
183 public long delay() {
|
|
184 return fDelay;
|
|
185 }
|
|
186
|
|
187 public String toString() {
|
|
188 return "LEGACY_WAIT"; //$NON-NLS-1$
|
|
189 }
|
|
190 };
|
|
191 /**
|
|
192 * EXIT: The task that triggers termination of the timer thread.
|
|
193 */
|
|
194 private final Task EXIT= new Task() {
|
|
195 public long delay() {
|
|
196 return 1;
|
|
197 }
|
|
198
|
|
199 public Task nextTask() {
|
|
200 Assert.isTrue(false);
|
|
201 return EXIT;
|
|
202 }
|
|
203
|
|
204 public void run() {
|
|
205 Assert.isTrue(false);
|
|
206 }
|
|
207
|
|
208 public String toString() {
|
|
209 return "EXIT"; //$NON-NLS-1$
|
|
210 }
|
|
211 };
|
|
212
|
|
213 /** The timer thread. */
|
|
214 private final Thread fThread;
|
|
215
|
|
216 /** The currently waiting / active task. */
|
|
217 private Task fTask;
|
|
218 /** The next wake up time. */
|
|
219 private long fNextWakeup;
|
|
220
|
|
221 private ICompletionProposal fCurrentProposal= null;
|
|
222 private Object fCurrentInfo= null;
|
|
223 private bool fAllowShowing= false;
|
|
224
|
|
225 private final Display fDisplay;
|
|
226 private final int fDelay;
|
|
227
|
|
228 /**
|
|
229 * Creates a new timer.
|
|
230 *
|
|
231 * @param display the display to use for display thread posting.
|
|
232 * @param delay the delay until to show additional info
|
|
233 */
|
|
234 public Timer(Display display, int delay) {
|
|
235 fDisplay= display;
|
|
236 fDelay= delay;
|
|
237 long current= System.currentTimeMillis();
|
|
238 schedule(IDLE, current);
|
|
239
|
|
240 fThread= new Thread(new Runnable() {
|
|
241 public void run() {
|
|
242 try {
|
|
243 loop();
|
|
244 } catch (InterruptedException x) {
|
|
245 }
|
|
246 }
|
|
247 }, JFaceTextMessages.getString("InfoPopup.info_delay_timer_name")); //$NON-NLS-1$
|
|
248 fThread.start();
|
|
249 }
|
|
250
|
|
251 /**
|
|
252 * Terminates the timer thread.
|
|
253 */
|
|
254 public synchronized final void terminate() {
|
|
255 schedule(EXIT, System.currentTimeMillis());
|
|
256 notifyAll();
|
|
257 }
|
|
258
|
|
259 /**
|
|
260 * Resets the timer thread as the selection has changed to a new proposal.
|
|
261 *
|
|
262 * @param p the new proposal
|
|
263 */
|
|
264 public final synchronized void reset(ICompletionProposal p) {
|
|
265 if (fCurrentProposal !is p) {
|
|
266 fCurrentProposal= p;
|
|
267 fCurrentInfo= null;
|
|
268 fAllowShowing= false;
|
|
269
|
|
270 long oldWakeup= fNextWakeup;
|
|
271 Task task= taskOnReset(p);
|
|
272 schedule(task, System.currentTimeMillis());
|
|
273 if (fNextWakeup < oldWakeup)
|
|
274 notifyAll();
|
|
275 }
|
|
276 }
|
|
277
|
|
278 private Task taskOnReset(ICompletionProposal p) {
|
|
279 if (p is null)
|
|
280 return IDLE;
|
|
281 if (isExt5(p))
|
|
282 return FIRST_WAIT;
|
|
283 return LEGACY_WAIT;
|
|
284 }
|
|
285
|
|
286 private synchronized void loop() throws InterruptedException {
|
|
287 long current= System.currentTimeMillis();
|
|
288 Task task= currentTask();
|
|
289
|
|
290 while (task !is EXIT) {
|
|
291 long delay= fNextWakeup - current;
|
|
292 if (delay <= 0) {
|
|
293 task.run();
|
|
294 task= task.nextTask();
|
|
295 schedule(task, current);
|
|
296 } else {
|
|
297 wait(delay);
|
|
298 current= System.currentTimeMillis();
|
|
299 task= currentTask();
|
|
300 }
|
|
301 }
|
|
302 }
|
|
303
|
|
304 private Task currentTask() {
|
|
305 return fTask;
|
|
306 }
|
|
307
|
|
308 private void schedule(Task task, long current) {
|
|
309 fTask= task;
|
|
310 long nextWakeup= current + task.delay();
|
|
311 if (nextWakeup <= current)
|
|
312 fNextWakeup= Long.MAX_VALUE;
|
|
313 else
|
|
314 fNextWakeup= nextWakeup;
|
|
315 }
|
|
316
|
|
317 private bool isExt5(ICompletionProposal p) {
|
|
318 return p instanceof ICompletionProposalExtension5;
|
|
319 }
|
|
320
|
|
321 ICompletionProposal getCurrentProposal() {
|
|
322 return fCurrentProposal;
|
|
323 }
|
|
324
|
|
325 ICompletionProposalExtension5 getCurrentProposalEx() {
|
|
326 Assert.isTrue(fCurrentProposal instanceof ICompletionProposalExtension5);
|
|
327 return (ICompletionProposalExtension5) fCurrentProposal;
|
|
328 }
|
|
329
|
|
330 synchronized void setInfo(ICompletionProposal proposal, Object info) {
|
|
331 if (proposal is fCurrentProposal) {
|
|
332 fCurrentInfo= info;
|
|
333 if (fAllowShowing) {
|
|
334 triggerShowing();
|
|
335 }
|
|
336 }
|
|
337 }
|
|
338
|
|
339 private void triggerShowing() {
|
|
340 final Object info= fCurrentInfo;
|
|
341 if (!fDisplay.isDisposed()) {
|
|
342 fDisplay.asyncExec(new Runnable() {
|
|
343 public void run() {
|
|
344 synchronized (Timer.this) {
|
|
345 if (info is fCurrentInfo) {
|
|
346 showInformation(fCurrentProposal, info);
|
|
347 }
|
|
348 }
|
|
349 }
|
|
350 });
|
|
351 }
|
|
352 }
|
|
353
|
|
354 /**
|
|
355 * Called in the display thread to show additional info.
|
|
356 *
|
|
357 * @param proposal the proposal to show information about
|
|
358 * @param info the information about <code>proposal</code>
|
|
359 */
|
|
360 protected abstract void showInformation(ICompletionProposal proposal, Object info);
|
|
361
|
|
362 void allowShowing() {
|
|
363 fAllowShowing= true;
|
|
364 triggerShowing();
|
|
365 }
|
|
366 }
|
|
367 /**
|
|
368 * Internal table selection listener.
|
|
369 */
|
|
370 private class TableSelectionListener : SelectionListener {
|
|
371
|
|
372 /*
|
|
373 * @see SelectionListener#widgetSelected(SelectionEvent)
|
|
374 */
|
|
375 public void widgetSelected(SelectionEvent e) {
|
|
376 handleTableSelectionChanged();
|
|
377 }
|
|
378
|
|
379 /*
|
|
380 * @see SelectionListener#widgetDefaultSelected(SelectionEvent)
|
|
381 */
|
|
382 public void widgetDefaultSelected(SelectionEvent e) {
|
|
383 }
|
|
384 }
|
|
385
|
|
386 /**
|
|
387 * Default control creator for the information control replacer.
|
|
388 * @since 3.4
|
|
389 */
|
|
390 private static class DefaultPresenterControlCreator : AbstractReusableInformationControlCreator {
|
|
391 public IInformationControl doCreateInformationControl(Shell shell) {
|
|
392 return new DefaultInformationControl(shell, true);
|
|
393 }
|
|
394 }
|
|
395
|
|
396 /** The proposal table. */
|
|
397 private Table fProposalTable;
|
|
398 /** The table selection listener */
|
|
399 private SelectionListener fSelectionListener= new TableSelectionListener();
|
|
400 /** The delay after which additional information is displayed */
|
|
401 private final int fDelay;
|
|
402 /**
|
|
403 * The timer thread.
|
|
404 * @since 3.2
|
|
405 */
|
|
406 private Timer fTimer;
|
|
407 /**
|
|
408 * The proposal most recently set by {@link #showInformation(ICompletionProposal, Object)},
|
|
409 * possibly <code>null</code>.
|
|
410 * @since 3.2
|
|
411 */
|
|
412 private ICompletionProposal fProposal;
|
|
413 /**
|
|
414 * The information most recently set by {@link #showInformation(ICompletionProposal, Object)},
|
|
415 * possibly <code>null</code>.
|
|
416 * @since 3.2
|
|
417 */
|
|
418 private Object fInformation;
|
|
419
|
|
420 /**
|
|
421 * Creates a new additional information controller.
|
|
422 *
|
|
423 * @param creator the information control creator to be used by this controller
|
|
424 * @param delay time in milliseconds after which additional info should be displayed
|
|
425 */
|
|
426 AdditionalInfoController(IInformationControlCreator creator, int delay) {
|
|
427 super(creator);
|
|
428 fDelay= delay;
|
|
429 setAnchor(ANCHOR_RIGHT);
|
|
430 setFallbackAnchors(new Anchor[] { ANCHOR_RIGHT, ANCHOR_LEFT, ANCHOR_BOTTOM });
|
|
431
|
|
432 /*
|
|
433 * Adjust the location by one pixel towards the proposal popup, so that the single pixel
|
|
434 * border of the additional info popup overlays with the border of the popup. This avoids
|
|
435 * having a double black line.
|
|
436 */
|
|
437 int spacing= -1;
|
|
438 setMargins(spacing, spacing); // see also adjustment in #computeLocation
|
|
439
|
|
440 InformationControlReplacer replacer= new InformationControlReplacer(new DefaultPresenterControlCreator());
|
|
441 getInternalAccessor().setInformationControlReplacer(replacer);
|
|
442 }
|
|
443
|
|
444 /*
|
|
445 * @see AbstractInformationControlManager#install(Control)
|
|
446 */
|
|
447 public void install(Control control) {
|
|
448
|
|
449 if (fProposalTable is control) {
|
|
450 // already installed
|
|
451 return;
|
|
452 }
|
|
453
|
|
454 super.install(control.getShell());
|
|
455
|
|
456 Assert.isTrue(control instanceof Table);
|
|
457 fProposalTable= (Table) control;
|
|
458 fProposalTable.addSelectionListener(fSelectionListener);
|
|
459 getInternalAccessor().getInformationControlReplacer().install(fProposalTable);
|
|
460
|
|
461 fTimer= new Timer(fProposalTable.getDisplay(), fDelay) {
|
|
462 protected void showInformation(ICompletionProposal proposal, Object info) {
|
|
463 InformationControlReplacer replacer= getInternalAccessor().getInformationControlReplacer();
|
|
464 if (replacer !is null)
|
|
465 replacer.hideInformationControl();
|
|
466 AdditionalInfoController.this.showInformation(proposal, info);
|
|
467 }
|
|
468 };
|
|
469 }
|
|
470
|
|
471 /*
|
|
472 * @see AbstractInformationControlManager#disposeInformationControl()
|
|
473 */
|
|
474 public void disposeInformationControl() {
|
|
475
|
|
476 if (fTimer !is null) {
|
|
477 fTimer.terminate();
|
|
478 fTimer= null;
|
|
479 }
|
|
480
|
|
481 fProposal= null;
|
|
482 fInformation= null;
|
|
483
|
|
484 if (fProposalTable !is null && !fProposalTable.isDisposed()) {
|
|
485 fProposalTable.removeSelectionListener(fSelectionListener);
|
|
486 fProposalTable= null;
|
|
487 }
|
|
488
|
|
489 super.disposeInformationControl();
|
|
490 }
|
|
491
|
|
492 /**
|
|
493 *Handles a change of the line selected in the associated selector.
|
|
494 */
|
|
495 public void handleTableSelectionChanged() {
|
|
496
|
|
497 if (fProposalTable !is null && !fProposalTable.isDisposed() && fProposalTable.isVisible()) {
|
|
498 TableItem[] selection= fProposalTable.getSelection();
|
|
499 if (selection !is null && selection.length > 0) {
|
|
500
|
|
501 TableItem item= selection[0];
|
|
502
|
|
503 Object d= item.getData();
|
|
504 if (d instanceof ICompletionProposal) {
|
|
505 ICompletionProposal p= (ICompletionProposal) d;
|
|
506 fTimer.reset(p);
|
|
507 }
|
|
508 }
|
|
509 }
|
|
510 }
|
|
511
|
|
512 void showInformation(ICompletionProposal proposal, Object info) {
|
|
513 if (fProposalTable is null || fProposalTable.isDisposed())
|
|
514 return;
|
|
515
|
|
516 if (fProposal is proposal && ((info is null && fInformation is null) || (info !is null && info.equals(fInformation))))
|
|
517 return;
|
|
518
|
|
519 fInformation= info;
|
|
520 fProposal= proposal;
|
|
521 showInformation();
|
|
522 }
|
|
523
|
|
524 /*
|
|
525 * @see AbstractInformationControlManager#computeInformation()
|
|
526 */
|
|
527 protected void computeInformation() {
|
|
528 if (fProposal instanceof ICompletionProposalExtension3)
|
|
529 setCustomInformationControlCreator(((ICompletionProposalExtension3) fProposal).getInformationControlCreator());
|
|
530 else
|
|
531 setCustomInformationControlCreator(null);
|
|
532
|
|
533 // compute subject area
|
|
534 Point size= fProposalTable.getShell().getSize();
|
|
535
|
|
536 // set information & subject area
|
|
537 setInformation(fInformation, new Rectangle(0, 0, size.x, size.y));
|
|
538 }
|
|
539
|
|
540 /*
|
|
541 * @see dwtx.jface.text.AbstractInformationControlManager#computeLocation(dwt.graphics.Rectangle, dwt.graphics.Point, dwtx.jface.text.AbstractInformationControlManager.Anchor)
|
|
542 */
|
|
543 protected Point computeLocation(Rectangle subjectArea, Point controlSize, Anchor anchor) {
|
|
544 Point location= super.computeLocation(subjectArea, controlSize, anchor);
|
|
545
|
|
546 /*
|
|
547 * The location is computed using subjectControl.toDisplay(), which does not include the
|
|
548 * trim of the subject control. As we want the additional info popup aligned with the outer
|
|
549 * coordinates of the proposal popup, adjust this here
|
|
550 */
|
|
551 Rectangle trim= fProposalTable.getShell().computeTrim(0, 0, 0, 0);
|
|
552 location.x += trim.x;
|
|
553 location.y += trim.y;
|
|
554
|
|
555 return location;
|
|
556 }
|
|
557
|
|
558 /*
|
|
559 * @see dwtx.jface.text.AbstractInformationControlManager#computeSizeConstraints(Control, IInformationControl)
|
|
560 */
|
|
561 protected Point computeSizeConstraints(Control subjectControl, IInformationControl informationControl) {
|
|
562 // at least as big as the proposal table
|
|
563 Point sizeConstraint= super.computeSizeConstraints(subjectControl, informationControl);
|
|
564 Point size= subjectControl.getShell().getSize();
|
|
565
|
|
566 // AbstractInformationControlManager#internalShowInformationControl(Rectangle, Object) adds trims
|
|
567 // to the computed constraints. Need to remove them here, to make the outer bounds of the additional
|
|
568 // info shell fit the bounds of the proposal shell:
|
|
569 if (fInformationControl instanceof IInformationControlExtension3) {
|
|
570 Rectangle shellTrim= ((IInformationControlExtension3) fInformationControl).computeTrim();
|
|
571 size.x -= shellTrim.width;
|
|
572 size.y -= shellTrim.height;
|
|
573 }
|
|
574
|
|
575 if (sizeConstraint.x < size.x)
|
|
576 sizeConstraint.x= size.x;
|
|
577 if (sizeConstraint.y < size.y)
|
|
578 sizeConstraint.y= size.y;
|
|
579 return sizeConstraint;
|
|
580 }
|
|
581
|
|
582 /*
|
|
583 * @see dwtx.jface.text.AbstractInformationControlManager#hideInformationControl()
|
|
584 */
|
|
585 protected void hideInformationControl() {
|
|
586 super.hideInformationControl();
|
|
587 }
|
|
588
|
|
589 /**
|
|
590 * @return the current information control, or <code>null</code> if none available
|
|
591 */
|
|
592 public IInformationControl getCurrentInformationControl2() {
|
|
593 return getInternalAccessor().getCurrentInformationControl();
|
|
594 }
|
|
595 }
|
|
596
|
|
597
|