misc.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / SmartBackspaceManager.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
18
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;
32
33 /**
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
39  * valid.
40  * <p>
41  * Undo specifications are removed after a number of typing runs.
42  * </p>
43  * 
44  * @since 3.0
45  */
46 public class SmartBackspaceManager {
47         /* independent of JDT - may be moved to jface.text */
48
49         /**
50          * An undo specification describes the change that should be executed if
51          * backspace is pressed at its trigger offset.
52          * 
53          * @since 3.0
54          */
55         public static final class UndoSpec {
56                 private final int triggerOffset;
57
58                 private final IRegion selection;
59
60                 private final TextEdit[] undoEdits;
61
62                 private final UndoSpec child;
63
64                 int lives;
65
66                 /**
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.
73                  * <p>
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.
78                  * </p>
79                  * 
80                  * @param triggerOffset
81                  *            the offset where this spec is active
82                  * @param selection
83                  *            the selection after executing the undo spec
84                  * @param edits
85                  *            the <code>TextEdit</code> s to perform when executing
86                  *            the spec
87                  * @param lives
88                  *            the number of <code>TypingRun</code> s before removing
89                  *            the spec
90                  * @param child
91                  *            a child specification that will be registered after
92                  *            executing this spec, or <code>null</code>
93                  */
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);
103                         }
104
105                         this.triggerOffset = triggerOffset;
106                         this.selection = selection;
107                         this.undoEdits = edits;
108                         this.lives = lives;
109                         this.child = child;
110                 }
111         }
112
113         private class BackspaceListener implements VerifyKeyListener {
114
115                 /*
116                  * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
117                  */
118                 public void verifyKey(VerifyEvent event) {
119                         if (fViewer != null && isBackspace(event)) {
120                                 int offset = getCaretOffset();
121                                 UndoSpec spec = removeEdit(offset);
122                                 if (spec != null) {
123                                         try {
124                                                 beginChange();
125                                                 for (int i = 0; i < spec.undoEdits.length; i++) {
126                                                         spec.undoEdits[i].apply(getDocument(),
127                                                                         TextEdit.UPDATE_REGIONS);
128                                                 }
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
135                                                 return;
136                                         } catch (BadLocationException e) {
137                                                 // fall back to standard bs
138                                                 return;
139                                         } finally {
140                                                 endChange();
141                                         }
142                                         event.doit = false;
143                                 }
144
145                         }
146                 }
147
148                 private void beginChange() {
149                         ITextViewer viewer = fViewer;
150                         if (viewer instanceof TextViewer) {
151                                 TextViewer v = (TextViewer) viewer;
152                                 v.getRewriteTarget().beginCompoundChange();
153                                 v.setRedraw(false);
154                         }
155                 }
156
157                 private void endChange() {
158                         ITextViewer viewer = fViewer;
159                         if (viewer instanceof TextViewer) {
160                                 TextViewer v = (TextViewer) viewer;
161                                 v.getRewriteTarget().endCompoundChange();
162                                 v.setRedraw(true);
163                         }
164                 }
165
166                 private boolean isBackspace(VerifyEvent event) {
167                         return event.doit == true && event.character == SWT.BS
168                                         && event.stateMask == 0;
169                 }
170
171                 private int getCaretOffset() {
172                         ITextViewer viewer = fViewer;
173                         Point point = viewer.getSelectedRange();
174                         return point.x;
175                 }
176
177         }
178
179         private ITextViewer fViewer;
180
181         private BackspaceListener fBackspaceListener;
182
183         private Map fSpecs;
184
185         private TypingRunDetector fRunDetector;
186
187         private ITypingRunListener fRunListener;
188
189         /**
190          * Registers an undo specification with this manager.
191          * 
192          * @param spec
193          *            the specification to register
194          * @throws IllegalStateException
195          *             if the manager is not installed
196          */
197         public void register(UndoSpec spec) {
198                 if (fViewer == null)
199                         throw new IllegalStateException();
200
201                 ensureListenerInstalled();
202                 addEdit(spec);
203         }
204
205         private void addEdit(UndoSpec spec) {
206                 Integer i = new Integer(spec.triggerOffset);
207                 fSpecs.put(i, spec);
208         }
209
210         private UndoSpec removeEdit(int offset) {
211                 Integer i = new Integer(offset);
212                 UndoSpec spec = (UndoSpec) fSpecs.remove(i);
213                 return spec;
214         }
215
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);
223                         else
224                                 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
225                 }
226         }
227
228         private void ensureListenerRemoved() {
229                 if (fBackspaceListener != null) {
230                         ITextViewer viewer = fViewer;
231                         if (viewer instanceof ITextViewerExtension)
232                                 ((ITextViewerExtension) viewer)
233                                                 .removeVerifyKeyListener(fBackspaceListener);
234                         else
235                                 viewer.getTextWidget().removeVerifyKeyListener(
236                                                 fBackspaceListener);
237                         fBackspaceListener = null;
238                 }
239         }
240
241         private IDocument getDocument() {
242                 return fViewer.getDocument();
243         }
244
245         /**
246          * Installs the receiver on a text viewer.
247          * 
248          * @param viewer
249          */
250         public void install(ITextViewer viewer) {
251                 Assert.isLegal(viewer != null);
252
253                 fViewer = viewer;
254                 fSpecs = new HashMap();
255                 fRunDetector = new TypingRunDetector();
256                 fRunDetector.install(viewer);
257                 fRunListener = new ITypingRunListener() {
258
259                         /*
260                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
261                          */
262                         public void typingRunStarted(TypingRun run) {
263                         }
264
265                         /*
266                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
267                          */
268                         public void typingRunEnded(TypingRun run, ChangeType reason) {
269                                 if (reason == TypingRun.SELECTION)
270                                         fSpecs.clear();
271                                 else
272                                         prune();
273                         }
274                 };
275                 fRunDetector.addTypingRunListener(fRunListener);
276         }
277
278         private void prune() {
279                 for (Iterator it = fSpecs.values().iterator(); it.hasNext();) {
280                         UndoSpec spec = (UndoSpec) it.next();
281                         if (--spec.lives < 0)
282                                 it.remove();
283                 }
284         }
285
286         /**
287          * Uninstalls the receiver. No undo specifications may be registered on an
288          * uninstalled manager.
289          */
290         public void uninstall() {
291                 if (fViewer != null) {
292                         fRunDetector.removeTypingRunListener(fRunListener);
293                         fRunDetector.uninstall();
294                         fRunDetector = null;
295                         ensureListenerRemoved();
296                         fViewer = null;
297                 }
298         }
299 }