home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / afc11 / JNotepad / src / JNoteFindReplaceDialog.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  7.5 KB  |  237 lines

  1. /*
  2.  * (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3.  */
  4. import com.ms.ui.*;
  5. import java.awt.*;
  6. /**
  7.  *    Adds find and replace algorithms to UIFindReplaceDialog as well as
  8.  *    support for using ITextOperationTargetExt interfaces.
  9.  *
  10.  *    @version    1.0, 8/1/97
  11.  */
  12. public class JNoteFindReplaceDialog extends UIFindReplaceDialog{
  13.     /**
  14.     *    Edit control we're doing find and replace in
  15.     */
  16.     protected ITextOperationTargetExt textTarget;
  17.     /**
  18.     *    The parent frame window
  19.     */
  20.     protected UIFrame parentFrame;
  21.     /**
  22.     *    Current search position
  23.     */
  24.     private int iCurrentSearchPos;
  25.     /**
  26.     *    Number of hits found so far.
  27.     */
  28.     private int iNumHits;
  29.     /**
  30.     *    Identifier specifying no search is active.    
  31.     */
  32.     private static final int NO_CURRENT_SEARCH = -2;
  33. /**
  34.  *    Creates a JNoteFindReplaceDialog
  35.  *
  36.  *    @param    frame    Parent frame
  37.  *    @param    title    Title of the dialog
  38.  *    @param    modal    true if the dialog is to be modal, false if it is to be modeless.
  39.  *    @param    target    The ITextOperationTargetExt object which will be searched.
  40.  */
  41.     JNoteFindReplaceDialog(UIFrame frame, 
  42.                            String title, 
  43.                            boolean modal, 
  44.                            ITextOperationTargetExt target){
  45.         super(frame, title, modal);
  46.         textTarget = target;
  47.         parentFrame = frame;
  48.         // init variables for starting a new search
  49.         startNewSearch();            
  50.     }
  51. /**
  52.  *    Overrides UIFindReplaceDialog.doFindNext(). Calls search() to do searching,
  53.  *    and highlights the found word. If no more words are left to find, displays a 
  54.  *    dialog box informing the user of this.
  55.  *
  56.  *    @returns    true if a word was found, false if not.
  57.  */
  58.     public boolean doFindNext(){
  59.         // find the index of the next search hit
  60.         int iHitStart = search();
  61.         // if the word is found
  62.         if (iHitStart > -1){
  63.             // hilight the word
  64.             int iFindTextLen = getFindText().length();
  65.             textTarget.setSelection(iHitStart, iHitStart+iFindTextLen);
  66.             textTarget.moveTheCaret(iHitStart+iFindTextLen);
  67.             return true;
  68.         }
  69.         UIMessageBox msgbox = null;
  70.         if (iNumHits > 0){
  71.             // if no more words found, splash up a message box and reset the search position
  72.             // we've finished searching
  73.             msgbox = new UIMessageBox(parentFrame, 
  74.                 JNotePad.loadString(ResourceIDs.IDS_MSGFINDTEXT),
  75.                 JNotePad.loadString(ResourceIDs.IDS_MSGSEARCHDONE),
  76.                 UIMessageBox.INFORMATION, UIButtonBar.OK);
  77.         }else{
  78.             // if no words found at all, splash up a message box and reset the search position
  79.             // no matching text found
  80.             msgbox = new UIMessageBox(parentFrame, 
  81.                 JNotePad.loadString(ResourceIDs.IDS_MSGFINDTEXT),
  82.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOMATCH),
  83.                 UIMessageBox.INFORMATION, UIButtonBar.OK);
  84.         }
  85.         msgbox.doModal();
  86.         // get focus back to us
  87.         // requestFocus();            
  88.         startNewSearch();
  89.         return false;
  90.     }
  91. /**
  92.  *    Overrides UIFindReplaceDialog.doReplace(). Calls search() to do searching,
  93.  *    and replaces the found word. If no more words are left to find, displays a 
  94.  *    dialog box informing the user of this.
  95.  *
  96.  *    @returns    true if a word was replaced, false if not.
  97.  */
  98.     public boolean doReplace(){
  99.         // start searching from the current selected text
  100.         int iSelStart = textTarget.getSelectionStart();
  101.         if (iSelStart != textTarget.getSelectionEnd()){
  102.             iCurrentSearchPos = iSelStart;
  103.         }
  104.         // find the index of the next search hit
  105.         int iHitStart = search();
  106.         // if word is found
  107.         if (iHitStart > -1){
  108.             int iFindTextLen = getFindText().length();
  109.             // select the found text
  110.             textTarget.setSelection(iHitStart, iHitStart+iFindTextLen);
  111.             textTarget.moveTheCaret(iHitStart+iFindTextLen);
  112.             // paste over it to replace it
  113.             textTarget.paste(getReplaceText());
  114.             iCurrentSearchPos = iHitStart + getReplaceText().length();
  115.             return true;
  116.         }
  117.         UIMessageBox msgbox = null;
  118.         if (iNumHits > 0){
  119.             // if word not found, splash up a message box and reset the search position
  120.             // replacing done
  121.             msgbox = new UIMessageBox(parentFrame, 
  122.                 JNotePad.loadString(ResourceIDs.IDS_MSGREPLACETEXT),
  123.                 JNotePad.loadString(ResourceIDs.IDS_MSGREPLACEDONE),
  124.                 UIMessageBox.INFORMATION, UIButtonBar.OK);
  125.         }else{
  126.             // if no more words found, splash up a message box and reset the search position
  127.             // no matching text found
  128.             msgbox = new UIMessageBox(parentFrame, 
  129.                 JNotePad.loadString(ResourceIDs.IDS_MSGREPLACETEXT),
  130.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOMATCH),
  131.                 UIMessageBox.INFORMATION, UIButtonBar.OK);
  132.         }
  133.         msgbox.doModal();
  134.         // get focus back to us
  135.         // requestFocus();
  136.         startNewSearch();
  137.         return false;
  138.     }
  139. /**
  140.  *    Overrides UIFindReplaceDialog.doReplaceAll(). Calls doReplace() until it
  141.  *    returns false, which means all words have been replaced.
  142.  *
  143.  *    @returns    true
  144.  */
  145.     public boolean doReplaceAll(){
  146.         iCurrentSearchPos = 0;
  147.         while (doReplace() == true)
  148.             ;
  149.         return true;
  150.     }
  151. /**
  152.  *    Informs the find replace dialog that we are starting a new search.
  153.  *    This is used so we know when a search has failed completely.
  154.  */
  155.     public void startNewSearch(){
  156.         iCurrentSearchPos = NO_CURRENT_SEARCH;
  157.         iNumHits = 0;
  158.     }
  159. /**
  160.  *    Overrides display. Calls the default display() function and then
  161.  *    sets focus to the find next button.
  162.  */
  163.     public int display(int style){
  164.         int iRet = super.display(style);
  165.         // set focus to the find/replace dialog
  166.         requestFocus();
  167.         return iRet;
  168.     }
  169. /**
  170.  *    Searches the text in textTarget. Uses String.indexOf() to do searching;
  171.  *    very slow, yes, but simple. Replace this with a Boyer-Moore search for speed.
  172.  *
  173.  *    @returns    the character index of the word that was found, or -1 if no word was found.
  174.  */
  175.     protected int search(){            
  176.         String str = textTarget.getText();
  177.         String findText = getFindText();
  178.         // check if this is the first time we're searching
  179.         if (iCurrentSearchPos == NO_CURRENT_SEARCH){
  180.             // set it to the beginning of the file if we're
  181.             // searching forward, or to the end of the
  182.             // file if we're searching backward
  183.             if (isForward())
  184.                 iCurrentSearchPos = 0;
  185.             else
  186.                 iCurrentSearchPos = str.length()-1;
  187.         }
  188.         int iIndex = -1;        
  189.         if (!isCaseSensitive()){
  190.             str = str.toLowerCase();
  191.             findText = findText.toLowerCase();                    
  192.         }
  193.         if (isForward()){
  194.             // do the searching forward
  195.             iIndex = str.indexOf(findText, iCurrentSearchPos);
  196.             iCurrentSearchPos = iIndex+1;
  197.         }else{
  198.             // do the searching backwards                    
  199.             iIndex = str.lastIndexOf(findText, iCurrentSearchPos);
  200.             iCurrentSearchPos = iIndex-1;
  201.         }
  202.         if (iIndex < 0)
  203.             return iIndex;
  204.         if (isWholeWord()){                    
  205.             // let's see if the characters before and after this hit
  206.             // are token separators
  207.             if (iIndex > 0){
  208.                 char c = str.charAt(iIndex-1);
  209.                 if (Character.isLetter(c) || c == '\''){
  210.                     // letters or an apostraphe are not valid word delimiters.
  211.                     // everything else separates words (punctuation, white space)
  212.                     
  213.                     // since this isn't a valid hit, run the search again.
  214.                     // The whole file will eventually be searched, so this
  215.                     // won't recurse forever.
  216.                     return search();
  217.                 }
  218.             }
  219.             int iFindTextEndIndex = findText.length()+iIndex;
  220.             if (iFindTextEndIndex < str.length()-1){
  221.                 char c = str.charAt(iFindTextEndIndex+1);
  222.                 if (Character.isLetter(c) || c == '\''){
  223.                     // letters or an apostraphe are not valid word delimiters.
  224.                     // everything else separates words (punctuation, white space)
  225.                     
  226.                     // since this isn't a valid hit, run the search again.
  227.                     // The whole file will eventually be searched, so this
  228.                     // won't recurse forever.
  229.                     return search();
  230.                 }
  231.             }
  232.         }
  233.         if (iIndex > -1)
  234.             ++iNumHits;
  235.         return iIndex;
  236.     }
  237. }