home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / AFC / JNotepad / src / JNoteFindReplaceDialog.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  7.8 KB  |  316 lines

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