Eclipse 3.x compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / link / LinkedPositionUI.java
index d02d674..7f455fe 100644 (file)
@@ -1,7 +1,13 @@
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
 package net.sourceforge.phpdt.internal.ui.text.link;
 
 import java.lang.reflect.InvocationTargetException;
@@ -14,12 +20,14 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.preference.IPreferenceStore;
 import org.eclipse.jface.preference.PreferenceConverter;
+import org.eclipse.jface.text.Assert;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.BadPositionCategoryException;
 import org.eclipse.jface.text.DefaultPositionUpdater;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IPositionUpdater;
 import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.IRewriteTarget;
 import org.eclipse.jface.text.ITextInputListener;
 import org.eclipse.jface.text.ITextListener;
 import org.eclipse.jface.text.ITextViewer;
@@ -29,7 +37,8 @@ import org.eclipse.jface.text.ITextViewerExtension3;
 import org.eclipse.jface.text.Position;
 import org.eclipse.jface.text.Region;
 import org.eclipse.jface.text.TextEvent;
-import org.eclipse.jface.util.Assert;
+import org.eclipse.jface.text.TextUtilities;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
 import org.eclipse.jface.util.IPropertyChangeListener;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.swt.SWT;
@@ -50,10 +59,12 @@ import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 
+
+
 /**
  * A user interface for <code>LinkedPositionManager</code>, using <code>ITextViewer</code>.
  */
-public class LinkedPositionUI implements LinkedPositionListener,
+public class LinkedPositionUI implements ILinkedPositionListener,
        ITextInputListener, ITextListener, ModifyListener, VerifyListener, VerifyKeyListener, PaintListener, IPropertyChangeListener, ShellListener {
 
        /**
@@ -82,15 +93,19 @@ public class LinkedPositionUI implements LinkedPositionListener,
        private static final int DOCUMENT_CHANGED= 4;   // document has changed
        public static final int UPDATE_CARET= 8;                // update caret
 
-       private static final String CARET_POSITION= "LinkedPositionUI.caret.position"; //$NON-NLS-1$
-       private static final IPositionUpdater fgUpdater= new DefaultPositionUpdater(CARET_POSITION);
        private static final IPreferenceStore fgStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
+       private static final String CARET_POSITION_PREFIX= "LinkedPositionUI.caret.position"; //$NON-NLS-1$
+       private static int fgCounter= 0;
+       
        
        private final ITextViewer fViewer;
-       private final LinkedPositionManager fManager;   
+       private final LinkedPositionManager fManager;
+       private final IPositionUpdater fUpdater;
+       private final String fPositionCategoryName;
        private Color fFrameColor;
 
        private int fFinalCaretOffset= -1; // no final caret offset
+       private Position fFinalCaretPosition;
 
        private Position fFramePosition;
        private int fInitialOffset= -1;
@@ -102,6 +117,15 @@ public class LinkedPositionUI implements LinkedPositionListener,
        private boolean fNeedRedraw;
        
        private String fContentType;
+       private Position fPreviousPosition;
+//     private ContentAssistant2 fAssistant;
+
+       /**     
+        * Flag that records the state of this ui object. As there are many different entities that may
+        * call leave or exit, these cannot always be sure whether the linked position infrastructure is
+        * still active. This is especially true for multithreaded situations. 
+        */
+       private boolean fIsActive= false;
 
        /**
         * Creates a user interface for <code>LinkedPositionManager</code>.
@@ -116,6 +140,9 @@ public class LinkedPositionUI implements LinkedPositionListener,
                fViewer= viewer;
                fManager= manager;
                
+               fPositionCategoryName= CARET_POSITION_PREFIX + (fgCounter++);
+               fUpdater= new DefaultPositionUpdater(fPositionCategoryName);
+               
                fManager.setLinkedPositionListener(this);
 
                initializeHighlightColor(viewer);
@@ -176,6 +203,8 @@ public class LinkedPositionUI implements LinkedPositionListener,
        /**
         * Sets the final position of the caret when the linked mode is exited
         * successfully by leaving the last linked position using TAB.
+        * The set position will be a TAB stop as well as the positions configured in the
+        * <code>LinkedPositionManager</code>.
         */
        public void setFinalCaretOffset(int offset) {
                fFinalCaretOffset= offset;      
@@ -201,6 +230,9 @@ public class LinkedPositionUI implements LinkedPositionListener,
         * @see LinkedPositionManager.LinkedPositionListener#setCurrentPositions(Position, int)
         */
        public void setCurrentPosition(Position position, int caretOffset) {
+               if (!fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is not active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               
                if (!fFramePosition.equals(position)) {
                        fNeedRedraw= true;
                        fFramePosition= position;
@@ -216,20 +248,29 @@ public class LinkedPositionUI implements LinkedPositionListener,
         * @see #exit(boolean)
         */
        public void enter() {
+               if (fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is already active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               else {
+                       fIsActive= true;
+                       // JavaPlugin.log(new Status(IStatus.INFO, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI activated: "+fPositionCategoryName, new Exception())); //$NON-NLS-1$
+               }
+               
 
                // track final caret
                IDocument document= fViewer.getDocument();
-               document.addPositionCategory(CARET_POSITION);
-               document.addPositionUpdater(fgUpdater);
+               document.addPositionCategory(fPositionCategoryName);
+               document.addPositionUpdater(fUpdater);
 
                try {
-                       if (fFinalCaretOffset != -1)
-                               document.addPosition(CARET_POSITION, new Position(fFinalCaretOffset));
+                       if (fFinalCaretOffset != -1) {
+                               fFinalCaretPosition= new Position(fFinalCaretOffset);
+                               document.addPosition(fPositionCategoryName, fFinalCaretPosition);
+                       }
                } catch (BadLocationException e) {
                        handleException(fViewer.getTextWidget().getShell(), e);
 
                } catch (BadPositionCategoryException e) {
-      PHPeclipsePlugin.log(e);
+                 PHPeclipsePlugin.log(e);
                        Assert.isTrue(false);
                }
 
@@ -247,7 +288,7 @@ public class LinkedPositionUI implements LinkedPositionListener,
 
                Shell shell= text.getShell();
                shell.addShellListener(this);
-
+               
                fFramePosition= (fInitialOffset == -1) ? fManager.getFirstPosition() : fManager.getPosition(fInitialOffset);
                if (fFramePosition == null) {
                        leave(UNINSTALL | COMMIT | UPDATE_CARET);
@@ -256,25 +297,37 @@ public class LinkedPositionUI implements LinkedPositionListener,
 
                fgStore.addPropertyChangeListener(this);
 
+//             try {
+//                     fContentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, fFramePosition.offset);
+//                     if (fViewer instanceof ITextViewerExtension2) {
+//                             ((ITextViewerExtension2) fViewer).prependAutoEditStrategy(fManager, fContentType);
+//                     } else {
+//                             Assert.isTrue(false);
+//                     }
+//
+//             } catch (BadLocationException e) {
+//                     handleException(fViewer.getTextWidget().getShell(), e);
+//             }
                try {
-                       fContentType= document.getContentType(fFramePosition.offset);
-                       if (fViewer instanceof ITextViewerExtension2) {
-                               ((ITextViewerExtension2) fViewer).prependAutoEditStrategy(fManager, fContentType);
-                       } else {
-                               Assert.isTrue(false);
-                       }
+                 fContentType= document.getContentType(fFramePosition.offset);
+                 if (fViewer instanceof ITextViewerExtension2) {
+                   ((ITextViewerExtension2) fViewer).prependAutoEditStrategy(fManager, fContentType);
+                 } else {
+                   Assert.isTrue(false);
+                 }
 
                } catch (BadLocationException e) {
-                       handleException(fViewer.getTextWidget().getShell(), e);
+                 handleException(fViewer.getTextWidget().getShell(), e);
                }
+               selectRegion();
+//             triggerContentAssist();
        }
 
        /*
-        * @see LinkedPositionManager.LinkedPositionListener#exit(boolean)
+        * @see org.eclipse.jdt.internal.ui.text.link.ILinkedPositionListener#exit(boolean)
         */
-       public void exit(boolean success) {
-               // no UNINSTALL since manager has already uninstalled itself
-               leave((success ? COMMIT : 0) | UPDATE_CARET);
+       public void exit(int flags) {
+               leave(flags);
        }
 
        /**
@@ -282,6 +335,9 @@ public class LinkedPositionUI implements LinkedPositionListener,
         * <code>enter()</code> must be called prior to a call to this method.
         */
        public IRegion getSelectedRegion() {
+               if (!fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is not active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               
                if (fFramePosition == null)
                        return new Region(fFinalCaretOffset, 0);
                else
@@ -289,6 +345,13 @@ public class LinkedPositionUI implements LinkedPositionListener,
        }
        
        private void leave(int flags) {
+               if (!fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is not active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               else {
+                       fIsActive= false;
+                       //JavaPlugin.log(new Status(IStatus.INFO, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI deactivated: "+fPositionCategoryName, new Exception())); //$NON-NLS-1$
+               }
+               
 
                fInitialOffset= -1;
                
@@ -302,16 +365,38 @@ public class LinkedPositionUI implements LinkedPositionListener,
                        fFrameColor= null;
                }                       
                
-               StyledText text= fViewer.getTextWidget();       
+               StyledText text= fViewer.getTextWidget();
+               // bail out if the styled text is null, meaning the viewer has been disposed (-> document is null as well)
+               // see pr https://bugs.eclipse.org/bugs/show_bug.cgi?id=46821
+               if (text == null)
+                       return;
+               
                text.removePaintListener(this);
                text.removeModifyListener(this);
                text.removeVerifyListener(this);
 
                Shell shell= text.getShell();
                shell.removeShellListener(this);
+               
+//             if (fAssistant != null) {
+//                     Display display= text.getDisplay();
+//                     if (display != null && !display.isDisposed()) {
+//                             display.asyncExec(new Runnable() {
+//                                     public void run() {
+//                                             if (fAssistant != null)  {
+//                                                     fAssistant.uninstall();
+//                                                     fAssistant= null;
+//                                             }
+//                                     }
+//                             });
+//                     }
+//             }
 
                ITextViewerExtension extension= (ITextViewerExtension) fViewer;
                extension.removeVerifyKeyListener(this);
+               
+               IRewriteTarget target= extension.getRewriteTarget();
+               target.endCompoundChange();
 
                if (fViewer instanceof ITextViewerExtension2 && fContentType != null)
                        ((ITextViewerExtension2) fViewer).removeAutoEditStrategy(fManager, fContentType);
@@ -327,7 +412,7 @@ public class LinkedPositionUI implements LinkedPositionListener,
                                ((flags & DOCUMENT_CHANGED) == 0) &&
                                ((flags & UPDATE_CARET) != 0))
                        {
-                               Position[] positions= document.getPositions(CARET_POSITION);
+                               Position[] positions= document.getPositions(fPositionCategoryName);
                                if ((positions != null) && (positions.length != 0)) {
                                        
                                        if (fViewer instanceof ITextViewerExtension3) {
@@ -345,8 +430,8 @@ public class LinkedPositionUI implements LinkedPositionListener,
                                }
                        }
 
-                       document.removePositionUpdater(fgUpdater);
-                       document.removePositionCategory(CARET_POSITION);
+                       document.removePositionUpdater(fUpdater);
+                       document.removePositionCategory(fPositionCategoryName);
                        
                        if (fExitListener != null)
                                fExitListener.exit(
@@ -354,7 +439,7 @@ public class LinkedPositionUI implements LinkedPositionListener,
                                        ((flags & DOCUMENT_CHANGED) != 0));
 
                } catch (BadPositionCategoryException e) {
-      PHPeclipsePlugin.log(e);
+                       PHPeclipsePlugin.log(e);
                        Assert.isTrue(false);
                }
 
@@ -363,36 +448,83 @@ public class LinkedPositionUI implements LinkedPositionListener,
        }
 
        private void next() {
+               if (!fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is not active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               
                redrawRegion();
                
-               fFramePosition= fManager.getNextPosition(fFramePosition.getOffset());
+               if (fFramePosition == fFinalCaretPosition)
+                       fFramePosition= fManager.getFirstPosition();
+               else
+                       fFramePosition= fManager.getNextPosition(fFramePosition.getOffset());
+               if (fFramePosition == null) {
+                       if (fFinalCaretPosition != null)
+                               fFramePosition= fFinalCaretPosition;
+                       else
+                               fFramePosition= fManager.getFirstPosition();
+               }
                if (fFramePosition == null) {
                        leave(UNINSTALL | COMMIT | UPDATE_CARET);
                } else {
                        selectRegion();
+//                     triggerContentAssist();
                        redrawRegion();
                }
        }
        
        private void previous() {
+               if (!fIsActive)
+                       ;//JavaPlugin.log(new Status(IStatus.WARNING, JavaPlugin.getPluginId(), IStatus.OK, "LinkedPositionUI is not active: "+fPositionCategoryName, new IllegalStateException())); //$NON-NLS-1$
+               
                redrawRegion();
                
-               Position position= fManager.getPreviousPosition(fFramePosition.getOffset());
-               if (position == null) {
-                       fViewer.getTextWidget().getDisplay().beep();
+               fFramePosition= fManager.getPreviousPosition(fFramePosition.getOffset());
+               if (fFramePosition == null) {
+                       if (fFinalCaretPosition != null)
+                               fFramePosition= fFinalCaretPosition;
+                       else
+                               fFramePosition= fManager.getLastPosition();
+               }
+               if (fFramePosition == null) {
+                       leave(UNINSTALL | COMMIT | UPDATE_CARET);
                } else {
-                       fFramePosition= position;
                        selectRegion();
+//                     triggerContentAssist();
                        redrawRegion();
                }
        }
 
+       /** Trigger content assist on choice positions */
+//     private void triggerContentAssist() {
+//             if (fFramePosition instanceof ProposalPosition) {
+//                     
+//                     ProposalPosition pp= (ProposalPosition) fFramePosition;
+//                     initializeContentAssistant();
+//                     if (fAssistant == null)
+//                             return;
+//                     fAssistant.setCompletions(pp.getChoices());
+//                     fAssistant.showPossibleCompletions();
+//             } else {
+//                     if (fAssistant != null)
+//                             fAssistant.setCompletions(new ICompletionProposal[0]);
+//             }
+//     }
+       
+       /** Lazy initialize content assistant for this linked ui */
+//     private void initializeContentAssistant() {
+//             if (fAssistant != null)
+//                     return;
+//             fAssistant= new ContentAssistant2();
+//             fAssistant.setDocumentPartitioning(IJavaPartitions.JAVA_PARTITIONING);
+//             fAssistant.install(fViewer);
+//     }
+
        /*
         * @see VerifyKeyListener#verifyKey(VerifyEvent)
         */
        public void verifyKey(VerifyEvent event) {
 
-               if (!event.doit)
+               if (!event.doit || !fIsActive)
                        return;
                
                Point selection= fViewer.getSelectedRange();
@@ -426,7 +558,23 @@ public class LinkedPositionUI implements LinkedPositionListener,
                        break;
 
                // ENTER
+               case 0x0A: // Ctrl+Enter
                case 0x0D:
+                       {
+//                     if (fAssistant != null && fAssistant.wasProposalChosen()) {
+//                             next();
+//                             event.doit= false;
+//                             break;
+//                     }
+               
+                               // if enter was treated as a document change, would it exceed variable range?
+                               if (!LinkedPositionManager.includes(fFramePosition, offset, length)
+                                               || (fFramePosition == fFinalCaretPosition)) {
+                                       leave(UNINSTALL | COMMIT);
+                                       return;
+                               }
+                       }
+                       
                        leave(UNINSTALL | COMMIT | UPDATE_CARET);
                        event.doit= false;
                        break;
@@ -436,17 +584,47 @@ public class LinkedPositionUI implements LinkedPositionListener,
                        leave(UNINSTALL | COMMIT);
                        event.doit= false;
                        break;
+                       
+               case ';':
+                       leave(UNINSTALL | COMMIT);
+                       event.doit= true;
+                       break;
+                       
+               default:
+                       if (event.character != 0) {
+                               if (!controlUndoBehavior(offset, length) || fFramePosition == fFinalCaretPosition) {
+                                       leave(UNINSTALL | COMMIT);
+                                       break;                                  
+                               }
+                       }
                }
        }
-
+       
+       private boolean controlUndoBehavior(int offset, int length) {
+                               
+               Position position= fManager.getEmbracingPosition(offset, length);
+               if (position != null) {
+                       
+                       ITextViewerExtension extension= (ITextViewerExtension) fViewer;
+                       IRewriteTarget target= extension.getRewriteTarget();
+                       
+                       if (fPreviousPosition != null && !fPreviousPosition.equals(position))
+                               target.endCompoundChange();
+                       target.beginCompoundChange();
+               }
+               
+               fPreviousPosition= position;
+               return fPreviousPosition != null;
+       }
+       
        /*
         * @see VerifyListener#verifyText(VerifyEvent)
         */
        public void verifyText(VerifyEvent event) {
                if (!event.doit)
                        return;
-
-
+       
+       
                int offset= 0;
                int length= 0;
                
@@ -606,7 +784,7 @@ public class LinkedPositionUI implements LinkedPositionListener,
                        ExceptionHandler.handle((InvocationTargetException)e, shell, title, null);
                else {
                        MessageDialog.openError(shell, title, e.getMessage());
-      PHPeclipsePlugin.log(e);
+                       PHPeclipsePlugin.log(e);
                }
        }
 
@@ -659,7 +837,42 @@ public class LinkedPositionUI implements LinkedPositionListener,
         * @see org.eclipse.swt.events.ShellListener#shellDeactivated(org.eclipse.swt.events.ShellEvent)
         */
        public void shellDeactivated(ShellEvent event) {
-               leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
+               // don't deactivate on focus lost, since the proposal popups may take focus
+               // plus: it doesn't hurt if you can check with another window without losing linked mode
+               // since there is no intrusive popup sticking out.
+               
+               // need to check first what happens on reentering based on an open action
+               // Seems to be no problem
+               
+               // TODO check whether we can leave it or uncomment it after debugging
+               // PS: why DOCUMENT_CHANGED? We want to trigger a redraw! (Shell deactivated does not mean
+               // it is not visible any longer.
+//             leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
+               
+               // Better:
+               // Check with content assistant and only leave if its not the proposal shell that took the 
+               // focus away.
+               
+               StyledText text;
+               Display display;
+
+//             if (fAssistant == null || fViewer == null || (text= fViewer.getTextWidget()) == null 
+//                             || (display= text.getDisplay()) == null || display.isDisposed()) {
+               if ( fViewer == null || (text= fViewer.getTextWidget()) == null 
+                     || (display= text.getDisplay()) == null || display.isDisposed()) {
+                       leave(UNINSTALL | COMMIT);
+               } else {
+                       // Post in UI thread since the assistant popup will only get the focus after we lose it.
+                       display.asyncExec(new Runnable() {
+                               public void run() {
+                                       // TODO add isDisposed / isUninstalled / hasLeft check? for now: check for content type,
+                                       // since it gets nullified in leave()
+                                       if (fIsActive) {// && (fAssistant == null || !fAssistant.hasFocus()))  {
+                                               leave(UNINSTALL | COMMIT);
+                                       }
+                               }
+                       });
+               }
        }
 
        /*
@@ -675,4 +888,4 @@ public class LinkedPositionUI implements LinkedPositionListener,
                leave(UNINSTALL | COMMIT | DOCUMENT_CHANGED);
        }
 
-}
\ No newline at end of file
+}