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

  1. //
  2. // (C) Copyright 1995 - 1999 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.     private UIDrawText wordsField;                        // field which holds the word count
  19.     private UIDrawText linesField;                        // field which holds the line count
  20.     private UIDrawText charsField;                        // field which holds the character count
  21.     private UIDrawText charsWithSpacesField;            // field which holds the characters with spaces count
  22.     private UIGroup fileBox;                            // group box which contains the file's name
  23.     private IFileOperationTargetExt  fileControl;            // file control we are performing statistics on.
  24.     
  25.     public JNoteStatsDialog(UIFrame parentframe, IFileOperationTargetExt  filecontrol){
  26.         super(parentframe, "File Statistics", true);
  27.         
  28.         init(filecontrol);
  29.     }
  30.     
  31.     private void init(IFileOperationTargetExt  filecontrol)
  32.     {
  33.         fileControl = filecontrol;
  34.         loadDialog(JNotePad.getResources(), IDR_FILESTATS_DIALOG);
  35.         
  36.         linesField = (UIDrawText)getComponentFromID(IDR_FILESTATS_LINES);
  37.         wordsField = (UIDrawText)getComponentFromID(IDR_FILESTATS_WORDS);
  38.         charsField = (UIDrawText)getComponentFromID(IDR_FILESTATS_CHARS);
  39.         charsWithSpacesField = (UIDrawText)getComponentFromID(IDR_FILESTATS_CHARS_W_SPACES);
  40.                                 
  41.         fileBox = (UIGroup)getComponentFromID(IDR_FILESTATS_FILEBOX);            
  42.         fileBox.setName(fileControl.getDisplayFileName());            
  43.         
  44.         invalidateAll();            
  45.     }
  46.     
  47.     
  48.     public void initDialog(){                                
  49.         fileBox = (UIGroup)getComponentFromID(IDR_FILESTATS_FILEBOX);            
  50.         fileBox.setName(fileControl.getDisplayFileName());                        
  51.         
  52.         String text = fileControl.getText();
  53.         int iTextLen = text.length();
  54.         
  55.         int iWords = 0;
  56.         int iLines = 0;
  57.         int iChars = 0;
  58.         int iSpaces = 0;
  59.         
  60.         int iIndex = 0;
  61.         char ch;
  62.         
  63.         //
  64.         // Words require some non-white space to be between thenm
  65.         //
  66.         boolean bNonWhiteSpace=false;
  67.         while (iIndex < iTextLen)
  68.         {
  69.             ch = text.charAt(iIndex);
  70.             
  71.             if (ch == '\n')
  72.             {
  73.                 if(bNonWhiteSpace)
  74.                     iWords++;
  75.                 iLines++; //This will fail if there is a line without "/n"
  76.                 bNonWhiteSpace=false;
  77.             }
  78.             else if (Character.isSpace(ch) && (ch != '\r'))
  79.             {
  80.                 iWords++;
  81.                 iSpaces++;
  82.             }
  83.             else
  84.             {
  85.                 iChars++;
  86.                 bNonWhiteSpace=true;
  87.             }
  88.             
  89.             if (".;,".indexOf(ch) > -1)
  90.             {
  91.                 iWords++;
  92.                 bNonWhiteSpace=false;
  93.             }
  94.             
  95.             iIndex++;                    
  96.         }
  97.  
  98.         if(bNonWhiteSpace)
  99.             iWords++;
  100.         String f = fileControl.getText();
  101.         if (f.charAt(f.length()-1)!='\n')
  102.             iLines++;
  103.         System.out.println("MY LINE COUNT "+iLines);
  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.