Mercurial > projects > dwt-addons
annotate dwtx/jface/text/contentassist/ContextInformationPopup.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | f70d9508c95c |
children | 3678e4f1a766 |
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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.contentassist.ContextInformationPopup; | |
14 | |
131 | 15 import dwtx.jface.text.contentassist.ContentAssistEvent; // packageimport |
16 import dwtx.jface.text.contentassist.Helper; // packageimport | |
17 import dwtx.jface.text.contentassist.PopupCloser; // packageimport | |
18 import dwtx.jface.text.contentassist.IContentAssistant; // packageimport | |
19 import dwtx.jface.text.contentassist.CompletionProposal; // packageimport | |
20 import dwtx.jface.text.contentassist.ICompletionProposalExtension5; // packageimport | |
21 import dwtx.jface.text.contentassist.IContextInformationValidator; // packageimport | |
22 import dwtx.jface.text.contentassist.IContentAssistListener; // packageimport | |
23 import dwtx.jface.text.contentassist.ICompletionProposalExtension6; // packageimport | |
24 import dwtx.jface.text.contentassist.ICompletionListener; // packageimport | |
25 import dwtx.jface.text.contentassist.ICompletionProposalExtension2; // packageimport | |
26 import dwtx.jface.text.contentassist.IContentAssistantExtension4; // packageimport | |
27 import dwtx.jface.text.contentassist.ContextInformation; // packageimport | |
28 import dwtx.jface.text.contentassist.ICompletionProposalExtension3; // packageimport | |
29 import dwtx.jface.text.contentassist.ContextInformationValidator; // packageimport | |
30 import dwtx.jface.text.contentassist.ICompletionProposal; // packageimport | |
31 import dwtx.jface.text.contentassist.IContentAssistProcessor; // packageimport | |
32 import dwtx.jface.text.contentassist.AdditionalInfoController; // packageimport | |
33 import dwtx.jface.text.contentassist.IContextInformationPresenter; // packageimport | |
34 import dwtx.jface.text.contentassist.ICompletionProposalExtension4; // packageimport | |
35 import dwtx.jface.text.contentassist.ICompletionListenerExtension; // packageimport | |
36 import dwtx.jface.text.contentassist.IContextInformationExtension; // packageimport | |
37 import dwtx.jface.text.contentassist.IContentAssistantExtension2; // packageimport | |
38 import dwtx.jface.text.contentassist.ContentAssistSubjectControlAdapter; // packageimport | |
39 import dwtx.jface.text.contentassist.CompletionProposalPopup; // packageimport | |
40 import dwtx.jface.text.contentassist.ICompletionProposalExtension; // packageimport | |
41 import dwtx.jface.text.contentassist.IContextInformation; // packageimport | |
42 import dwtx.jface.text.contentassist.IContentAssistantExtension3; // packageimport | |
43 import dwtx.jface.text.contentassist.ContentAssistant; // packageimport | |
44 import dwtx.jface.text.contentassist.IContentAssistantExtension; // packageimport | |
45 import dwtx.jface.text.contentassist.JFaceTextMessages; // packageimport | |
46 | |
47 | |
129 | 48 import dwt.dwthelper.utils; |
49 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
50 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
138
diff
changeset
|
51 |
129 | 52 |
53 import dwt.DWT; | |
54 import dwt.custom.BusyIndicator; | |
55 import dwt.custom.StyledText; | |
56 import dwt.events.KeyEvent; | |
57 import dwt.events.SelectionAdapter; | |
58 import dwt.events.SelectionEvent; | |
59 import dwt.events.SelectionListener; | |
60 import dwt.events.VerifyEvent; | |
61 import dwt.graphics.Color; | |
62 import dwt.graphics.Point; | |
63 import dwt.graphics.Rectangle; | |
64 import dwt.layout.GridData; | |
65 import dwt.layout.GridLayout; | |
66 import dwt.widgets.Control; | |
67 import dwt.widgets.Display; | |
68 import dwt.widgets.Shell; | |
69 import dwt.widgets.Table; | |
70 import dwt.widgets.TableItem; | |
71 import dwtx.jface.contentassist.IContentAssistSubjectControl; | |
72 import dwtx.jface.text.ITextViewer; | |
73 import dwtx.jface.text.TextPresentation; | |
74 | |
75 /** | |
76 * Represents the state necessary for embedding contexts. | |
77 * | |
78 * @since 2.0 | |
79 */ | |
80 static class ContextFrame { | |
81 | |
82 final int fBeginOffset; | |
83 final int fOffset; | |
84 final int fVisibleOffset; | |
85 final IContextInformation fInformation; | |
86 final IContextInformationValidator fValidator; | |
87 final IContextInformationPresenter fPresenter; | |
88 | |
89 /* | |
90 * @since 3.1 | |
91 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
92 public this(IContextInformation information, int beginOffset, int offset, int visibleOffset, IContextInformationValidator validator, IContextInformationPresenter presenter) { |
129 | 93 fInformation = information; |
94 fBeginOffset = beginOffset; | |
95 fOffset = offset; | |
96 fVisibleOffset = visibleOffset; | |
97 fValidator = validator; | |
98 fPresenter = presenter; | |
99 } | |
100 | |
101 /* | |
102 * @see java.lang.Object#equals(java.lang.Object) | |
103 * @since 3.0 | |
104 */ | |
105 public bool equals(Object obj) { | |
138 | 106 if ( cast(ContextFrame)obj ) { |
134 | 107 ContextFrame frame= cast(ContextFrame) obj; |
129 | 108 return fInformation.equals(frame.fInformation) && fBeginOffset is frame.fBeginOffset; |
109 } | |
110 return super.equals(obj); | |
111 } | |
112 | |
113 /* | |
114 * @see java.lang.Object#hashCode() | |
115 * @since 3.1 | |
116 */ | |
117 public int hashCode() { | |
118 return (fInformation.hashCode() << 16) | fBeginOffset; | |
119 } | |
120 } | |
121 | |
158 | 122 alias ContextFrame ContextInformationPopup_ContextFrame; |
123 /** | |
124 * This class is used to present context information to the user. | |
125 * If multiple contexts are valid at the current cursor location, | |
126 * a list is presented from which the user may choose one context. | |
127 * Once the user makes their choice, or if there was only a single | |
128 * possible context, the context information is shown in a tool tip like popup. <p> | |
129 * If the tool tip is visible and the user wants to see context information of | |
130 * a context embedded into the one for which context information is displayed, | |
131 * context information for the embedded context is shown. As soon as the | |
132 * cursor leaves the embedded context area, the context information for | |
133 * the embedding context is shown again. | |
134 * | |
135 * @see IContextInformation | |
136 * @see IContextInformationValidator | |
137 */ | |
138 class ContextInformationPopup : IContentAssistListener { | |
139 | |
140 | |
141 | |
129 | 142 private ITextViewer fViewer; |
143 private ContentAssistant fContentAssistant; | |
144 | |
145 private PopupCloser fPopupCloser= new PopupCloser(); | |
146 private Shell fContextSelectorShell; | |
147 private Table fContextSelectorTable; | |
148 private IContextInformation[] fContextSelectorInput; | |
149 private String fLineDelimiter= null; | |
150 | |
151 private Shell fContextInfoPopup; | |
152 private StyledText fContextInfoText; | |
153 private TextPresentation fTextPresentation; | |
154 | |
155 private Stack fContextFrameStack= new Stack(); | |
156 /** | |
157 * The content assist subject control. | |
158 * | |
159 * @since 3.0 | |
160 */ | |
161 private IContentAssistSubjectControl fContentAssistSubjectControl; | |
162 /** | |
163 * The content assist subject control adapter. | |
164 * | |
165 * @since 3.0 | |
166 */ | |
167 private ContentAssistSubjectControlAdapter fContentAssistSubjectControlAdapter; | |
168 | |
169 /** | |
170 * Selection listener on the text widget which is active | |
171 * while a context information pop up is shown. | |
172 * | |
173 * @since 3.0 | |
174 */ | |
175 private SelectionListener fTextWidgetSelectionListener; | |
176 | |
177 /** | |
178 * The last removed context frame is remembered in order to not re-query the | |
179 * user about which context should be used. | |
180 * | |
181 * @since 3.0 | |
182 */ | |
183 private ContextFrame fLastContext= null; | |
184 | |
185 /** | |
186 * Creates a new context information popup. | |
187 * | |
188 * @param contentAssistant the content assist for computing the context information | |
189 * @param viewer the viewer on top of which the context information is shown | |
190 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
191 public this(ContentAssistant contentAssistant, ITextViewer viewer) { |
129 | 192 fContentAssistant= contentAssistant; |
193 fViewer= viewer; | |
194 fContentAssistSubjectControlAdapter= new ContentAssistSubjectControlAdapter(fViewer); | |
195 } | |
196 | |
197 /** | |
198 * Creates a new context information popup. | |
199 * | |
200 * @param contentAssistant the content assist for computing the context information | |
201 * @param contentAssistSubjectControl the content assist subject control on top of which the context information is shown | |
202 * @since 3.0 | |
203 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
204 public this(ContentAssistant contentAssistant, IContentAssistSubjectControl contentAssistSubjectControl) { |
129 | 205 fContentAssistant= contentAssistant; |
206 fContentAssistSubjectControl= contentAssistSubjectControl; | |
207 fContentAssistSubjectControlAdapter= new ContentAssistSubjectControlAdapter(fContentAssistSubjectControl); | |
208 } | |
209 | |
210 /** | |
211 * Shows all possible contexts for the given cursor position of the viewer. | |
212 * | |
213 * @param autoActivated <code>true</code> if auto activated | |
214 * @return a potential error message or <code>null</code> in case of no error | |
215 */ | |
158 | 216 public String showContextProposals(bool autoActivated) { |
129 | 217 final Control control= fContentAssistSubjectControlAdapter.getControl(); |
158 | 218 BusyIndicator.showWhile(control.getDisplay(), dgRunnable( { |
219 int offset= fContentAssistSubjectControlAdapter.getSelectedRange().x; | |
129 | 220 |
158 | 221 IContextInformation[] contexts= computeContextInformation(offset); |
222 int count = (contexts is null ? 0 : contexts.length); | |
223 if (count is 1) { | |
129 | 224 |
158 | 225 ContextFrame frame= createContextFrame(contexts[0], offset); |
226 if (isDuplicate(frame)) | |
227 validateContextInformation(); | |
228 else | |
229 // Show context information directly | |
230 internalShowContextInfo(frame); | |
231 | |
232 } else if (count > 0) { | |
129 | 233 |
158 | 234 // if any of the proposed context matches any of the contexts on the stack, |
235 // assume that one (so, if context info is invoked repeatedly, the current | |
236 // info is kept) | |
237 for (int i= 0; i < contexts.length; i++) { | |
238 IContextInformation info= contexts[i]; | |
239 ContextFrame frame= createContextFrame(info, offset); | |
240 | |
241 // check top of stack and stored context | |
242 if (isDuplicate(frame)) { | |
129 | 243 validateContextInformation(); |
158 | 244 return; |
245 } | |
129 | 246 |
158 | 247 if (isLastFrame(frame)) { |
248 internalShowContextInfo(frame); | |
249 return; | |
250 } | |
129 | 251 |
158 | 252 // also check all other contexts |
253 for (Iterator it= fContextFrameStack.iterator(); it.hasNext(); ) { | |
254 ContextFrame stackFrame= cast(ContextFrame) it.next(); | |
255 if (stackFrame.equals(frame)) { | |
129 | 256 validateContextInformation(); |
257 return; | |
258 } | |
158 | 259 } |
260 } | |
129 | 261 |
158 | 262 // otherwise: |
263 // Precise context must be selected | |
129 | 264 |
158 | 265 if (fLineDelimiter is null) |
266 fLineDelimiter= fContentAssistSubjectControlAdapter.getLineDelimiter(); | |
129 | 267 |
158 | 268 createContextSelector(); |
269 setContexts(contexts); | |
270 displayContextSelector(); | |
129 | 271 } |
158 | 272 })); |
129 | 273 |
274 return getErrorMessage(); | |
275 } | |
276 | |
277 /** | |
278 * Displays the given context information for the given offset. | |
279 * | |
280 * @param info the context information | |
281 * @param offset the offset | |
282 * @since 2.0 | |
283 */ | |
158 | 284 public void showContextInformation(IContextInformation info, int offset) { |
129 | 285 Control control= fContentAssistSubjectControlAdapter.getControl(); |
158 | 286 BusyIndicator.showWhile(control.getDisplay(), dgRunnable( { |
287 if (info_ is null) | |
288 validateContextInformation(); | |
289 else { | |
290 ContextFrame frame= createContextFrame(info_, offset_); | |
291 if (isDuplicate(frame)) | |
129 | 292 validateContextInformation(); |
158 | 293 else |
294 internalShowContextInfo(frame); | |
295 hideContextSelector(); | |
129 | 296 } |
158 | 297 }, info, offset )); |
129 | 298 } |
299 | |
300 /** | |
301 * Displays the given context information for the given offset. | |
302 * | |
303 * @param frame the context frame to display, or <code>null</code> | |
304 * @since 3.0 | |
305 */ | |
306 private void internalShowContextInfo(ContextFrame frame) { | |
307 if (frame !is null) { | |
308 fContextFrameStack.push(frame); | |
309 if (fContextFrameStack.size() is 1) | |
310 fLastContext= null; | |
311 internalShowContextFrame(frame, fContextFrameStack.size() is 1); | |
312 validateContextInformation(); | |
313 } | |
314 } | |
315 | |
316 /** | |
317 * Creates a context frame for the given offset. | |
318 * | |
319 * @param information the context information | |
320 * @param offset the offset | |
321 * @return the created context frame | |
322 * @since 3.0 | |
323 */ | |
324 private ContextFrame createContextFrame(IContextInformation information, int offset) { | |
325 IContextInformationValidator validator= fContentAssistSubjectControlAdapter.getContextInformationValidator(fContentAssistant, offset); | |
326 | |
327 if (validator !is null) { | |
138 | 328 int beginOffset= ( cast(IContextInformationExtension)information ) ? (cast(IContextInformationExtension) information).getContextInformationPosition() : offset; |
129 | 329 if (beginOffset is -1) beginOffset= offset; |
330 int visibleOffset= fContentAssistSubjectControlAdapter.getWidgetSelectionRange().x - (offset - beginOffset); | |
331 IContextInformationPresenter presenter = fContentAssistSubjectControlAdapter.getContextInformationPresenter(fContentAssistant, offset); | |
332 return new ContextFrame(information, beginOffset, offset, visibleOffset, validator, presenter); | |
333 } | |
334 | |
335 return null; | |
336 } | |
337 | |
338 /** | |
339 * Compares <code>frame</code> with the top of the stack, returns <code>true</code> | |
340 * if the frames are the same. | |
341 * | |
342 * @param frame the frame to check | |
343 * @return <code>true</code> if <code>frame</code> matches the top of the stack | |
344 * @since 3.0 | |
345 */ | |
346 private bool isDuplicate(ContextFrame frame) { | |
347 if (frame is null) | |
348 return false; | |
349 if (fContextFrameStack.isEmpty()) | |
350 return false; | |
351 // stack not empty | |
134 | 352 ContextFrame top= cast(ContextFrame) fContextFrameStack.peek(); |
129 | 353 return frame.equals(top); |
354 } | |
355 | |
356 /** | |
357 * Compares <code>frame</code> with most recently removed context frame, returns <code>true</code> | |
358 * if the frames are the same. | |
359 * | |
360 * @param frame the frame to check | |
361 * @return <code>true</code> if <code>frame</code> matches the most recently removed | |
362 * @since 3.0 | |
363 */ | |
364 private bool isLastFrame(ContextFrame frame) { | |
365 return frame !is null && frame.equals(fLastContext); | |
366 } | |
367 | |
368 /** | |
369 * Shows the given context frame. | |
370 * | |
371 * @param frame the frame to display | |
372 * @param initial <code>true</code> if this is the first frame to be displayed | |
373 * @since 2.0 | |
374 */ | |
375 private void internalShowContextFrame(ContextFrame frame, bool initial) { | |
376 | |
377 fContentAssistSubjectControlAdapter.installValidator(frame); | |
378 | |
379 if (frame.fPresenter !is null) { | |
380 if (fTextPresentation is null) | |
381 fTextPresentation= new TextPresentation(); | |
382 fContentAssistSubjectControlAdapter.installContextInformationPresenter(frame); | |
383 frame.fPresenter.updatePresentation(frame.fOffset, fTextPresentation); | |
384 } | |
385 | |
386 createContextInfoPopup(); | |
387 | |
388 fContextInfoText.setText(frame.fInformation.getInformationDisplayString()); | |
389 if (fTextPresentation !is null) | |
390 TextPresentation.applyTextPresentation(fTextPresentation, fContextInfoText); | |
391 resize(frame.fVisibleOffset); | |
392 | |
393 if (initial) { | |
394 if (fContentAssistant.addContentAssistListener(this, ContentAssistant.CONTEXT_INFO_POPUP)) { | |
395 if (fContentAssistSubjectControlAdapter.getControl() !is null) { | |
135 | 396 fTextWidgetSelectionListener= new class() SelectionAdapter { |
129 | 397 /* |
398 * @see dwt.events.SelectionAdapter#widgetSelected(dwt.events.SelectionEvent) | |
399 */ | |
400 public void widgetSelected(SelectionEvent e) { | |
401 validateContextInformation(); | |
402 }}; | |
403 fContentAssistSubjectControlAdapter.addSelectionListener(fTextWidgetSelectionListener); | |
404 } | |
405 fContentAssistant.addToLayout(this, fContextInfoPopup, ContentAssistant.LayoutManager.LAYOUT_CONTEXT_INFO_POPUP, frame.fVisibleOffset); | |
406 fContextInfoPopup.setVisible(true); | |
407 } | |
408 } else { | |
409 fContentAssistant.layout(ContentAssistant.LayoutManager.LAYOUT_CONTEXT_INFO_POPUP, frame.fVisibleOffset); | |
410 } | |
411 } | |
412 | |
413 /** | |
414 * Computes all possible context information for the given offset. | |
415 * | |
416 * @param offset the offset | |
417 * @return all possible context information for the given offset | |
418 * @since 2.0 | |
419 */ | |
420 private IContextInformation[] computeContextInformation(int offset) { | |
421 return fContentAssistSubjectControlAdapter.computeContextInformation(fContentAssistant, offset); | |
422 } | |
423 | |
424 /** | |
425 *Returns the error message generated while computing context information. | |
426 * | |
427 * @return the error message | |
428 */ | |
429 private String getErrorMessage() { | |
430 return fContentAssistant.getErrorMessage(); | |
431 } | |
432 | |
433 /** | |
434 * Creates the context information popup. This is the tool tip like overlay window. | |
435 */ | |
436 private void createContextInfoPopup() { | |
437 if (Helper.okToUse(fContextInfoPopup)) | |
438 return; | |
439 | |
440 Control control= fContentAssistSubjectControlAdapter.getControl(); | |
441 Display display= control.getDisplay(); | |
442 | |
443 fContextInfoPopup= new Shell(control.getShell(), DWT.NO_TRIM | DWT.ON_TOP); | |
444 fContextInfoPopup.setBackground(display.getSystemColor(DWT.COLOR_BLACK)); | |
445 | |
446 fContextInfoText= new StyledText(fContextInfoPopup, DWT.MULTI | DWT.READ_ONLY | DWT.WRAP); | |
447 | |
448 Color c= fContentAssistant.getContextInformationPopupBackground(); | |
449 if (c is null) | |
450 c= display.getSystemColor(DWT.COLOR_INFO_BACKGROUND); | |
451 fContextInfoText.setBackground(c); | |
452 | |
453 c= fContentAssistant.getContextInformationPopupForeground(); | |
454 if (c is null) | |
455 c= display.getSystemColor(DWT.COLOR_INFO_FOREGROUND); | |
456 fContextInfoText.setForeground(c); | |
457 } | |
458 | |
459 /** | |
460 * Resizes the context information popup. | |
461 * | |
462 * @param offset the caret offset in widget coordinates | |
463 * @since 2.0 | |
464 */ | |
465 private void resize(int offset) { | |
466 Point size= fContextInfoText.computeSize(DWT.DEFAULT, DWT.DEFAULT, true); | |
467 final int TEXT_PAD= 0; | |
468 final int BORDER_PAD= 2; | |
469 final int PAD= TEXT_PAD + BORDER_PAD; | |
470 size.x += PAD; | |
471 Rectangle bounds= fContentAssistant.getLayoutManager().computeBoundsAboveBelow(fContextInfoPopup, size, offset); | |
472 if (bounds.width < size.x) | |
473 // we don't fit on the screen - try again and wrap | |
474 size= fContextInfoText.computeSize(bounds.width - PAD, DWT.DEFAULT, true); | |
158 | 475 |
129 | 476 size.x += TEXT_PAD; |
477 fContextInfoText.setSize(size); | |
478 fContextInfoText.setLocation(1,1); | |
479 size.x += BORDER_PAD; | |
480 size.y += BORDER_PAD; | |
481 fContextInfoPopup.setSize(size); | |
482 } | |
483 | |
484 /** | |
485 * Hides the context information popup. | |
486 */ | |
487 private void hideContextInfoPopup() { | |
488 | |
489 if (Helper.okToUse(fContextInfoPopup)) { | |
490 | |
491 int size= fContextFrameStack.size(); | |
492 if (size > 0) { | |
134 | 493 fLastContext= cast(ContextFrame) fContextFrameStack.pop(); |
129 | 494 -- size; |
495 } | |
496 | |
497 if (size > 0) { | |
134 | 498 ContextFrame current= cast(ContextFrame) fContextFrameStack.peek(); |
129 | 499 internalShowContextFrame(current, false); |
500 } else { | |
501 | |
502 fContentAssistant.removeContentAssistListener(this, ContentAssistant.CONTEXT_INFO_POPUP); | |
503 | |
504 if (fContentAssistSubjectControlAdapter.getControl() !is null) | |
505 fContentAssistSubjectControlAdapter.removeSelectionListener(fTextWidgetSelectionListener); | |
506 fTextWidgetSelectionListener= null; | |
507 | |
508 fContextInfoPopup.setVisible(false); | |
509 fContextInfoPopup.dispose(); | |
510 fContextInfoPopup= null; | |
511 | |
512 if (fTextPresentation !is null) { | |
513 fTextPresentation.clear(); | |
514 fTextPresentation= null; | |
515 } | |
516 } | |
517 } | |
518 | |
519 if (fContextInfoPopup is null) | |
520 fContentAssistant.contextInformationClosed(); | |
521 } | |
522 | |
523 /** | |
524 * Creates the context selector in case the user has the choice between multiple valid contexts | |
525 * at a given offset. | |
526 */ | |
527 private void createContextSelector() { | |
528 if (Helper.okToUse(fContextSelectorShell)) | |
529 return; | |
530 | |
531 Control control= fContentAssistSubjectControlAdapter.getControl(); | |
532 fContextSelectorShell= new Shell(control.getShell(), DWT.ON_TOP | DWT.RESIZE); | |
533 GridLayout layout= new GridLayout(); | |
534 layout.marginWidth= 0; | |
535 layout.marginHeight= 0; | |
536 fContextSelectorShell.setLayout(layout); | |
537 fContextSelectorShell.setBackground(control.getDisplay().getSystemColor(DWT.COLOR_BLACK)); | |
538 | |
539 | |
540 fContextSelectorTable= new Table(fContextSelectorShell, DWT.H_SCROLL | DWT.V_SCROLL); | |
541 fContextSelectorTable.setLocation(1, 1); | |
542 GridData gd= new GridData(GridData.FILL_BOTH); | |
543 gd.heightHint= fContextSelectorTable.getItemHeight() * 10; | |
544 gd.widthHint= 300; | |
545 fContextSelectorTable.setLayoutData(gd); | |
546 | |
547 fContextSelectorShell.pack(true); | |
548 | |
549 Color c= fContentAssistant.getContextSelectorBackground(); | |
550 if (c is null) | |
551 c= control.getDisplay().getSystemColor(DWT.COLOR_INFO_BACKGROUND); | |
552 fContextSelectorTable.setBackground(c); | |
553 | |
554 c= fContentAssistant.getContextSelectorForeground(); | |
555 if (c is null) | |
556 c= control.getDisplay().getSystemColor(DWT.COLOR_INFO_FOREGROUND); | |
557 fContextSelectorTable.setForeground(c); | |
558 | |
135 | 559 fContextSelectorTable.addSelectionListener(new class() SelectionListener { |
129 | 560 public void widgetSelected(SelectionEvent e) { |
561 } | |
562 | |
563 public void widgetDefaultSelected(SelectionEvent e) { | |
564 insertSelectedContext(); | |
565 hideContextSelector(); | |
566 } | |
567 }); | |
568 | |
569 fPopupCloser.install(fContentAssistant, fContextSelectorTable); | |
570 | |
571 fContextSelectorTable.setHeaderVisible(false); | |
572 fContentAssistant.addToLayout(this, fContextSelectorShell, ContentAssistant.LayoutManager.LAYOUT_CONTEXT_SELECTOR, fContentAssistant.getSelectionOffset()); | |
573 } | |
574 | |
575 /** | |
576 * Returns the minimal required height for the popup, may return 0 if the popup has not been | |
577 * created yet. | |
158 | 578 * |
129 | 579 * @return the minimal height |
580 * @since 3.3 | |
581 */ | |
582 int getMinimalHeight() { | |
583 int height= 0; | |
584 if (Helper.okToUse(fContextSelectorTable)) { | |
585 int items= fContextSelectorTable.getItemHeight() * 10; | |
586 Rectangle trim= fContextSelectorTable.computeTrim(0, 0, DWT.DEFAULT, items); | |
587 height= trim.height; | |
588 } | |
589 return height; | |
590 } | |
591 | |
592 /** | |
593 * Causes the context information of the context selected in the context selector | |
594 * to be displayed in the context information popup. | |
595 */ | |
596 private void insertSelectedContext() { | |
597 int i= fContextSelectorTable.getSelectionIndex(); | |
598 | |
599 if (i < 0 || i >= fContextSelectorInput.length) | |
600 return; | |
601 | |
602 int offset= fContentAssistSubjectControlAdapter.getSelectedRange().x; | |
603 internalShowContextInfo(createContextFrame(fContextSelectorInput[i], offset)); | |
604 } | |
605 | |
606 /** | |
607 * Sets the contexts in the context selector to the given set. | |
608 * | |
609 * @param contexts the possible contexts | |
610 */ | |
611 private void setContexts(IContextInformation[] contexts) { | |
612 if (Helper.okToUse(fContextSelectorTable)) { | |
613 | |
614 fContextSelectorInput= contexts; | |
615 | |
616 fContextSelectorTable.setRedraw(false); | |
617 fContextSelectorTable.removeAll(); | |
618 | |
619 TableItem item; | |
620 IContextInformation t; | |
621 for (int i= 0; i < contexts.length; i++) { | |
622 t= contexts[i]; | |
623 item= new TableItem(fContextSelectorTable, DWT.NULL); | |
624 if (t.getImage() !is null) | |
625 item.setImage(t.getImage()); | |
626 item.setText(t.getContextDisplayString()); | |
627 } | |
628 | |
629 fContextSelectorTable.select(0); | |
630 fContextSelectorTable.setRedraw(true); | |
631 } | |
632 } | |
633 | |
634 /** | |
635 * Displays the context selector. | |
636 */ | |
637 private void displayContextSelector() { | |
638 if (fContentAssistant.addContentAssistListener(this, ContentAssistant.CONTEXT_SELECTOR)) | |
639 fContextSelectorShell.setVisible(true); | |
640 } | |
641 | |
642 /** | |
643 * Hides the context selector. | |
644 */ | |
645 private void hideContextSelector() { | |
646 if (Helper.okToUse(fContextSelectorShell)) { | |
647 fContentAssistant.removeContentAssistListener(this, ContentAssistant.CONTEXT_SELECTOR); | |
648 | |
649 fPopupCloser.uninstall(); | |
650 fContextSelectorShell.setVisible(false); | |
651 fContextSelectorShell.dispose(); | |
652 fContextSelectorShell= null; | |
653 } | |
654 | |
655 if (!Helper.okToUse(fContextInfoPopup)) | |
656 fContentAssistant.contextInformationClosed(); | |
657 } | |
658 | |
659 /** | |
660 *Returns whether the context selector has the focus. | |
661 * | |
662 * @return <code>true</code> if the context selector has the focus | |
663 */ | |
664 public bool hasFocus() { | |
665 if (Helper.okToUse(fContextSelectorShell)) | |
666 return (fContextSelectorShell.isFocusControl() || fContextSelectorTable.isFocusControl()); | |
667 | |
668 return false; | |
669 } | |
670 | |
671 /** | |
672 * Hides context selector and context information popup. | |
673 */ | |
674 public void hide() { | |
675 hideContextSelector(); | |
676 hideContextInfoPopup(); | |
677 } | |
678 | |
679 /** | |
680 * Returns whether this context information popup is active. I.e., either | |
681 * a context selector or context information is displayed. | |
682 * | |
683 * @return <code>true</code> if the context selector is active | |
684 */ | |
685 public bool isActive() { | |
686 return (Helper.okToUse(fContextInfoPopup) || Helper.okToUse(fContextSelectorShell)); | |
687 } | |
688 | |
689 /* | |
690 * @see IContentAssistListener#verifyKey(VerifyEvent) | |
691 */ | |
692 public bool verifyKey(VerifyEvent e) { | |
693 if (Helper.okToUse(fContextSelectorShell)) | |
694 return contextSelectorKeyPressed(e); | |
695 if (Helper.okToUse(fContextInfoPopup)) | |
696 return contextInfoPopupKeyPressed(e); | |
697 return true; | |
698 } | |
699 | |
700 /** | |
701 * Processes a key stroke in the context selector. | |
702 * | |
703 * @param e the verify event describing the key stroke | |
704 * @return <code>true</code> if processing can be stopped | |
705 */ | |
706 private bool contextSelectorKeyPressed(VerifyEvent e) { | |
707 | |
708 char key= e.character; | |
709 if (key is 0) { | |
710 | |
711 int newSelection= fContextSelectorTable.getSelectionIndex(); | |
712 int visibleRows= (fContextSelectorTable.getSize().y / fContextSelectorTable.getItemHeight()) - 1; | |
713 int itemCount= fContextSelectorTable.getItemCount(); | |
714 switch (e.keyCode) { | |
715 case DWT.ARROW_UP : | |
716 newSelection -= 1; | |
717 if (newSelection < 0) | |
718 newSelection= itemCount - 1; | |
719 break; | |
720 | |
721 case DWT.ARROW_DOWN : | |
722 newSelection += 1; | |
723 if (newSelection > itemCount - 1) | |
724 newSelection= 0; | |
725 break; | |
726 | |
727 case DWT.PAGE_DOWN : | |
728 newSelection += visibleRows; | |
729 if (newSelection >= itemCount) | |
730 newSelection= itemCount - 1; | |
731 break; | |
732 | |
733 case DWT.PAGE_UP : | |
734 newSelection -= visibleRows; | |
735 if (newSelection < 0) | |
736 newSelection= 0; | |
737 break; | |
738 | |
739 case DWT.HOME : | |
740 newSelection= 0; | |
741 break; | |
742 | |
743 case DWT.END : | |
744 newSelection= itemCount - 1; | |
745 break; | |
746 | |
747 default : | |
748 if (e.keyCode !is DWT.CAPS_LOCK && e.keyCode !is DWT.MOD1 && e.keyCode !is DWT.MOD2 && e.keyCode !is DWT.MOD3 && e.keyCode !is DWT.MOD4) | |
749 hideContextSelector(); | |
750 return true; | |
751 } | |
752 | |
753 fContextSelectorTable.setSelection(newSelection); | |
754 fContextSelectorTable.showSelection(); | |
755 e.doit= false; | |
756 return false; | |
757 | |
758 } else if ('\t' is key) { | |
759 // switch focus to selector shell | |
760 e.doit= false; | |
761 fContextSelectorShell.setFocus(); | |
762 return false; | |
763 } else if (key is DWT.ESC) { | |
764 e.doit= false; | |
765 hideContextSelector(); | |
766 } | |
767 | |
768 return true; | |
769 } | |
770 | |
771 /** | |
772 * Processes a key stroke while the info popup is up. | |
773 * | |
774 * @param e the verify event describing the key stroke | |
775 * @return <code>true</code> if processing can be stopped | |
776 */ | |
777 private bool contextInfoPopupKeyPressed(KeyEvent e) { | |
778 | |
779 char key= e.character; | |
780 if (key is 0) { | |
781 | |
782 switch (e.keyCode) { | |
783 case DWT.ARROW_LEFT: | |
784 case DWT.ARROW_RIGHT: | |
785 validateContextInformation(); | |
786 break; | |
787 default: | |
788 if (e.keyCode !is DWT.CAPS_LOCK && e.keyCode !is DWT.MOD1 && e.keyCode !is DWT.MOD2 && e.keyCode !is DWT.MOD3 && e.keyCode !is DWT.MOD4) | |
789 hideContextInfoPopup(); | |
790 break; | |
791 } | |
792 | |
793 } else if (key is DWT.ESC) { | |
794 e.doit= false; | |
795 hideContextInfoPopup(); | |
796 } else { | |
797 validateContextInformation(); | |
798 } | |
799 return true; | |
800 } | |
801 | |
802 /* | |
803 * @see IEventConsumer#processEvent(VerifyEvent) | |
804 */ | |
805 public void processEvent(VerifyEvent event) { | |
806 if (Helper.okToUse(fContextSelectorShell)) | |
807 contextSelectorProcessEvent(event); | |
808 if (Helper.okToUse(fContextInfoPopup)) | |
809 contextInfoPopupProcessEvent(event); | |
810 } | |
811 | |
812 /** | |
813 * Processes a key stroke in the context selector. | |
814 * | |
815 * @param e the verify event describing the key stroke | |
816 */ | |
817 private void contextSelectorProcessEvent(VerifyEvent e) { | |
818 | |
819 if (e.start is e.end && e.text !is null && e.text.equals(fLineDelimiter)) { | |
820 e.doit= false; | |
821 insertSelectedContext(); | |
822 } | |
823 | |
824 hideContextSelector(); | |
825 } | |
826 | |
827 /** | |
828 * Processes a key stroke while the info popup is up. | |
829 * | |
830 * @param e the verify event describing the key stroke | |
831 */ | |
832 private void contextInfoPopupProcessEvent(VerifyEvent e) { | |
833 if (e.start !is e.end && (e.text is null || e.text.length() is 0)) | |
834 validateContextInformation(); | |
835 } | |
836 | |
837 /** | |
838 * Validates the context information for the viewer's actual cursor position. | |
839 */ | |
840 private void validateContextInformation() { | |
841 /* | |
842 * Post the code in the event queue in order to ensure that the | |
843 * action described by this verify key event has already been executed. | |
844 * Otherwise, we'd validate the context information based on the | |
845 * pre-key-stroke state. | |
846 */ | |
847 if (!Helper.okToUse(fContextInfoPopup)) | |
848 return; | |
849 | |
135 | 850 fContextInfoPopup.getDisplay().asyncExec(new class() Runnable { |
129 | 851 |
134 | 852 private ContextFrame fFrame= cast(ContextFrame) fContextFrameStack.peek(); |
129 | 853 |
854 public void run() { | |
855 // only do this if no other frames have been added in between | |
856 if (!fContextFrameStack.isEmpty() && fFrame is fContextFrameStack.peek()) { | |
857 int offset= fContentAssistSubjectControlAdapter.getSelectedRange().x; | |
858 | |
859 // iterate all contexts on the stack | |
860 while (Helper.okToUse(fContextInfoPopup) && !fContextFrameStack.isEmpty()) { | |
134 | 861 ContextFrame top= cast(ContextFrame) fContextFrameStack.peek(); |
129 | 862 if (top.fValidator is null || !top.fValidator.isContextInformationValid(offset)) { |
863 hideContextInfoPopup(); // loop variant: reduces the number of contexts on the stack | |
864 } else if (top.fPresenter !is null && top.fPresenter.updatePresentation(offset, fTextPresentation)) { | |
865 int widgetOffset= fContentAssistSubjectControlAdapter.getWidgetSelectionRange().x; | |
866 TextPresentation.applyTextPresentation(fTextPresentation, fContextInfoText); | |
867 resize(widgetOffset); | |
868 break; | |
869 } else | |
870 break; | |
871 } | |
872 } | |
873 } | |
874 }); | |
875 } | |
876 } |