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