home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Sample.bin / FindReplaceEngine.java < prev    next >
Text File  |  1997-09-02  |  3KB  |  101 lines

  1. import java.lang.*;
  2. import java.awt.*;
  3.  
  4. public class FindReplaceEngine extends java.lang.Object
  5. {
  6.     private FindReplace FR;
  7.     
  8.     public FindReplaceEngine(FindReplace thefindreplace){
  9.         FR = thefindreplace;
  10.     }
  11.     
  12.     void FindIt() {
  13.  
  14.         int suspect_length;
  15.         int foundit_index;
  16.         String found_string;
  17.         String string_to_find;
  18.         String body;
  19.         boolean foundit;
  20.         boolean eofReached;
  21.  
  22.         body = FR.getBody().getText();  //Set all the strings to uppercase for compare
  23.         body = body.toUpperCase();
  24.         foundit = false;        //Assume we won't find it
  25.         FR.setFoundIt(foundit);
  26.         eofReached = false;
  27.         string_to_find = FR.getFind().toUpperCase();
  28.         suspect_length = string_to_find.length();
  29.  
  30.         while(!foundit && !eofReached) {
  31.             foundit_index = body.indexOf(string_to_find, FR.getIndex());
  32.             if(foundit_index == -1) eofReached = true;
  33.             else {
  34.                 if(FR.isCase()) {
  35.                     found_string = FR.getBody().getText().substring(foundit_index, foundit_index + suspect_length);
  36.                     if(found_string.equals(FR.getFind())) {
  37.                         foundit = true;
  38.                     } else { FR.setIndex(foundit_index + suspect_length); }
  39.                 } else foundit = true;
  40.                 
  41.                 if(foundit) {
  42.                     FR.setIndex(foundit_index + suspect_length-1);
  43.                     FR.getBody().select(foundit_index, foundit_index + suspect_length);
  44.                     FR.incOccur();
  45.                 } 
  46.              }
  47.         }
  48.         FR.setFoundIt(foundit);
  49.     }
  50.     
  51.     public int ReplaceIt()
  52.     {
  53.         int start;
  54.         int end;
  55.         int replace_length;
  56.         int extra_chars;
  57.         StringBuffer workingfile;
  58.                 
  59.         TextArea ta = (TextArea)FR.getBody();  
  60.         start = ta.getSelectionStart();
  61.         end = ta.getSelectionEnd();
  62.         extra_chars = LinesDown(start);
  63.         start-=extra_chars;
  64.         end-=extra_chars-1;
  65.         ta.replaceRange(FR.getReplace(), start , end);
  66.         return start + FR.getReplace().length();
  67.  
  68.  
  69.     }
  70.     
  71.     public void ReplaceAll()
  72.     {
  73.         FindIt();
  74.         while(FR.isFoundIt()) {
  75.             FR.setIndex(ReplaceIt());
  76.             FindIt();
  77.         }
  78.     }
  79.     
  80.     /*
  81.         The '/n' is counted as a character so we must find out
  82.         how many lines down the word found is and subtract that
  83.         many chars from the count.
  84.     */
  85.     public int LinesDown(int ending)
  86.     {
  87.         String working_body;
  88.         int whichchar = 0;
  89.         int line = 0;
  90.         
  91.         working_body = FR.getBody().getText();
  92.         
  93.         while(whichchar <= ending) {
  94.             if(working_body.charAt(whichchar) == '\n') line++;
  95.             whichchar++;
  96.         }
  97.         
  98.         return line;
  99.     }
  100.         
  101. }