JIDE 3.5.15

com.jidesoft.search
Class FindAndReplace

java.lang.Object
  extended by com.jidesoft.search.FindAndReplace

public class FindAndReplace
extends Object

FindAndReplace is the class that does the find and replace feature in CodeEditor, or as a matter of fact, anything that has a piece or several pieces of text.

This class also persists the settings for FindAndReplaceDialog. Each time CodeEditor pops up a FindAndReplaceDialog, CodeEditor will retrieve previous settings stored in this class to configure the dialog well. If you want to persist those settings between sessions, please invoke those methods, like setForward(boolean)/isForward(), setUseRegex(boolean)/isUseRegex(), to load/save those settings. Those settings include:

 setUseRegex/isUseRegex
 setFindHistory/getFindHistory
 setFindText/getFindText
 setReplaceText/getReplaceText
 setReplaceHistory/getReplaceHistory
 setUseWildcards/isUseWildcards
 setUseRegexOrWildcards/isUseRegexOrWildcards
 setMatchCase/isMatchCase
 setMatchWholeWord/isMatchWholeWord
 setOrigin/getOrigin
 setReversed/isReversed
 setShowFindAll/isShowFindAll
 


Field Summary
protected  Pattern _pattern
           
static FindResult EMPTY_RESULT
          Empty result means that there is no matching any more to terminate the searching in current file.
static int ORIGIN_ENTIRE
           
static int ORIGIN_FROM_CURSOR
           
static int SCOPE_GLOBAL
           
static int SCOPE_SELECTION
           
 
Constructor Summary
FindAndReplace()
          The constructor.
 
Method Summary
 void addFindAndReplaceListener(FindAndReplaceListener FindAndReplaceListener)
          Adds a listener.
 void addTarget(FindAndReplaceTarget target)
          Adds FindAndReplaceTarget in use.
protected  FindResult findNext(Matcher m, int startPosition)
          Finds the next matching result from the start position with the matcher.
protected  void fireFindAndReplaceEvent(int status)
          Fire FindAndReplaceEvents.
protected  void fireFindAndReplaceEvent(int status, FindResult findResult)
          Fire FindAndReplaceEvents.
protected  void fireFindAndReplaceEvent(int status, FindResults findResults)
          Fire FindAndReplaceEvents.
protected  void fireFindAndReplaceEvent(int status, FindResult findResult, String replaceString)
          Fire FindAndReplaceEvents.
protected  void fireFindAndReplaceEvent(int status, String fileName)
          Fire FindAndReplaceEvents.
protected  void fireFindAndReplaceEvent(int status, String fileName, FindResults findResults)
          Fire FindAndReplaceEvents.
 FindAndReplaceListener[] getFindAndReplaceListeners()
          Gets the FindAndReplaceListeners register on this class.
 Vector<String> getFindHistory()
          Gets the find history.
 String getFindText()
          Gets the find text.
protected  int getFlags()
          Gets searching flags.
protected  Matcher getMatcher()
          Gets the matcher prepared for searching.
 int getOrigin()
          Gets the origin.
 Vector<String> getReplaceHistory()
          Gets the replace history.
 String getReplaceText()
          Gets the replace text.
 int getScope()
          Not implemented yet.
 FindAndReplaceTarget getTarget()
          Gets current target in use.
 FindAndReplaceTarget[] getTargets()
          Gets all possible FindAndReplaceTarget instances.
protected  void initialCharSequence()
          Initialize the char sequence prepared for searching.
 boolean isForward()
          Gets the flag indicating if the search is the same direction with isReversed().
 boolean isMatchCase()
          Gets the flag indicating if the search is case sensitive.
 boolean isMatchWholeWord()
          Gets the flag indicating if only matching whole word.
 boolean isReplace()
          Gets the flag indicating if the replace option is currently visible in the dialog.
 boolean isReversed()
          Gets the flag indicating if the search is forward or backward.
 boolean isShowFindAll()
          Gets the flag indicating if the "Find All" button should be visible.
 boolean isTargetChanged()
          Gets the flag indicating if the target in use is just changed.
 boolean isUseRegex()
          Sets the flag indicating if regex is to be used.
 boolean isUseRegexOrWildcards()
          Gets the flag indicating Regex or wildcards is in use.
 boolean isUseWildcards()
          Gets the flag indicating if wildcards are to be used.
protected  int promptForReplace()
          Prompt a dialog for customer to choose if he/she needs replace the match or not.
 void removeFindAndReplaceListener(FindAndReplaceListener FindAndReplaceListener)
          Removes a listener.
 void removeTarget(FindAndReplaceTarget target)
          Removes FindAndReplaceTarget in use.
 int replace(FindResult findResult, String replacement)
          Process to replace the find result with the replacement
 void search()
          Process searching.
 void search(Matcher m)
          Process to search in the document.
 void searchAgain()
          Process searching again.
 FindResults searchAll()
          Process searching all.
protected  void searchFinished()
          Process when search is finished.
 void setFindHistory(Vector<String> findHistory)
          Sets the find history.
 void setFindText(String findText)
          Sets the find text.
 void setForward(boolean forward)
          Sets the flag indicating if the search is the same direction with isReversed().
 void setMatchCase(boolean matchCase)
          Sets the flag indicating if the search is case sensitive.
 void setMatchWholeWord(boolean matchWholeWord)
          Sets the flag indicating if only matching whole word.
 void setOrigin(int origin)
          Sets the origin.
 void setReplace(boolean replace)
          Sets the flag indicating if the replace option is currently visible in the dialog.
 void setReplaceHistory(Vector<String> replaceHistory)
          Sets the replace history.
 void setReplaceText(String replaceText)
          Sets the replace text.
 void setReversed(boolean reversed)
          Sets the flag indicating if the search is forward or backward.
 void setScope(int scope)
          Not implemented yet.
 void setShowFindAll(boolean showFindAll)
          Sets the flag indicating if the "Find All" button should be visible.
 void setTarget(FindAndReplaceTarget target)
          Sets FindAndReplaceTarget in use.
 void setUseRegex(boolean useRegex)
          Gets the flag indicating if regex is to be used.
 void setUseRegexOrWildcards(boolean useRegexOrWildcards)
          Sets the flag indicating Regex or wildcards is in use.
 void setUseWildcards(boolean useWildcards)
          Sets the flag indicating if wildcards are to be used.
 void textChanged()
          Process when the search text is changed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCOPE_GLOBAL

public static final int SCOPE_GLOBAL
See Also:
Constant Field Values

SCOPE_SELECTION

public static final int SCOPE_SELECTION
See Also:
Constant Field Values

ORIGIN_ENTIRE

public static final int ORIGIN_ENTIRE
See Also:
Constant Field Values

ORIGIN_FROM_CURSOR

public static final int ORIGIN_FROM_CURSOR
See Also:
Constant Field Values

_pattern

protected Pattern _pattern

EMPTY_RESULT

public static FindResult EMPTY_RESULT
Empty result means that there is no matching any more to terminate the searching in current file.

Constructor Detail

FindAndReplace

public FindAndReplace()
The constructor.

Method Detail

getTarget

public FindAndReplaceTarget getTarget()
Gets current target in use.

Returns:
the FindAndReplaceTarget instance.

setTarget

public void setTarget(FindAndReplaceTarget target)
Sets FindAndReplaceTarget in use.

Parameters:
target - the FindAndReplaceTarget instance

addTarget

public void addTarget(FindAndReplaceTarget target)
Adds FindAndReplaceTarget in use.

Parameters:
target - the FindAndReplaceTarget instance

removeTarget

public void removeTarget(FindAndReplaceTarget target)
Removes FindAndReplaceTarget in use.

Parameters:
target - the FindAndReplaceTarget instance

getTargets

public FindAndReplaceTarget[] getTargets()
Gets all possible FindAndReplaceTarget instances.

Returns:
all possible FindAndReplaceTarget instances

getFindText

public String getFindText()
Gets the find text.

Returns:
the find text.

setFindText

public void setFindText(String findText)
Sets the find text.

Parameters:
findText - the find text

getReplaceText

public String getReplaceText()
Gets the replace text.

Returns:
the replace text.

setReplaceText

public void setReplaceText(String replaceText)
Sets the replace text.

Parameters:
replaceText - the replace text

getFindHistory

public Vector<String> getFindHistory()
Gets the find history.

Returns:
the find history.

setFindHistory

public void setFindHistory(Vector<String> findHistory)
Sets the find history.

Parameters:
findHistory - the find history

getReplaceHistory

public Vector<String> getReplaceHistory()
Gets the replace history.

Returns:
the replace history.

setReplaceHistory

public void setReplaceHistory(Vector<String> replaceHistory)
Sets the replace history.

Parameters:
replaceHistory - the replace history

isUseRegexOrWildcards

public boolean isUseRegexOrWildcards()
Gets the flag indicating Regex or wildcards is in use.

Returns:
true if regex or wildcards is in use. Otherwise false.

setUseRegexOrWildcards

public void setUseRegexOrWildcards(boolean useRegexOrWildcards)
Sets the flag indicating Regex or wildcards is in use.

Parameters:
useRegexOrWildcards - the flag

isUseWildcards

public boolean isUseWildcards()
Gets the flag indicating if wildcards are to be used.

Returns:
true if wildcards are to be used. Otherwise false.

setUseWildcards

public void setUseWildcards(boolean useWildcards)
Sets the flag indicating if wildcards are to be used.

Parameters:
useWildcards - the flag

isUseRegex

public boolean isUseRegex()
Sets the flag indicating if regex is to be used.

Returns:
true if regex is to be used. Otherwise false.

setUseRegex

public void setUseRegex(boolean useRegex)
Gets the flag indicating if regex is to be used.

Parameters:
useRegex - the flag

isMatchWholeWord

public boolean isMatchWholeWord()
Gets the flag indicating if only matching whole word.

Returns:
true if only matching whole word. Otherwise false.

setMatchWholeWord

public void setMatchWholeWord(boolean matchWholeWord)
Sets the flag indicating if only matching whole word.

Parameters:
matchWholeWord - the flag

isMatchCase

public boolean isMatchCase()
Gets the flag indicating if the search is case sensitive.

Returns:
true if the search is case sensitive. Otherwise false.

setMatchCase

public void setMatchCase(boolean matchCase)
Sets the flag indicating if the search is case sensitive.

Parameters:
matchCase - the flag

getScope

public int getScope()
Not implemented yet.

Returns:
the scope.

setScope

public void setScope(int scope)
Not implemented yet.

Parameters:
scope - the scope

getOrigin

public int getOrigin()
Gets the origin.

Returns:
the origin.

setOrigin

public void setOrigin(int origin)
Sets the origin.

Parameters:
origin - the origin

searchAgain

public void searchAgain()
Process searching again.


getFlags

protected int getFlags()
Gets searching flags.

Returns:
the flags.

getMatcher

protected Matcher getMatcher()
Gets the matcher prepared for searching.

Returns:
the matcher.

initialCharSequence

protected void initialCharSequence()
Initialize the char sequence prepared for searching.


search

public void search()
Process searching.


searchAll

public FindResults searchAll()
Process searching all.

Returns:
all the find results.

search

public void search(Matcher m)
Process to search in the document.

Parameters:
m - the matcher

promptForReplace

protected int promptForReplace()
Prompt a dialog for customer to choose if he/she needs replace the match or not.

Returns:
the dialog result.

findNext

protected FindResult findNext(Matcher m,
                              int startPosition)
Finds the next matching result from the start position with the matcher.

Parameters:
m - the matcher
startPosition - the start position
Returns:
the next matching result. EMPTY_RESULT if nothing found.
Since:
3.3.0

replace

public int replace(FindResult findResult,
                   String replacement)
Process to replace the find result with the replacement

Parameters:
findResult - the find result
replacement - the string replacement
Returns:
the length gap that this replacement make to the document. Could be negative, 0 or positive numbers.

textChanged

public void textChanged()
Process when the search text is changed.


searchFinished

protected void searchFinished()
Process when search is finished.


isForward

public boolean isForward()
Gets the flag indicating if the search is the same direction with isReversed().

This is a dynamic flag triggered by CodeEditor.findNext() and CodeEditor.findPrevious(). In most cases, this flag is not supposed to be saved and loaded.

Returns:
true if searching the same directioni with isReversed(). Otherwise false.

setForward

public void setForward(boolean forward)
Sets the flag indicating if the search is the same direction with isReversed().

Parameters:
forward - the flag

isReversed

public boolean isReversed()
Gets the flag indicating if the search is forward or backward.

Returns:
true if searching backward. Otherwise false.

setReversed

public void setReversed(boolean reversed)
Sets the flag indicating if the search is forward or backward.

Parameters:
reversed - the flag

isReplace

public boolean isReplace()
Gets the flag indicating if the replace option is currently visible in the dialog.

Returns:
true if the dialog is a replace dialog. Otherwise false.

setReplace

public void setReplace(boolean replace)
Sets the flag indicating if the replace option is currently visible in the dialog.

Parameters:
replace - the flag

isShowFindAll

public boolean isShowFindAll()
Gets the flag indicating if the "Find All" button should be visible.

Returns:
true if the button is visible. Otherwise false.

setShowFindAll

public void setShowFindAll(boolean showFindAll)
Sets the flag indicating if the "Find All" button should be visible.

Parameters:
showFindAll - the flag

addFindAndReplaceListener

public void addFindAndReplaceListener(FindAndReplaceListener FindAndReplaceListener)
Adds a listener.

Parameters:
FindAndReplaceListener - the listener

removeFindAndReplaceListener

public void removeFindAndReplaceListener(FindAndReplaceListener FindAndReplaceListener)
Removes a listener.

Parameters:
FindAndReplaceListener - the listener

getFindAndReplaceListeners

public FindAndReplaceListener[] getFindAndReplaceListeners()
Gets the FindAndReplaceListeners register on this class.

Returns:
the FindAndReplaceListeners.

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status,
                                       FindResults findResults)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired
findResults - the find results

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status,
                                       FindResult findResult)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired
findResult - the find result

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status,
                                       FindResult findResult,
                                       String replaceString)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired
findResult - the find result
replaceString - the replace string

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status,
                                       String fileName,
                                       FindResults findResults)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired
fileName - the file name to store the find results
findResults - the find results

fireFindAndReplaceEvent

protected void fireFindAndReplaceEvent(int status,
                                       String fileName)
Fire FindAndReplaceEvents.

Parameters:
status - the status to be fired
fileName - the file name to store the find results

isTargetChanged

public boolean isTargetChanged()
Gets the flag indicating if the target in use is just changed.

Returns:
true if the target is changed. Otherwise false.

JIDE 3.5.15