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;
20 //import org.eclipse.jface.text.Assert;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.ITextViewerExtension;
27 import org.eclipse.jface.text.TextViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.VerifyKeyListener;
30 import org.eclipse.swt.events.VerifyEvent;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.text.edits.MalformedTreeException;
33 import org.eclipse.text.edits.TextEdit;
36 * Installs as a verify key listener on a viewer and overwrites the behaviour of
37 * the backspace key. Clients may register undo specifications for certain
38 * offsets in a document. The <code>SmartBackspaceManager</code> will manage
39 * the specfications and execute the contained <code>TextEdit</code>s when
40 * backspace is pressed at the given offset and the specification is still
43 * Undo specifications are removed after a number of typing runs.
48 public class SmartBackspaceManager {
49 /* independent of JDT - may be moved to jface.text */
52 * An undo specification describes the change that should be executed if
53 * backspace is pressed at its trigger offset.
57 public static final class UndoSpec {
58 private final int triggerOffset;
60 private final IRegion selection;
62 private final TextEdit[] undoEdits;
64 private final UndoSpec child;
69 * Creates a new spec. A specification consists of a number of
70 * <code>TextEdit</code> s that will be executed when backspace is
71 * pressed at <code>triggerOffset</code>. The spec will be removed
72 * when it is executed, or if more than <code>lives</code>
73 * <code>TypingRun</code>s
74 * have ended after registering the spec.
76 * Optionally, a child specification can be registered. After executing
77 * the spec, the child spec will be registered with the manager. This
78 * allows to create chains of <code>UndoSpec</code>s that will be
79 * executed upon repeated pressing of backspace.
82 * @param triggerOffset
83 * the offset where this spec is active
85 * the selection after executing the undo spec
87 * the <code>TextEdit</code> s to perform when executing
90 * the number of <code>TypingRun</code> s before removing
93 * a child specification that will be registered after
94 * executing this spec, or <code>null</code>
96 public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits,
97 int lives, UndoSpec child) {
98 Assert.isLegal(triggerOffset >= 0);
99 Assert.isLegal(selection != null);
100 Assert.isLegal(lives >= 0);
101 Assert.isLegal(edits != null);
102 Assert.isLegal(edits.length > 0);
103 for (int i = 0; i < edits.length; i++) {
104 Assert.isLegal(edits[i] != null);
107 this.triggerOffset = triggerOffset;
108 this.selection = selection;
109 this.undoEdits = edits;
115 private class BackspaceListener implements VerifyKeyListener {
118 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
120 public void verifyKey(VerifyEvent event) {
121 if (fViewer != null && isBackspace(event)) {
122 int offset = getCaretOffset();
123 UndoSpec spec = removeEdit(offset);
127 for (int i = 0; i < spec.undoEdits.length; i++) {
128 spec.undoEdits[i].apply(getDocument(),
129 TextEdit.UPDATE_REGIONS);
131 fViewer.setSelectedRange(spec.selection.getOffset(),
132 spec.selection.getLength());
133 if (spec.child != null)
134 register(spec.child);
135 } catch (MalformedTreeException e) {
136 // fall back to standard bs
138 } catch (BadLocationException e) {
139 // fall back to standard bs
150 private void beginChange() {
151 ITextViewer viewer = fViewer;
152 if (viewer instanceof TextViewer) {
153 TextViewer v = (TextViewer) viewer;
154 v.getRewriteTarget().beginCompoundChange();
159 private void endChange() {
160 ITextViewer viewer = fViewer;
161 if (viewer instanceof TextViewer) {
162 TextViewer v = (TextViewer) viewer;
163 v.getRewriteTarget().endCompoundChange();
168 private boolean isBackspace(VerifyEvent event) {
169 return event.doit == true && event.character == SWT.BS
170 && event.stateMask == 0;
173 private int getCaretOffset() {
174 ITextViewer viewer = fViewer;
175 Point point = viewer.getSelectedRange();
181 private ITextViewer fViewer;
183 private BackspaceListener fBackspaceListener;
187 private TypingRunDetector fRunDetector;
189 private ITypingRunListener fRunListener;
192 * Registers an undo specification with this manager.
195 * the specification to register
196 * @throws IllegalStateException
197 * if the manager is not installed
199 public void register(UndoSpec spec) {
201 throw new IllegalStateException();
203 ensureListenerInstalled();
207 private void addEdit(UndoSpec spec) {
208 Integer i = new Integer(spec.triggerOffset);
212 private UndoSpec removeEdit(int offset) {
213 Integer i = new Integer(offset);
214 UndoSpec spec = (UndoSpec) fSpecs.remove(i);
218 private void ensureListenerInstalled() {
219 if (fBackspaceListener == null) {
220 fBackspaceListener = new BackspaceListener();
221 ITextViewer viewer = fViewer;
222 if (viewer instanceof ITextViewerExtension)
223 ((ITextViewerExtension) viewer)
224 .prependVerifyKeyListener(fBackspaceListener);
226 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
230 private void ensureListenerRemoved() {
231 if (fBackspaceListener != null) {
232 ITextViewer viewer = fViewer;
233 if (viewer instanceof ITextViewerExtension)
234 ((ITextViewerExtension) viewer)
235 .removeVerifyKeyListener(fBackspaceListener);
237 viewer.getTextWidget().removeVerifyKeyListener(
239 fBackspaceListener = null;
243 private IDocument getDocument() {
244 return fViewer.getDocument();
248 * Installs the receiver on a text viewer.
252 public void install(ITextViewer viewer) {
253 Assert.isLegal(viewer != null);
256 fSpecs = new HashMap();
257 fRunDetector = new TypingRunDetector();
258 fRunDetector.install(viewer);
259 fRunListener = new ITypingRunListener() {
262 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
264 public void typingRunStarted(TypingRun run) {
268 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
270 public void typingRunEnded(TypingRun run, ChangeType reason) {
271 if (reason == TypingRun.SELECTION)
277 fRunDetector.addTypingRunListener(fRunListener);
280 private void prune() {
281 for (Iterator it = fSpecs.values().iterator(); it.hasNext();) {
282 UndoSpec spec = (UndoSpec) it.next();
283 if (--spec.lives < 0)
289 * Uninstalls the receiver. No undo specifications may be registered on an
290 * uninstalled manager.
292 public void uninstall() {
293 if (fViewer != null) {
294 fRunDetector.removeTypingRunListener(fRunListener);
295 fRunDetector.uninstall();
297 ensureListenerRemoved();