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

  1. //
  2. // (c) 1998 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5. /**
  6. *    JNoteStatsDialog
  7. *
  8. *    Displays file statistics/properties in a dialog box. Displays line,
  9. *    word, character, and character with spaces count. 
  10. *
  11. *    @version    1.0, 8/3/97
  12. */
  13.  
  14. import com.ms.ui.*;
  15. import java.util.StringTokenizer;
  16.  
  17. public class JNoteStatsDialog extends JNoteDialog implements IConstants
  18. {
  19.     private UIDrawText wordsField;                        // field which holds the word count
  20.     private UIDrawText linesField;                        // field which holds the line count
  21.     private UIDrawText charsField;                        // field which holds the character count
  22.     private UIDrawText charsWithSpacesField;            // field which holds the characters with spaces count
  23.     private UIGroup fileBox;                            // group box which contains the file's name
  24.     private IFileOperationTargetExt  fileControl;            // file control we are performing statistics on.
  25.     
  26.     public JNoteStatsDialog(UIFrame parentframe, IFileOperationTargetExt  filecontrol)
  27.     {
  28.         super(parentframe, "File Statistics", true);
  29.         
  30.         init(filecontrol);
  31.     }
  32.     
  33.     private void init(IFileOperationTargetExt  filecontrol)
  34.     {
  35.         fileControl = filecontrol;
  36.         loadDialog(JNotePad.getResources(), IDR_FILESTATS_DIALOG);
  37.         
  38.         linesField = (UIDrawText)getComponentFromID(IDR_FILESTATS_LINES);
  39.         wordsField = (UIDrawText)getComponentFromID(IDR_FILESTATS_WORDS);
  40.         charsField = (UIDrawText)getComponentFromID(IDR_FILESTATS_CHARS);
  41.         charsWithSpacesField = (UIDrawText)getComponentFromID(IDR_FILESTATS_CHARS_W_SPACES);
  42.                                 
  43.         fileBox = (UIGroup)getComponentFromID(IDR_FILESTATS_FILEBOX);            
  44.         fileBox.setName(fileControl.getDisplayFileName());            
  45.         
  46.         invalidateAll();            
  47.     }
  48.     
  49.     
  50.     public void initDialog()
  51.     {                                
  52.         fileBox = (UIGroup)getComponentFromID(IDR_FILESTATS_FILEBOX);            
  53.         fileBox.setName(fileControl.getDisplayFileName());                        
  54.         
  55.         String text = fileControl.getText();
  56.         int iTextLen = text.length();
  57.         
  58.         int iWords = 0;
  59.         int iLines = 0;
  60.         int iChars = 0;
  61.         int iSpaces = 0;
  62.         
  63.         int iIndex = 0;
  64.         char ch;
  65.         
  66.         //
  67.         // Words require some non-white space to be between thenm
  68.         //
  69.         boolean bNonWhiteSpace=false;
  70.         while (iIndex < iTextLen)
  71.         {
  72.             ch = text.charAt(iIndex);
  73.             
  74.             if (ch == '\n')
  75.             {
  76.                 if(bNonWhiteSpace)
  77.                     iWords++;
  78.                 iLines++;
  79.                 bNonWhiteSpace=false;
  80.             }
  81.             else if (Character.isSpace(ch) && (ch != '\r'))
  82.             {
  83.                 iWords++;
  84.                 iSpaces++;
  85.             }
  86.             else
  87.             {
  88.                 iChars++;
  89.                 bNonWhiteSpace=true;
  90.             }
  91.             
  92.             if (".;,".indexOf(ch) > -1)
  93.             {
  94.                 iWords++;
  95.                 bNonWhiteSpace=false;
  96.             }
  97.             
  98.             iIndex++;                    
  99.         }
  100.  
  101.         if(bNonWhiteSpace)
  102.             iWords++;
  103.  
  104.         wordsField.setValueText(String.valueOf(iWords));
  105.         linesField.setValueText(String.valueOf(iLines));
  106.         charsField.setValueText(String.valueOf(iChars));
  107.         charsWithSpacesField.setValueText(String.valueOf(iChars+iSpaces));            
  108.  
  109.         getComponentFromID(IDR_FILESTATS_OK).requestFocus();
  110.     }
  111.     
  112. }
  113.