bugfix 1412408
[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
35 /**
36  * Installs as a verify key listener on a viewer and overwrites the behaviour
37  * of the backspace key. Clients may register undo specifications for certain
38  * offsets in a document. The <code>SmartBackspaceManager</code> will manage the
39  * specfications and execute the contained <code>TextEdit</code>s when backspace
40  * is pressed at the given offset and the specification is still valid.
41  * <p>
42  * Undo specifications are removed after a number of typing runs.
43  * </p>
44  *  
45  * @since 3.0
46  */
47 public class SmartBackspaceManager {
48         /* independent of JDT - may be moved to jface.text */
49
50         /**
51          * An undo specification describes the change that should be executed if
52          * backspace is pressed at its trigger offset.
53          *  
54          * @since 3.0
55          */
56         public static final class UndoSpec {
57                 private final int triggerOffset;
58                 private final IRegion selection;
59                 private final TextEdit[] undoEdits;
60                 private final UndoSpec child;
61                 int lives;
62                 
63                 /**
64                  * Creates a new spec. A specification consists of a number of
65                  * <code>TextEdit</code> s that will be executed when backspace is
66                  * pressed at <code>triggerOffset</code>. The spec will be removed
67                  * when it is executed, or if more than <code>lives</code>
68                  * <code>TypingRun</code>s have ended after registering the spec.
69                  * <p>
70                  * Optionally, a child specification can be registered. After executing
71                  * the spec, the child spec will be registered with the manager. This allows
72                  * to create chains of <code>UndoSpec</code>s that will be executed upon
73                  * repeated pressing of backspace.
74                  * </p>
75                  * 
76                  * @param triggerOffset the offset where this spec is active
77                  * @param selection the selection after executing the undo spec
78                  * @param edits the <code>TextEdit</code> s to perform when executing
79                  *        the spec
80                  * @param lives the number of <code>TypingRun</code> s before removing
81                  *        the spec
82                  * @param child a child specification that will be registered after
83                  *        executing this spec, or <code>null</code>
84                  */
85                 public UndoSpec(int triggerOffset, IRegion selection, TextEdit[] edits, int lives, UndoSpec child) {
86                         Assert.isLegal(triggerOffset >= 0);
87                         Assert.isLegal(selection != null);
88                         Assert.isLegal(lives >= 0);
89                         Assert.isLegal(edits != null);
90                         Assert.isLegal(edits.length > 0);
91                         for (int i= 0; i < edits.length; i++) {
92                                 Assert.isLegal(edits[i] != null);
93                         }
94                         
95                         this.triggerOffset= triggerOffset;
96                         this.selection= selection;
97                         this.undoEdits= edits;
98                         this.lives= lives;
99                         this.child= child;
100                 }
101         }
102
103
104         private class BackspaceListener implements VerifyKeyListener {
105
106                 /*
107                  * @see org.eclipse.swt.custom.VerifyKeyListener#verifyKey(org.eclipse.swt.events.VerifyEvent)
108                  */
109                 public void verifyKey(VerifyEvent event) {
110                         if (fViewer != null && isBackspace(event)) {
111                                 int offset= getCaretOffset();
112                                 UndoSpec spec= removeEdit(offset);
113                                 if (spec != null) {
114                                         try {
115                                                 beginChange();
116                                                 for (int i= 0; i < spec.undoEdits.length; i++) {
117                                                         spec.undoEdits[i].apply(getDocument(), TextEdit.UPDATE_REGIONS);
118                                                 }
119                                                 fViewer.setSelectedRange(spec.selection.getOffset(), spec.selection.getLength());
120                                                 if (spec.child != null)
121                                                         register(spec.child);
122                                         } catch (MalformedTreeException e) {
123                                                 // fall back to standard bs
124                                                 return;
125                                         } catch (BadLocationException e) {
126                                                 // fall back to standard bs
127                                                 return;
128                                         } finally {
129                                                 endChange();
130                                         }
131                                         event.doit= false;
132                                 }
133                                 
134                         }
135                 }
136
137                 private void beginChange() {
138                         ITextViewer viewer= fViewer;
139                         if (viewer instanceof TextViewer) {
140                                 TextViewer v= (TextViewer) viewer;
141                                 v.getRewriteTarget().beginCompoundChange();
142                                 v.setRedraw(false);
143                         }
144                 }
145
146                 private void endChange() {
147                         ITextViewer viewer= fViewer;
148                         if (viewer instanceof TextViewer) {
149                                 TextViewer v= (TextViewer) viewer;
150                                 v.getRewriteTarget().endCompoundChange();
151                                 v.setRedraw(true);
152                         }
153                 }
154
155                 private boolean isBackspace(VerifyEvent event) {
156                         return event.doit == true && event.character == SWT.BS && event.stateMask == 0;
157                 }
158
159                 private int getCaretOffset() {
160                         ITextViewer viewer= fViewer;
161                         Point point= viewer.getSelectedRange();
162                         return point.x;
163                 }
164
165         }
166         
167         private ITextViewer fViewer;
168         private BackspaceListener fBackspaceListener;
169         private Map fSpecs;
170         private TypingRunDetector fRunDetector;
171         private ITypingRunListener fRunListener;
172
173         /**
174          * Registers an undo specification with this manager.
175          * 
176          * @param spec the specification to register
177          * @throws IllegalStateException if the manager is not installed
178          */
179         public void register(UndoSpec spec) {
180                 if (fViewer == null)
181                         throw new IllegalStateException();
182                 
183                 ensureListenerInstalled();
184                 addEdit(spec);
185         }
186
187         private void addEdit(UndoSpec spec) {
188                 Integer i= new Integer(spec.triggerOffset);
189                 fSpecs.put(i, spec);
190         }
191         
192         private UndoSpec removeEdit(int offset) {
193                 Integer i= new Integer(offset);
194                 UndoSpec spec= (UndoSpec) fSpecs.remove(i);
195                 return spec;
196         }
197
198         private void ensureListenerInstalled() {
199                 if (fBackspaceListener == null) {
200                         fBackspaceListener= new BackspaceListener();
201                         ITextViewer viewer= fViewer;
202                         if (viewer instanceof ITextViewerExtension)
203                                 ((ITextViewerExtension) viewer).prependVerifyKeyListener(fBackspaceListener);
204                         else
205                                 viewer.getTextWidget().addVerifyKeyListener(fBackspaceListener);
206                 }
207         }
208
209         private void ensureListenerRemoved() {
210                 if (fBackspaceListener != null) {
211                         ITextViewer viewer= fViewer;
212                         if (viewer instanceof ITextViewerExtension)
213                                 ((ITextViewerExtension) viewer).removeVerifyKeyListener(fBackspaceListener);
214                         else
215                                 viewer.getTextWidget().removeVerifyKeyListener(fBackspaceListener);
216                         fBackspaceListener= null;
217                 }
218         }
219
220         private IDocument getDocument() {
221                 return fViewer.getDocument();
222         }
223
224         /**
225          * Installs the receiver on a text viewer.
226          * 
227          * @param viewer
228          */
229         public void install(ITextViewer viewer) {
230                 Assert.isLegal(viewer != null);
231                 
232                 fViewer= viewer;
233                 fSpecs= new HashMap();
234                 fRunDetector= new TypingRunDetector();
235                 fRunDetector.install(viewer);
236                 fRunListener= new ITypingRunListener() {
237
238                         /*
239                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunStarted(org.eclipse.jface.text.TypingRunDetector.TypingRun)
240                          */
241                         public void typingRunStarted(TypingRun run) {
242                         }
243
244                         /*
245                          * @see org.eclipse.jface.text.TypingRunDetector.ITypingRunListener#typingRunEnded(org.eclipse.jface.text.TypingRunDetector.TypingRun)
246                          */
247                         public void typingRunEnded(TypingRun run, ChangeType reason) {
248                                 if (reason == TypingRun.SELECTION)
249                                         fSpecs.clear();
250                                 else
251                                         prune();
252                         }
253                 };
254                 fRunDetector.addTypingRunListener(fRunListener);
255         }
256         
257         private void prune() {
258                 for (Iterator it= fSpecs.values().iterator(); it.hasNext();) {
259                         UndoSpec spec= (UndoSpec) it.next();
260                         if (--spec.lives < 0)
261                                 it.remove();
262                 }
263         }
264         
265         /**
266          * Uninstalls the receiver. No undo specifications may be registered on an
267          * uninstalled manager.
268          */
269         public void uninstall() {
270                 if (fViewer != null) {
271                         fRunDetector.removeTypingRunListener(fRunListener);
272                         fRunDetector.uninstall();
273                         fRunDetector= null;
274                         ensureListenerRemoved();
275                         fViewer= null;
276                 }
277         }
278 }