home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-09-03 | 2.8 KB | 101 lines |
- import java.lang.*;
- import java.awt.*;
-
- public class FindReplaceEngine extends java.lang.Object
- {
- private FindReplace FR;
-
- public FindReplaceEngine(FindReplace thefindreplace){
- FR = thefindreplace;
- }
-
- void FindIt() {
-
- int suspect_length;
- int foundit_index;
- String found_string;
- String string_to_find;
- String body;
- boolean foundit;
- boolean eofReached;
-
- body = FR.getBody().getText(); //Set all the strings to uppercase for compare
- body = body.toUpperCase();
- foundit = false; //Assume we won't find it
- FR.setFoundIt(foundit);
- eofReached = false;
- string_to_find = FR.getFind().toUpperCase();
- suspect_length = string_to_find.length();
-
- while(!foundit && !eofReached) {
- foundit_index = body.indexOf(string_to_find, FR.getIndex());
- if(foundit_index == -1) eofReached = true;
- else {
- if(FR.isCase()) {
- found_string = FR.getBody().getText().substring(foundit_index, foundit_index + suspect_length);
- if(found_string.equals(FR.getFind())) {
- foundit = true;
- } else { FR.setIndex(foundit_index + suspect_length); }
- } else foundit = true;
-
- if(foundit) {
- FR.setIndex(foundit_index + suspect_length-1);
- FR.getBody().select(foundit_index, foundit_index + suspect_length);
- FR.incOccur();
- }
- }
- }
- FR.setFoundIt(foundit);
- }
-
- public int ReplaceIt()
- {
- int start;
- int end;
- int replace_length;
- int extra_chars;
- StringBuffer workingfile;
-
- TextArea ta = (TextArea)FR.getBody();
- start = ta.getSelectionStart();
- end = ta.getSelectionEnd();
- extra_chars = LinesDown(start);
- start-=extra_chars;
- end-=extra_chars-1;
- ta.replaceRange(FR.getReplace(), start , end);
- return start + FR.getReplace().length();
-
-
- }
-
- public void ReplaceAll()
- {
- FindIt();
- while(FR.isFoundIt()) {
- FR.setIndex(ReplaceIt());
- FindIt();
- }
- }
-
- /*
- The '/n' is counted as a character so we must find out
- how many lines down the word found is and subtract that
- many chars from the count.
- */
- public int LinesDown(int ending)
- {
- String working_body;
- int whichchar = 0;
- int line = 0;
-
- working_body = FR.getBody().getText();
-
- while(whichchar <= ending) {
- if(working_body.charAt(whichchar) == '\n') line++;
- whichchar++;
- }
-
- return line;
- }
-
- }