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