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