edu.rice.cs.util.swing
Class FindReplaceMachine

java.lang.Object
  |
  +--edu.rice.cs.util.swing.FindReplaceMachine

public class FindReplaceMachine
extends Object

Implementation of logic of find/replace over a document.

Version:
$Id: FindReplaceMachine.java,v 1.6 2002/06/12 18:29:58 jhsia Exp $

Field Summary
private  Position _current
          The position in _doc which the machine is currently at.
private  Document _doc
          The document on which FindReplaceMachine is operating.
private  String _findWord
          The word being sought.
private  boolean _found
           
private  boolean _matchCase
           
private  String _replaceWord
          The word to replace the word being sought.
private  Position _start
          The position in _doc from which the searches started.
private  boolean _wrapped
           
 
Constructor Summary
FindReplaceMachine()
          NonModal version
 
Method Summary
 FindResult findNext()
          Finds the next occurrence of the find word and returns an offset at the end of that occurrence or -1 if the word was not found.
 int getCurrentOffset()
          Gets the character offset to which this machine is currently pointing.
 String getFindWord()
           
 String getReplaceWord()
           
 int getStartOffset()
          Gets the character offset at which this machine started operations.
 boolean isOnMatch()
          Determine if the machine is on an instance of the find word.
 void makeCurrentOffsetStart()
           
 int replaceAll()
          Replaces all occurences of the find word with the replace word.
 boolean replaceCurrent()
          If we're on a match for the find word, replace it with the replace word.
 void setDocument(Document doc)
           
 void setFindWord(String word)
          Change the word being sought.
 void setMatchCase(boolean matchCase)
           
 void setPosition(int pos)
           
 void setReplaceWord(String word)
          Change the replacing word.
 void setStart(int pos)
           
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

_doc

private Document _doc
The document on which FindReplaceMachine is operating.

_start

private Position _start
The position in _doc from which the searches started.

_current

private Position _current
The position in _doc which the machine is currently at.

_findWord

private String _findWord
The word being sought.

_replaceWord

private String _replaceWord
The word to replace the word being sought.

_found

private boolean _found

_wrapped

private boolean _wrapped

_matchCase

private boolean _matchCase
Constructor Detail

FindReplaceMachine

public FindReplaceMachine()
NonModal version
Method Detail

setMatchCase

public void setMatchCase(boolean matchCase)

setDocument

public void setDocument(Document doc)

setPosition

public void setPosition(int pos)

setStart

public void setStart(int pos)

getStartOffset

public int getStartOffset()
Gets the character offset at which this machine started operations.

getCurrentOffset

public int getCurrentOffset()
Gets the character offset to which this machine is currently pointing.

makeCurrentOffsetStart

public void makeCurrentOffsetStart()

getFindWord

public String getFindWord()

getReplaceWord

public String getReplaceWord()

setFindWord

public void setFindWord(String word)
Change the word being sought.
Parameters:
word - the new word to seek

setReplaceWord

public void setReplaceWord(String word)
Change the replacing word.
Parameters:
word - the new replacing word

isOnMatch

public boolean isOnMatch()
Determine if the machine is on an instance of the find word.
Returns:
true if the current position is right after an instance of the find word.

findNext

public FindResult findNext()
Finds the next occurrence of the find word and returns an offset at the end of that occurrence or -1 if the word was not found. Selectors should select backwards the length of the find word from the find offset. This position is stored in the current offset of the machine, and that is why it is after: in subsequent searches, the same instance won't be found twice. Also returns a flag indicating whether the end of the document was reached and wrapped around. This is done using the FindResult class which just contains an integer and a flag.
Returns:
a FindResult object containing foundOffset and aflag indicating wrapping to the beginning during a search

replaceCurrent

public boolean replaceCurrent()
If we're on a match for the find word, replace it with the replace word.

replaceAll

public int replaceAll()
Replaces all occurences of the find word with the replace word. Checks to see if the entire document is searched in case the find word is equivalent to the replace word in which case an infinite loop would otherwise occur.