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;
34 * Installs as a verify key listener on a viewer and overwrites the behaviour of
35 * the backspace key. Clients may register undo specifications for certain
36 * offsets in a document. The <code>SmartBackspaceManager</code> will manage
37 * the specfications and execute the contained <code>TextEdit</code>s when
38 * backspace is pressed at the given offset and the specification is still
41 * Undo specifications are removed after a number of typing runs.
46 public class SmartBackspaceManager {
47 /* independent of JDT - may be moved to jface.text */
50 * An undo specification describes the change that should be executed if
51 * backspace is pressed at its trigger offset.
55 public static final class UndoSpec {
56 private final int triggerOffset;
58 private final IRegion selection;
60 private final TextEdit[] undoEdits;
62 private final UndoSpec child;
67 * Creates a new spec. A specification consists of a number of
68 * <code>TextEdit</code> s that will be executed when backspace is
69 * pressed at <code>triggerOffset</code>. The spec will be removed
70 * when it is executed, or if more than <code>lives</code>
71 * <code>TypingRun</code>s
72 * have ended after registering the spec.
74 * Optionally, a child specification can be registered. After executing
75 * the spec, the child spec will be registered with the manager. This
76 * allows to create chains of <code>UndoSpec</code>s that will be
77 * executed upon repeated pressing of backspace.
80 * @param triggerOffset
81 * the offset where this spec is active
83 * the selection after executing the undo spec
85 * the <code>TextEdit</code> s to perform when executing
88 * the number of <code>TypingRun</code> s before removing
91 * a child specification that will be registered after
92 * executing this spec, or <code>null</code>
94 public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits,
95 int lives, UndoSpec child) {
96 Assert.isLegal(triggerOffset >= 0);
97 Assert.isLegal(selection != null);
98 Assert.isLegal(lives >= 0);
99 Assert.isLegal(edits != null);
100 Assert.isLegal(edits.length > 0);
101 for (int i = 0; i < edits.length; i++) {
102 Assert.isLegal(edits[i] != null);
105 this.triggerOffset = triggerOffset;
106 this.selection = selection;
107 this.undoEdits = edits;
113 private class BackspaceListener implements VerifyKeyListener {
116 * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
118 public void verifyKey(VerifyEvent event) {
119 if (fViewer != null && isBackspace(event)) {
120 int offset = getCaretOffset();
121 UndoSpec spec = removeEdit(offset);
125 for (int i = 0; i < spec.undoEdits.length; i++) {
126 spec.undoEdits[i].apply(getDocument(),
127 TextEdit.UPDATE_REGIONS);
129 fViewer.setSelectedRange(spec.selection.getOffset(),
130 spec.selection.getLength());
131 if (spec.child != null)
132 register(spec.child);
133 } catch (MalformedTreeException e) {
134 // fall back to standard bs
136 } catch (BadLocationException e) {
137 // fall back to standard bs
148 private void beginChange() {
149 ITextViewer viewer = fViewer;
150 if (viewer instanceof TextViewer) {
151 TextViewer v = (TextViewer) viewer;
152 v.getRewriteTarget().beginCompoundChange();
157 private void endChange() {
158 ITextViewer viewer = fViewer;
159 if (viewer instanceof TextViewer) {
160 TextViewer v = (TextViewer) viewer;
161 v.getRewriteTarget().endCompoundChange();
166 private boolean isBackspace(VerifyEvent event) {
167 return event.doit == true && event.character == SWT.BS
168 && event.stateMask == 0;
171 private int getCaretOffset() {
172 ITextViewer viewer = fViewer;
173 Point point = viewer.getSelectedRange();
179 private ITextViewer fViewer;
181 private BackspaceListener fBackspaceListener;
185 private TypingRunDetector fRunDetector;
187 private ITypingRunListener fRunListener;
190 * Registers an undo specification with this manager.
193 * the specification to register
194 * @throws IllegalStateException
195 * if the manager is not installed
197 public void register(UndoSpec spec) {
199 throw new IllegalStateException();
201 ensureListenerInstalled();
205 private void addEdit(UndoSpec spec) {
206 Integer i = new Integer(spec.triggerOffset);
210 private UndoSpec removeEdit(int offset) {
211 Integer i = new Integer(offset);
212 UndoSpec spec = (UndoSpec) fSpecs.remove(i);
216 private void ensureListenerInstalled() {
217 if (fBackspaceListener == null) {
218 fBackspaceListener = new BackspaceListener();
219 ITextViewer viewer = fViewer;
220 if (viewer instanceof ITextViewerExtension)
221 ((ITextViewerExtension) viewer)
222 .prependVerifyKeyListener(fBackspaceListener);
224 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
228 private void ensureListenerRemoved() {
229 if (fBackspaceListener != null) {
230 ITextViewer viewer = fViewer;
231 if (viewer instanceof ITextViewerExtension)
232 ((ITextViewerExtension) viewer)
233 .removeVerifyKeyListener(fBackspaceListener);
235 viewer.getTextWidget().removeVerifyKeyListener(
237 fBackspaceListener = null;
241 private IDocument getDocument() {
242 return fViewer.getDocument();
246 * Installs the receiver on a text viewer.
250 public void install(ITextViewer viewer) {
251 Assert.isLegal(viewer != null);
254 fSpecs = new HashMap();
255 fRunDetector = new TypingRunDetector();
256 fRunDetector.install(viewer);
257 fRunListener = new ITypingRunListener() {
260 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
262 public void typingRunStarted(TypingRun run) {
266 * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
268 public void typingRunEnded(TypingRun run, ChangeType reason) {
269 if (reason == TypingRun.SELECTION)
275 fRunDetector.addTypingRunListener(fRunListener);
278 private void prune() {
279 for (Iterator it = fSpecs.values().iterator(); it.hasNext();) {
280 UndoSpec spec = (UndoSpec) it.next();
281 if (--spec.lives < 0)
287 * Uninstalls the receiver. No undo specifications may be registered on an
288 * uninstalled manager.
290 public void uninstall() {
291 if (fViewer != null) {
292 fRunDetector.removeTypingRunListener(fRunListener);
293 fRunDetector.uninstall();
295 ensureListenerRemoved();