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.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.StyledText;
21 import org.eclipse.swt.events.FocusEvent;
22 import org.eclipse.swt.events.FocusListener;
23 import org.eclipse.swt.events.KeyEvent;
24 import org.eclipse.swt.events.KeyListener;
25 import org.eclipse.swt.events.MouseEvent;
26 import org.eclipse.swt.events.MouseListener;
28 import org.eclipse.jface.text.Assert;
29 import org.eclipse.jface.text.DocumentEvent;
30 import org.eclipse.jface.text.ITextListener;
31 import org.eclipse.jface.text.ITextViewer;
32 import org.eclipse.jface.text.TextEvent;
34 import net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
38 * When connected to a text viewer, a <code>TypingRunDetector</code> observes
39 * <code>TypingRun</code> events. A typing run is a sequence of similar text
40 * modifications, such as inserting or deleting single characters.
42 * Listeners are informed about the start and end of a <code>TypingRun</code>.
47 public class TypingRunDetector {
49 * Implementation note: This class is independent of JDT and may be pulled
50 * up to jface.text if needed.
54 private static final boolean DEBUG= false;
57 * Instances of this class abstract a text modification into a simple
58 * description. Typing runs consists of a sequence of one or more modifying
59 * changes of the same type. Every change records the type of change
60 * described by a text modification, and an offset it can be followed by
61 * another change of the same run.
63 private static final class Change {
64 private ChangeType fType;
65 private int fNextOffset;
68 * Creates a new change of type <code>type</code>.
70 * @param type the <code>ChangeType</code> of the new change
71 * @param nextOffset the offset of the next change in a typing run
73 public Change(ChangeType type, int nextOffset) {
75 fNextOffset= nextOffset;
79 * Returns <code>true</code> if the receiver can extend the typing
80 * range the last change of which is described by <code>change</code>.
82 * @param change the last change in a typing run
83 * @return <code>true</code> if the receiver is a valid extension to
84 * <code>change</code>,<code>false</code> otherwise
86 public boolean canFollow(Change change) {
87 if (fType == TypingRun.NO_CHANGE)
89 else if (fType.equals(TypingRun.UNKNOWN))
91 if (fType.equals(change.fType)) {
92 if (fType == TypingRun.DELETE)
93 return fNextOffset == change.fNextOffset - 1;
94 else if (fType == TypingRun.INSERT)
95 return fNextOffset == change.fNextOffset + 1;
96 else if (fType == TypingRun.OVERTYPE)
97 return fNextOffset == change.fNextOffset + 1;
98 else if (fType == TypingRun.SELECTION)
105 * Returns <code>true</code> if the receiver describes a text
106 * modification, <code>false</code> if it describes a focus /
109 * @return <code>true</code> if the receiver is a text modification
111 public boolean isModification() {
112 return fType.isModification();
116 * @see java.lang.Object#toString()
118 public String toString() {
119 return fType.toString() + "@" + fNextOffset; //$NON-NLS-1$
123 * Returns the change type of this change.
125 * @return the change type of this change
127 public ChangeType getType() {
133 * Observes any events that modify the content of the document displayed in
134 * the editor. Since text events may start a new run, this listener is
135 * always registered if the detector is connected.
137 private class TextListener implements ITextListener {
140 * @see org.eclipse.jface.text.ITextListener#textChanged(org.eclipse.jface.text.TextEvent)
142 public void textChanged(TextEvent event) {
143 handleTextChanged(event);
148 * Observes non-modifying events that will end a run, such as clicking into
149 * the editor, moving the caret, and the editor losing focus. These events
150 * can never start a run, therefore this listener is only registered if
151 * there is an ongoing run.
153 private class SelectionListener implements MouseListener, KeyListener, FocusListener {
156 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
158 public void focusGained(FocusEvent e) {
159 handleSelectionChanged();
163 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
165 public void focusLost(FocusEvent e) {
169 * @see MouseListener#mouseDoubleClick
171 public void mouseDoubleClick(MouseEvent e) {
175 * If the right mouse button is pressed, the current editing command is closed
176 * @see MouseListener#mouseDown
178 public void mouseDown(MouseEvent e) {
180 handleSelectionChanged();
184 * @see MouseListener#mouseUp
186 public void mouseUp(MouseEvent e) {
190 * @see KeyListener#keyPressed
192 public void keyReleased(KeyEvent e) {
196 * On cursor keys, the current editing command is closed
197 * @see KeyListener#keyPressed
199 public void keyPressed(KeyEvent e) {
204 case SWT.ARROW_RIGHT:
209 handleSelectionChanged();
215 /** The listeners. */
216 private final Set fListeners= new HashSet();
218 * The viewer we work upon. Set to <code>null</code> in
219 * <code>uninstall</code>.
221 private ITextViewer fViewer;
222 /** The text event listener. */
223 private final TextListener fTextListener= new TextListener();
225 * The selection listener. Set to <code>null</code> when no run is active.
227 private SelectionListener fSelectionListener;
229 /* state variables */
231 /** The most recently observed change. Never <code>null</code>. */
232 private Change fLastChange;
233 /** The current run, or <code>null</code> if there is none. */
234 private TypingRun fRun;
237 * Installs the receiver with a text viewer.
239 * @param viewer the viewer to install on
241 public void install(ITextViewer viewer) {
242 Assert.isLegal(viewer != null);
248 * Initializes the state variables and registers any permanent listeners.
250 private void connect() {
251 if (fViewer != null) {
252 fLastChange= new Change(TypingRun.UNKNOWN, -1);
254 fSelectionListener= null;
255 fViewer.addTextListener(fTextListener);
260 * Uninstalls the receiver and removes all listeners. <code>install()</code>
261 * must be called for events to be generated.
263 public void uninstall() {
264 if (fViewer != null) {
272 * Disconnects any registered listeners.
274 private void disconnect() {
275 fViewer.removeTextListener(fTextListener);
276 ensureSelectionListenerRemoved();
280 * Adds a listener for <code>TypingRun</code> events. Repeatedly adding
281 * the same listener instance has no effect. Listeners may be added even
282 * if the receiver is neither connected nor installed.
284 * @param listener the listener add
286 public void addTypingRunListener(ITypingRunListener listener) {
287 Assert.isLegal(listener != null);
288 fListeners.add(listener);
289 if (fListeners.size() == 1)
294 * Removes the listener from this manager. If <code>listener</code> is not
295 * registered with the receiver, nothing happens.
297 * @param listener the listener to remove, or <code>null</code>
299 public void removeTypingRunListener(ITypingRunListener listener) {
300 fListeners.remove(listener);
301 if (fListeners.size() == 0)
306 * Handles an incoming text event.
308 * @param event the text event that describes the text modification
310 void handleTextChanged(TextEvent event) {
311 Change type= computeChange(event);
316 * Computes the change abstraction given a text event.
318 * @param event the text event to analyze
319 * @return a change object describing the event
321 private Change computeChange(TextEvent event) {
322 DocumentEvent e= event.getDocumentEvent();
324 return new Change(TypingRun.NO_CHANGE, -1);
326 int start= e.getOffset();
327 int end= e.getOffset() + e.getLength();
328 String newText= e.getText();
330 newText= new String();
333 // no replace / delete / overwrite
334 if (newText.length() == 1)
335 return new Change(TypingRun.INSERT, end + 1);
336 } else if (start == end - 1) {
337 if (newText.length() == 1)
338 return new Change(TypingRun.OVERTYPE, end);
339 if (newText.length() == 0)
340 return new Change(TypingRun.DELETE, start);
343 return new Change(TypingRun.UNKNOWN, -1);
347 * Handles an incoming selection event.
349 void handleSelectionChanged() {
350 handleChange(new Change(TypingRun.SELECTION, -1));
354 * State machine. Changes state given the current state and the incoming
357 * @param change the incoming change
359 private void handleChange(Change change) {
360 if (change.getType() == TypingRun.NO_CHANGE)
364 System.err.println("Last change: " + fLastChange); //$NON-NLS-1$
366 if (!change.canFollow(fLastChange))
367 endIfStarted(change);
369 if (change.isModification())
373 System.err.println("New change: " + change); //$NON-NLS-1$
377 * Starts a new run if there is none and informs all listeners. If there
378 * already is a run, nothing happens.
380 private void startOrContinue() {
383 System.err.println("+Start run"); //$NON-NLS-1$
384 fRun= new TypingRun(fLastChange.getType());
385 ensureSelectionListenerAdded();
391 * Returns <code>true</code> if there is an active run, <code>false</code>
394 * @return <code>true</code> if there is an active run, <code>false</code>
397 private boolean hasRun() {
402 * Ends any active run and informs all listeners. If there is none, nothing
405 * @param change the change that triggered ending the active run
407 private void endIfStarted(Change change) {
409 ensureSelectionListenerRemoved();
411 System.err.println("-End run"); //$NON-NLS-1$
412 fireRunEnded(fRun, change.getType());
418 * Adds the selection listener to the text widget underlying the viewer, if
421 private void ensureSelectionListenerAdded() {
422 if (fSelectionListener == null) {
423 fSelectionListener= new SelectionListener();
424 StyledText textWidget= fViewer.getTextWidget();
425 textWidget.addFocusListener(fSelectionListener);
426 textWidget.addKeyListener(fSelectionListener);
427 textWidget.addMouseListener(fSelectionListener);
432 * If there is a selection listener, it is removed from the text widget
433 * underlying the viewer.
435 private void ensureSelectionListenerRemoved() {
436 if (fSelectionListener != null) {
437 StyledText textWidget= fViewer.getTextWidget();
438 textWidget.removeFocusListener(fSelectionListener);
439 textWidget.removeKeyListener(fSelectionListener);
440 textWidget.removeMouseListener(fSelectionListener);
441 fSelectionListener= null;
446 * Informs all listeners about a newly started <code>TypingRun</code>.
448 * @param run the new run
450 private void fireRunBegun(TypingRun run) {
451 List listeners= new ArrayList(fListeners);
452 for (Iterator it= listeners.iterator(); it.hasNext();) {
453 ITypingRunListener listener= (ITypingRunListener) it.next();
454 listener.typingRunStarted(fRun);
459 * Informs all listeners about an ended <code>TypingRun</code>.
461 * @param run the previously active run
462 * @param reason the type of change that caused the run to be ended
464 private void fireRunEnded(TypingRun run, ChangeType reason) {
465 List listeners= new ArrayList(fListeners);
466 for (Iterator it= listeners.iterator(); it.hasNext();) {
467 ITypingRunListener listener= (ITypingRunListener) it.next();
468 listener.typingRunEnded(fRun, reason);