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.link.LinkedModeUI;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.Arrays;
|
|
19 import java.util.HashSet;
|
|
20 import java.util.Iterator;
|
|
21 import java.util.List;
|
|
22 import java.util.Set;
|
|
23
|
|
24 import dwt.DWT;
|
|
25 import dwt.custom.StyledText;
|
|
26 import dwt.custom.VerifyKeyListener;
|
|
27 import dwt.events.ShellEvent;
|
|
28 import dwt.events.ShellListener;
|
|
29 import dwt.events.VerifyEvent;
|
|
30 import dwt.graphics.Point;
|
|
31 import dwt.widgets.Display;
|
|
32 import dwt.widgets.Shell;
|
|
33 import dwtx.core.runtime.Assert;
|
|
34 import dwtx.jface.internal.text.link.contentassist.ContentAssistant2;
|
|
35 import dwtx.jface.internal.text.link.contentassist.IProposalListener;
|
|
36 import dwtx.jface.text.BadLocationException;
|
|
37 import dwtx.jface.text.BadPartitioningException;
|
|
38 import dwtx.jface.text.BadPositionCategoryException;
|
|
39 import dwtx.jface.text.DefaultPositionUpdater;
|
|
40 import dwtx.jface.text.DocumentCommand;
|
|
41 import dwtx.jface.text.DocumentEvent;
|
|
42 import dwtx.jface.text.IAutoEditStrategy;
|
|
43 import dwtx.jface.text.IDocument;
|
|
44 import dwtx.jface.text.IDocumentExtension3;
|
|
45 import dwtx.jface.text.IDocumentListener;
|
|
46 import dwtx.jface.text.IEditingSupport;
|
|
47 import dwtx.jface.text.IEditingSupportRegistry;
|
|
48 import dwtx.jface.text.IPositionUpdater;
|
|
49 import dwtx.jface.text.IRegion;
|
|
50 import dwtx.jface.text.IRewriteTarget;
|
|
51 import dwtx.jface.text.ITextInputListener;
|
|
52 import dwtx.jface.text.ITextOperationTarget;
|
|
53 import dwtx.jface.text.ITextSelection;
|
|
54 import dwtx.jface.text.ITextViewer;
|
|
55 import dwtx.jface.text.ITextViewerExtension;
|
|
56 import dwtx.jface.text.ITextViewerExtension2;
|
|
57 import dwtx.jface.text.ITextViewerExtension5;
|
|
58 import dwtx.jface.text.Position;
|
|
59 import dwtx.jface.text.Region;
|
|
60 import dwtx.jface.text.contentassist.ICompletionProposal;
|
|
61 import dwtx.jface.text.contentassist.ICompletionProposalExtension6;
|
|
62 import dwtx.jface.text.source.IAnnotationModel;
|
|
63 import dwtx.jface.text.source.IAnnotationModelExtension;
|
|
64 import dwtx.jface.text.source.ISourceViewer;
|
|
65 import dwtx.jface.viewers.IPostSelectionProvider;
|
|
66 import dwtx.jface.viewers.ISelection;
|
|
67 import dwtx.jface.viewers.ISelectionChangedListener;
|
|
68 import dwtx.jface.viewers.SelectionChangedEvent;
|
|
69
|
|
70 /**
|
|
71 * The UI for linked mode. Detects events that influence behavior of the linked mode
|
|
72 * UI and acts upon them.
|
|
73 * <p>
|
|
74 * <code>LinkedModeUI</code> relies on all added
|
|
75 * <code>LinkedModeUITarget</code>s to provide implementations of
|
|
76 * <code>ITextViewer</code> that implement <code>ITextViewerExtension</code>,
|
|
77 * and the documents being edited to implement <code>IDocumentExtension3</code>.
|
|
78 * </p>
|
|
79 * <p>
|
|
80 * Clients may instantiate and extend this class.
|
|
81 * </p>
|
|
82 *
|
|
83 * @since 3.0
|
|
84 */
|
|
85 public class LinkedModeUI {
|
|
86
|
|
87 /* cycle constants */
|
|
88 /**
|
|
89 * Constant indicating that this UI should never cycle from the last
|
|
90 * position to the first and vice versa.
|
|
91 */
|
|
92 public static final Object CYCLE_NEVER= new Object();
|
|
93 /**
|
|
94 * Constant indicating that this UI should always cycle from the last
|
|
95 * position to the first and vice versa.
|
|
96 */
|
|
97 public static final Object CYCLE_ALWAYS= new Object();
|
|
98 /**
|
|
99 * Constant indicating that this UI should cycle from the last position to
|
|
100 * the first and vice versa if its model is not nested.
|
|
101 */
|
|
102 public static final Object CYCLE_WHEN_NO_PARENT= new Object();
|
|
103
|
|
104 /**
|
|
105 * Listener that gets notified when the linked mode UI switches its focus position.
|
|
106 * <p>
|
|
107 * Clients may implement this interface.
|
|
108 * </p>
|
|
109 */
|
|
110 public interface ILinkedModeUIFocusListener {
|
|
111 /**
|
|
112 * Called when the UI for the linked mode leaves a linked position.
|
|
113 *
|
|
114 * @param position the position being left
|
|
115 * @param target the target where <code>position</code> resides in
|
|
116 */
|
|
117 void linkingFocusLost(LinkedPosition position, LinkedModeUITarget target);
|
|
118 /**
|
|
119 * Called when the UI for the linked mode gives focus to a linked position.
|
|
120 *
|
|
121 * @param position the position being entered
|
|
122 * @param target the target where <code>position</code> resides in
|
|
123 */
|
|
124 void linkingFocusGained(LinkedPosition position, LinkedModeUITarget target);
|
|
125 }
|
|
126
|
|
127 /**
|
|
128 * Null object implementation of focus listener.
|
|
129 */
|
|
130 private static final class EmtpyFocusListener : ILinkedModeUIFocusListener {
|
|
131
|
|
132 public void linkingFocusGained(LinkedPosition position, LinkedModeUITarget target) {
|
|
133 // ignore
|
|
134 }
|
|
135
|
|
136 public void linkingFocusLost(LinkedPosition position, LinkedModeUITarget target) {
|
|
137 // ignore
|
|
138 }
|
|
139 }
|
|
140
|
|
141 /**
|
|
142 * A link target consists of a viewer and gets notified if the linked mode UI on
|
|
143 * it is being shown.
|
|
144 * <p>
|
|
145 * Clients may extend this class.
|
|
146 * </p>
|
|
147 * @since 3.0
|
|
148 */
|
|
149 public static abstract class LinkedModeUITarget : ILinkedModeUIFocusListener {
|
|
150 /**
|
|
151 * Returns the viewer represented by this target, never <code>null</code>.
|
|
152 *
|
|
153 * @return the viewer associated with this target.
|
|
154 */
|
|
155 public abstract ITextViewer getViewer();
|
|
156
|
|
157 /**
|
|
158 * The viewer's text widget is initialized when the UI first connects
|
|
159 * to the viewer and never changed thereafter. This is to keep the
|
|
160 * reference of the widget that we have registered our listeners with,
|
|
161 * as the viewer, when it gets disposed, does not remember it, resulting
|
|
162 * in a situation where we cannot uninstall the listeners and a memory leak.
|
|
163 */
|
|
164 StyledText fWidget;
|
|
165
|
|
166 /** The cached shell - same reason as fWidget. */
|
|
167 Shell fShell;
|
|
168
|
|
169 /** The registered listener, or <code>null</code>. */
|
|
170 KeyListener fKeyListener;
|
|
171
|
|
172 /** The cached custom annotation model. */
|
|
173 LinkedPositionAnnotations fAnnotationModel;
|
|
174 }
|
|
175
|
|
176 private static final class EmptyTarget : LinkedModeUITarget {
|
|
177
|
|
178 private ITextViewer fTextViewer;
|
|
179
|
|
180 /**
|
|
181 * @param viewer the viewer
|
|
182 */
|
|
183 public EmptyTarget(ITextViewer viewer) {
|
|
184 Assert.isNotNull(viewer);
|
|
185 fTextViewer= viewer;
|
|
186 }
|
|
187
|
|
188 /*
|
|
189 * @see dwtx.jdt.internal.ui.text.link2.LinkedModeUI.ILinkedUITarget#getViewer()
|
|
190 */
|
|
191 public ITextViewer getViewer() {
|
|
192 return fTextViewer;
|
|
193 }
|
|
194
|
|
195 /**
|
|
196 * {@inheritDoc}
|
|
197 */
|
|
198 public void linkingFocusLost(LinkedPosition position, LinkedModeUITarget target) {
|
|
199 }
|
|
200
|
|
201 /**
|
|
202 * {@inheritDoc}
|
|
203 */
|
|
204 public void linkingFocusGained(LinkedPosition position, LinkedModeUITarget target) {
|
|
205 }
|
|
206
|
|
207 }
|
|
208
|
|
209 /**
|
|
210 * Listens for state changes in the model.
|
|
211 */
|
|
212 private final class ExitListener : ILinkedModeListener {
|
|
213 public void left(LinkedModeModel model, int flags) {
|
|
214 leave(ILinkedModeListener.EXIT_ALL | flags);
|
|
215 }
|
|
216
|
|
217 public void suspend(LinkedModeModel model) {
|
|
218 disconnect();
|
|
219 redraw();
|
|
220 }
|
|
221
|
|
222 public void resume(LinkedModeModel model, int flags) {
|
|
223 if ((flags & ILinkedModeListener.EXIT_ALL) !is 0) {
|
|
224 leave(flags);
|
|
225 } else {
|
|
226 connect();
|
|
227 if ((flags & ILinkedModeListener.SELECT) !is 0)
|
|
228 select();
|
|
229 ensureAnnotationModelInstalled();
|
|
230 redraw();
|
|
231 }
|
|
232 }
|
|
233 }
|
|
234
|
|
235 /**
|
|
236 * Exit flags returned if a custom exit policy wants to exit linked mode.
|
|
237 * <p>
|
|
238 * Clients may instantiate this class.
|
|
239 * </p>
|
|
240 */
|
|
241 public static class ExitFlags {
|
|
242 /** The flags to return in the <code>leave</code> method. */
|
|
243 public int flags;
|
|
244 /** The doit flag of the checked <code>VerifyKeyEvent</code>. */
|
|
245 public bool doit;
|
|
246 /**
|
|
247 * Creates a new instance.
|
|
248 *
|
|
249 * @param flags the exit flags
|
|
250 * @param doit the doit flag for the verify event
|
|
251 */
|
|
252 public ExitFlags(int flags, bool doit) {
|
|
253 this.flags= flags;
|
|
254 this.doit= doit;
|
|
255 }
|
|
256 }
|
|
257
|
|
258 /**
|
|
259 * An exit policy can be registered by a caller to get custom exit
|
|
260 * behavior.
|
|
261 * <p>
|
|
262 * Clients may implement this interface.
|
|
263 * </p>
|
|
264 */
|
|
265 public interface IExitPolicy {
|
|
266 /**
|
|
267 * Checks whether the linked mode should be left after receiving the
|
|
268 * given <code>VerifyEvent</code> and selection. Note that the event
|
|
269 * carries widget coordinates as opposed to <code>offset</code> and
|
|
270 * <code>length</code> which are document coordinates.
|
|
271 *
|
|
272 * @param model the linked mode model
|
|
273 * @param event the verify event
|
|
274 * @param offset the offset of the current selection
|
|
275 * @param length the length of the current selection
|
|
276 * @return valid exit flags or <code>null</code> if no special action
|
|
277 * should be taken
|
|
278 */
|
|
279 ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length);
|
|
280 }
|
|
281
|
|
282 /**
|
|
283 * A NullObject implementation of <code>IExitPolicy</code>.
|
|
284 */
|
|
285 private static class NullExitPolicy : IExitPolicy {
|
|
286 /*
|
|
287 * @see dwtx.jdt.internal.ui.text.link2.LinkedModeUI.IExitPolicy#doExit(dwt.events.VerifyEvent, int, int)
|
|
288 */
|
|
289 public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
|
|
290 return null;
|
|
291 }
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * Listens for shell events and acts upon them.
|
|
296 */
|
|
297 private class Closer : ShellListener, ITextInputListener {
|
|
298
|
|
299 public void shellActivated(ShellEvent e) {
|
|
300 }
|
|
301
|
|
302 public void shellClosed(ShellEvent e) {
|
|
303 leave(ILinkedModeListener.EXIT_ALL);
|
|
304 }
|
|
305
|
|
306 public void shellDeactivated(ShellEvent e) {
|
|
307 // TODO re-enable after debugging
|
|
308 // if (true) return;
|
|
309
|
|
310 // from LinkedPositionUI:
|
|
311
|
|
312 // don't deactivate on focus lost, since the proposal popups may take focus
|
|
313 // plus: it doesn't hurt if you can check with another window without losing linked mode
|
|
314 // since there is no intrusive popup sticking out.
|
|
315
|
|
316 // need to check first what happens on reentering based on an open action
|
|
317 // Seems to be no problem
|
|
318
|
|
319 // Better:
|
|
320 // Check with content assistant and only leave if its not the proposal shell that took the
|
|
321 // focus away.
|
|
322
|
|
323 StyledText text;
|
|
324 final ITextViewer viewer;
|
|
325 Display display;
|
|
326
|
|
327 if (fCurrentTarget is null || (text= fCurrentTarget.fWidget) is null
|
|
328 || text.isDisposed() || (display= text.getDisplay()) is null
|
|
329 || display.isDisposed()
|
|
330 || (viewer= fCurrentTarget.getViewer()) is null)
|
|
331 {
|
|
332 leave(ILinkedModeListener.EXIT_ALL);
|
|
333 }
|
|
334 else
|
|
335 {
|
|
336 // Post in UI thread since the assistant popup will only get the focus after we lose it.
|
|
337 display.asyncExec(new Runnable() {
|
|
338 public void run() {
|
|
339 if (fIsActive && viewer instanceof IEditingSupportRegistry) {
|
|
340 IEditingSupport[] helpers= ((IEditingSupportRegistry) viewer).getRegisteredSupports();
|
|
341 for (int i= 0; i < helpers.length; i++) {
|
|
342 if (helpers[i].ownsFocusShell())
|
|
343 return;
|
|
344 }
|
|
345 }
|
|
346
|
|
347 // else
|
|
348 leave(ILinkedModeListener.EXIT_ALL);
|
|
349
|
|
350 }
|
|
351 });
|
|
352 }
|
|
353 }
|
|
354
|
|
355 public void shellDeiconified(ShellEvent e) {
|
|
356 }
|
|
357
|
|
358 public void shellIconified(ShellEvent e) {
|
|
359 leave(ILinkedModeListener.EXIT_ALL);
|
|
360 }
|
|
361
|
|
362 /*
|
|
363 * @see dwtx.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument)
|
|
364 */
|
|
365 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
|
|
366 leave(ILinkedModeListener.EXIT_ALL);
|
|
367 }
|
|
368
|
|
369 /*
|
|
370 * @see dwtx.jface.text.ITextInputListener#inputDocumentChanged(dwtx.jface.text.IDocument, dwtx.jface.text.IDocument)
|
|
371 */
|
|
372 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
|
|
373 }
|
|
374
|
|
375 }
|
|
376
|
|
377 /**
|
|
378 * @since 3.1
|
|
379 */
|
|
380 private class DocumentListener : IDocumentListener {
|
|
381 /*
|
|
382 * @see dwtx.jface.text.IDocumentListener#documentAboutToBeChanged(dwtx.jface.text.DocumentEvent)
|
|
383 */
|
|
384 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
385
|
|
386 // default behavior: any document change outside a linked position
|
|
387 // causes us to exit
|
|
388 int end= event.getOffset() + event.getLength();
|
|
389 for (int offset= event.getOffset(); offset <= end; offset++) {
|
|
390 if (!fModel.anyPositionContains(offset)) {
|
|
391 ITextViewer viewer= fCurrentTarget.getViewer();
|
|
392 if (fFramePosition !is null && viewer instanceof IEditingSupportRegistry) {
|
|
393 IEditingSupport[] helpers= ((IEditingSupportRegistry) viewer).getRegisteredSupports();
|
|
394 for (int i= 0; i < helpers.length; i++) {
|
|
395 if (helpers[i].isOriginator(null, new Region(fFramePosition.getOffset(), fFramePosition.getLength())))
|
|
396 return;
|
|
397 }
|
|
398 }
|
|
399
|
|
400 leave(ILinkedModeListener.EXTERNAL_MODIFICATION);
|
|
401 return;
|
|
402 }
|
|
403 }
|
|
404 }
|
|
405
|
|
406 /*
|
|
407 * @see dwtx.jface.text.IDocumentListener#documentChanged(dwtx.jface.text.DocumentEvent)
|
|
408 */
|
|
409 public void documentChanged(DocumentEvent event) {
|
|
410 }
|
|
411 }
|
|
412
|
|
413 /**
|
|
414 * Listens for key events, checks the exit policy for custom exit
|
|
415 * strategies but defaults to handling Tab, Enter, and Escape.
|
|
416 */
|
|
417 private class KeyListener : VerifyKeyListener {
|
|
418
|
|
419 private bool fIsEnabled= true;
|
|
420
|
|
421 public void verifyKey(VerifyEvent event) {
|
|
422
|
|
423 if (!event.doit || !fIsEnabled)
|
|
424 return;
|
|
425
|
|
426 Point selection= fCurrentTarget.getViewer().getSelectedRange();
|
|
427 int offset= selection.x;
|
|
428 int length= selection.y;
|
|
429
|
|
430 // if the custom exit policy returns anything, use that
|
|
431 ExitFlags exitFlags= fExitPolicy.doExit(fModel, event, offset, length);
|
|
432 if (exitFlags !is null) {
|
|
433 leave(exitFlags.flags);
|
|
434 event.doit= exitFlags.doit;
|
|
435 return;
|
|
436 }
|
|
437
|
|
438 // standard behavior:
|
|
439 // (Shift+)Tab: jumps from position to position, depending on cycle mode
|
|
440 // Enter: accepts all entries and leaves all (possibly stacked) environments, the last sets the caret
|
|
441 // Esc: accepts all entries and leaves all (possibly stacked) environments, the caret stays
|
|
442 // ? what do we do to leave one level of a cycling model that is stacked?
|
|
443 // -> This is only the case if the level was set up with forced cycling (CYCLE_ALWAYS), in which case
|
|
444 // the caller is sure that one does not need by-level exiting.
|
|
445 switch (event.character) {
|
|
446 // [SHIFT-]TAB = hop between edit boxes
|
|
447 case 0x09:
|
|
448 if (!(fExitPosition !is null && fExitPosition.includes(offset)) && !fModel.anyPositionContains(offset)) {
|
|
449 // outside any edit box -> leave (all? TODO should only leave the affected, level and forward to the next upper)
|
|
450 leave(ILinkedModeListener.EXIT_ALL);
|
|
451 break;
|
|
452 }
|
|
453
|
|
454 if (event.stateMask is DWT.SHIFT)
|
|
455 previous();
|
|
456 else
|
|
457 next();
|
|
458
|
|
459 event.doit= false;
|
|
460 break;
|
|
461
|
|
462 // ENTER
|
|
463 case 0x0A:
|
|
464 // Ctrl+Enter on WinXP
|
|
465 case 0x0D:
|
|
466 // if ((fExitPosition !is null && fExitPosition.includes(offset)) || !fModel.anyPositionContains(offset)) {
|
|
467 if (!fModel.anyPositionContains(offset)) {
|
|
468 // if ((fExitPosition is null || !fExitPosition.includes(offset)) && !fModel.anyPositionContains(offset)) {
|
|
469 // outside any edit box or on exit position -> leave (all? TODO should only leave the affected, level and forward to the next upper)
|
|
470 leave(ILinkedModeListener.EXIT_ALL);
|
|
471 break;
|
|
472 }
|
|
473
|
|
474 // normal case: exit entire stack and put caret to final position
|
|
475 leave(ILinkedModeListener.EXIT_ALL | ILinkedModeListener.UPDATE_CARET);
|
|
476 event.doit= false;
|
|
477 break;
|
|
478
|
|
479 // ESC
|
|
480 case 0x1B:
|
|
481 // exit entire stack and leave caret
|
|
482 leave(ILinkedModeListener.EXIT_ALL);
|
|
483 event.doit= false;
|
|
484 break;
|
|
485
|
|
486 default:
|
|
487 if (event.character !is 0) {
|
|
488 if (!controlUndoBehavior(offset, length)) {
|
|
489 leave(ILinkedModeListener.EXIT_ALL);
|
|
490 break;
|
|
491 }
|
|
492 }
|
|
493 }
|
|
494 }
|
|
495
|
|
496 private bool controlUndoBehavior(int offset, int length) {
|
|
497 LinkedPosition position= fModel.findPosition(new LinkedPosition(fCurrentTarget.getViewer().getDocument(), offset, length, LinkedPositionGroup.NO_STOP));
|
|
498 if (position !is null) {
|
|
499
|
|
500 // if the last position is not the same and there is an open change: close it.
|
|
501 if (!position.equals(fPreviousPosition))
|
|
502 endCompoundChange();
|
|
503
|
|
504 beginCompoundChange();
|
|
505 }
|
|
506
|
|
507 fPreviousPosition= position;
|
|
508 return fPreviousPosition !is null;
|
|
509 }
|
|
510
|
|
511 /**
|
|
512 * @param enabled the new enabled state
|
|
513 */
|
|
514 public void setEnabled(bool enabled) {
|
|
515 fIsEnabled= enabled;
|
|
516 }
|
|
517
|
|
518 }
|
|
519
|
|
520 /**
|
|
521 * Installed as post selection listener on the watched viewer. Updates the
|
|
522 * linked position after cursor movement, even to positions not in the
|
|
523 * iteration list.
|
|
524 */
|
|
525 private class MySelectionListener : ISelectionChangedListener {
|
|
526
|
|
527 /*
|
|
528 * @see dwtx.jface.viewers.ISelectionChangedListener#selectionChanged(dwtx.jface.viewers.SelectionChangedEvent)
|
|
529 */
|
|
530 public void selectionChanged(SelectionChangedEvent event) {
|
|
531 ISelection selection= event.getSelection();
|
|
532 if (selection instanceof ITextSelection) {
|
|
533 ITextSelection textsel= (ITextSelection) selection;
|
|
534 if (event.getSelectionProvider() instanceof ITextViewer) {
|
|
535 IDocument doc= ((ITextViewer) event.getSelectionProvider()).getDocument();
|
|
536 if (doc !is null) {
|
|
537 int offset= textsel.getOffset();
|
|
538 int length= textsel.getLength();
|
|
539 if (offset >= 0 && length >= 0) {
|
|
540 LinkedPosition find= new LinkedPosition(doc, offset, length, LinkedPositionGroup.NO_STOP);
|
|
541 LinkedPosition pos= fModel.findPosition(find);
|
|
542 if (pos is null && fExitPosition !is null && fExitPosition.includes(find))
|
|
543 pos= fExitPosition;
|
|
544
|
|
545 if (pos !is null)
|
|
546 switchPosition(pos, false, false);
|
|
547 }
|
|
548 }
|
|
549 }
|
|
550 }
|
|
551 }
|
|
552
|
|
553 }
|
|
554
|
|
555 private class ProposalListener : IProposalListener {
|
|
556
|
|
557 /*
|
|
558 * @see dwtx.jface.internal.text.link.contentassist.IProposalListener#proposalChosen(dwtx.jface.text.contentassist.ICompletionProposal)
|
|
559 */
|
|
560 public void proposalChosen(ICompletionProposal proposal) {
|
|
561 next();
|
|
562 }
|
|
563 }
|
|
564
|
|
565 /** The current viewer. */
|
|
566 private LinkedModeUITarget fCurrentTarget;
|
|
567 /**
|
|
568 * The manager of the linked positions we provide a UI for.
|
|
569 * @since 3.1
|
|
570 */
|
|
571 private LinkedModeModel fModel;
|
|
572 /** The set of viewers we manage. */
|
|
573 private LinkedModeUITarget[] fTargets;
|
|
574 /** The iterator over the tab stop positions. */
|
|
575 private TabStopIterator fIterator;
|
|
576
|
|
577 /* Our team of event listeners */
|
|
578 /** The shell listener. */
|
|
579 private Closer fCloser= new Closer();
|
|
580 /** The linked mode listener. */
|
|
581 private ILinkedModeListener fLinkedListener= new ExitListener();
|
|
582 /** The selection listener. */
|
|
583 private MySelectionListener fSelectionListener= new MySelectionListener();
|
|
584 /** The content assist listener. */
|
|
585 private ProposalListener fProposalListener= new ProposalListener();
|
|
586 /**
|
|
587 * The document listener.
|
|
588 * @since 3.1
|
|
589 */
|
|
590 private IDocumentListener fDocumentListener= new DocumentListener();
|
|
591
|
|
592 /** The last caret position, used by fCaretListener. */
|
|
593 private final Position fCaretPosition= new Position(0, 0);
|
|
594 /** The exit policy to control custom exit behavior */
|
|
595 private IExitPolicy fExitPolicy= new NullExitPolicy();
|
|
596 /** The current frame position shown in the UI, or <code>null</code>. */
|
|
597 private LinkedPosition fFramePosition;
|
|
598 /** The last visited position, used for undo / redo. */
|
|
599 private LinkedPosition fPreviousPosition;
|
|
600 /** The content assistant used to show proposals. */
|
|
601 private ContentAssistant2 fAssistant;
|
|
602 /** The exit position. */
|
|
603 private LinkedPosition fExitPosition;
|
|
604 /** State indicator to prevent multiple invocation of leave. */
|
|
605 private bool fIsActive= false;
|
|
606 /** The position updater for the exit position. */
|
|
607 private IPositionUpdater fPositionUpdater= new DefaultPositionUpdater(getCategory());
|
|
608 /** Whether to show context info. */
|
|
609 private bool fDoContextInfo= false;
|
|
610 /** Whether we have begun a compound change, but not yet closed. */
|
|
611 private bool fHasOpenCompoundChange= false;
|
|
612 /** The position listener. */
|
|
613 private ILinkedModeUIFocusListener fPositionListener= new EmtpyFocusListener();
|
|
614 private IAutoEditStrategy fAutoEditVetoer= new IAutoEditStrategy() {
|
|
615
|
|
616 /*
|
|
617 * @see dwtx.jface.text.IAutoEditStrategy#customizeDocumentCommand(dwtx.jface.text.IDocument, dwtx.jface.text.DocumentCommand)
|
|
618 */
|
|
619 public void customizeDocumentCommand(IDocument document, DocumentCommand command) {
|
|
620 // invalidate the change to ensure that the change is performed on the document only.
|
|
621 if (fModel.anyPositionContains(command.offset)) {
|
|
622 command.doit= false;
|
|
623 command.caretOffset= command.offset + command.length;
|
|
624 }
|
|
625
|
|
626 }
|
|
627 };
|
|
628
|
|
629
|
|
630 /** Whether this UI is in simple highlighting mode or not. */
|
|
631 private bool fSimple;
|
|
632
|
|
633 /**
|
|
634 * Tells whether colored labels support is enabled.
|
|
635 * @since 3.4
|
|
636 */
|
|
637 private bool fIsColoredLabelsSupportEnabled= false;
|
|
638
|
|
639 /**
|
|
640 * Creates a new UI on the given model and the set of viewers. The model
|
|
641 * must provide a tab stop sequence with a non-empty list of tab stops.
|
|
642 *
|
|
643 * @param model the linked mode model
|
|
644 * @param targets the non-empty list of targets upon which the linked mode
|
|
645 * UI should act
|
|
646 */
|
|
647 public LinkedModeUI(LinkedModeModel model, LinkedModeUITarget[] targets) {
|
|
648 constructor(model, targets);
|
|
649 }
|
|
650
|
|
651 /**
|
|
652 * Convenience constructor for just one viewer.
|
|
653 *
|
|
654 * @param model the linked mode model
|
|
655 * @param viewer the viewer upon which the linked mode UI should act
|
|
656 */
|
|
657 public LinkedModeUI(LinkedModeModel model, ITextViewer viewer) {
|
|
658 constructor(model, new LinkedModeUITarget[]{new EmptyTarget(viewer)});
|
|
659 }
|
|
660
|
|
661 /**
|
|
662 * Convenience constructor for multiple viewers.
|
|
663 *
|
|
664 * @param model the linked mode model
|
|
665 * @param viewers the non-empty list of viewers upon which the linked mode
|
|
666 * UI should act
|
|
667 */
|
|
668 public LinkedModeUI(LinkedModeModel model, ITextViewer[] viewers) {
|
|
669 LinkedModeUITarget[] array= new LinkedModeUITarget[viewers.length];
|
|
670 for (int i= 0; i < array.length; i++) {
|
|
671 array[i]= new EmptyTarget(viewers[i]);
|
|
672 }
|
|
673 constructor(model, array);
|
|
674 }
|
|
675
|
|
676 /**
|
|
677 * Convenience constructor for one target.
|
|
678 *
|
|
679 * @param model the linked mode model
|
|
680 * @param target the target upon which the linked mode UI should act
|
|
681 */
|
|
682 public LinkedModeUI(LinkedModeModel model, LinkedModeUITarget target) {
|
|
683 constructor(model, new LinkedModeUITarget[]{target});
|
|
684 }
|
|
685
|
|
686 /**
|
|
687 * This does the actual constructor work.
|
|
688 *
|
|
689 * @param model the linked mode model
|
|
690 * @param targets the non-empty array of targets upon which the linked mode UI
|
|
691 * should act
|
|
692 */
|
|
693 private void constructor(LinkedModeModel model, LinkedModeUITarget[] targets) {
|
|
694 Assert.isNotNull(model);
|
|
695 Assert.isNotNull(targets);
|
|
696 Assert.isTrue(targets.length > 0);
|
|
697 Assert.isTrue(model.getTabStopSequence().size() > 0);
|
|
698
|
|
699 fModel= model;
|
|
700 fTargets= targets;
|
|
701 fCurrentTarget= targets[0];
|
|
702 fIterator= new TabStopIterator(fModel.getTabStopSequence());
|
|
703 fIterator.setCycling(!fModel.isNested());
|
|
704 fModel.addLinkingListener(fLinkedListener);
|
|
705
|
|
706 fAssistant= new ContentAssistant2();
|
|
707 fAssistant.addProposalListener(fProposalListener);
|
|
708 // TODO find a way to set up content assistant.
|
|
709 // fAssistant.setDocumentPartitioning(IJavaPartitions.JAVA_PARTITIONING);
|
|
710 fAssistant.enableColoredLabels(fIsColoredLabelsSupportEnabled);
|
|
711 fCaretPosition.delete();
|
|
712 }
|
|
713
|
|
714 /**
|
|
715 * Starts this UI on the first position.
|
|
716 */
|
|
717 public void enter() {
|
|
718 fIsActive= true;
|
|
719 connect();
|
|
720 next();
|
|
721 }
|
|
722
|
|
723 /**
|
|
724 * Sets an <code>IExitPolicy</code> to customize the exit behavior of
|
|
725 * this linked mode UI.
|
|
726 *
|
|
727 * @param policy the exit policy to use.
|
|
728 */
|
|
729 public void setExitPolicy(IExitPolicy policy) {
|
|
730 fExitPolicy= policy;
|
|
731 }
|
|
732
|
|
733 /**
|
|
734 * Sets the exit position to move the caret to when linked mode mode is
|
|
735 * exited.
|
|
736 *
|
|
737 * @param target the target where the exit position is located
|
|
738 * @param offset the offset of the exit position
|
|
739 * @param length the length of the exit position (in case there should be a
|
|
740 * selection)
|
|
741 * @param sequence set to the tab stop position of the exit position, or
|
|
742 * <code>LinkedPositionGroup.NO_STOP</code> if there should be no
|
|
743 * tab stop.
|
|
744 * @throws BadLocationException if the position is not valid in the viewer's
|
|
745 * document
|
|
746 */
|
|
747 public void setExitPosition(LinkedModeUITarget target, int offset, int length, int sequence) throws BadLocationException {
|
|
748 // remove any existing exit position
|
|
749 if (fExitPosition !is null) {
|
|
750 fExitPosition.getDocument().removePosition(fExitPosition);
|
|
751 fIterator.removePosition(fExitPosition);
|
|
752 fExitPosition= null;
|
|
753 }
|
|
754
|
|
755 IDocument doc= target.getViewer().getDocument();
|
|
756 if (doc is null)
|
|
757 return;
|
|
758
|
|
759 fExitPosition= new LinkedPosition(doc, offset, length, sequence);
|
|
760 doc.addPosition(fExitPosition); // gets removed in leave()
|
|
761 if (sequence !is LinkedPositionGroup.NO_STOP)
|
|
762 fIterator.addPosition(fExitPosition);
|
|
763
|
|
764 }
|
|
765
|
|
766 /**
|
|
767 * Sets the exit position to move the caret to when linked mode is exited.
|
|
768 *
|
|
769 * @param viewer the viewer where the exit position is located
|
|
770 * @param offset the offset of the exit position
|
|
771 * @param length the length of the exit position (in case there should be a
|
|
772 * selection)
|
|
773 * @param sequence set to the tab stop position of the exit position, or
|
|
774 * <code>LinkedPositionGroup.NO_STOP</code> if there should be no tab stop.
|
|
775 * @throws BadLocationException if the position is not valid in the
|
|
776 * viewer's document
|
|
777 */
|
|
778 public void setExitPosition(ITextViewer viewer, int offset, int length, int sequence) throws BadLocationException {
|
|
779 setExitPosition(new EmptyTarget(viewer), offset, length, sequence);
|
|
780 }
|
|
781
|
|
782 /**
|
|
783 * Sets the cycling mode to either of <code>CYCLING_ALWAYS</code>,
|
|
784 * <code>CYCLING_NEVER</code>, or <code>CYCLING_WHEN_NO_PARENT</code>,
|
|
785 * which is the default.
|
|
786 *
|
|
787 * @param mode the new cycling mode.
|
|
788 */
|
|
789 public void setCyclingMode(Object mode) {
|
|
790 if (mode !is CYCLE_ALWAYS && mode !is CYCLE_NEVER && mode !is CYCLE_WHEN_NO_PARENT)
|
|
791 throw new IllegalArgumentException();
|
|
792
|
|
793 if (mode is CYCLE_ALWAYS || mode is CYCLE_WHEN_NO_PARENT && !fModel.isNested())
|
|
794 fIterator.setCycling(true);
|
|
795 else
|
|
796 fIterator.setCycling(false);
|
|
797 }
|
|
798
|
|
799 void next() {
|
|
800 if (fIterator.hasNext(fFramePosition)) {
|
|
801 switchPosition(fIterator.next(fFramePosition), true, true);
|
|
802 return;
|
|
803 }
|
|
804 leave(ILinkedModeListener.UPDATE_CARET);
|
|
805 }
|
|
806
|
|
807 void previous() {
|
|
808 if (fIterator.hasPrevious(fFramePosition)) {
|
|
809 switchPosition(fIterator.previous(fFramePosition), true, true);
|
|
810 } else
|
|
811 // dont't update caret, but rather select the current frame
|
|
812 leave(ILinkedModeListener.SELECT);
|
|
813 }
|
|
814
|
|
815 private void triggerContextInfo() {
|
|
816 ITextOperationTarget target= fCurrentTarget.getViewer().getTextOperationTarget();
|
|
817 if (target !is null) {
|
|
818 if (target.canDoOperation(ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION))
|
|
819 target.doOperation(ISourceViewer.CONTENTASSIST_CONTEXT_INFORMATION);
|
|
820 }
|
|
821 }
|
|
822
|
|
823 /** Trigger content assist on choice positions */
|
|
824 private void triggerContentAssist() {
|
|
825 if (fFramePosition instanceof ProposalPosition) {
|
|
826 ProposalPosition pp= (ProposalPosition) fFramePosition;
|
|
827 ICompletionProposal[] choices= pp.getChoices();
|
|
828 if (choices !is null && choices.length > 0) {
|
|
829 fAssistant.setCompletions(choices);
|
|
830 fAssistant.showPossibleCompletions();
|
|
831 return;
|
|
832 }
|
|
833 }
|
|
834
|
|
835 fAssistant.setCompletions(new ICompletionProposal[0]);
|
|
836 fAssistant.hidePossibleCompletions();
|
|
837 }
|
|
838
|
|
839 private void switchPosition(LinkedPosition pos, bool select, bool showProposals) {
|
|
840 Assert.isNotNull(pos);
|
|
841 if (pos.equals(fFramePosition))
|
|
842 return;
|
|
843
|
|
844 if (fFramePosition !is null && fCurrentTarget !is null)
|
|
845 fPositionListener.linkingFocusLost(fFramePosition, fCurrentTarget);
|
|
846
|
|
847 // undo
|
|
848 endCompoundChange();
|
|
849
|
|
850 redraw(); // redraw current position being left - usually not needed
|
|
851 IDocument oldDoc= fFramePosition is null ? null : fFramePosition.getDocument();
|
|
852 IDocument newDoc= pos.getDocument();
|
|
853
|
|
854 switchViewer(oldDoc, newDoc, pos);
|
|
855 fFramePosition= pos;
|
|
856
|
|
857 if (select)
|
|
858 select();
|
|
859 if (fFramePosition is fExitPosition && !fIterator.isCycling())
|
|
860 leave(ILinkedModeListener.NONE);
|
|
861 else {
|
|
862 redraw(); // redraw new position
|
|
863 ensureAnnotationModelInstalled();
|
|
864 }
|
|
865 if (showProposals)
|
|
866 triggerContentAssist();
|
|
867 if (fFramePosition !is fExitPosition && fDoContextInfo)
|
|
868 triggerContextInfo();
|
|
869
|
|
870 if (fFramePosition !is null && fCurrentTarget !is null)
|
|
871 fPositionListener.linkingFocusGained(fFramePosition, fCurrentTarget);
|
|
872
|
|
873 }
|
|
874
|
|
875 private void ensureAnnotationModelInstalled() {
|
|
876 LinkedPositionAnnotations lpa= fCurrentTarget.fAnnotationModel;
|
|
877 if (lpa !is null) {
|
|
878 ITextViewer viewer= fCurrentTarget.getViewer();
|
|
879 if (viewer instanceof ISourceViewer) {
|
|
880 ISourceViewer sv= (ISourceViewer) viewer;
|
|
881 IAnnotationModel model= sv.getAnnotationModel();
|
|
882 if (model instanceof IAnnotationModelExtension) {
|
|
883 IAnnotationModelExtension ext= (IAnnotationModelExtension) model;
|
|
884 IAnnotationModel ourModel= ext.getAnnotationModel(getUniqueKey());
|
|
885 if (ourModel is null) {
|
|
886 ext.addAnnotationModel(getUniqueKey(), lpa);
|
|
887 }
|
|
888 }
|
|
889 }
|
|
890 }
|
|
891 }
|
|
892
|
|
893 private void uninstallAnnotationModel(LinkedModeUITarget target) {
|
|
894 ITextViewer viewer= target.getViewer();
|
|
895 if (viewer instanceof ISourceViewer) {
|
|
896 ISourceViewer sv= (ISourceViewer) viewer;
|
|
897 IAnnotationModel model= sv.getAnnotationModel();
|
|
898 if (model instanceof IAnnotationModelExtension) {
|
|
899 IAnnotationModelExtension ext= (IAnnotationModelExtension) model;
|
|
900 ext.removeAnnotationModel(getUniqueKey());
|
|
901 }
|
|
902 }
|
|
903 }
|
|
904
|
|
905 private void switchViewer(IDocument oldDoc, IDocument newDoc, LinkedPosition pos) {
|
|
906 if (oldDoc !is newDoc) {
|
|
907
|
|
908 // redraw current document with new position before switching viewer
|
|
909 if (fCurrentTarget.fAnnotationModel !is null)
|
|
910 fCurrentTarget.fAnnotationModel.switchToPosition(fModel, pos);
|
|
911
|
|
912 LinkedModeUITarget target= null;
|
|
913 for (int i= 0; i < fTargets.length; i++) {
|
|
914 if (fTargets[i].getViewer().getDocument() is newDoc) {
|
|
915 target= fTargets[i];
|
|
916 break;
|
|
917 }
|
|
918 }
|
|
919 if (target !is fCurrentTarget) {
|
|
920 disconnect();
|
|
921 fCurrentTarget= target;
|
|
922 target.linkingFocusLost(fFramePosition, target);
|
|
923 connect();
|
|
924 ensureAnnotationModelInstalled();
|
|
925 if (fCurrentTarget !is null)
|
|
926 fCurrentTarget.linkingFocusGained(pos, fCurrentTarget);
|
|
927 }
|
|
928 }
|
|
929 }
|
|
930
|
|
931 private void select() {
|
|
932 ITextViewer viewer= fCurrentTarget.getViewer();
|
|
933 if (viewer instanceof ITextViewerExtension5) {
|
|
934 ITextViewerExtension5 extension5= (ITextViewerExtension5) viewer;
|
|
935 extension5.exposeModelRange(new Region(fFramePosition.offset, fFramePosition.length));
|
|
936 } else if (!viewer.overlapsWithVisibleRegion(fFramePosition.offset, fFramePosition.length)) {
|
|
937 viewer.resetVisibleRegion();
|
|
938 }
|
|
939 viewer.revealRange(fFramePosition.offset, fFramePosition.length);
|
|
940 viewer.setSelectedRange(fFramePosition.offset, fFramePosition.length);
|
|
941 }
|
|
942
|
|
943 private void redraw() {
|
|
944 if (fCurrentTarget.fAnnotationModel !is null)
|
|
945 fCurrentTarget.fAnnotationModel.switchToPosition(fModel, fFramePosition);
|
|
946 }
|
|
947
|
|
948 private void connect() {
|
|
949 Assert.isNotNull(fCurrentTarget);
|
|
950 ITextViewer viewer= fCurrentTarget.getViewer();
|
|
951 Assert.isNotNull(viewer);
|
|
952 fCurrentTarget.fWidget= viewer.getTextWidget();
|
|
953 if (fCurrentTarget.fWidget is null)
|
|
954 leave(ILinkedModeListener.EXIT_ALL);
|
|
955
|
|
956 if (fCurrentTarget.fKeyListener is null) {
|
|
957 fCurrentTarget.fKeyListener= new KeyListener();
|
|
958 ((ITextViewerExtension) viewer).prependVerifyKeyListener(fCurrentTarget.fKeyListener);
|
|
959 } else
|
|
960 fCurrentTarget.fKeyListener.setEnabled(true);
|
|
961
|
|
962 registerAutoEditVetoer(viewer);
|
|
963
|
|
964 ((IPostSelectionProvider) viewer).addPostSelectionChangedListener(fSelectionListener);
|
|
965
|
|
966 createAnnotationModel();
|
|
967
|
|
968 showSelection();
|
|
969
|
|
970 fCurrentTarget.fShell= fCurrentTarget.fWidget.getShell();
|
|
971 if (fCurrentTarget.fShell is null)
|
|
972 leave(ILinkedModeListener.EXIT_ALL);
|
|
973 fCurrentTarget.fShell.addShellListener(fCloser);
|
|
974
|
|
975 fAssistant.install(viewer);
|
|
976
|
|
977 viewer.addTextInputListener(fCloser);
|
|
978
|
|
979 viewer.getDocument().addDocumentListener(fDocumentListener);
|
|
980 }
|
|
981
|
|
982 /**
|
|
983 * Reveals the selection on the current target's widget, if it is valid.
|
|
984 */
|
|
985 private void showSelection() {
|
|
986 final StyledText widget= fCurrentTarget.fWidget;
|
|
987 if (widget is null || widget.isDisposed())
|
|
988 return;
|
|
989
|
|
990 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=132263
|
|
991 widget.getDisplay().asyncExec(new Runnable() {
|
|
992 public void run() {
|
|
993 if (!widget.isDisposed())
|
|
994 try {
|
|
995 widget.showSelection();
|
|
996 } catch (IllegalArgumentException e) {
|
|
997 /*
|
|
998 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66914
|
|
999 * if the StyledText is in setRedraw(false) mode, its
|
|
1000 * selection may not be up2date and calling showSelection
|
|
1001 * will throw an IAE.
|
|
1002 * We don't have means to find out whether the selection is valid
|
|
1003 * or whether the widget is redrawing or not therefore we try
|
|
1004 * and ignore an IAE.
|
|
1005 */
|
|
1006 }
|
|
1007 }
|
|
1008 });
|
|
1009 }
|
|
1010
|
|
1011 /**
|
|
1012 * Registers our auto edit vetoer with the viewer.
|
|
1013 *
|
|
1014 * @param viewer the viewer we want to veto ui-triggered changes within
|
|
1015 * linked positions
|
|
1016 */
|
|
1017 private void registerAutoEditVetoer(ITextViewer viewer) {
|
|
1018 try {
|
|
1019 String[] contentTypes= getContentTypes(viewer.getDocument());
|
|
1020 if (viewer instanceof ITextViewerExtension2) {
|
|
1021 ITextViewerExtension2 vExtension= ((ITextViewerExtension2) viewer);
|
|
1022 for (int i= 0; i < contentTypes.length; i++) {
|
|
1023 vExtension.prependAutoEditStrategy(fAutoEditVetoer, contentTypes[i]);
|
|
1024 }
|
|
1025 } else {
|
|
1026 Assert.isTrue(false);
|
|
1027 }
|
|
1028
|
|
1029 } catch (BadPartitioningException e) {
|
|
1030 leave(ILinkedModeListener.EXIT_ALL);
|
|
1031 }
|
|
1032 }
|
|
1033
|
|
1034 private void unregisterAutoEditVetoer(ITextViewer viewer) {
|
|
1035 try {
|
|
1036 String[] contentTypes= getContentTypes(viewer.getDocument());
|
|
1037 if (viewer instanceof ITextViewerExtension2) {
|
|
1038 ITextViewerExtension2 vExtension= ((ITextViewerExtension2) viewer);
|
|
1039 for (int i= 0; i < contentTypes.length; i++) {
|
|
1040 vExtension.removeAutoEditStrategy(fAutoEditVetoer, contentTypes[i]);
|
|
1041 }
|
|
1042 } else {
|
|
1043 Assert.isTrue(false);
|
|
1044 }
|
|
1045 } catch (BadPartitioningException e) {
|
|
1046 leave(ILinkedModeListener.EXIT_ALL);
|
|
1047 }
|
|
1048 }
|
|
1049
|
|
1050 /**
|
|
1051 * Returns all possible content types of <code>document</code>.
|
|
1052 *
|
|
1053 * @param document the document
|
|
1054 * @return all possible content types of <code>document</code>
|
|
1055 * @throws BadPartitioningException
|
|
1056 * @since 3.1
|
|
1057 */
|
|
1058 private String[] getContentTypes(IDocument document) throws BadPartitioningException {
|
|
1059 if (document instanceof IDocumentExtension3) {
|
|
1060 IDocumentExtension3 ext= (IDocumentExtension3) document;
|
|
1061 String[] partitionings= ext.getPartitionings();
|
|
1062 Set contentTypes= new HashSet(20);
|
|
1063 for (int i= 0; i < partitionings.length; i++) {
|
|
1064 contentTypes.addAll(Arrays.asList(ext.getLegalContentTypes(partitionings[i])));
|
|
1065 }
|
|
1066 contentTypes.add(IDocument.DEFAULT_CONTENT_TYPE);
|
|
1067 return (String[]) contentTypes.toArray(new String[contentTypes.size()]);
|
|
1068 }
|
|
1069 return document.getLegalContentTypes();
|
|
1070 }
|
|
1071
|
|
1072 private void createAnnotationModel() {
|
|
1073 if (fCurrentTarget.fAnnotationModel is null) {
|
|
1074 LinkedPositionAnnotations lpa= new LinkedPositionAnnotations();
|
|
1075 if (fSimple) {
|
|
1076 lpa.markExitTarget(true);
|
|
1077 lpa.markFocus(false);
|
|
1078 lpa.markSlaves(false);
|
|
1079 lpa.markTargets(false);
|
|
1080 }
|
|
1081 lpa.setTargets(fIterator.getPositions());
|
|
1082 lpa.setExitTarget(fExitPosition);
|
|
1083 lpa.connect(fCurrentTarget.getViewer().getDocument());
|
|
1084 fCurrentTarget.fAnnotationModel= lpa;
|
|
1085 }
|
|
1086 }
|
|
1087
|
|
1088 private String getUniqueKey() {
|
|
1089 return "linked.annotationmodelkey."+toString(); //$NON-NLS-1$
|
|
1090 }
|
|
1091
|
|
1092 private void disconnect() {
|
|
1093 Assert.isNotNull(fCurrentTarget);
|
|
1094 ITextViewer viewer= fCurrentTarget.getViewer();
|
|
1095 Assert.isNotNull(viewer);
|
|
1096
|
|
1097 viewer.getDocument().removeDocumentListener(fDocumentListener);
|
|
1098
|
|
1099 fAssistant.uninstall();
|
|
1100 fAssistant.removeProposalListener(fProposalListener);
|
|
1101
|
|
1102 fCurrentTarget.fWidget= null;
|
|
1103
|
|
1104 Shell shell= fCurrentTarget.fShell;
|
|
1105 fCurrentTarget.fShell= null;
|
|
1106
|
|
1107 if (shell !is null && !shell.isDisposed())
|
|
1108 shell.removeShellListener(fCloser);
|
|
1109
|
|
1110 // this one is asymmetric: we don't install the model in
|
|
1111 // connect, but leave it to its callers to ensure they
|
|
1112 // have the model installed if they need it
|
|
1113 uninstallAnnotationModel(fCurrentTarget);
|
|
1114
|
|
1115 unregisterAutoEditVetoer(viewer);
|
|
1116
|
|
1117 // don't remove the verify key listener to let it keep its position
|
|
1118 // in the listener queue
|
|
1119 if (fCurrentTarget.fKeyListener !is null)
|
|
1120 fCurrentTarget.fKeyListener.setEnabled(false);
|
|
1121
|
|
1122 ((IPostSelectionProvider) viewer).removePostSelectionChangedListener(fSelectionListener);
|
|
1123
|
|
1124 redraw();
|
|
1125 }
|
|
1126
|
|
1127 void leave(final int flags) {
|
|
1128 if (!fIsActive)
|
|
1129 return;
|
|
1130 fIsActive= false;
|
|
1131
|
|
1132 endCompoundChange();
|
|
1133
|
|
1134 Display display= null;
|
|
1135 if (fCurrentTarget.fWidget !is null && !fCurrentTarget.fWidget.isDisposed())
|
|
1136 display= fCurrentTarget.fWidget.getDisplay();
|
|
1137
|
|
1138 if (fCurrentTarget.fAnnotationModel !is null)
|
|
1139 fCurrentTarget.fAnnotationModel.removeAllAnnotations();
|
|
1140 disconnect();
|
|
1141
|
|
1142 for (int i= 0; i < fTargets.length; i++) {
|
|
1143 LinkedModeUITarget target= fTargets[i];
|
|
1144 ITextViewer viewer= target.getViewer();
|
|
1145 if (target.fKeyListener !is null) {
|
|
1146 ((ITextViewerExtension) viewer).removeVerifyKeyListener(target.fKeyListener);
|
|
1147 target.fKeyListener= null;
|
|
1148 }
|
|
1149
|
|
1150 viewer.removeTextInputListener(fCloser);
|
|
1151 }
|
|
1152
|
|
1153 for (int i= 0; i < fTargets.length; i++) {
|
|
1154
|
|
1155 if (fTargets[i].fAnnotationModel !is null) {
|
|
1156 fTargets[i].fAnnotationModel.removeAllAnnotations();
|
|
1157 fTargets[i].fAnnotationModel.disconnect(fTargets[i].getViewer().getDocument());
|
|
1158 fTargets[i].fAnnotationModel= null;
|
|
1159 }
|
|
1160
|
|
1161 uninstallAnnotationModel(fTargets[i]);
|
|
1162 }
|
|
1163
|
|
1164
|
|
1165 if ((flags & ILinkedModeListener.UPDATE_CARET) !is 0 && fExitPosition !is null && fFramePosition !is fExitPosition && !fExitPosition.isDeleted())
|
|
1166 switchPosition(fExitPosition, true, false);
|
|
1167
|
|
1168 final List docs= new ArrayList();
|
|
1169 for (int i= 0; i < fTargets.length; i++) {
|
|
1170 IDocument doc= fTargets[i].getViewer().getDocument();
|
|
1171 if (doc !is null)
|
|
1172 docs.add(doc);
|
|
1173 }
|
|
1174
|
|
1175 fModel.stopForwarding(flags);
|
|
1176
|
|
1177 Runnable runnable= new Runnable() {
|
|
1178 public void run() {
|
|
1179 if (fExitPosition !is null)
|
|
1180 fExitPosition.getDocument().removePosition(fExitPosition);
|
|
1181
|
|
1182 for (Iterator iter = docs.iterator(); iter.hasNext(); ) {
|
|
1183 IDocument doc= (IDocument) iter.next();
|
|
1184 doc.removePositionUpdater(fPositionUpdater);
|
|
1185 bool uninstallCat= false;
|
|
1186 String[] cats= doc.getPositionCategories();
|
|
1187 for (int j= 0; j < cats.length; j++) {
|
|
1188 if (getCategory().equals(cats[j])) {
|
|
1189 uninstallCat= true;
|
|
1190 break;
|
|
1191 }
|
|
1192 }
|
|
1193 if (uninstallCat)
|
|
1194 try {
|
|
1195 doc.removePositionCategory(getCategory());
|
|
1196 } catch (BadPositionCategoryException e) {
|
|
1197 // ignore
|
|
1198 }
|
|
1199 }
|
|
1200 fModel.exit(flags);
|
|
1201 }
|
|
1202 };
|
|
1203
|
|
1204 // remove positions (both exit positions AND linked positions in the
|
|
1205 // model) asynchronously to make sure that the annotation painter
|
|
1206 // gets correct document offsets.
|
|
1207 if (display !is null)
|
|
1208 display.asyncExec(runnable);
|
|
1209 else
|
|
1210 runnable.run();
|
|
1211 }
|
|
1212
|
|
1213 private void endCompoundChange() {
|
|
1214 if (fHasOpenCompoundChange) {
|
|
1215 ITextViewerExtension extension= (ITextViewerExtension) fCurrentTarget.getViewer();
|
|
1216 IRewriteTarget target= extension.getRewriteTarget();
|
|
1217 target.endCompoundChange();
|
|
1218 fHasOpenCompoundChange= false;
|
|
1219 }
|
|
1220 }
|
|
1221
|
|
1222 private void beginCompoundChange() {
|
|
1223 if (!fHasOpenCompoundChange) {
|
|
1224 ITextViewerExtension extension= (ITextViewerExtension) fCurrentTarget.getViewer();
|
|
1225 IRewriteTarget target= extension.getRewriteTarget();
|
|
1226 target.beginCompoundChange();
|
|
1227 fHasOpenCompoundChange= true;
|
|
1228 }
|
|
1229 }
|
|
1230
|
|
1231 /**
|
|
1232 * Returns the currently selected region or <code>null</code>.
|
|
1233 *
|
|
1234 * @return the currently selected region or <code>null</code>
|
|
1235 */
|
|
1236 public IRegion getSelectedRegion() {
|
|
1237 if (fFramePosition !is null)
|
|
1238 return new Region(fFramePosition.getOffset(), fFramePosition.getLength());
|
|
1239 if (fExitPosition !is null)
|
|
1240 return new Region(fExitPosition.getOffset(), fExitPosition.getLength());
|
|
1241 return null;
|
|
1242 }
|
|
1243
|
|
1244 private String getCategory() {
|
|
1245 return toString();
|
|
1246 }
|
|
1247
|
|
1248 /**
|
|
1249 * Sets the context info property. If set to <code>true</code>, context
|
|
1250 * info will be invoked on the current target's viewer whenever a position
|
|
1251 * is switched.
|
|
1252 *
|
|
1253 * @param doContextInfo <code>true</code> if context information should be
|
|
1254 * displayed
|
|
1255 */
|
|
1256 public void setDoContextInfo(bool doContextInfo) {
|
|
1257 fDoContextInfo= doContextInfo;
|
|
1258 }
|
|
1259
|
|
1260 /**
|
|
1261 * Sets the focus callback which will get informed when the focus of the
|
|
1262 * linked mode UI changes.
|
|
1263 * <p>
|
|
1264 * If there is a listener installed already, it will be replaced.
|
|
1265 * </p>
|
|
1266 *
|
|
1267 * @param listener the new listener, never <code>null</code>.
|
|
1268 */
|
|
1269 protected void setPositionListener(ILinkedModeUIFocusListener listener) {
|
|
1270 Assert.isNotNull(listener);
|
|
1271 fPositionListener= listener;
|
|
1272 }
|
|
1273
|
|
1274 /**
|
|
1275 * Sets the "simple" mode of the receiver. A linked mode UI in simple mode
|
|
1276 * merely draws the exit position, but not the target, focus, and slave
|
|
1277 * positions. Default is <code>false</code>. This method must be called
|
|
1278 * before it is entered.
|
|
1279 *
|
|
1280 * @param simple <code>true</code> if the UI should be in simple mode.
|
|
1281 */
|
|
1282 public void setSimpleMode(bool simple) {
|
|
1283 fSimple= simple;
|
|
1284 }
|
|
1285
|
|
1286 /**
|
|
1287 * Enables the support for colored labels in the proposal popup.
|
|
1288 * <p>Completion proposals can implement {@link ICompletionProposalExtension6}
|
|
1289 * to provide colored proposal labels.</p>
|
|
1290 *
|
|
1291 * @param isEnabled if <code>true</code> the support for colored labels is enabled in the proposal popup
|
|
1292 * @since 3.4
|
|
1293 */
|
|
1294 public void enableColoredLabels(bool isEnabled) {
|
|
1295 fIsColoredLabelsSupportEnabled= isEnabled;
|
|
1296 }
|
|
1297
|
|
1298 }
|