1) Moved net.sourceforge.phpeclipse.ui\src\net\sourceforge\phpdt back to net.sourcefo...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / SmartBackspaceManager.java
index 5661707..8005ff0 100644 (file)
@@ -16,7 +16,9 @@ import java.util.Map;
 
 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
 
-import org.eclipse.jface.text.Assert;
+//incastrix
+//import org.eclipse.jface.text.Assert;
+import org.eclipse.core.runtime.Assert;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
@@ -30,18 +32,17 @@ import org.eclipse.swt.graphics.Point;
 import org.eclipse.text.edits.MalformedTreeException;
 import org.eclipse.text.edits.TextEdit;
 
-
-
 /**
- * Installs as a verify key listener on a viewer and overwrites the behaviour
- * of the backspace key. Clients may register undo specifications for certain
- * offsets in a document. The <code>SmartBackspaceManager</code> will manage the
- * specfications and execute the contained <code>TextEdit</code>s when backspace
- * is pressed at the given offset and the specification is still valid.
+ * Installs as a verify key listener on a viewer and overwrites the behaviour of
+ * the backspace key. Clients may register undo specifications for certain
+ * offsets in a document. The <code>SmartBackspaceManager</code> will manage
+ * the specfications and execute the contained <code>TextEdit</code>s when
+ * backspace is pressed at the given offset and the specification is still
+ * valid.
  * <p>
  * Undo specifications are removed after a number of typing runs.
  * </p>
- *  
+ * 
  * @since 3.0
  */
 public class SmartBackspaceManager {
@@ -50,57 +51,67 @@ public class SmartBackspaceManager {
        /**
         * An undo specification describes the change that should be executed if
         * backspace is pressed at its trigger offset.
-        *  
+        * 
         * @since 3.0
         */
        public static final class UndoSpec {
                private final int triggerOffset;
+
                private final IRegion selection;
+
                private final TextEdit[] undoEdits;
+
                private final UndoSpec child;
+
                int lives;
-               
+
                /**
                 * Creates a new spec. A specification consists of a number of
                 * <code>TextEdit</code> s that will be executed when backspace is
                 * pressed at <code>triggerOffset</code>. The spec will be removed
                 * when it is executed, or if more than <code>lives</code>
-                * <code>TypingRun</code>s have ended after registering the spec.
+                * <code>TypingRun</code>s
+                * have ended after registering the spec.
                 * <p>
                 * Optionally, a child specification can be registered. After executing
-                * the spec, the child spec will be registered with the manager. This allows
-                * to create chains of <code>UndoSpec</code>s that will be executed upon
-                * repeated pressing of backspace.
+                * the spec, the child spec will be registered with the manager. This
+                * allows to create chains of <code>UndoSpec</code>s that will be
+                * executed upon repeated pressing of backspace.
                 * </p>
                 * 
-                * @param triggerOffset the offset where this spec is active
-                * @param selection the selection after executing the undo spec
-                * @param edits the <code>TextEdit</code> s to perform when executing
-                *        the spec
-                * @param lives the number of <code>TypingRun</code> s before removing
-                *        the spec
-                * @param child a child specification that will be registered after
-                *        executing this spec, or <code>null</code>
+                * @param triggerOffset
+                *            the offset where this spec is active
+                * @param selection
+                *            the selection after executing the undo spec
+                * @param edits
+                *            the <code>TextEdit</code> s to perform when executing
+                *            the spec
+                * @param lives
+                *            the number of <code>TypingRun</code> s before removing
+                *            the spec
+                * @param child
+                *            a child specification that will be registered after
+                *            executing this spec, or <code>null</code>
                 */
-               public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits, int lives, UndoSpec child) {
+               public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits,
+                               int lives, UndoSpec child) {
                        Assert.isLegal(triggerOffset >= 0);
                        Assert.isLegal(selection != null);
                        Assert.isLegal(lives >= 0);
                        Assert.isLegal(edits != null);
                        Assert.isLegal(edits.length > 0);
-                       for (int i= 0; i < edits.length; i++) {
+                       for (int i = 0; i < edits.length; i++) {
                                Assert.isLegal(edits[i] != null);
                        }
-                       
-                       this.triggerOffset= triggerOffset;
-                       this.selection= selection;
-                       this.undoEdits= edits;
-                       this.lives= lives;
-                       this.child= child;
+
+                       this.triggerOffset = triggerOffset;
+                       this.selection = selection;
+                       this.undoEdits = edits;
+                       this.lives = lives;
+                       this.child = child;
                }
        }
 
-
        private class BackspaceListener implements VerifyKeyListener {
 
                /*
@@ -108,15 +119,17 @@ public class SmartBackspaceManager {
                 */
                public void verifyKey(VerifyEvent event) {
                        if (fViewer != null && isBackspace(event)) {
-                               int offset= getCaretOffset();
-                               UndoSpec spec= removeEdit(offset);
+                               int offset = getCaretOffset();
+                               UndoSpec spec = removeEdit(offset);
                                if (spec != null) {
                                        try {
                                                beginChange();
-                                               for (int i= 0; i < spec.undoEdits.length; i++) {
-                                                       spec.undoEdits[i].apply(getDocument(), TextEdit.UPDATE_REGIONS);
+                                               for (int i = 0; i < spec.undoEdits.length; i++) {
+                                                       spec.undoEdits[i].apply(getDocument(),
+                                                                       TextEdit.UPDATE_REGIONS);
                                                }
-                                               fViewer.setSelectedRange(spec.selection.getOffset(), spec.selection.getLength());
+                                               fViewer.setSelectedRange(spec.selection.getOffset(),
+                                                               spec.selection.getLength());
                                                if (spec.child != null)
                                                        register(spec.child);
                                        } catch (MalformedTreeException e) {
@@ -128,79 +141,87 @@ public class SmartBackspaceManager {
                                        } finally {
                                                endChange();
                                        }
-                                       event.doit= false;
+                                       event.doit = false;
                                }
-                               
+
                        }
                }
 
                private void beginChange() {
-                       ITextViewer viewer= fViewer;
+                       ITextViewer viewer = fViewer;
                        if (viewer instanceof TextViewer) {
-                               TextViewer v= (TextViewer) viewer;
+                               TextViewer v = (TextViewer) viewer;
                                v.getRewriteTarget().beginCompoundChange();
                                v.setRedraw(false);
                        }
                }
 
                private void endChange() {
-                       ITextViewer viewer= fViewer;
+                       ITextViewer viewer = fViewer;
                        if (viewer instanceof TextViewer) {
-                               TextViewer v= (TextViewer) viewer;
+                               TextViewer v = (TextViewer) viewer;
                                v.getRewriteTarget().endCompoundChange();
                                v.setRedraw(true);
                        }
                }
 
                private boolean isBackspace(VerifyEvent event) {
-                       return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
+                       return event.doit == true && event.character == SWT.BS
+                                       && event.stateMask == 0;
                }
 
                private int getCaretOffset() {
-                       ITextViewer viewer= fViewer;
-                       Point point= viewer.getSelectedRange();
+                       ITextViewer viewer = fViewer;
+                       Point point = viewer.getSelectedRange();
                        return point.x;
                }
 
        }
-       
+
        private ITextViewer fViewer;
+
        private BackspaceListener fBackspaceListener;
+
        private Map fSpecs;
+
        private TypingRunDetector fRunDetector;
+
        private ITypingRunListener fRunListener;
 
        /**
         * Registers an undo specification with this manager.
         * 
-        * @param spec the specification to register
-        * @throws IllegalStateException if the manager is not installed
+        * @param spec
+        *            the specification to register
+        * @throws IllegalStateException
+        *             if the manager is not installed
         */
        public void register(UndoSpec spec) {
                if (fViewer == null)
                        throw new IllegalStateException();
-               
+
                ensureListenerInstalled();
                addEdit(spec);
        }
 
        private void addEdit(UndoSpec spec) {
-               Integer i= new Integer(spec.triggerOffset);
+               Integer i = new Integer(spec.triggerOffset);
                fSpecs.put(i, spec);
        }
-       
+
        private UndoSpec removeEdit(int offset) {
-               Integer i= new Integer(offset);
-               UndoSpec spec= (UndoSpec) fSpecs.remove(i);
+               Integer i = new Integer(offset);
+               UndoSpec spec = (UndoSpec) fSpecs.remove(i);
                return spec;
        }
 
        private void ensureListenerInstalled() {
                if (fBackspaceListener == null) {
-                       fBackspaceListener= new BackspaceListener();
-                       ITextViewer viewer= fViewer;
+                       fBackspaceListener = new BackspaceListener();
+                       ITextViewer viewer = fViewer;
                        if (viewer instanceof ITextViewerExtension)
-                               ((ITextViewerExtension) viewer).prependVerifyKeyListener(fBackspaceListener);
+                               ((ITextViewerExtension) viewer)
+                                               .prependVerifyKeyListener(fBackspaceListener);
                        else
                                viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
                }
@@ -208,12 +229,14 @@ public class SmartBackspaceManager {
 
        private void ensureListenerRemoved() {
                if (fBackspaceListener != null) {
-                       ITextViewer viewer= fViewer;
+                       ITextViewer viewer = fViewer;
                        if (viewer instanceof ITextViewerExtension)
-                               ((ITextViewerExtension) viewer).removeVerifyKeyListener(fBackspaceListener);
+                               ((ITextViewerExtension) viewer)
+                                               .removeVerifyKeyListener(fBackspaceListener);
                        else
-                               viewer.getTextWidget().removeVerifyKeyListener(fBackspaceListener);
-                       fBackspaceListener= null;
+                               viewer.getTextWidget().removeVerifyKeyListener(
+                                               fBackspaceListener);
+                       fBackspaceListener = null;
                }
        }
 
@@ -228,12 +251,12 @@ public class SmartBackspaceManager {
         */
        public void install(ITextViewer viewer) {
                Assert.isLegal(viewer != null);
-               
-               fViewer= viewer;
-               fSpecs= new HashMap();
-               fRunDetector= new TypingRunDetector();
+
+               fViewer = viewer;
+               fSpecs = new HashMap();
+               fRunDetector = new TypingRunDetector();
                fRunDetector.install(viewer);
-               fRunListener= new ITypingRunListener() {
+               fRunListener = new ITypingRunListener() {
 
                        /*
                         * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
@@ -253,15 +276,15 @@ public class SmartBackspaceManager {
                };
                fRunDetector.addTypingRunListener(fRunListener);
        }
-       
+
        private void prune() {
-               for (Iterator it= fSpecs.values().iterator(); it.hasNext();) {
-                       UndoSpec spec= (UndoSpec) it.next();
+               for (Iterator it = fSpecs.values().iterator(); it.hasNext();) {
+                       UndoSpec spec = (UndoSpec) it.next();
                        if (--spec.lives < 0)
                                it.remove();
                }
        }
-       
+
        /**
         * Uninstalls the receiver. No undo specifications may be registered on an
         * uninstalled manager.
@@ -270,9 +293,9 @@ public class SmartBackspaceManager {
                if (fViewer != null) {
                        fRunDetector.removeTypingRunListener(fRunListener);
                        fRunDetector.uninstall();
-                       fRunDetector= null;
+                       fRunDetector = null;
                        ensureListenerRemoved();
-                       fViewer= null;
+                       fViewer = null;
                }
        }
 }