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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.util.HashMap;
14 import java.util.Iterator;
17 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
19 import org.eclipse.jface.text.Assert;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.ITextViewerExtension;
25 import org.eclipse.jface.text.TextViewer;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.custom.VerifyKeyListener;
28 import org.eclipse.swt.events.VerifyEvent;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.text.edits.MalformedTreeException;
31 import org.eclipse.text.edits.TextEdit;
36 * Installs as a verify key listener on a viewer and overwrites the behaviour
37 * of the backspace key. Clients may register undo specifications for certain
38 * offsets in a document. The <code>SmartBackspaceManager</code> will manage the
39 * specfications and execute the contained <code>TextEdit</code>s when backspace
40 * is pressed at the given offset and the specification is still valid.
42 * Undo specifications are removed after a number of typing runs.
47 public class SmartBackspaceManager {
48 /* independent of JDT - may be moved to jface.text */
51 * An undo specification describes the change that should be executed if
52 * backspace is pressed at its trigger offset.
56 public static final class UndoSpec {
57 private final int triggerOffset;
58 private final IRegion selection;
59 private final TextEdit[] undoEdits;
60 private final UndoSpec child;
64 * Creates a new spec. A specification consists of a number of
65 * <code>TextEdit</code> s that will be executed when backspace is
66 * pressed at <code>triggerOffset</code>. The spec will be removed
67 * when it is executed, or if more than <code>lives</code>
68 * <code>TypingRun</code>s have ended after registering the spec.
70 * Optionally, a child specification can be registered. After executing
71 * the spec, the child spec will be registered with the manager. This allows
72 * to create chains of <code>UndoSpec</code>s that will be executed upon
73 * repeated pressing of backspace.
76 * @param triggerOffset the offset where this spec is active
77 * @param selection the selection after executing the undo spec
78 * @param edits the <code>TextEdit</code> s to perform when executing
80 * @param lives the number of <code>TypingRun</code> s before removing
82 * @param child a child specification that will be registered after
83 * executing this spec, or <code>null</code>
85 public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits, int lives, UndoSpec child) {
86 Assert.isLegal(triggerOffset >= 0);
87 Assert.isLegal(selection != null);
88 Assert.isLegal(lives >= 0);
89 Assert.isLegal(edits != null);
90 Assert.isLegal(edits.length > 0);
91 for (int i= 0; i < edits.length; i++) {
92 Assert.isLegal(edits[i] != null);
95 this.triggerOffset= triggerOffset;
96 this.selection= selection;
97 this.undoEdits= edits;
104 private class BackspaceListener implements VerifyKeyListener {
107 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
109 public void verifyKey(VerifyEvent event) {
110 if (fViewer != null && isBackspace(event)) {
111 int offset= getCaretOffset();
112 UndoSpec spec= removeEdit(offset);
116 for (int i= 0; i < spec.undoEdits.length; i++) {
117 spec.undoEdits[i].apply(getDocument(), TextEdit.UPDATE_REGIONS);
119 fViewer.setSelectedRange(spec.selection.getOffset(), spec.selection.getLength());
120 if (spec.child != null)
121 register(spec.child);
122 } catch (MalformedTreeException e) {
123 // fall back to standard bs
125 } catch (BadLocationException e) {
126 // fall back to standard bs
137 private void beginChange() {
138 ITextViewer viewer= fViewer;
139 if (viewer instanceof TextViewer) {
140 TextViewer v= (TextViewer) viewer;
141 v.getRewriteTarget().beginCompoundChange();
146 private void endChange() {
147 ITextViewer viewer= fViewer;
148 if (viewer instanceof TextViewer) {
149 TextViewer v= (TextViewer) viewer;
150 v.getRewriteTarget().endCompoundChange();
155 private boolean isBackspace(VerifyEvent event) {
156 return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
159 private int getCaretOffset() {
160 ITextViewer viewer= fViewer;
161 Point point= viewer.getSelectedRange();
167 private ITextViewer fViewer;
168 private BackspaceListener fBackspaceListener;
170 private TypingRunDetector fRunDetector;
171 private ITypingRunListener fRunListener;
174 * Registers an undo specification with this manager.
176 * @param spec the specification to register
177 * @throws IllegalStateException if the manager is not installed
179 public void register(UndoSpec spec) {
181 throw new IllegalStateException();
183 ensureListenerInstalled();
187 private void addEdit(UndoSpec spec) {
188 Integer i= new Integer(spec.triggerOffset);
192 private UndoSpec removeEdit(int offset) {
193 Integer i= new Integer(offset);
194 UndoSpec spec= (UndoSpec) fSpecs.remove(i);
198 private void ensureListenerInstalled() {
199 if (fBackspaceListener == null) {
200 fBackspaceListener= new BackspaceListener();
201 ITextViewer viewer= fViewer;
202 if (viewer instanceof ITextViewerExtension)
203 ((ITextViewerExtension) viewer).prependVerifyKeyListener(fBackspaceListener);
205 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
209 private void ensureListenerRemoved() {
210 if (fBackspaceListener != null) {
211 ITextViewer viewer= fViewer;
212 if (viewer instanceof ITextViewerExtension)
213 ((ITextViewerExtension) viewer).removeVerifyKeyListener(fBackspaceListener);
215 viewer.getTextWidget().removeVerifyKeyListener(fBackspaceListener);
216 fBackspaceListener= null;
220 private IDocument getDocument() {
221 return fViewer.getDocument();
225 * Installs the receiver on a text viewer.
229 public void install(ITextViewer viewer) {
230 Assert.isLegal(viewer != null);
233 fSpecs= new HashMap();
234 fRunDetector= new TypingRunDetector();
235 fRunDetector.install(viewer);
236 fRunListener= new ITypingRunListener() {
239 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
241 public void typingRunStarted(TypingRun run) {
245 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
247 public void typingRunEnded(TypingRun run, ChangeType reason) {
248 if (reason == TypingRun.SELECTION)
254 fRunDetector.addTypingRunListener(fRunListener);
257 private void prune() {
258 for (Iterator it= fSpecs.values().iterator(); it.hasNext();) {
259 UndoSpec spec= (UndoSpec) it.next();
260 if (--spec.lives < 0)
266 * Uninstalls the receiver. No undo specifications may be registered on an
267 * uninstalled manager.
269 public void uninstall() {
270 if (fViewer != null) {
271 fRunDetector.removeTypingRunListener(fRunListener);
272 fRunDetector.uninstall();
274 ensureListenerRemoved();