Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/link/contentassist/CompletionProposalPopup2.d @ 159:7926b636c282
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 01:57:58 +0200 |
parents | 25f1f92fa3df |
children | 1a5b8f8129df |
rev | line source |
---|---|
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 * Sean Montgomery, sean_montgomery@comcast.net - https://bugs.eclipse.org/bugs/show_bug.cgi?id=116454 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.internal.text.link.contentassist.CompletionProposalPopup2; | |
15 | |
131 | 16 import dwtx.jface.internal.text.link.contentassist.IProposalListener; // packageimport |
17 import dwtx.jface.internal.text.link.contentassist.LineBreakingReader; // packageimport | |
18 import dwtx.jface.internal.text.link.contentassist.ContextInformationPopup2; // packageimport | |
19 import dwtx.jface.internal.text.link.contentassist.ContentAssistMessages; // packageimport | |
20 import dwtx.jface.internal.text.link.contentassist.Helper2; // packageimport | |
21 import dwtx.jface.internal.text.link.contentassist.PopupCloser2; // packageimport | |
22 import dwtx.jface.internal.text.link.contentassist.IContentAssistListener2; // packageimport | |
23 import dwtx.jface.internal.text.link.contentassist.ContentAssistant2; // packageimport | |
24 import dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2; // packageimport | |
25 | |
26 | |
129 | 27 import dwt.dwthelper.utils; |
28 | |
29 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
30 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
150
diff
changeset
|
31 |
129 | 32 |
33 import dwt.DWT; | |
34 import dwt.custom.StyleRange; | |
35 import dwt.custom.StyledText; | |
36 import dwt.events.ControlEvent; | |
37 import dwt.events.ControlListener; | |
38 import dwt.events.DisposeEvent; | |
39 import dwt.events.DisposeListener; | |
40 import dwt.events.KeyEvent; | |
41 import dwt.events.KeyListener; | |
42 import dwt.events.SelectionEvent; | |
43 import dwt.events.SelectionListener; | |
44 import dwt.events.VerifyEvent; | |
45 import dwt.graphics.Color; | |
46 import dwt.graphics.Point; | |
47 import dwt.layout.GridData; | |
48 import dwt.layout.GridLayout; | |
49 import dwt.widgets.Control; | |
50 import dwt.widgets.Shell; | |
51 import dwt.widgets.Table; | |
52 import dwt.widgets.TableItem; | |
53 import dwtx.jface.internal.text.TableOwnerDrawSupport; | |
54 import dwtx.jface.resource.JFaceResources; | |
55 import dwtx.jface.text.BadLocationException; | |
56 import dwtx.jface.text.DocumentEvent; | |
57 import dwtx.jface.text.IDocument; | |
58 import dwtx.jface.text.IDocumentListener; | |
59 import dwtx.jface.text.IEditingSupport; | |
60 import dwtx.jface.text.IEditingSupportRegistry; | |
61 import dwtx.jface.text.IRegion; | |
62 import dwtx.jface.text.IRewriteTarget; | |
63 import dwtx.jface.text.ITextViewer; | |
64 import dwtx.jface.text.ITextViewerExtension; | |
65 import dwtx.jface.text.TextUtilities; | |
66 import dwtx.jface.text.contentassist.ICompletionProposal; | |
67 import dwtx.jface.text.contentassist.ICompletionProposalExtension; | |
68 import dwtx.jface.text.contentassist.ICompletionProposalExtension2; | |
69 import dwtx.jface.text.contentassist.ICompletionProposalExtension6; | |
70 import dwtx.jface.text.contentassist.IContextInformation; | |
71 import dwtx.jface.viewers.StyledString; | |
72 | |
73 | |
74 | |
75 /** | |
76 * This class is used to present proposals to the user. If additional | |
77 * information exists for a proposal, then selecting that proposal | |
78 * will result in the information being displayed in a secondary | |
79 * window. | |
80 * | |
81 * @see dwtx.jface.text.contentassist.ICompletionProposal | |
82 * @see dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2 | |
83 */ | |
84 class CompletionProposalPopup2 : IContentAssistListener2 { | |
85 | |
86 /** The associated text viewer */ | |
87 private ITextViewer fViewer; | |
88 /** The associated content assistant */ | |
89 private ContentAssistant2 fContentAssistant; | |
90 /** The used additional info controller */ | |
91 private AdditionalInfoController2 fAdditionalInfoController; | |
92 /** The closing strategy for this completion proposal popup */ | |
93 private PopupCloser2 fPopupCloser= new PopupCloser2(); | |
94 /** The popup shell */ | |
95 private Shell fProposalShell; | |
96 /** The proposal table */ | |
97 private Table fProposalTable; | |
98 /** Indicates whether a completion proposal is being inserted */ | |
99 private bool fInserting= false; | |
100 /** The key listener to control navigation */ | |
101 private KeyListener fKeyListener; | |
102 /** List of document events used for filtering proposals */ | |
103 private List fDocumentEvents= new ArrayList(); | |
104 /** Listener filling the document event queue */ | |
105 private IDocumentListener fDocumentListener; | |
106 /** Reentrance count for <code>filterProposals</code> */ | |
107 private long fInvocationCounter= 0; | |
108 /** The filter list of proposals */ | |
109 private ICompletionProposal[] fFilteredProposals; | |
110 /** The computed list of proposals */ | |
111 private ICompletionProposal[] fComputedProposals; | |
112 /** The offset for which the proposals have been computed */ | |
113 private int fInvocationOffset; | |
114 /** The offset for which the computed proposals have been filtered */ | |
115 private int fFilterOffset; | |
116 /** The default line delimiter of the viewer's widget */ | |
117 private String fLineDelimiter; | |
118 /** The most recently selected proposal. */ | |
119 private ICompletionProposal fLastProposal; | |
120 /** | |
121 * Tells whether colored labels support is enabled. | |
122 * Only valid while the popup is active. | |
150 | 123 * |
129 | 124 * @since 3.4 |
125 */ | |
126 private bool fIsColoredLabelsSupportEnabled= false; | |
150 | 127 |
159 | 128 private IEditingSupport fFocusEditingSupport; |
129 private void fFocusEditingSupport_init() { | |
130 fFocusEditingSupport = new class() IEditingSupport { | |
129 | 131 |
159 | 132 public bool isOriginator(DocumentEvent event, IRegion focus) { |
133 return false; | |
134 } | |
129 | 135 |
159 | 136 public bool ownsFocusShell() { |
137 return Helper2.okToUse(fProposalShell) && fProposalShell.isFocusControl() | |
138 || Helper2.okToUse(fProposalTable) && fProposalTable.isFocusControl(); | |
139 } | |
129 | 140 |
159 | 141 }; |
142 } | |
143 private IEditingSupport fModificationEditingSupport; | |
144 private void fModificationEditingSupport_init() { | |
145 fModificationEditingSupport = new class() IEditingSupport { | |
146 | |
147 public bool isOriginator(DocumentEvent event, IRegion focus) { | |
148 if (fViewer !is null) { | |
149 Point selection= fViewer.getSelectedRange(); | |
150 return selection.x <= focus.getOffset() + focus.getLength() && selection.x + selection.y >= focus.getOffset(); | |
151 } | |
152 return false; | |
129 | 153 } |
154 | |
159 | 155 public bool ownsFocusShell() { |
156 return false; | |
157 } | |
129 | 158 |
159 | 159 }; |
160 } | |
129 | 161 |
162 /** | |
163 * Creates a new completion proposal popup for the given elements. | |
164 * | |
165 * @param contentAssistant the content assistant feeding this popup | |
166 * @param viewer the viewer on top of which this popup appears | |
167 * @param infoController the info control collaborating with this popup | |
168 * @since 2.0 | |
169 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
170 public this(ContentAssistant2 contentAssistant, ITextViewer viewer, AdditionalInfoController2 infoController) { |
159 | 171 |
172 fModificationEditingSupport_init(); | |
173 fFocusEditingSupport_init(); | |
174 | |
129 | 175 fContentAssistant= contentAssistant; |
176 fViewer= viewer; | |
177 fAdditionalInfoController= infoController; | |
178 } | |
179 | |
180 /** | |
181 * Computes and presents completion proposals. The flag indicates whether this call has | |
182 * be made out of an auto activation context. | |
183 * | |
184 * @param autoActivated <code>true</code> if auto activation context | |
185 * @return an error message or <code>null</code> in case of no error | |
186 */ | |
158 | 187 public String showProposals(bool autoActivated) { |
129 | 188 |
189 if (fKeyListener is null) { | |
135 | 190 fKeyListener= new class() KeyListener { |
129 | 191 public void keyPressed(KeyEvent e) { |
192 if (!Helper2.okToUse(fProposalShell)) | |
193 return; | |
194 | |
195 if (e.character is 0 && e.keyCode is DWT.CTRL) { | |
196 // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754 | |
197 int index= fProposalTable.getSelectionIndex(); | |
198 if (index >= 0) | |
199 selectProposal(index, true); | |
200 } | |
201 } | |
202 | |
203 public void keyReleased(KeyEvent e) { | |
204 if (!Helper2.okToUse(fProposalShell)) | |
205 return; | |
206 | |
207 if (e.character is 0 && e.keyCode is DWT.CTRL) { | |
208 // http://dev.eclipse.org/bugs/show_bug.cgi?id=34754 | |
209 int index= fProposalTable.getSelectionIndex(); | |
210 if (index >= 0) | |
211 selectProposal(index, false); | |
212 } | |
213 } | |
214 }; | |
215 } | |
216 | |
217 final StyledText styledText= fViewer.getTextWidget(); | |
218 if (styledText !is null && !styledText.isDisposed()) | |
219 styledText.addKeyListener(fKeyListener); | |
220 | |
135 | 221 // BusyIndicator.showWhile(styledText.getDisplay(), new class() Runnable { |
129 | 222 // public void run() { |
223 | |
224 fInvocationOffset= fViewer.getSelectedRange().x; | |
225 // lazily compute proposals | |
226 // if (fComputedProposals is null) fComputedProposals= computeProposals(fContentAssistant.getCompletionPosition()); | |
227 fComputedProposals= computeProposals(fInvocationOffset); | |
228 | |
229 int count= (fComputedProposals is null ? 0 : fComputedProposals.length); | |
230 if (count is 0) { | |
231 | |
232 if (!autoActivated) | |
233 styledText.getDisplay().beep(); | |
234 | |
235 } else { | |
236 | |
237 if (count is 1 && !autoActivated && fContentAssistant.isAutoInserting()) | |
238 | |
134 | 239 insertProposal(fComputedProposals[0], cast(wchar) 0, 0, fInvocationOffset); |
129 | 240 |
241 else { | |
242 | |
243 if (fLineDelimiter is null) | |
244 fLineDelimiter= styledText.getLineDelimiter(); | |
245 | |
246 createProposalSelector(); | |
247 setProposals(fComputedProposals); | |
248 resizeProposalSelector(true); | |
249 displayProposals(); | |
250 } | |
251 } | |
252 // } | |
253 // }); | |
254 | |
255 return getErrorMessage(); | |
256 } | |
257 | |
258 /** | |
259 * Returns the completion proposal available at the given offset of the | |
260 * viewer's document. Delegates the work to the content assistant. | |
261 * | |
262 * @param offset the offset | |
263 * @return the completion proposals available at this offset | |
264 */ | |
265 private ICompletionProposal[] computeProposals(int offset) { | |
266 return fContentAssistant.computeCompletionProposals(fViewer, offset); | |
267 } | |
268 | |
269 /** | |
270 * Returns the error message. | |
271 * | |
272 * @return the error message | |
273 */ | |
274 private String getErrorMessage() { | |
275 return fContentAssistant.getErrorMessage(); | |
276 } | |
277 | |
278 /** | |
279 * Creates the proposal selector. | |
280 */ | |
281 private void createProposalSelector() { | |
282 if (Helper2.okToUse(fProposalShell)) | |
283 return; | |
284 | |
285 Control control= fViewer.getTextWidget(); | |
286 fProposalShell= new Shell(control.getShell(), DWT.ON_TOP); | |
287 // fProposalShell= new Shell(control.getShell(), DWT.ON_TOP | DWT.RESIZE ); | |
288 fProposalTable= new Table(fProposalShell, DWT.H_SCROLL | DWT.V_SCROLL); | |
289 // fProposalTable= new Table(fProposalShell, DWT.H_SCROLL | DWT.V_SCROLL); | |
150 | 290 |
129 | 291 |
292 fIsColoredLabelsSupportEnabled= fContentAssistant.isColoredLabelsSupportEnabled(); | |
293 if (fIsColoredLabelsSupportEnabled) | |
294 TableOwnerDrawSupport.install(fProposalTable); | |
295 | |
296 fProposalTable.setLocation(0, 0); | |
297 if (fAdditionalInfoController !is null) | |
298 fAdditionalInfoController.setSizeConstraints(50, 10, true, false); | |
299 | |
300 GridLayout layout= new GridLayout(); | |
301 layout.marginWidth= 0; | |
302 layout.marginHeight= 0; | |
303 fProposalShell.setLayout(layout); | |
304 | |
305 GridData data= new GridData(GridData.FILL_BOTH); | |
306 fProposalTable.setLayoutData(data); | |
307 | |
308 fProposalShell.pack(); | |
309 | |
310 // set location | |
311 Point currentLocation= fProposalShell.getLocation(); | |
312 Point newLocation= getLocation(); | |
313 if ((newLocation.x < currentLocation.x && newLocation.y is currentLocation.y) || newLocation.y < currentLocation.y) | |
314 fProposalShell.setLocation(newLocation); | |
315 | |
316 if (fAdditionalInfoController !is null) { | |
135 | 317 fProposalShell.addControlListener(new class() ControlListener { |
129 | 318 |
319 public void controlMoved(ControlEvent e) {} | |
320 | |
321 public void controlResized(ControlEvent e) { | |
322 // resets the cached resize constraints | |
323 fAdditionalInfoController.setSizeConstraints(50, 10, true, false); | |
324 } | |
325 }); | |
326 } | |
327 | |
328 fProposalShell.setBackground(control.getDisplay().getSystemColor(DWT.COLOR_BLACK)); | |
329 | |
330 Color c= control.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND); | |
331 fProposalTable.setBackground(c); | |
332 | |
333 c= control.getDisplay().getSystemColor(DWT.COLOR_INFO_FOREGROUND); | |
334 fProposalTable.setForeground(c); | |
335 | |
135 | 336 fProposalTable.addSelectionListener(new class() SelectionListener { |
129 | 337 |
338 public void widgetSelected(SelectionEvent e) {} | |
339 | |
340 public void widgetDefaultSelected(SelectionEvent e) { | |
341 selectProposalWithMask(e.stateMask); | |
342 } | |
343 }); | |
344 | |
345 fPopupCloser.install(fContentAssistant, fProposalTable); | |
346 | |
135 | 347 fProposalShell.addDisposeListener(new class() DisposeListener { |
129 | 348 public void widgetDisposed(DisposeEvent e) { |
349 unregister(); // but don't dispose the shell, since we're being called from its disposal event! | |
350 } | |
351 }); | |
352 | |
353 fProposalTable.setHeaderVisible(false); | |
354 fContentAssistant.addToLayout(this, fProposalShell, ContentAssistant2.LayoutManager.LAYOUT_PROPOSAL_SELECTOR, fContentAssistant.getSelectionOffset()); | |
355 } | |
356 | |
357 /** | |
358 * Returns the proposal selected in the proposal selector. | |
359 * | |
360 * @return the selected proposal | |
361 * @since 2.0 | |
362 */ | |
363 private ICompletionProposal getSelectedProposal() { | |
364 int i= fProposalTable.getSelectionIndex(); | |
365 if (i < 0 || i >= fFilteredProposals.length) | |
366 return null; | |
367 return fFilteredProposals[i]; | |
368 } | |
369 | |
370 /** | |
371 * Takes the selected proposal and applies it. | |
372 * | |
373 * @param stateMask the state mask | |
374 * @since 2.1 | |
375 */ | |
376 private void selectProposalWithMask(int stateMask) { | |
377 ICompletionProposal p= getSelectedProposal(); | |
378 hide(); | |
379 if (p !is null) | |
134 | 380 insertProposal(p, cast(wchar) 0, stateMask, fViewer.getSelectedRange().x); |
129 | 381 } |
382 | |
383 /** | |
384 * Applies the given proposal at the given offset. The given character is the | |
385 * one that triggered the insertion of this proposal. | |
386 * | |
387 * @param p the completion proposal | |
388 * @param trigger the trigger character | |
389 * @param stateMask the state mask of the keyboard event triggering the insertion | |
390 * @param offset the offset | |
391 * @since 2.1 | |
392 */ | |
158 | 393 private void insertProposal(ICompletionProposal p, char trigger, int stateMask, int offset) { |
129 | 394 |
395 fInserting= true; | |
396 IRewriteTarget target= null; | |
397 IEditingSupportRegistry registry= null; | |
398 | |
399 try { | |
400 | |
401 IDocument document= fViewer.getDocument(); | |
402 | |
138 | 403 if ( cast(ITextViewerExtension)fViewer ) { |
134 | 404 ITextViewerExtension extension= cast(ITextViewerExtension) fViewer; |
129 | 405 target= extension.getRewriteTarget(); |
406 } | |
407 | |
408 if (target !is null) | |
409 target.beginCompoundChange(); | |
410 | |
138 | 411 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 412 registry= cast(IEditingSupportRegistry) fViewer; |
129 | 413 registry.register(fModificationEditingSupport); |
414 } | |
415 | |
138 | 416 if ( cast(ICompletionProposalExtension2)p ) { |
134 | 417 ICompletionProposalExtension2 e= cast(ICompletionProposalExtension2) p; |
129 | 418 e.apply(fViewer, trigger, stateMask, offset); |
138 | 419 } else if ( cast(ICompletionProposalExtension)p ) { |
134 | 420 ICompletionProposalExtension e= cast(ICompletionProposalExtension) p; |
129 | 421 e.apply(document, trigger, offset); |
422 } else { | |
423 p.apply(document); | |
424 } | |
425 | |
426 Point selection= p.getSelection(document); | |
427 if (selection !is null) { | |
428 fViewer.setSelectedRange(selection.x, selection.y); | |
429 fViewer.revealRange(selection.x, selection.y); | |
430 } | |
431 | |
432 IContextInformation info= p.getContextInformation(); | |
433 if (info !is null) { | |
434 | |
435 int position; | |
138 | 436 if ( cast(ICompletionProposalExtension)p ) { |
134 | 437 ICompletionProposalExtension e= cast(ICompletionProposalExtension) p; |
129 | 438 position= e.getContextInformationPosition(); |
439 } else { | |
440 if (selection is null) | |
441 selection= fViewer.getSelectedRange(); | |
442 position= selection.x + selection.y; | |
443 } | |
444 | |
445 fContentAssistant.showContextInformation(info, position); | |
446 } | |
447 | |
448 fContentAssistant.fireProposalChosen(p); | |
449 | |
450 } finally { | |
451 if (target !is null) | |
452 target.endCompoundChange(); | |
453 | |
454 if (registry !is null) | |
455 registry.unregister(fModificationEditingSupport); | |
456 | |
457 fInserting= false; | |
458 } | |
459 } | |
460 | |
461 /** | |
462 * Returns whether this popup has the focus. | |
463 * | |
464 * @return <code>true</code> if the popup has the focus | |
465 */ | |
466 public bool hasFocus() { | |
467 if (Helper2.okToUse(fProposalShell)) | |
468 return (fProposalShell.isFocusControl() || fProposalTable.isFocusControl()); | |
469 | |
470 return false; | |
471 } | |
472 | |
473 /** | |
474 * Hides this popup. | |
475 */ | |
476 public void hide() { | |
477 | |
478 unregister(); | |
479 | |
138 | 480 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 481 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 482 registry.unregister(fFocusEditingSupport); |
483 } | |
484 | |
485 if (Helper2.okToUse(fProposalShell)) { | |
486 fContentAssistant.removeContentAssistListener(this, ContentAssistant2.PROPOSAL_SELECTOR); | |
487 | |
488 fPopupCloser.uninstall(); | |
489 // see bug 47511: setVisible may run the event loop on GTK | |
490 // and trigger a rentrant call - have to make sure we don't | |
491 // dispose another shell that was already brought up in a | |
492 // reentrant call when calling setVisible() | |
493 Shell tempShell= fProposalShell; | |
494 fProposalShell= null; | |
495 tempShell.setVisible(false); | |
496 tempShell.dispose(); | |
497 } | |
498 } | |
499 | |
500 private void unregister() { | |
501 if (fDocumentListener !is null) { | |
502 IDocument document= fViewer.getDocument(); | |
503 if (document !is null) | |
504 document.removeDocumentListener(fDocumentListener); | |
505 fDocumentListener= null; | |
506 } | |
507 fDocumentEvents.clear(); | |
508 | |
509 StyledText styledText= fViewer.getTextWidget(); | |
510 if (fKeyListener !is null && styledText !is null && !styledText.isDisposed()) | |
511 styledText.removeKeyListener(fKeyListener); | |
512 | |
513 if (fLastProposal !is null) { | |
138 | 514 if ( cast(ICompletionProposalExtension2)fLastProposal ) { |
134 | 515 ICompletionProposalExtension2 extension= cast(ICompletionProposalExtension2) fLastProposal; |
129 | 516 extension.unselected(fViewer); |
517 } | |
518 | |
519 fLastProposal= null; | |
520 } | |
521 | |
522 fFilteredProposals= null; | |
523 | |
524 fContentAssistant.possibleCompletionsClosed(); | |
525 } | |
526 | |
527 /** | |
528 *Returns whether this popup is active. It is active if the propsal selector is visible. | |
529 * | |
530 * @return <code>true</code> if this popup is active | |
531 */ | |
532 public bool isActive() { | |
533 return fProposalShell !is null && !fProposalShell.isDisposed(); | |
534 } | |
535 | |
536 /** | |
537 * Initializes the proposal selector with these given proposals. | |
538 * | |
539 * @param proposals the proposals | |
540 */ | |
541 private void setProposals(ICompletionProposal[] proposals) { | |
542 if (Helper2.okToUse(fProposalTable)) { | |
543 | |
544 ICompletionProposal oldProposal= getSelectedProposal(); | |
138 | 545 if ( cast(ICompletionProposalExtension2)oldProposal ) |
134 | 546 (cast(ICompletionProposalExtension2) oldProposal).unselected(fViewer); |
129 | 547 |
548 fFilteredProposals= proposals; | |
549 | |
550 fProposalTable.setRedraw(false); | |
551 fProposalTable.removeAll(); | |
552 | |
553 Point selection= fViewer.getSelectedRange(); | |
554 int endOffset; | |
555 endOffset= selection.x + selection.y; | |
556 IDocument document= fViewer.getDocument(); | |
557 bool validate= false; | |
558 if (selection.y !is 0 && document !is null) validate= true; | |
559 int selectionIndex= 0; | |
560 | |
561 TableItem item; | |
562 ICompletionProposal p; | |
563 for (int i= 0; i < proposals.length; i++) { | |
564 p= proposals[i]; | |
565 item= new TableItem(fProposalTable, DWT.NULL); | |
566 if (p.getImage() !is null) | |
567 item.setImage(p.getImage()); | |
150 | 568 |
129 | 569 String displayString; |
570 StyleRange[] styleRanges= null; | |
143 | 571 if (fIsColoredLabelsSupportEnabled && cast(ICompletionProposalExtension6)p ) { |
134 | 572 StyledString styledString= (cast(ICompletionProposalExtension6)p).getStyledDisplayString(); |
129 | 573 displayString= styledString.getString(); |
574 styleRanges= styledString.getStyleRanges(); | |
575 } else | |
576 displayString= p.getDisplayString(); | |
577 | |
578 item.setText(displayString); | |
579 if (fIsColoredLabelsSupportEnabled) | |
580 TableOwnerDrawSupport.storeStyleRanges(item, 0, styleRanges); | |
150 | 581 |
129 | 582 item.setData(p); |
583 | |
584 if (validate && validateProposal(document, p, endOffset, null)) { | |
585 selectionIndex= i; | |
586 validate= false; | |
587 } | |
588 } | |
589 | |
590 resizeProposalSelector(false); | |
591 | |
592 selectProposal(selectionIndex, false); | |
593 fProposalTable.setRedraw(true); | |
594 } | |
595 } | |
596 | |
597 private void resizeProposalSelector(bool adjustWidth) { | |
598 // in order to fill in the table items so size computation works correctly | |
599 // will cause flicker, though | |
600 fProposalTable.setRedraw(true); | |
601 | |
134 | 602 int width= adjustWidth ? DWT.DEFAULT : (cast(GridData)fProposalTable.getLayoutData()).widthHint; |
129 | 603 Point size= fProposalTable.computeSize(width, DWT.DEFAULT, true); |
604 | |
605 GridData data= new GridData(GridData.FILL_BOTH); | |
606 data.widthHint= adjustWidth ? Math.min(size.x, 300) : width; | |
607 data.heightHint= Math.min(getTableHeightHint(fProposalTable, fProposalTable.getItemCount()), getTableHeightHint(fProposalTable, 10)); | |
608 fProposalTable.setLayoutData(data); | |
609 | |
610 fProposalShell.layout(true); | |
611 fProposalShell.pack(); | |
612 | |
613 if (adjustWidth) { | |
614 fProposalShell.setLocation(getLocation()); | |
615 } | |
616 } | |
617 | |
618 /** | |
619 * Computes the table hight hint for <code>table</code>. | |
620 * | |
621 * @param table the table to compute the height for | |
622 * @param rows the number of rows to compute the height for | |
623 * @return the height hint for <code>table</code> | |
624 */ | |
625 private int getTableHeightHint(Table table, int rows) { | |
626 if (table.getFont().equals(JFaceResources.getDefaultFont())) | |
627 table.setFont(JFaceResources.getDialogFont()); | |
628 int result= table.getItemHeight() * rows; | |
629 if (table.getLinesVisible()) | |
630 result+= table.getGridLineWidth() * (rows - 1); | |
631 | |
632 // TODO adjust to correct size. +4 works on windows, but not others | |
633 // return result + 4; | |
634 return result; | |
635 } | |
636 | |
637 private bool validateProposal(IDocument document, ICompletionProposal p, int offset, DocumentEvent event) { | |
638 // detect selected | |
138 | 639 if ( cast(ICompletionProposalExtension2)p ) { |
134 | 640 ICompletionProposalExtension2 e= cast(ICompletionProposalExtension2) p; |
129 | 641 if (e.validate(document, offset, event)) |
642 return true; | |
138 | 643 } else if ( cast(ICompletionProposalExtension)p ) { |
134 | 644 ICompletionProposalExtension e= cast(ICompletionProposalExtension) p; |
129 | 645 if (e.isValidFor(document, offset)) |
646 return true; | |
647 } | |
648 return false; | |
649 } | |
650 | |
651 /** | |
652 * Returns the graphical location at which this popup should be made visible. | |
653 * | |
654 * @return the location of this popup | |
655 */ | |
656 private Point getLocation() { | |
657 StyledText text= fViewer.getTextWidget(); | |
658 Point selection= text.getSelection(); | |
659 Point p= text.getLocationAtOffset(selection.x); | |
660 p.x -= fProposalShell.getBorderWidth(); | |
661 if (p.x < 0) p.x= 0; | |
662 if (p.y < 0) p.y= 0; | |
663 p= new Point(p.x, p.y + text.getLineHeight(selection.x)); | |
664 p= text.toDisplay(p); | |
665 return p; | |
666 } | |
667 | |
668 /** | |
669 *Displays this popup and install the additional info controller, so that additional info | |
670 * is displayed when a proposal is selected and additional info is available. | |
671 */ | |
672 private void displayProposals() { | |
673 if (fContentAssistant.addContentAssistListener(this, ContentAssistant2.PROPOSAL_SELECTOR)) { | |
674 | |
675 if (fDocumentListener is null) | |
135 | 676 fDocumentListener= new class() IDocumentListener { |
129 | 677 public void documentAboutToBeChanged(DocumentEvent event) { |
678 if (!fInserting) | |
679 fDocumentEvents.add(event); | |
680 } | |
681 | |
682 public void documentChanged(DocumentEvent event) { | |
683 if (!fInserting) | |
684 filterProposals(); | |
685 } | |
686 }; | |
687 IDocument document= fViewer.getDocument(); | |
688 if (document !is null) | |
689 document.addDocumentListener(fDocumentListener); | |
690 | |
691 | |
138 | 692 if ( cast(IEditingSupportRegistry)fViewer ) { |
134 | 693 IEditingSupportRegistry registry= cast(IEditingSupportRegistry) fViewer; |
129 | 694 registry.register(fFocusEditingSupport); |
695 } | |
696 | |
697 fProposalShell.setVisible(true); | |
698 // see bug 47511: setVisible may run the event loop on GTK | |
699 // and trigger a rentrant call - have to check whether we are still | |
700 // visible | |
701 if (!Helper2.okToUse(fProposalShell)) | |
702 return; | |
703 | |
704 | |
705 if (fAdditionalInfoController !is null) { | |
706 fAdditionalInfoController.install(fProposalTable); | |
707 fAdditionalInfoController.handleTableSelectionChanged(); | |
708 } | |
709 } | |
710 } | |
711 | |
712 /* | |
713 * @see IContentAssistListener#verifyKey(VerifyEvent) | |
714 */ | |
715 public bool verifyKey(VerifyEvent e) { | |
716 if (!Helper2.okToUse(fProposalShell)) | |
717 return true; | |
718 | |
719 char key= e.character; | |
720 if (key is 0) { | |
721 int newSelection= fProposalTable.getSelectionIndex(); | |
722 int visibleRows= (fProposalTable.getSize().y / fProposalTable.getItemHeight()) - 1; | |
723 bool smartToggle= false; | |
724 switch (e.keyCode) { | |
725 | |
726 case DWT.ARROW_LEFT : | |
727 case DWT.ARROW_RIGHT : | |
728 filterProposals(); | |
729 return true; | |
730 | |
731 case DWT.ARROW_UP : | |
732 newSelection -= 1; | |
733 if (newSelection < 0) | |
734 newSelection= fProposalTable.getItemCount() - 1; | |
735 break; | |
736 | |
737 case DWT.ARROW_DOWN : | |
738 newSelection += 1; | |
739 if (newSelection > fProposalTable.getItemCount() - 1) | |
740 newSelection= 0; | |
741 break; | |
742 | |
743 case DWT.PAGE_DOWN : | |
744 newSelection += visibleRows; | |
745 if (newSelection >= fProposalTable.getItemCount()) | |
746 newSelection= fProposalTable.getItemCount() - 1; | |
747 break; | |
748 | |
749 case DWT.PAGE_UP : | |
750 newSelection -= visibleRows; | |
751 if (newSelection < 0) | |
752 newSelection= 0; | |
753 break; | |
754 | |
755 case DWT.HOME : | |
756 newSelection= 0; | |
757 break; | |
758 | |
759 case DWT.END : | |
760 newSelection= fProposalTable.getItemCount() - 1; | |
761 break; | |
762 | |
763 default : | |
764 if (e.keyCode !is DWT.MOD1 && e.keyCode !is DWT.MOD2 && e.keyCode !is DWT.MOD3 && e.keyCode !is DWT.MOD4) | |
765 hide(); | |
766 return true; | |
767 } | |
768 | |
769 selectProposal(newSelection, smartToggle); | |
770 | |
771 e.doit= false; | |
772 return false; | |
773 | |
774 } | |
775 | |
776 // key !is 0 | |
777 switch (key) { | |
778 case 0x1B: // Esc | |
779 e.doit= false; | |
780 hide(); | |
781 break; | |
782 | |
783 case '\n': // Ctrl-Enter on w2k | |
784 case '\r': // Enter | |
785 if ((e.stateMask & DWT.CTRL) is 0) { | |
786 e.doit= false; | |
787 selectProposalWithMask(e.stateMask); | |
788 } | |
789 break; | |
790 | |
791 // in linked mode: hide popup | |
792 // plus: don't invalidate the event in order to give LinkedUI a chance to handle it | |
793 case '\t': | |
794 // hide(); | |
795 break; | |
796 | |
797 default: | |
798 ICompletionProposal p= getSelectedProposal(); | |
138 | 799 if ( cast(ICompletionProposalExtension)p ) { |
134 | 800 ICompletionProposalExtension t= cast(ICompletionProposalExtension) p; |
129 | 801 char[] triggers= t.getTriggerCharacters(); |
802 if (contains(triggers, key)) { | |
803 hide(); | |
804 if (key is ';') { | |
805 e.doit= true; | |
134 | 806 insertProposal(p, cast(wchar) 0, e.stateMask, fViewer.getSelectedRange().x); |
129 | 807 } else { |
808 e.doit= false; | |
809 insertProposal(p, key, e.stateMask, fViewer.getSelectedRange().x); | |
810 } | |
811 } | |
812 } | |
813 } | |
814 | |
815 return true; | |
816 } | |
817 | |
818 /** | |
819 * Selects the entry with the given index in the proposal selector and feeds | |
820 * the selection to the additional info controller. | |
821 * | |
822 * @param index the index in the list | |
823 * @param smartToggle <code>true</code> if the smart toogle key has been pressed | |
824 * @since 2.1 | |
825 */ | |
826 private void selectProposal(int index, bool smartToggle) { | |
827 | |
828 ICompletionProposal oldProposal= getSelectedProposal(); | |
138 | 829 if ( cast(ICompletionProposalExtension2)oldProposal ) |
134 | 830 (cast(ICompletionProposalExtension2) oldProposal).unselected(fViewer); |
129 | 831 |
832 ICompletionProposal proposal= fFilteredProposals[index]; | |
138 | 833 if ( cast(ICompletionProposalExtension2)proposal ) |
134 | 834 (cast(ICompletionProposalExtension2) proposal).selected(fViewer, smartToggle); |
129 | 835 |
836 fLastProposal= proposal; | |
837 | |
838 fProposalTable.setSelection(index); | |
839 fProposalTable.showSelection(); | |
840 if (fAdditionalInfoController !is null) | |
841 fAdditionalInfoController.handleTableSelectionChanged(); | |
842 } | |
843 | |
844 /** | |
845 * Returns whether the given character is contained in the given array of | |
846 * characters. | |
847 * | |
848 * @param characters the list of characters | |
849 * @param c the character to look for in the list | |
850 * @return <code>true</code> if character belongs to the list | |
851 * @since 2.0 | |
852 */ | |
853 private bool contains(char[] characters, char c) { | |
854 | |
855 if (characters is null) | |
856 return false; | |
857 | |
858 for (int i= 0; i < characters.length; i++) { | |
859 if (c is characters[i]) | |
860 return true; | |
861 } | |
862 | |
863 return false; | |
864 } | |
865 | |
866 /* | |
867 * @see IEventConsumer#processEvent(VerifyEvent) | |
868 */ | |
869 public void processEvent(VerifyEvent e) { | |
870 } | |
871 | |
872 /** | |
873 * Filters the displayed proposal based on the given cursor position and the | |
874 * offset of the original invocation of the content assistant. | |
875 */ | |
876 private void filterProposals() { | |
877 ++ fInvocationCounter; | |
878 Control control= fViewer.getTextWidget(); | |
135 | 879 control.getDisplay().asyncExec(new class() Runnable { |
129 | 880 long fCounter= fInvocationCounter; |
881 public void run() { | |
882 | |
883 if (fCounter !is fInvocationCounter) return; | |
884 | |
885 int offset= fViewer.getSelectedRange().x; | |
886 ICompletionProposal[] proposals= null; | |
887 try { | |
888 if (offset > -1) { | |
889 DocumentEvent event= TextUtilities.mergeProcessedDocumentEvents(fDocumentEvents); | |
890 proposals= computeFilteredProposals(offset, event); | |
891 } | |
892 } catch (BadLocationException x) { | |
893 } finally { | |
894 fDocumentEvents.clear(); | |
895 } | |
896 fFilterOffset= offset; | |
897 | |
898 if (proposals !is null && proposals.length > 0) | |
899 setProposals(proposals); | |
900 else | |
901 hide(); | |
902 } | |
903 }); | |
904 } | |
905 | |
906 /** | |
907 * Computes the subset of already computed propsals that are still valid for | |
908 * the given offset. | |
909 * | |
910 * @param offset the offset | |
911 * @param event the merged document event | |
912 * @return the set of filtered proposals | |
913 * @since 2.0 | |
914 */ | |
915 private ICompletionProposal[] computeFilteredProposals(int offset, DocumentEvent event) { | |
916 | |
917 if (offset is fInvocationOffset && event is null) | |
918 return fComputedProposals; | |
919 | |
920 if (offset < fInvocationOffset) { | |
921 return null; | |
922 } | |
923 | |
924 ICompletionProposal[] proposals= fComputedProposals; | |
925 if (offset > fFilterOffset) | |
926 proposals= fFilteredProposals; | |
927 | |
928 if (proposals is null) | |
929 return null; | |
930 | |
931 IDocument document= fViewer.getDocument(); | |
932 int length= proposals.length; | |
933 List filtered= new ArrayList(length); | |
934 for (int i= 0; i < length; i++) { | |
935 | |
150 | 936 if (cast(ICompletionProposalExtension2)proposals[i] ) { |
129 | 937 |
134 | 938 ICompletionProposalExtension2 p= cast(ICompletionProposalExtension2) proposals[i]; |
129 | 939 if (p.validate(document, offset, event)) |
940 filtered.add(p); | |
941 | |
150 | 942 } else if (cast(ICompletionProposalExtension)proposals[i] ) { |
129 | 943 |
134 | 944 ICompletionProposalExtension p= cast(ICompletionProposalExtension) proposals[i]; |
129 | 945 if (p.isValidFor(document, offset)) |
946 filtered.add(p); | |
947 | |
948 } else { | |
949 // restore original behavior | |
950 fInvocationOffset= offset; | |
951 fComputedProposals= computeProposals(fInvocationOffset); | |
952 return fComputedProposals; | |
953 } | |
954 } | |
955 | |
956 ICompletionProposal[] p= new ICompletionProposal[filtered.size()]; | |
957 filtered.toArray(p); | |
958 return p; | |
959 } | |
960 | |
961 /** | |
962 * Requests the proposal shell to take focus. | |
963 * | |
964 * @since 3.0 | |
965 */ | |
966 public void setFocus() { | |
967 if (Helper2.okToUse(fProposalShell)) | |
968 fProposalShell.setFocus(); | |
969 } | |
970 } |