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 org.eclipse.text.edits.MalformedTreeException;
18 import org.eclipse.text.edits.TextEdit;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.VerifyKeyListener;
22 import org.eclipse.swt.events.VerifyEvent;
23 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.jface.text.Assert;
26 import org.eclipse.jface.text.BadLocationException;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IRegion;
29 import org.eclipse.jface.text.ITextViewer;
30 import org.eclipse.jface.text.ITextViewerExtension;
31 import org.eclipse.jface.text.TextViewer;
33 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
38 * Installs as a verify key listener on a viewer and overwrites the behaviour
39 * of the backspace key. Clients may register undo specifications for certain
40 * offsets in a document. The <code>SmartBackspaceManager</code> will manage the
41 * specfications and execute the contained <code>TextEdit</code>s when backspace
42 * is pressed at the given offset and the specification is still valid.
44 * Undo specifications are removed after a number of typing runs.
49 public class SmartBackspaceManager {
50 /* independent of JDT - may be moved to jface.text */
53 * An undo specification describes the change that should be executed if
54 * backspace is pressed at its trigger offset.
58 public static final class UndoSpec {
59 private final int triggerOffset;
60 private final IRegion selection;
61 private final TextEdit[] undoEdits;
62 private final UndoSpec child;
66 * Creates a new spec. A specification consists of a number of
67 * <code>TextEdit</code> s that will be executed when backspace is
68 * pressed at <code>triggerOffset</code>. The spec will be removed
69 * when it is executed, or if more than <code>lives</code>
70 * <code>TypingRun</code>s have ended after registering the spec.
72 * Optionally, a child specification can be registered. After executing
73 * the spec, the child spec will be registered with the manager. This allows
74 * to create chains of <code>UndoSpec</code>s that will be executed upon
75 * repeated pressing of backspace.
78 * @param triggerOffset the offset where this spec is active
79 * @param selection the selection after executing the undo spec
80 * @param edits the <code>TextEdit</code> s to perform when executing
82 * @param lives the number of <code>TypingRun</code> s before removing
84 * @param child a child specification that will be registered after
85 * executing this spec, or <code>null</code>
87 public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits, int lives, UndoSpec child) {
88 Assert.isLegal(triggerOffset >= 0);
89 Assert.isLegal(selection != null);
90 Assert.isLegal(lives >= 0);
91 Assert.isLegal(edits != null);
92 Assert.isLegal(edits.length > 0);
93 for (int i= 0; i < edits.length; i++) {
94 Assert.isLegal(edits[i] != null);
97 this.triggerOffset= triggerOffset;
98 this.selection= selection;
99 this.undoEdits= edits;
106 private class BackspaceListener implements VerifyKeyListener {
109 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
111 public void verifyKey(VerifyEvent event) {
112 if (fViewer != null && isBackspace(event)) {
113 int offset= getCaretOffset();
114 UndoSpec spec= removeEdit(offset);
118 for (int i= 0; i < spec.undoEdits.length; i++) {
119 spec.undoEdits[i].apply(getDocument(), TextEdit.UPDATE_REGIONS);
121 fViewer.setSelectedRange(spec.selection.getOffset(), spec.selection.getLength());
122 if (spec.child != null)
123 register(spec.child);
124 } catch (MalformedTreeException e) {
125 // fall back to standard bs
127 } catch (BadLocationException e) {
128 // fall back to standard bs
139 private void beginChange() {
140 ITextViewer viewer= fViewer;
141 if (viewer instanceof TextViewer) {
142 TextViewer v= (TextViewer) viewer;
143 v.getRewriteTarget().beginCompoundChange();
148 private void endChange() {
149 ITextViewer viewer= fViewer;
150 if (viewer instanceof TextViewer) {
151 TextViewer v= (TextViewer) viewer;
152 v.getRewriteTarget().endCompoundChange();
157 private boolean isBackspace(VerifyEvent event) {
158 return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
161 private int getCaretOffset() {
162 ITextViewer viewer= fViewer;
163 Point point= viewer.getSelectedRange();
169 private ITextViewer fViewer;
170 private BackspaceListener fBackspaceListener;
172 private TypingRunDetector fRunDetector;
173 private ITypingRunListener fRunListener;
176 * Registers an undo specification with this manager.
178 * @param spec the specification to register
179 * @throws IllegalStateException if the manager is not installed
181 public void register(UndoSpec spec) {
183 throw new IllegalStateException();
185 ensureListenerInstalled();
189 private void addEdit(UndoSpec spec) {
190 Integer i= new Integer(spec.triggerOffset);
194 private UndoSpec removeEdit(int offset) {
195 Integer i= new Integer(offset);
196 UndoSpec spec= (UndoSpec) fSpecs.remove(i);
200 private void ensureListenerInstalled() {
201 if (fBackspaceListener == null) {
202 fBackspaceListener= new BackspaceListener();
203 ITextViewer viewer= fViewer;
204 if (viewer instanceof ITextViewerExtension)
205 ((ITextViewerExtension) viewer).prependVerifyKeyListener(fBackspaceListener);
207 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
211 private void ensureListenerRemoved() {
212 if (fBackspaceListener != null) {
213 ITextViewer viewer= fViewer;
214 if (viewer instanceof ITextViewerExtension)
215 ((ITextViewerExtension) viewer).removeVerifyKeyListener(fBackspaceListener);
217 viewer.getTextWidget().removeVerifyKeyListener(fBackspaceListener);
218 fBackspaceListener= null;
222 private IDocument getDocument() {
223 return fViewer.getDocument();
227 * Installs the receiver on a text viewer.
231 public void install(ITextViewer viewer) {
232 Assert.isLegal(viewer != null);
235 fSpecs= new HashMap();
236 fRunDetector= new TypingRunDetector();
237 fRunDetector.install(viewer);
238 fRunListener= new ITypingRunListener() {
241 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
243 public void typingRunStarted(TypingRun run) {
247 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
249 public void typingRunEnded(TypingRun run, ChangeType reason) {
250 if (reason == TypingRun.SELECTION)
256 fRunDetector.addTypingRunListener(fRunListener);
259 private void prune() {
260 for (Iterator it= fSpecs.values().iterator(); it.hasNext();) {
261 UndoSpec spec= (UndoSpec) it.next();
262 if (--spec.lives < 0)
268 * Uninstalls the receiver. No undo specifications may be registered on an
269 * uninstalled manager.
271 public void uninstall() {
272 if (fViewer != null) {
273 fRunDetector.removeTypingRunListener(fRunListener);
274 fRunDetector.uninstall();
276 ensureListenerRemoved();