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