Mercurial > projects > dwt-addons
comparison dwtx/jface/internal/text/link/contentassist/AdditionalInfoController2.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | b56e9be9fe88 |
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 | |
14 module dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2; | |
15 | |
16 import dwt.dwthelper.utils; | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 import dwt.events.SelectionEvent; | |
23 import dwt.events.SelectionListener; | |
24 import dwt.graphics.Point; | |
25 import dwt.graphics.Rectangle; | |
26 import dwt.widgets.Control; | |
27 import dwt.widgets.Table; | |
28 import dwt.widgets.TableItem; | |
29 import dwtx.core.runtime.Assert; | |
30 import dwtx.jface.text.AbstractInformationControlManager; | |
31 import dwtx.jface.text.IInformationControl; | |
32 import dwtx.jface.text.IInformationControlCreator; | |
33 import dwtx.jface.text.contentassist.ICompletionProposal; | |
34 import dwtx.jface.text.contentassist.ICompletionProposalExtension3; | |
35 | |
36 | |
37 | |
38 /** | |
39 * Displays the additional information available for a completion proposal. | |
40 * | |
41 * @since 2.0 | |
42 */ | |
43 class AdditionalInfoController2 : AbstractInformationControlManager , Runnable { | |
44 | |
45 /** | |
46 * Internal table selection listener. | |
47 */ | |
48 private class TableSelectionListener : SelectionListener { | |
49 | |
50 /* | |
51 * @see SelectionListener#widgetSelected(SelectionEvent) | |
52 */ | |
53 public void widgetSelected(SelectionEvent e) { | |
54 handleTableSelectionChanged(); | |
55 } | |
56 | |
57 /* | |
58 * @see SelectionListener#widgetDefaultSelected(SelectionEvent) | |
59 */ | |
60 public void widgetDefaultSelected(SelectionEvent e) { | |
61 } | |
62 } | |
63 | |
64 | |
65 /** The proposal table */ | |
66 private Table fProposalTable; | |
67 /** The thread controlling the delayed display of the additional info */ | |
68 private Thread fThread; | |
69 /** Indicates whether the display delay has been reset */ | |
70 private bool fIsReset= false; | |
71 /** Object to synchronize display thread and table selection changes */ | |
72 private final Object fMutex= new Object(); | |
73 /** Thread access lock. */ | |
74 private final Object fThreadAccess= new Object(); | |
75 /** Object to synchronize initial display of additional info */ | |
76 private Object fStartSignal; | |
77 /** The table selection listener */ | |
78 private SelectionListener fSelectionListener= new TableSelectionListener(); | |
79 /** The delay after which additional information is displayed */ | |
80 private int fDelay; | |
81 | |
82 | |
83 /** | |
84 * Creates a new additional information controller. | |
85 * | |
86 * @param creator the information control creator to be used by this controller | |
87 * @param delay time in milliseconds after which additional info should be displayed | |
88 */ | |
89 AdditionalInfoController2(IInformationControlCreator creator, int delay) { | |
90 super(creator); | |
91 fDelay= delay; | |
92 setAnchor(ANCHOR_RIGHT); | |
93 setFallbackAnchors(new Anchor[] {ANCHOR_RIGHT, ANCHOR_LEFT, ANCHOR_BOTTOM }); | |
94 } | |
95 | |
96 /* | |
97 * @see AbstractInformationControlManager#install(Control) | |
98 */ | |
99 public void install(Control control) { | |
100 | |
101 if (fProposalTable is control) { | |
102 // already installed | |
103 return; | |
104 } | |
105 | |
106 super.install(control); | |
107 | |
108 Assert.isTrue(control instanceof Table); | |
109 fProposalTable= (Table) control; | |
110 fProposalTable.addSelectionListener(fSelectionListener); | |
111 synchronized (fThreadAccess) { | |
112 if (fThread !is null) | |
113 fThread.interrupt(); | |
114 fThread= new Thread(this, ContentAssistMessages.getString("InfoPopup.info_delay_timer_name")); //$NON-NLS-1$ | |
115 | |
116 fStartSignal= new Object(); | |
117 synchronized (fStartSignal) { | |
118 fThread.start(); | |
119 try { | |
120 // wait until thread is ready | |
121 fStartSignal.wait(); | |
122 } catch (InterruptedException x) { | |
123 } | |
124 } | |
125 } | |
126 } | |
127 | |
128 /* | |
129 * @see AbstractInformationControlManager#disposeInformationControl() | |
130 */ | |
131 public void disposeInformationControl() { | |
132 | |
133 synchronized (fThreadAccess) { | |
134 if (fThread !is null) { | |
135 fThread.interrupt(); | |
136 fThread= null; | |
137 } | |
138 } | |
139 | |
140 if (fProposalTable !is null && !fProposalTable.isDisposed()) { | |
141 fProposalTable.removeSelectionListener(fSelectionListener); | |
142 fProposalTable= null; | |
143 } | |
144 | |
145 super.disposeInformationControl(); | |
146 } | |
147 | |
148 /* | |
149 * @see java.lang.Runnable#run() | |
150 */ | |
151 public void run() { | |
152 try { | |
153 while (true) { | |
154 | |
155 synchronized (fMutex) { | |
156 | |
157 if (fStartSignal !is null) { | |
158 synchronized (fStartSignal) { | |
159 fStartSignal.notifyAll(); | |
160 fStartSignal= null; | |
161 } | |
162 } | |
163 | |
164 // Wait for a selection event to occur. | |
165 fMutex.wait(); | |
166 | |
167 while (true) { | |
168 fIsReset= false; | |
169 // Delay before showing the popup. | |
170 fMutex.wait(fDelay); | |
171 if (!fIsReset) | |
172 break; | |
173 } | |
174 } | |
175 | |
176 if (fProposalTable !is null && !fProposalTable.isDisposed()) { | |
177 fProposalTable.getDisplay().asyncExec(new Runnable() { | |
178 public void run() { | |
179 if (!fIsReset) | |
180 showInformation(); | |
181 } | |
182 }); | |
183 } | |
184 | |
185 } | |
186 } catch (InterruptedException e) { | |
187 } | |
188 | |
189 synchronized (fThreadAccess) { | |
190 // only null fThread if it is us! | |
191 if (Thread.currentThread() is fThread) | |
192 fThread= null; | |
193 } | |
194 } | |
195 | |
196 /** | |
197 *Handles a change of the line selected in the associated selector. | |
198 */ | |
199 public void handleTableSelectionChanged() { | |
200 | |
201 if (fProposalTable !is null && !fProposalTable.isDisposed() && fProposalTable.isVisible()) { | |
202 synchronized (fMutex) { | |
203 fIsReset= true; | |
204 fMutex.notifyAll(); | |
205 } | |
206 } | |
207 } | |
208 | |
209 /* | |
210 * @see AbstractInformationControlManager#computeInformation() | |
211 */ | |
212 protected void computeInformation() { | |
213 | |
214 if (fProposalTable is null || fProposalTable.isDisposed()) | |
215 return; | |
216 | |
217 TableItem[] selection= fProposalTable.getSelection(); | |
218 if (selection !is null && selection.length > 0) { | |
219 | |
220 TableItem item= selection[0]; | |
221 | |
222 // compute information | |
223 String information= null; | |
224 Object d= item.getData(); | |
225 | |
226 if (d instanceof ICompletionProposal) { | |
227 ICompletionProposal p= (ICompletionProposal) d; | |
228 information= p.getAdditionalProposalInfo(); | |
229 } | |
230 | |
231 if (d instanceof ICompletionProposalExtension3) | |
232 setCustomInformationControlCreator(((ICompletionProposalExtension3) d).getInformationControlCreator()); | |
233 else | |
234 setCustomInformationControlCreator(null); | |
235 | |
236 // compute subject area | |
237 setMargins(4, -1); | |
238 Rectangle area= fProposalTable.getBounds(); | |
239 area.x= 0; // subject area is the whole subject control | |
240 area.y= 0; | |
241 | |
242 // set information & subject area | |
243 setInformation(information, area); | |
244 } | |
245 } | |
246 | |
247 /* | |
248 * @see dwtx.jface.text.AbstractInformationControlManager#computeSizeConstraints(Control, IInformationControl) | |
249 */ | |
250 protected Point computeSizeConstraints(Control subjectControl, IInformationControl informationControl) { | |
251 // at least as big as the proposal table | |
252 Point sizeConstraint= super.computeSizeConstraints(subjectControl, informationControl); | |
253 Point size= subjectControl.getSize(); | |
254 if (sizeConstraint.x < size.x) | |
255 sizeConstraint.x= size.x; | |
256 if (sizeConstraint.y < size.y) | |
257 sizeConstraint.y= size.y; | |
258 return sizeConstraint; | |
259 } | |
260 } | |
261 | |
262 |