X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/TypingRunDetector.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/TypingRunDetector.java index c7c5536..d74a9c3 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/TypingRunDetector.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/TypingRunDetector.java @@ -32,7 +32,6 @@ import org.eclipse.swt.events.KeyListener; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; - /** * When connected to a text viewer, a TypingRunDetector observes * TypingRun events. A typing run is a sequence of similar text @@ -48,10 +47,10 @@ public class TypingRunDetector { * Implementation note: This class is independent of JDT and may be pulled * up to jface.text if needed. */ - + /** Debug flag. */ - private static final boolean DEBUG= false; - + private static final boolean DEBUG = false; + /** * Instances of this class abstract a text modification into a simple * description. Typing runs consists of a sequence of one or more modifying @@ -61,24 +60,28 @@ public class TypingRunDetector { */ private static final class Change { private ChangeType fType; + private int fNextOffset; - + /** * Creates a new change of type type. * - * @param type the ChangeType of the new change - * @param nextOffset the offset of the next change in a typing run + * @param type + * the ChangeType of the new change + * @param nextOffset + * the offset of the next change in a typing run */ public Change(ChangeType type, int nextOffset) { - fType= type; - fNextOffset= nextOffset; + fType = type; + fNextOffset = nextOffset; } - + /** * Returns true if the receiver can extend the typing * range the last change of which is described by change. * - * @param change the last change in a typing run + * @param change + * the last change in a typing run * @return true if the receiver is a valid extension to * change,false otherwise */ @@ -117,7 +120,7 @@ public class TypingRunDetector { public String toString() { return fType.toString() + "@" + fNextOffset; //$NON-NLS-1$ } - + /** * Returns the change type of this change. * @@ -127,7 +130,7 @@ public class TypingRunDetector { return fType; } } - + /** * Observes any events that modify the content of the document displayed in * the editor. Since text events may start a new run, this listener is @@ -142,14 +145,15 @@ public class TypingRunDetector { handleTextChanged(event); } } - + /** * Observes non-modifying events that will end a run, such as clicking into * the editor, moving the caret, and the editor losing focus. These events * can never start a run, therefore this listener is only registered if * there is an ongoing run. */ - private class SelectionListener implements MouseListener, KeyListener, FocusListener { + private class SelectionListener implements MouseListener, KeyListener, + FocusListener { /* * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent) @@ -163,22 +167,24 @@ public class TypingRunDetector { */ public void focusLost(FocusEvent e) { } - + /* * @see MouseListener#mouseDoubleClick */ public void mouseDoubleClick(MouseEvent e) { } - + /* - * If the right mouse button is pressed, the current editing command is closed + * If the right mouse button is pressed, the current editing command is + * closed + * * @see MouseListener#mouseDown */ public void mouseDown(MouseEvent e) { if (e.button == 1) handleSelectionChanged(); } - + /* * @see MouseListener#mouseUp */ @@ -190,67 +196,73 @@ public class TypingRunDetector { */ public void keyReleased(KeyEvent e) { } - + /* * On cursor keys, the current editing command is closed + * * @see KeyListener#keyPressed */ public void keyPressed(KeyEvent e) { switch (e.keyCode) { - case SWT.ARROW_UP: - case SWT.ARROW_DOWN: - case SWT.ARROW_LEFT: - case SWT.ARROW_RIGHT: - case SWT.END: - case SWT.HOME: - case SWT.PAGE_DOWN: - case SWT.PAGE_UP: - handleSelectionChanged(); - break; + case SWT.ARROW_UP: + case SWT.ARROW_DOWN: + case SWT.ARROW_LEFT: + case SWT.ARROW_RIGHT: + case SWT.END: + case SWT.HOME: + case SWT.PAGE_DOWN: + case SWT.PAGE_UP: + handleSelectionChanged(); + break; } } } - + /** The listeners. */ - private final Set fListeners= new HashSet(); + private final Set fListeners = new HashSet(); + /** * The viewer we work upon. Set to null in * uninstall. */ private ITextViewer fViewer; + /** The text event listener. */ - private final TextListener fTextListener= new TextListener(); - /** + private final TextListener fTextListener = new TextListener(); + + /** * The selection listener. Set to null when no run is active. */ private SelectionListener fSelectionListener; - + /* state variables */ - + /** The most recently observed change. Never null. */ private Change fLastChange; + /** The current run, or null if there is none. */ private TypingRun fRun; - + /** * Installs the receiver with a text viewer. * - * @param viewer the viewer to install on + * @param viewer + * the viewer to install on */ public void install(ITextViewer viewer) { Assert.isLegal(viewer != null); - fViewer= viewer; + fViewer = viewer; connect(); } - + /** * Initializes the state variables and registers any permanent listeners. */ private void connect() { if (fViewer != null) { - fLastChange= new Change(TypingRun.UNKNOWN, -1); - fRun= null; - fSelectionListener= null; + fLastChange = new Change(TypingRun.UNKNOWN, -1); + fRun = null; + fSelectionListener = null; fViewer.addTextListener(fTextListener); } } @@ -263,10 +275,10 @@ public class TypingRunDetector { if (fViewer != null) { fListeners.clear(); disconnect(); - fViewer= null; + fViewer = null; } } - + /** * Disconnects any registered listeners. */ @@ -277,10 +289,11 @@ public class TypingRunDetector { /** * Adds a listener for TypingRun events. Repeatedly adding - * the same listener instance has no effect. Listeners may be added even - * if the receiver is neither connected nor installed. + * the same listener instance has no effect. Listeners may be added even if + * the receiver is neither connected nor installed. * - * @param listener the listener add + * @param listener + * the listener add */ public void addTypingRunListener(ITypingRunListener listener) { Assert.isLegal(listener != null); @@ -288,46 +301,49 @@ public class TypingRunDetector { if (fListeners.size() == 1) connect(); } - + /** * Removes the listener from this manager. If listener is not * registered with the receiver, nothing happens. - * - * @param listener the listener to remove, or null + * + * @param listener + * the listener to remove, or null */ public void removeTypingRunListener(ITypingRunListener listener) { fListeners.remove(listener); if (fListeners.size() == 0) disconnect(); } - + /** * Handles an incoming text event. * - * @param event the text event that describes the text modification + * @param event + * the text event that describes the text modification */ void handleTextChanged(TextEvent event) { - Change type= computeChange(event); + Change type = computeChange(event); handleChange(type); } /** * Computes the change abstraction given a text event. * - * @param event the text event to analyze + * @param event + * the text event to analyze * @return a change object describing the event */ private Change computeChange(TextEvent event) { - DocumentEvent e= event.getDocumentEvent(); + DocumentEvent e = event.getDocumentEvent(); if (e == null) return new Change(TypingRun.NO_CHANGE, -1); - - int start= e.getOffset(); - int end= e.getOffset() + e.getLength(); - String newText= e.getText(); + + int start = e.getOffset(); + int end = e.getOffset() + e.getLength(); + String newText = e.getText(); if (newText == null) - newText= new String(); - + newText = new String(); + if (start == end) { // no replace / delete / overwrite if (newText.length() == 1) @@ -338,36 +354,37 @@ public class TypingRunDetector { if (newText.length() == 0) return new Change(TypingRun.DELETE, start); } - + return new Change(TypingRun.UNKNOWN, -1); } - + /** * Handles an incoming selection event. */ void handleSelectionChanged() { handleChange(new Change(TypingRun.SELECTION, -1)); } - + /** * State machine. Changes state given the current state and the incoming * change. * - * @param change the incoming change + * @param change + * the incoming change */ private void handleChange(Change change) { if (change.getType() == TypingRun.NO_CHANGE) return; - + if (DEBUG) System.err.println("Last change: " + fLastChange); //$NON-NLS-1$ if (!change.canFollow(fLastChange)) endIfStarted(change); - fLastChange= change; + fLastChange = change; if (change.isModification()) startOrContinue(); - + if (DEBUG) System.err.println("New change: " + change); //$NON-NLS-1$ } @@ -380,7 +397,7 @@ public class TypingRunDetector { if (!hasRun()) { if (DEBUG) System.err.println("+Start run"); //$NON-NLS-1$ - fRun= new TypingRun(fLastChange.getType()); + fRun = new TypingRun(fLastChange.getType()); ensureSelectionListenerAdded(); fireRunBegun(fRun); } @@ -401,7 +418,8 @@ public class TypingRunDetector { * Ends any active run and informs all listeners. If there is none, nothing * happens. * - * @param change the change that triggered ending the active run + * @param change + * the change that triggered ending the active run */ private void endIfStarted(Change change) { if (hasRun()) { @@ -409,7 +427,7 @@ public class TypingRunDetector { if (DEBUG) System.err.println("-End run"); //$NON-NLS-1$ fireRunEnded(fRun, change.getType()); - fRun= null; + fRun = null; } } @@ -419,8 +437,8 @@ public class TypingRunDetector { */ private void ensureSelectionListenerAdded() { if (fSelectionListener == null) { - fSelectionListener= new SelectionListener(); - StyledText textWidget= fViewer.getTextWidget(); + fSelectionListener = new SelectionListener(); + StyledText textWidget = fViewer.getTextWidget(); textWidget.addFocusListener(fSelectionListener); textWidget.addKeyListener(fSelectionListener); textWidget.addMouseListener(fSelectionListener); @@ -433,23 +451,24 @@ public class TypingRunDetector { */ private void ensureSelectionListenerRemoved() { if (fSelectionListener != null) { - StyledText textWidget= fViewer.getTextWidget(); + StyledText textWidget = fViewer.getTextWidget(); textWidget.removeFocusListener(fSelectionListener); textWidget.removeKeyListener(fSelectionListener); textWidget.removeMouseListener(fSelectionListener); - fSelectionListener= null; + fSelectionListener = null; } } /** * Informs all listeners about a newly started TypingRun. * - * @param run the new run + * @param run + * the new run */ private void fireRunBegun(TypingRun run) { - List listeners= new ArrayList(fListeners); - for (Iterator it= listeners.iterator(); it.hasNext();) { - ITypingRunListener listener= (ITypingRunListener) it.next(); + List listeners = new ArrayList(fListeners); + for (Iterator it = listeners.iterator(); it.hasNext();) { + ITypingRunListener listener = (ITypingRunListener) it.next(); listener.typingRunStarted(fRun); } } @@ -457,13 +476,15 @@ public class TypingRunDetector { /** * Informs all listeners about an ended TypingRun. * - * @param run the previously active run - * @param reason the type of change that caused the run to be ended + * @param run + * the previously active run + * @param reason + * the type of change that caused the run to be ended */ private void fireRunEnded(TypingRun run, ChangeType reason) { - List listeners= new ArrayList(fListeners); - for (Iterator it= listeners.iterator(); it.hasNext();) { - ITypingRunListener listener= (ITypingRunListener) it.next(); + List listeners = new ArrayList(fListeners); + for (Iterator it = listeners.iterator(); it.hasNext();) { + ITypingRunListener listener = (ITypingRunListener) it.next(); listener.typingRunEnded(fRun, reason); } }