synchronized from quantum plugin
[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 org.eclipse.text.edits.MalformedTreeException;
18 import org.eclipse.text.edits.TextEdit;
19
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;
24
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;
32
33 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
34
35
36
37 /**
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.
43  * <p>
44  * Undo specifications are removed after a number of typing runs.
45  * </p>
46  *  
47  * @since 3.0
48  */
49 public class SmartBackspaceManager {
50         /* independent of JDT - may be moved to jface.text */
51
52         /**
53          * An undo specification describes the change that should be executed if
54          * backspace is pressed at its trigger offset.
55          *  
56          * @since 3.0
57          */
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;
63                 int lives;
64                 
65                 /**
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.
71                  * <p>
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.
76                  * </p>
77                  * 
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
81                  *        the spec
82                  * @param lives the number of <code>TypingRun</code> s before removing
83                  *        the spec
84                  * @param child a child specification that will be registered after
85                  *        executing this spec, or <code>null</code>
86                  */
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);
95                         }
96                         
97                         this.triggerOffset= triggerOffset;
98                         this.selection= selection;
99                         this.undoEdits= edits;
100                         this.lives= lives;
101                         this.child= child;
102                 }
103         }
104
105
106         private class BackspaceListener implements VerifyKeyListener {
107
108                 /*
109                  * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
110                  */
111                 public void verifyKey(VerifyEvent event) {
112                         if (fViewer != null && isBackspace(event)) {
113                                 int offset= getCaretOffset();
114                                 UndoSpec spec= removeEdit(offset);
115                                 if (spec != null) {
116                                         try {
117                                                 beginChange();
118                                                 for (int i= 0; i < spec.undoEdits.length; i++) {
119                                                         spec.undoEdits[i].apply(getDocument(), TextEdit.UPDATE_REGIONS);
120                                                 }
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
126                                                 return;
127                                         } catch (BadLocationException e) {
128                                                 // fall back to standard bs
129                                                 return;
130                                         } finally {
131                                                 endChange();
132                                         }
133                                         event.doit= false;
134                                 }
135                                 
136                         }
137                 }
138
139                 private void beginChange() {
140                         ITextViewer viewer= fViewer;
141                         if (viewer instanceof TextViewer) {
142                                 TextViewer v= (TextViewer) viewer;
143                                 v.getRewriteTarget().beginCompoundChange();
144                                 v.setRedraw(false);
145                         }
146                 }
147
148                 private void endChange() {
149                         ITextViewer viewer= fViewer;
150                         if (viewer instanceof TextViewer) {
151                                 TextViewer v= (TextViewer) viewer;
152                                 v.getRewriteTarget().endCompoundChange();
153                                 v.setRedraw(true);
154                         }
155                 }
156
157                 private boolean isBackspace(VerifyEvent event) {
158                         return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
159                 }
160
161                 private int getCaretOffset() {
162                         ITextViewer viewer= fViewer;
163                         Point point= viewer.getSelectedRange();
164                         return point.x;
165                 }
166
167         }
168         
169         private ITextViewer fViewer;
170         private BackspaceListener fBackspaceListener;
171         private Map fSpecs;
172         private TypingRunDetector fRunDetector;
173         private ITypingRunListener fRunListener;
174
175         /**
176          * Registers an undo specification with this manager.
177          * 
178          * @param spec the specification to register
179          * @throws IllegalStateException if the manager is not installed
180          */
181         public void register(UndoSpec spec) {
182                 if (fViewer == null)
183                         throw new IllegalStateException();
184                 
185                 ensureListenerInstalled();
186                 addEdit(spec);
187         }
188
189         private void addEdit(UndoSpec spec) {
190                 Integer i= new Integer(spec.triggerOffset);
191                 fSpecs.put(i, spec);
192         }
193         
194         private UndoSpec removeEdit(int offset) {
195                 Integer i= new Integer(offset);
196                 UndoSpec spec= (UndoSpec) fSpecs.remove(i);
197                 return spec;
198         }
199
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);
206                         else
207                                 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
208                 }
209         }
210
211         private void ensureListenerRemoved() {
212                 if (fBackspaceListener != null) {
213                         ITextViewer viewer= fViewer;
214                         if (viewer instanceof ITextViewerExtension)
215                                 ((ITextViewerExtension) viewer).removeVerifyKeyListener(fBackspaceListener);
216                         else
217                                 viewer.getTextWidget().removeVerifyKeyListener(fBackspaceListener);
218                         fBackspaceListener= null;
219                 }
220         }
221
222         private IDocument getDocument() {
223                 return fViewer.getDocument();
224         }
225
226         /**
227          * Installs the receiver on a text viewer.
228          * 
229          * @param viewer
230          */
231         public void install(ITextViewer viewer) {
232                 Assert.isLegal(viewer != null);
233                 
234                 fViewer= viewer;
235                 fSpecs= new HashMap();
236                 fRunDetector= new TypingRunDetector();
237                 fRunDetector.install(viewer);
238                 fRunListener= new ITypingRunListener() {
239
240                         /*
241                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
242                          */
243                         public void typingRunStarted(TypingRun run) {
244                         }
245
246                         /*
247                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
248                          */
249                         public void typingRunEnded(TypingRun run, ChangeType reason) {
250                                 if (reason == TypingRun.SELECTION)
251                                         fSpecs.clear();
252                                 else
253                                         prune();
254                         }
255                 };
256                 fRunDetector.addTypingRunListener(fRunListener);
257         }
258         
259         private void prune() {
260                 for (Iterator it= fSpecs.values().iterator(); it.hasNext();) {
261                         UndoSpec spec= (UndoSpec) it.next();
262                         if (--spec.lives < 0)
263                                 it.remove();
264                 }
265         }
266         
267         /**
268          * Uninstalls the receiver. No undo specifications may be registered on an
269          * uninstalled manager.
270          */
271         public void uninstall() {
272                 if (fViewer != null) {
273                         fRunDetector.removeTypingRunListener(fRunListener);
274                         fRunDetector.uninstall();
275                         fRunDetector= null;
276                         ensureListenerRemoved();
277                         fViewer= null;
278                 }
279         }
280 }