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 net.sourceforge.phpdt.internal.ui.text.TypingRun.ChangeType;
22 //import org.eclipse.jface.text.Assert;
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.jface.text.DocumentEvent;
25 import org.eclipse.jface.text.ITextListener;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.TextEvent;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.custom.StyledText;
30 import org.eclipse.swt.events.FocusEvent;
31 import org.eclipse.swt.events.FocusListener;
32 import org.eclipse.swt.events.KeyEvent;
33 import org.eclipse.swt.events.KeyListener;
34 import org.eclipse.swt.events.MouseEvent;
35 import org.eclipse.swt.events.MouseListener;
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;
66 private int fNextOffset;
69 * Creates a new change of type <code>type</code>.
72 * the <code>ChangeType</code> of the new change
74 * the offset of the next change in a typing run
76 public Change(ChangeType type, int nextOffset) {
78 fNextOffset = nextOffset;
82 * Returns <code>true</code> if the receiver can extend the typing
83 * range the last change of which is described by <code>change</code>.
86 * the last change in a typing run
87 * @return <code>true</code> if the receiver is a valid extension to
88 * <code>change</code>,<code>false</code> otherwise
90 public boolean canFollow(Change change) {
91 if (fType == TypingRun.NO_CHANGE)
93 else if (fType.equals(TypingRun.UNKNOWN))
95 if (fType.equals(change.fType)) {
96 if (fType == TypingRun.DELETE)
97 return fNextOffset == change.fNextOffset - 1;
98 else if (fType == TypingRun.INSERT)
99 return fNextOffset == change.fNextOffset + 1;
100 else if (fType == TypingRun.OVERTYPE)
101 return fNextOffset == change.fNextOffset + 1;
102 else if (fType == TypingRun.SELECTION)
109 * Returns <code>true</code> if the receiver describes a text
110 * modification, <code>false</code> if it describes a focus /
113 * @return <code>true</code> if the receiver is a text modification
115 public boolean isModification() {
116 return fType.isModification();
120 * @see java.lang.Object#toString()
122 public String toString() {
123 return fType.toString() + "@" + fNextOffset; //$NON-NLS-1$
127 * Returns the change type of this change.
129 * @return the change type of this change
131 public ChangeType getType() {
137 * Observes any events that modify the content of the document displayed in
138 * the editor. Since text events may start a new run, this listener is
139 * always registered if the detector is connected.
141 private class TextListener implements ITextListener {
144 * @see org.eclipse.jface.text.ITextListener#textChanged(org.eclipse.jface.text.TextEvent)
146 public void textChanged(TextEvent event) {
147 handleTextChanged(event);
152 * Observes non-modifying events that will end a run, such as clicking into
153 * the editor, moving the caret, and the editor losing focus. These events
154 * can never start a run, therefore this listener is only registered if
155 * there is an ongoing run.
157 private class SelectionListener implements MouseListener, KeyListener,
161 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
163 public void focusGained(FocusEvent e) {
164 handleSelectionChanged();
168 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
170 public void focusLost(FocusEvent e) {
174 * @see MouseListener#mouseDoubleClick
176 public void mouseDoubleClick(MouseEvent e) {
180 * If the right mouse button is pressed, the current editing command is
183 * @see MouseListener#mouseDown
185 public void mouseDown(MouseEvent e) {
187 handleSelectionChanged();
191 * @see MouseListener#mouseUp
193 public void mouseUp(MouseEvent e) {
197 * @see KeyListener#keyPressed
199 public void keyReleased(KeyEvent e) {
203 * On cursor keys, the current editing command is closed
205 * @see KeyListener#keyPressed
207 public void keyPressed(KeyEvent e) {
212 case SWT.ARROW_RIGHT:
217 handleSelectionChanged();
223 /** The listeners. */
224 private final Set fListeners = new HashSet();
227 * The viewer we work upon. Set to <code>null</code> in
228 * <code>uninstall</code>.
230 private ITextViewer fViewer;
232 /** The text event listener. */
233 private final TextListener fTextListener = new TextListener();
236 * The selection listener. Set to <code>null</code> when no run is active.
238 private SelectionListener fSelectionListener;
240 /* state variables */
242 /** The most recently observed change. Never <code>null</code>. */
243 private Change fLastChange;
245 /** The current run, or <code>null</code> if there is none. */
246 private TypingRun fRun;
249 * Installs the receiver with a text viewer.
252 * the viewer to install on
254 public void install(ITextViewer viewer) {
255 Assert.isLegal(viewer != null);
261 * Initializes the state variables and registers any permanent listeners.
263 private void connect() {
264 if (fViewer != null) {
265 fLastChange = new Change(TypingRun.UNKNOWN, -1);
267 fSelectionListener = null;
268 fViewer.addTextListener(fTextListener);
273 * Uninstalls the receiver and removes all listeners. <code>install()</code>
274 * must be called for events to be generated.
276 public void uninstall() {
277 if (fViewer != null) {
285 * Disconnects any registered listeners.
287 private void disconnect() {
288 fViewer.removeTextListener(fTextListener);
289 ensureSelectionListenerRemoved();
293 * Adds a listener for <code>TypingRun</code> events. Repeatedly adding
294 * the same listener instance has no effect. Listeners may be added even if
295 * the receiver is neither connected nor installed.
300 public void addTypingRunListener(ITypingRunListener listener) {
301 Assert.isLegal(listener != null);
302 fListeners.add(listener);
303 if (fListeners.size() == 1)
308 * Removes the listener from this manager. If <code>listener</code> is not
309 * registered with the receiver, nothing happens.
312 * the listener to remove, or <code>null</code>
314 public void removeTypingRunListener(ITypingRunListener listener) {
315 fListeners.remove(listener);
316 if (fListeners.size() == 0)
321 * Handles an incoming text event.
324 * the text event that describes the text modification
326 void handleTextChanged(TextEvent event) {
327 Change type = computeChange(event);
332 * Computes the change abstraction given a text event.
335 * the text event to analyze
336 * @return a change object describing the event
338 private Change computeChange(TextEvent event) {
339 DocumentEvent e = event.getDocumentEvent();
341 return new Change(TypingRun.NO_CHANGE, -1);
343 int start = e.getOffset();
344 int end = e.getOffset() + e.getLength();
345 String newText = e.getText();
347 newText = new String();
350 // no replace / delete / overwrite
351 if (newText.length() == 1)
352 return new Change(TypingRun.INSERT, end + 1);
353 } else if (start == end - 1) {
354 if (newText.length() == 1)
355 return new Change(TypingRun.OVERTYPE, end);
356 if (newText.length() == 0)
357 return new Change(TypingRun.DELETE, start);
360 return new Change(TypingRun.UNKNOWN, -1);
364 * Handles an incoming selection event.
366 void handleSelectionChanged() {
367 handleChange(new Change(TypingRun.SELECTION, -1));
371 * State machine. Changes state given the current state and the incoming
375 * the incoming change
377 private void handleChange(Change change) {
378 if (change.getType() == TypingRun.NO_CHANGE)
382 System.err.println("Last change: " + fLastChange); //$NON-NLS-1$
384 if (!change.canFollow(fLastChange))
385 endIfStarted(change);
386 fLastChange = change;
387 if (change.isModification())
391 System.err.println("New change: " + change); //$NON-NLS-1$
395 * Starts a new run if there is none and informs all listeners. If there
396 * already is a run, nothing happens.
398 private void startOrContinue() {
401 System.err.println("+Start run"); //$NON-NLS-1$
402 fRun = new TypingRun(fLastChange.getType());
403 ensureSelectionListenerAdded();
409 * Returns <code>true</code> if there is an active run, <code>false</code>
412 * @return <code>true</code> if there is an active run, <code>false</code>
415 private boolean hasRun() {
420 * Ends any active run and informs all listeners. If there is none, nothing
424 * the change that triggered ending the active run
426 private void endIfStarted(Change change) {
428 ensureSelectionListenerRemoved();
430 System.err.println("-End run"); //$NON-NLS-1$
431 fireRunEnded(fRun, change.getType());
437 * Adds the selection listener to the text widget underlying the viewer, if
440 private void ensureSelectionListenerAdded() {
441 if (fSelectionListener == null) {
442 fSelectionListener = new SelectionListener();
443 StyledText textWidget = fViewer.getTextWidget();
444 textWidget.addFocusListener(fSelectionListener);
445 textWidget.addKeyListener(fSelectionListener);
446 textWidget.addMouseListener(fSelectionListener);
451 * If there is a selection listener, it is removed from the text widget
452 * underlying the viewer.
454 private void ensureSelectionListenerRemoved() {
455 if (fSelectionListener != null) {
456 StyledText textWidget = fViewer.getTextWidget();
457 textWidget.removeFocusListener(fSelectionListener);
458 textWidget.removeKeyListener(fSelectionListener);
459 textWidget.removeMouseListener(fSelectionListener);
460 fSelectionListener = null;
465 * Informs all listeners about a newly started <code>TypingRun</code>.
470 private void fireRunBegun(TypingRun run) {
471 List listeners = new ArrayList(fListeners);
472 for (Iterator it = listeners.iterator(); it.hasNext();) {
473 ITypingRunListener listener = (ITypingRunListener) it.next();
474 listener.typingRunStarted(fRun);
479 * Informs all listeners about an ended <code>TypingRun</code>.
482 * the previously active run
484 * the type of change that caused the run to be ended
486 private void fireRunEnded(TypingRun run, ChangeType reason) {
487 List listeners = new ArrayList(fListeners);
488 for (Iterator it = listeners.iterator(); it.hasNext();) {
489 ITypingRunListener listener = (ITypingRunListener) it.next();
490 listener.typingRunEnded(fRun, reason);