replace deprecated org.eclipse.jface.text.Assert with org.eclipse.core.runtime.Assert
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / text / link / LinkedPositionUI.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.link;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
16 import net.sourceforge.phpdt.ui.PreferenceConstants;
17 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import net.sourceforge.phpeclipse.ui.WebUI;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 //incastrix
25 //import org.eclipse.jface.text.Assert;
26 import org.eclipse.core.runtime.Assert;
27 import org.eclipse.jface.text.BadLocationException;
28 import org.eclipse.jface.text.BadPositionCategoryException;
29 import org.eclipse.jface.text.DefaultPositionUpdater;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.IPositionUpdater;
32 import org.eclipse.jface.text.IRegion;
33 import org.eclipse.jface.text.IRewriteTarget;
34 import org.eclipse.jface.text.ITextInputListener;
35 import org.eclipse.jface.text.ITextListener;
36 import org.eclipse.jface.text.ITextViewer;
37 import org.eclipse.jface.text.ITextViewerExtension;
38 import org.eclipse.jface.text.ITextViewerExtension2;
39 import org.eclipse.jface.text.ITextViewerExtension5;
40 import org.eclipse.jface.text.Position;
41 import org.eclipse.jface.text.Region;
42 import org.eclipse.jface.text.TextEvent;
43 import org.eclipse.jface.util.IPropertyChangeListener;
44 import org.eclipse.jface.util.PropertyChangeEvent;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.custom.StyledText;
47 import org.eclipse.swt.custom.VerifyKeyListener;
48 import org.eclipse.swt.events.ModifyEvent;
49 import org.eclipse.swt.events.ModifyListener;
50 import org.eclipse.swt.events.PaintEvent;
51 import org.eclipse.swt.events.PaintListener;
52 import org.eclipse.swt.events.ShellEvent;
53 import org.eclipse.swt.events.ShellListener;
54 import org.eclipse.swt.events.VerifyEvent;
55 import org.eclipse.swt.events.VerifyListener;
56 import org.eclipse.swt.graphics.Color;
57 import org.eclipse.swt.graphics.GC;
58 import org.eclipse.swt.graphics.Point;
59 import org.eclipse.swt.graphics.RGB;
60 import org.eclipse.swt.widgets.Display;
61 import org.eclipse.swt.widgets.Shell;
62
63 /**
64  * A user interface for <code>LinkedPositionManager</code>, using
65  * <code>ITextViewer</code>.
66  */
67 public class LinkedPositionUI implements ILinkedPositionListener,
68                 ITextInputListener, ITextListener, ModifyListener, VerifyListener,
69                 VerifyKeyListener, PaintListener, IPropertyChangeListener,
70                 ShellListener {
71
72         /**
73          * A listener for notification when the user cancelled the edit operation.
74          */
75         public interface ExitListener {
76                 void exit(boolean accept);
77         }
78
79         public static class ExitFlags {
80                 public int flags;
81
82                 public boolean doit;
83
84                 public ExitFlags(int flags, boolean doit) {
85                         this.flags = flags;
86                         this.doit = doit;
87                 }
88         }
89
90         public interface ExitPolicy {
91                 ExitFlags doExit(LinkedPositionManager manager, VerifyEvent event,
92                                 int offset, int length);
93         }
94
95         // leave flags
96         private static final int UNINSTALL = 1; // uninstall linked position manager
97
98         public static final int COMMIT = 2; // commit changes
99
100         private static final int DOCUMENT_CHANGED = 4; // document has changed
101
102         public static final int UPDATE_CARET = 8; // update caret
103
104         private static final IPreferenceStore fgStore = WebUI
105                         .getDefault().getPreferenceStore();
106
107         private static final String CARET_POSITION_PREFIX = "LinkedPositionUI.caret.position"; //$NON-NLS-1$
108
109         private static int fgCounter = 0;
110
111         private final ITextViewer fViewer;
112
113         private final LinkedPositionManager fManager;
114
115         private final IPositionUpdater fUpdater;
116
117         private final String fPositionCategoryName;
118
119         private Color fFrameColor;
120
121         private int fFinalCaretOffset = -1; // no final caret offset
122
123         private Position fFinalCaretPosition;
124
125         private Position fFramePosition;
126
127         private int fInitialOffset = -1;
128
129         private int fCaretOffset;
130
131         private ExitPolicy fExitPolicy;
132
133         private ExitListener fExitListener;
134
135         private boolean fNeedRedraw;
136
137         private String fContentType;
138
139         private Position fPreviousPosition;
140
141         // private ContentAssistant2 fAssistant;
142
143         /**
144          * Flag that records the state of this ui object. As there are many
145          * different entities that may call leave or exit, these cannot always be
146          * sure whether the linked position infrastructure is still active. This is
147          * especially true for multithreaded situations.
148          */
149         private boolean fIsActive = false;
150
151         /**
152          * Creates a user interface for <code>LinkedPositionManager</code>.
153          * 
154          * @param viewer
155          *            the text viewer.
156          * @param manager
157          *            the <code>LinkedPositionManager</code> managing a
158          *            <code>IDocument</code> of the <code>ITextViewer</code>.
159          */
160         public LinkedPositionUI(ITextViewer viewer, LinkedPositionManager manager) {
161                 Assert.isNotNull(viewer);
162                 Assert.isNotNull(manager);
163
164                 fViewer = viewer;
165                 fManager = manager;
166
167                 fPositionCategoryName = CARET_POSITION_PREFIX + (fgCounter++);
168                 fUpdater = new DefaultPositionUpdater(fPositionCategoryName);
169
170                 fManager.setLinkedPositionListener(this);
171
172                 initializeHighlightColor(viewer);
173         }
174
175         /*
176          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
177          */
178         public void propertyChange(PropertyChangeEvent event) {
179                 if (event.getProperty().equals(
180                                 PreferenceConstants.EDITOR_LINKED_POSITION_COLOR)) {
181                         initializeHighlightColor(fViewer);
182                         redrawRegion();
183                 }
184         }
185
186         private void initializeHighlightColor(ITextViewer viewer) {
187
188                 if (fFrameColor != null)
189                         fFrameColor.dispose();
190
191                 StyledText text = viewer.getTextWidget();
192                 if (text != null) {
193                         Display display = text.getDisplay();
194                         fFrameColor = createColor(fgStore,
195                                         PreferenceConstants.EDITOR_LINKED_POSITION_COLOR, display);
196                 }
197         }
198
199         /**
200          * Creates a color from the information stored in the given preference
201          * store. Returns <code>null</code> if there is no such information
202          * available.
203          */
204         private Color createColor(IPreferenceStore store, String key,
205                         Display display) {
206
207                 RGB rgb = null;
208
209                 if (store.contains(key)) {
210
211                         if (store.isDefault(key))
212                                 rgb = PreferenceConverter.getDefaultColor(store, key);
213                         else
214                                 rgb = PreferenceConverter.getColor(store, key);
215
216                         if (rgb != null)
217                                 return new Color(display, rgb);
218                 }
219
220                 return null;
221         }
222
223         /**
224          * Sets the initial offset.
225          * 
226          * @param offset
227          */
228         public void setInitialOffset(int offset) {
229                 fInitialOffset = offset;
230         }
231
232         /**
233          * Sets the final position of the caret when the linked mode is exited
234          * successfully by leaving the last linked position using TAB. The set
235          * position will be a TAB stop as well as the positions configured in the
236          * <code>LinkedPositionManager</code>.
237          */
238         public void setFinalCaretOffset(int offset) {
239                 fFinalCaretOffset = offset;
240         }
241
242         /**
243          * Sets a <code>CancelListener</code> which is notified if the linked mode
244          * is exited unsuccessfully by hitting ESC.
245          */
246         public void setCancelListener(ExitListener listener) {
247                 fExitListener = listener;
248         }
249
250         /**
251          * Sets an <code>ExitPolicy</code> which decides when and how the linked
252          * mode is exited.
253          */
254         public void setExitPolicy(ExitPolicy policy) {
255                 fExitPolicy = policy;
256         }
257
258         /*
259          * @see LinkedPositionManager.LinkedPositionListener#setCurrentPositions(Position,
260          *      int)
261          */
262         public void setCurrentPosition(Position position, int caretOffset) {
263                 if (!fIsActive)
264                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
265                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
266                                 // not active: "+fPositionCategoryName, new
267                                 // IllegalStateException())); //$NON-NLS-1$
268
269                 if (!fFramePosition.equals(position)) {
270                         fNeedRedraw = true;
271                         fFramePosition = position;
272                 }
273
274                 fCaretOffset = caretOffset;
275         }
276
277         /**
278          * Enters the linked mode. The linked mode can be left by calling
279          * <code>exit</code>.
280          * 
281          * @see #exit(boolean)
282          */
283         public void enter() {
284                 if (fIsActive)
285                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
286                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
287                                 // already active: "+fPositionCategoryName, new
288                                 // IllegalStateException())); //$NON-NLS-1$
289                 else {
290                         fIsActive = true;
291                         // JavaPlugin.log(new Status(IStatus.INFO, JavaPlugin.getPluginId(),
292                         // IStatus.OK, "LinkedPositionUI activated: "+fPositionCategoryName,
293                         // new Exception())); //$NON-NLS-1$
294                 }
295
296                 // track final caret
297                 IDocument document = fViewer.getDocument();
298                 document.addPositionCategory(fPositionCategoryName);
299                 document.addPositionUpdater(fUpdater);
300
301                 try {
302                         if (fFinalCaretOffset != -1) {
303                                 fFinalCaretPosition = new Position(fFinalCaretOffset);
304                                 document
305                                                 .addPosition(fPositionCategoryName, fFinalCaretPosition);
306                         }
307                 } catch (BadLocationException e) {
308                         handleException(fViewer.getTextWidget().getShell(), e);
309
310                 } catch (BadPositionCategoryException e) {
311                         WebUI.log(e);
312                         Assert.isTrue(false);
313                 }
314
315                 fViewer.addTextInputListener(this);
316                 fViewer.addTextListener(this);
317
318                 ITextViewerExtension extension = (ITextViewerExtension) fViewer;
319                 extension.prependVerifyKeyListener(this);
320
321                 StyledText text = fViewer.getTextWidget();
322                 text.addVerifyListener(this);
323                 text.addModifyListener(this);
324                 text.addPaintListener(this);
325                 text.showSelection();
326
327                 Shell shell = text.getShell();
328                 shell.addShellListener(this);
329
330                 fFramePosition = (fInitialOffset == -1) ? fManager.getFirstPosition()
331                                 : fManager.getPosition(fInitialOffset);
332                 if (fFramePosition == null) {
333                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
334                         return;
335                 }
336
337                 fgStore.addPropertyChangeListener(this);
338
339                 // try {
340                 // fContentType= TextUtilities.getContentType(document,
341                 // IJavaPartitions.JAVA_PARTITIONING, fFramePosition.offset);
342                 // if (fViewer instanceof ITextViewerExtension2) {
343                 // ((ITextViewerExtension2) fViewer).prependAutoEditStrategy(fManager,
344                 // fContentType);
345                 // } else {
346                 // Assert.isTrue(false);
347                 // }
348                 //
349                 // } catch (BadLocationException e) {
350                 // handleException(fViewer.getTextWidget().getShell(), e);
351                 // }
352                 try {
353                         fContentType = document.getContentType(fFramePosition.offset);
354                         if (fViewer instanceof ITextViewerExtension2) {
355                                 ((ITextViewerExtension2) fViewer).prependAutoEditStrategy(
356                                                 fManager, fContentType);
357                         } else {
358                                 Assert.isTrue(false);
359                         }
360
361                 } catch (BadLocationException e) {
362                         handleException(fViewer.getTextWidget().getShell(), e);
363                 }
364                 selectRegion();
365                 // triggerContentAssist();
366         }
367
368         /*
369          * @see net.sourceforge.phpdt.internal.ui.text.link.ILinkedPositionListener#exit(boolean)
370          */
371         public void exit(int flags) {
372                 leave(flags);
373         }
374
375         /**
376          * Returns the cursor selection, after having entered the linked mode.
377          * <code>enter()</code> must be called prior to a call to this method.
378          */
379         public IRegion getSelectedRegion() {
380                 if (!fIsActive)
381                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
382                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
383                                 // not active: "+fPositionCategoryName, new
384                                 // IllegalStateException())); //$NON-NLS-1$
385
386                 if (fFramePosition == null)
387                         return new Region(fFinalCaretOffset, 0);
388                 else
389                         return new Region(fFramePosition.getOffset(), fFramePosition
390                                         .getLength());
391         }
392
393         private void leave(int flags) {
394                 if (!fIsActive)
395                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
396                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
397                                 // not active: "+fPositionCategoryName, new
398                                 // IllegalStateException())); //$NON-NLS-1$
399                 else {
400                         fIsActive = false;
401                         // JavaPlugin.log(new Status(IStatus.INFO, JavaPlugin.getPluginId(),
402                         // IStatus.OK, "LinkedPositionUI deactivated:
403                         // "+fPositionCategoryName, new Exception())); //$NON-NLS-1$
404                 }
405
406                 fInitialOffset = -1;
407
408                 if ((flags & UNINSTALL) != 0)
409                         fManager.uninstall((flags & COMMIT) != 0);
410
411                 fgStore.removePropertyChangeListener(this);
412
413                 if (fFrameColor != null) {
414                         fFrameColor.dispose();
415                         fFrameColor = null;
416                 }
417
418                 StyledText text = fViewer.getTextWidget();
419                 // bail out if the styled text is null, meaning the viewer has been
420                 // disposed (-> document is null as well)
421                 // see pr https://bugs.eclipse.org/bugs/show_bug.cgi?id=46821
422                 if (text == null)
423                         return;
424
425                 text.removePaintListener(this);
426                 text.removeModifyListener(this);
427                 text.removeVerifyListener(this);
428
429                 Shell shell = text.getShell();
430                 shell.removeShellListener(this);
431
432                 // if (fAssistant != null) {
433                 // Display display= text.getDisplay();
434                 // if (display != null && !display.isDisposed()) {
435                 // display.asyncExec(new Runnable() {
436                 // public void run() {
437                 // if (fAssistant != null) {
438                 // fAssistant.uninstall();
439                 // fAssistant= null;
440                 // }
441                 // }
442                 // });
443                 // }
444                 // }
445
446                 ITextViewerExtension extension = (ITextViewerExtension) fViewer;
447                 extension.removeVerifyKeyListener(this);
448
449                 IRewriteTarget target = extension.getRewriteTarget();
450                 target.endCompoundChange();
451
452                 if (fViewer instanceof ITextViewerExtension2 && fContentType != null)
453                         ((ITextViewerExtension2) fViewer).removeAutoEditStrategy(fManager,
454                                         fContentType);
455                 fContentType = null;
456
457                 fViewer.removeTextListener(this);
458                 fViewer.removeTextInputListener(this);
459
460                 try {
461                         IDocument document = fViewer.getDocument();
462
463                         if (((flags & COMMIT) != 0) && ((flags & DOCUMENT_CHANGED) == 0)
464                                         && ((flags & UPDATE_CARET) != 0)) {
465                                 Position[] positions = document
466                                                 .getPositions(fPositionCategoryName);
467                                 if ((positions != null) && (positions.length != 0)) {
468
469                                         if (fViewer instanceof ITextViewerExtension5) {
470                                                 ITextViewerExtension5 extension3 = (ITextViewerExtension5) fViewer;
471                                                 int widgetOffset = extension3
472                                                                 .modelOffset2WidgetOffset(positions[0]
473                                                                                 .getOffset());
474                                                 if (widgetOffset >= 0)
475                                                         text.setSelection(widgetOffset, widgetOffset);
476
477                                         } else {
478                                                 IRegion region = fViewer.getVisibleRegion();
479                                                 int offset = positions[0].getOffset()
480                                                                 - region.getOffset();
481                                                 if ((offset >= 0) && (offset <= region.getLength()))
482                                                         text.setSelection(offset, offset);
483                                         }
484                                 }
485                         }
486
487                         document.removePositionUpdater(fUpdater);
488                         document.removePositionCategory(fPositionCategoryName);
489
490                         if (fExitListener != null)
491                                 fExitListener.exit(((flags & COMMIT) != 0)
492                                                 || ((flags & DOCUMENT_CHANGED) != 0));
493
494                 } catch (BadPositionCategoryException e) {
495                         WebUI.log(e);
496                         Assert.isTrue(false);
497                 }
498
499                 if ((flags & DOCUMENT_CHANGED) == 0)
500                         text.redraw();
501         }
502
503         private void next() {
504                 if (!fIsActive)
505                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
506                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
507                                 // not active: "+fPositionCategoryName, new
508                                 // IllegalStateException())); //$NON-NLS-1$
509
510                 redrawRegion();
511
512                 if (fFramePosition == fFinalCaretPosition)
513                         fFramePosition = fManager.getFirstPosition();
514                 else
515                         fFramePosition = fManager.getNextPosition(fFramePosition
516                                         .getOffset());
517                 if (fFramePosition == null) {
518                         if (fFinalCaretPosition != null)
519                                 fFramePosition = fFinalCaretPosition;
520                         else
521                                 fFramePosition = fManager.getFirstPosition();
522                 }
523                 if (fFramePosition == null) {
524                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
525                 } else {
526                         selectRegion();
527                         // triggerContentAssist();
528                         redrawRegion();
529                 }
530         }
531
532         private void previous() {
533                 if (!fIsActive)
534                         ;// JavaPlugin.log(new Status(IStatus.WARNING,
535                                 // JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is
536                                 // not active: "+fPositionCategoryName, new
537                                 // IllegalStateException())); //$NON-NLS-1$
538
539                 redrawRegion();
540
541                 fFramePosition = fManager.getPreviousPosition(fFramePosition
542                                 .getOffset());
543                 if (fFramePosition == null) {
544                         if (fFinalCaretPosition != null)
545                                 fFramePosition = fFinalCaretPosition;
546                         else
547                                 fFramePosition = fManager.getLastPosition();
548                 }
549                 if (fFramePosition == null) {
550                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
551                 } else {
552                         selectRegion();
553                         // triggerContentAssist();
554                         redrawRegion();
555                 }
556         }
557
558         /** Trigger content assist on choice positions */
559         // private void triggerContentAssist() {
560         // if (fFramePosition instanceof ProposalPosition) {
561         //
562         // ProposalPosition pp= (ProposalPosition) fFramePosition;
563         // initializeContentAssistant();
564         // if (fAssistant == null)
565         // return;
566         // fAssistant.setCompletions(pp.getChoices());
567         // fAssistant.showPossibleCompletions();
568         // } else {
569         // if (fAssistant != null)
570         // fAssistant.setCompletions(new ICompletionProposal[0]);
571         // }
572         // }
573         /** Lazy initialize content assistant for this linked ui */
574         // private void initializeContentAssistant() {
575         // if (fAssistant != null)
576         // return;
577         // fAssistant= new ContentAssistant2();
578         // fAssistant.setDocumentPartitioning(IJavaPartitions.JAVA_PARTITIONING);
579         // fAssistant.install(fViewer);
580         // }
581         /*
582          * @see VerifyKeyListener#verifyKey(VerifyEvent)
583          */
584         public void verifyKey(VerifyEvent event) {
585
586                 if (!event.doit || !fIsActive)
587                         return;
588
589                 Point selection = fViewer.getSelectedRange();
590                 int offset = selection.x;
591                 int length = selection.y;
592
593                 ExitFlags exitFlags = fExitPolicy == null ? null : fExitPolicy.doExit(
594                                 fManager, event, offset, length);
595                 if (exitFlags != null) {
596                         leave(UNINSTALL | exitFlags.flags);
597                         event.doit = exitFlags.doit;
598                         return;
599                 }
600
601                 switch (event.character) {
602                 // [SHIFT-]TAB = hop between edit boxes
603                 case 0x09: {
604                         // if tab was treated as a document change, would it exceed variable
605                         // range?
606                         if (!LinkedPositionManager.includes(fFramePosition, offset, length)) {
607                                 leave(UNINSTALL | COMMIT);
608                                 return;
609                         }
610                 }
611
612                         if (event.stateMask == SWT.SHIFT)
613                                 previous();
614                         else
615                                 next();
616
617                         event.doit = false;
618                         break;
619
620                 // ENTER
621                 case 0x0A: // Ctrl+Enter
622                 case 0x0D: {
623                         // if (fAssistant != null && fAssistant.wasProposalChosen()) {
624                         // next();
625                         // event.doit= false;
626                         // break;
627                         // }
628
629                         // if enter was treated as a document change, would it exceed
630                         // variable range?
631                         if (!LinkedPositionManager.includes(fFramePosition, offset, length)
632                                         || (fFramePosition == fFinalCaretPosition)) {
633                                 leave(UNINSTALL | COMMIT);
634                                 return;
635                         }
636                 }
637
638                         leave(UNINSTALL | COMMIT | UPDATE_CARET);
639                         event.doit = false;
640                         break;
641
642                 // ESC
643                 case 0x1B:
644                         leave(UNINSTALL | COMMIT);
645                         event.doit = false;
646                         break;
647
648                 case ';':
649                         leave(UNINSTALL | COMMIT);
650                         event.doit = true;
651                         break;
652
653                 default:
654                         if (event.character != 0) {
655                                 if (!controlUndoBehavior(offset, length)
656                                                 || fFramePosition == fFinalCaretPosition) {
657                                         leave(UNINSTALL | COMMIT);
658                                         break;
659                                 }
660                         }
661                 }
662         }
663
664         private boolean controlUndoBehavior(int offset, int length) {
665
666                 Position position = fManager.getEmbracingPosition(offset, length);
667                 if (position != null) {
668
669                         ITextViewerExtension extension = (ITextViewerExtension) fViewer;
670                         IRewriteTarget target = extension.getRewriteTarget();
671
672                         if (fPreviousPosition != null
673                                         && !fPreviousPosition.equals(position))
674                                 target.endCompoundChange();
675                         target.beginCompoundChange();
676                 }
677
678                 fPreviousPosition = position;
679                 return fPreviousPosition != null;
680         }
681
682         /*
683          * @see VerifyListener#verifyText(VerifyEvent)
684          */
685         public void verifyText(VerifyEvent event) {
686                 if (!event.doit)
687                         return;
688
689                 int offset = 0;
690                 int length = 0;
691
692                 if (fViewer instanceof ITextViewerExtension5) {
693                         ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer;
694                         IRegion modelRange = extension.widgetRange2ModelRange(new Region(
695                                         event.start, event.end - event.start));
696                         if (modelRange == null)
697                                 return;
698
699                         offset = modelRange.getOffset();
700                         length = modelRange.getLength();
701
702                 } else {
703                         IRegion visibleRegion = fViewer.getVisibleRegion();
704                         offset = event.start + visibleRegion.getOffset();
705                         length = event.end - event.start;
706                 }
707
708                 // allow changes only within linked positions when coming through UI
709                 if (!fManager.anyPositionIncludes(offset, length))
710                         leave(UNINSTALL | COMMIT);
711         }
712
713         /*
714          * @see PaintListener#paintControl(PaintEvent)
715          */
716         public void paintControl(PaintEvent event) {
717                 if (fFramePosition == null)
718                         return;
719
720                 IRegion widgetRange = asWidgetRange(fFramePosition);
721                 if (widgetRange == null) {
722                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
723                         return;
724                 }
725
726                 int offset = widgetRange.getOffset();
727                 int length = widgetRange.getLength();
728
729                 StyledText text = fViewer.getTextWidget();
730
731                 // support for bidi
732                 Point minLocation = getMinimumLocation(text, offset, length);
733                 Point maxLocation = getMaximumLocation(text, offset, length);
734
735                 int x1 = minLocation.x;
736                 int x2 = minLocation.x + maxLocation.x - minLocation.x - 1;
737                 int y = minLocation.y + text.getLineHeight() - 1;
738
739                 GC gc = event.gc;
740                 gc.setForeground(fFrameColor);
741                 gc.drawLine(x1, y, x2, y);
742         }
743
744         protected IRegion asWidgetRange(Position position) {
745                 if (fViewer instanceof ITextViewerExtension5) {
746
747                         ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer;
748                         return extension.modelRange2WidgetRange(new Region(position
749                                         .getOffset(), position.getLength()));
750
751                 } else {
752
753                         IRegion region = fViewer.getVisibleRegion();
754                         if (includes(region, position))
755                                 return new Region(position.getOffset() - region.getOffset(),
756                                                 position.getLength());
757                 }
758
759                 return null;
760         }
761
762         private static Point getMinimumLocation(StyledText text, int offset,
763                         int length) {
764                 Point minLocation = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
765
766                 for (int i = 0; i <= length; i++) {
767                         Point location = text.getLocationAtOffset(offset + i);
768
769                         if (location.x < minLocation.x)
770                                 minLocation.x = location.x;
771                         if (location.y < minLocation.y)
772                                 minLocation.y = location.y;
773                 }
774
775                 return minLocation;
776         }
777
778         private static Point getMaximumLocation(StyledText text, int offset,
779                         int length) {
780                 Point maxLocation = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
781
782                 for (int i = 0; i <= length; i++) {
783                         Point location = text.getLocationAtOffset(offset + i);
784
785                         if (location.x > maxLocation.x)
786                                 maxLocation.x = location.x;
787                         if (location.y > maxLocation.y)
788                                 maxLocation.y = location.y;
789                 }
790
791                 return maxLocation;
792         }
793
794         private void redrawRegion() {
795                 IRegion widgetRange = asWidgetRange(fFramePosition);
796                 if (widgetRange == null) {
797                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
798                         return;
799                 }
800
801                 StyledText text = fViewer.getTextWidget();
802                 if (text != null && !text.isDisposed())
803                         text.redrawRange(widgetRange.getOffset(), widgetRange.getLength(),
804                                         true);
805         }
806
807         private void selectRegion() {
808
809                 IRegion widgetRange = asWidgetRange(fFramePosition);
810                 if (widgetRange == null) {
811                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
812                         return;
813                 }
814
815                 StyledText text = fViewer.getTextWidget();
816                 if (text != null && !text.isDisposed()) {
817                         int start = widgetRange.getOffset();
818                         int end = widgetRange.getLength() + start;
819                         text.setSelection(start, end);
820                 }
821         }
822
823         private void updateCaret() {
824
825                 IRegion widgetRange = asWidgetRange(fFramePosition);
826                 if (widgetRange == null) {
827                         leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
828                         return;
829                 }
830
831                 int offset = widgetRange.getOffset() + fCaretOffset;
832                 StyledText text = fViewer.getTextWidget();
833                 if (text != null && !text.isDisposed())
834                         text.setCaretOffset(offset);
835         }
836
837         /*
838          * @see ModifyListener#modifyText(ModifyEvent)
839          */
840         public void modifyText(ModifyEvent e) {
841                 // reposition caret after StyledText
842                 redrawRegion();
843                 updateCaret();
844         }
845
846         private static void handleException(Shell shell, Exception e) {
847                 String title = LinkedPositionMessages
848                                 .getString("LinkedPositionUI.error.title"); //$NON-NLS-1$
849                 if (e instanceof CoreException)
850                         ExceptionHandler.handle((CoreException) e, shell, title, null);
851                 else if (e instanceof InvocationTargetException)
852                         ExceptionHandler.handle((InvocationTargetException) e, shell,
853                                         title, null);
854                 else {
855                         MessageDialog.openError(shell, title, e.getMessage());
856                         WebUI.log(e);
857                 }
858         }
859
860         /*
861          * @see ITextInputListener#inputDocumentAboutToBeChanged(IDocument,
862          *      IDocument)
863          */
864         public void inputDocumentAboutToBeChanged(IDocument oldInput,
865                         IDocument newInput) {
866                 // 5326: leave linked mode on document change
867                 int flags = UNINSTALL | COMMIT
868                                 | (oldInput.equals(newInput) ? 0 : DOCUMENT_CHANGED);
869                 leave(flags);
870         }
871
872         /*
873          * @see ITextInputListener#inputDocumentChanged(IDocument, IDocument)
874          */
875         public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
876         }
877
878         private static boolean includes(IRegion region, Position position) {
879                 return position.getOffset() >= region.getOffset()
880                                 && position.getOffset() + position.getLength() <= region
881                                                 .getOffset()
882                                                 + region.getLength();
883         }
884
885         /*
886          * @see org.eclipse.jface.text.ITextListener#textChanged(TextEvent)
887          */
888         public void textChanged(TextEvent event) {
889                 if (!fNeedRedraw)
890                         return;
891
892                 redrawRegion();
893                 fNeedRedraw = false;
894         }
895
896         /*
897          * @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent)
898          */
899         public void shellActivated(ShellEvent event) {
900         }
901
902         /*
903          * @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent)
904          */
905         public void shellClosed(ShellEvent event) {
906                 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
907         }
908
909         /*
910          * @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent)
911          */
912         public void shellDeactivated(ShellEvent event) {
913                 // don't deactivate on focus lost, since the proposal popups may take
914                 // focus
915                 // plus: it doesn't hurt if you can check with another window without
916                 // losing linked mode
917                 // since there is no intrusive popup sticking out.
918
919                 // need to check first what happens on reentering based on an open
920                 // action
921                 // Seems to be no problem
922
923                 // TODO check whether we can leave it or uncomment it after debugging
924                 // PS: why DOCUMENT_CHANGED? We want to trigger a redraw! (Shell
925                 // deactivated does not mean
926                 // it is not visible any longer.
927                 // leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
928
929                 // Better:
930                 // Check with content assistant and only leave if its not the proposal
931                 // shell that took the
932                 // focus away.
933
934                 StyledText text;
935                 Display display;
936
937                 // if (fAssistant == null || fViewer == null || (text=
938                 // fViewer.getTextWidget()) == null
939                 // || (display= text.getDisplay()) == null || display.isDisposed()) {
940                 if (fViewer == null || (text = fViewer.getTextWidget()) == null
941                                 || (display = text.getDisplay()) == null
942                                 || display.isDisposed()) {
943                         leave(UNINSTALL | COMMIT);
944                 } else {
945                         // Post in UI thread since the assistant popup will only get the
946                         // focus after we lose it.
947                         display.asyncExec(new Runnable() {
948                                 public void run() {
949                                         // TODO add isDisposed / isUninstalled / hasLeft check? for
950                                         // now: check for content type,
951                                         // since it gets nullified in leave()
952                                         if (fIsActive) {// && (fAssistant == null ||
953                                                                         // !fAssistant.hasFocus())) {
954                                                 leave(UNINSTALL | COMMIT);
955                                         }
956                                 }
957                         });
958                 }
959         }
960
961         /*
962          * @see org.eclipse.swt.events.ShellListener#shellDeiconified(org.eclipse.swt.events.ShellEvent)
963          */
964         public void shellDeiconified(ShellEvent event) {
965         }
966
967         /*
968          * @see org.eclipse.swt.events.ShellListener#shellIconified(org.eclipse.swt.events.ShellEvent)
969          */
970         public void shellIconified(ShellEvent event) {
971                 leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
972         }
973
974 }