import java.util.Iterator;
import java.util.Map;
-import org.eclipse.text.edits.MalformedTreeException;
-import org.eclipse.text.edits.TextEdit;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.VerifyKeyListener;
-import org.eclipse.swt.events.VerifyEvent;
-import org.eclipse.swt.graphics.Point;
+import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITextViewerExtension;
import org.eclipse.jface.text.TextViewer;
-
-import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
-
-
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.VerifyKeyListener;
+import org.eclipse.swt.events.VerifyEvent;
+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 {
/**
* 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 {
/*
*/
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) {
} 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);
}
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;
}
}
*/
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)
};
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.
if (fViewer != null) {
fRunDetector.removeTypingRunListener(fRunListener);
fRunDetector.uninstall();
- fRunDetector= null;
+ fRunDetector = null;
ensureListenerRemoved();
- fViewer= null;
+ fViewer = null;
}
}
}