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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4.  
  5. import FileControl;
  6. import java.io.*;
  7. import java.awt.Frame;
  8. import com.ms.ui.*;
  9. import java.util.StringTokenizer;
  10.  
  11. /**
  12. *    Implementation of abstract class FileControl for disk files. Encapsulates a 
  13. *    file and handles file loading/saving/etc into an edit control.
  14. *
  15. *    @see     FileControl
  16. *
  17. *    @version    1.0, 7/15/97
  18. */
  19.  
  20. public class DiskFileControl extends FileControl
  21. {
  22.     /**
  23.     *    Whether the file separates lines with CRLFs or CRs.    
  24.     */
  25.     protected boolean bIsCRLF;
  26.  
  27.     /**
  28.      * Creates a new DiskFileControl.
  29.      *
  30.      *    @param    toTarget    Edit control which this file will display in
  31.      *    @param    parentFrame    Parent frame window.
  32.      */
  33.     public DiskFileControl(ITextOperationTargetExt toTarget, UIFrame parentFrame)
  34.     {
  35.         super(toTarget, parentFrame);
  36.         bIsCRLF=true;
  37.     }
  38.     
  39.     /**
  40.      *    Creates a new file in the edit control. Part of IFileOperationTarget. 
  41.      */
  42.     public void newFile()
  43.     {
  44.         editControl.setText("");
  45.         setFile(null,null);
  46.     }
  47.     
  48.     // assumes that the new file name has already been set
  49.     // via setFileName()
  50.     /**
  51.      *    Saves the file to disk under a different
  52.      *    name. Assumes that the new filename has been set using setFileName(). Part of IFileOperationTarget. 
  53.      */
  54.     public boolean saveFileAs()
  55.     {            
  56.         return saveFile();
  57.     }
  58.     
  59.     /**
  60.      *    Saves the file to disk under the current
  61.      *    name. Simply writes the contents of the edit control to disk. Part of IFileOperationTarget. 
  62.      */
  63.     public boolean saveFile()
  64.     {
  65.         if (isReadOnly())
  66.         {
  67.             // file cannot be written to
  68.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  69.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  70.                 JNotePad.loadString(ResourceIDs.IDS_MSGREADONLY),
  71.                 box.STOP, UIButtonBar.OK);
  72.             box.doModal();
  73.             return false;        
  74.         }
  75.         
  76.         DataOutputStream file = null;            
  77.  
  78.         // display a wait cursor while we're waiting
  79.         int iOldCursor = showWaitCursor();
  80.         
  81.         try 
  82.         {
  83.             file = new DataOutputStream(new FileOutputStream(getFileLocation()));
  84.         }
  85.         catch (IOException e)
  86.         {
  87.             // file save problem
  88.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  89.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE), 
  90.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOSAVE),
  91.                 box.STOP, UIButtonBar.OK);
  92.             box.doModal();
  93.             
  94.             // restore old cursor
  95.             setCursor(iOldCursor);
  96.  
  97.             return false;
  98.         }    
  99.         
  100.         boolean ret = true;
  101.         
  102.         // characters which mark end of line. If bIsCRLF is true, then append
  103.         // a carriage return-line feed pair, otherwise just use a carriage return
  104.         String lineTerminator = (bIsCRLF ? "\r\n" : "\r");
  105.  
  106.         try 
  107.         {
  108.             String line = editControl.getText();
  109.             //file.writeBytes(line);
  110.  
  111.             //line.replace('\n', '\r');
  112.             int iIndex = line.indexOf('\n');
  113.             int iOldIndex = 0;
  114.             while (iIndex > -1)
  115.             {
  116.                 file.writeBytes(line.substring(iOldIndex, iIndex));
  117.  
  118.                 file.writeBytes(lineTerminator);
  119.                 
  120.                 iOldIndex = iIndex+1;
  121.                 iIndex = line.indexOf('\n', iOldIndex);
  122.             }
  123.             if (line.lastIndexOf('\n')<line.length()-1){
  124.                 System.out.println("Doing partial line write ");
  125.                 file.writeBytes(line.substring(
  126.                                     line.lastIndexOf('\n')+1,
  127.                                     line.length()));
  128.         }
  129.         }
  130.         catch (IOException e)
  131.         {
  132.             // can't write to disk
  133.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  134.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  135.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOSAVE),
  136.                 box.STOP, UIButtonBar.OK);
  137.             box.doModal();
  138.             
  139.             ret = false;
  140.         }
  141.         
  142.         try
  143.         {
  144.             file.close();
  145.         }
  146.         catch (IOException e1)
  147.         {
  148.         }
  149.         
  150.         // newly saved files are clean (not dirty)
  151.         setDirtyFlag(false);
  152.  
  153.         // restore the old cursor
  154.         setCursor(iOldCursor);
  155.         
  156.         return ret;
  157.     }
  158.     
  159.     /**
  160.     *    Checks if the file uses CRLF to separate lines or CRs only. 
  161.     *    Returns the next string in the file and sets the bIsCRLF member variable. Note that
  162.     *    this string has a carriage return appended to it.
  163.     */
  164.     private String checkIfCRLF(DataInputStream file)
  165.     {
  166.         StringBuffer buffer = new StringBuffer(85);
  167.         byte[] b = new byte[1];
  168.         boolean bHitCR = false;
  169.         
  170.         bIsCRLF = false;
  171.  
  172.         try
  173.         {
  174.             // read file until CR hit. Set a flag and stop reading when that happens.
  175.             int iNumRead = 0;
  176.             b[0] = 0;
  177.             while (iNumRead > -1)
  178.             {        
  179.                 iNumRead = file.read(b);
  180.                 
  181.                 if (b[0] == '\r')
  182.                 {
  183.                     bHitCR = true;
  184.                     iNumRead = -2;
  185.                 }
  186.                 else
  187.                 {
  188.                     buffer.append((char)b[0]);
  189.                 }                
  190.             } 
  191.  
  192.             // if we've hit a CR
  193.             if (bHitCR == true)
  194.             {
  195.                 // read next char. If this fails, b[0] will be unchanged, which is fine.
  196.                 // we only want to do something special if b[0] is \n (i.e. the next char
  197.                 // is LF) and if this hits EOF, b[0] will remain what it was. 
  198.                 file.read(b);
  199.                 
  200.                 // append a carriage return 
  201.                 buffer.append('\n');
  202.                 if (b[0] == '\n')
  203.                 {
  204.                     // CR followed by a LF. Clearly we're using CRLFs in this file.
  205.                     bIsCRLF = true;                    
  206.                 }
  207.                 else
  208.                 {
  209.                     // not using CRLF in this class. Append the character we read.
  210.                     bIsCRLF = false;                    
  211.                     buffer.append((char)b[0]);
  212.                 }                
  213.             }
  214.  
  215.         } 
  216.         catch (IOException e)
  217.         {
  218.             return null; // !!
  219.         }
  220.  
  221.         return buffer.toString();
  222.             
  223.     }
  224.  
  225.     /**
  226.     *    Opens a file from disk and loads it into
  227.     *    the edit control. Assumes filename has already been set by setFileName(). Part of IFileOperationTarget. 
  228.     */
  229.     public boolean openFile()
  230.     {        
  231.         DataInputStream file = null;
  232.         
  233.         try 
  234.         {
  235.             file = new DataInputStream(new FileInputStream(getFileLocation()));
  236.         }
  237.         catch (IOException e)
  238.         {
  239.             // file not found
  240.             UIMessageBox box = new UIMessageBox(new UIFrame(),
  241.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  242.                 JNotePad.loadString(ResourceIDs.IDS_MSGFILENOTFOUND)+getFileLocation(),
  243.                 box.STOP, UIButtonBar.OK);
  244.             box.doModal();
  245.             
  246.             return false;
  247.         }
  248.         
  249.         // display a wait cursor while we're waiting
  250.         int iOldCursor = showWaitCursor();
  251.  
  252.         boolean ret = true;                
  253.         
  254.         // check if we should read files with CR or CRLF at end of the lines.
  255.         String line = checkIfCRLF(file);
  256.         editControl.setText("");
  257.         editControl.appendText(line);
  258.         
  259.         try 
  260.         {                    
  261.             line = file.readLine();            
  262.  
  263.             while (line != null)
  264.             {
  265.                 editControl.appendText(line+"\n");
  266.                 line = file.readLine();
  267.             } 
  268.             
  269.         }
  270.         catch (IOException e)
  271.         {
  272.             // file read error
  273.             UIMessageBox box = new UIMessageBox(new UIFrame(),
  274.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  275.                 JNotePad.loadString(ResourceIDs.IDS_MSGFILEREAD),
  276.                 box.STOP, UIButtonBar.OK);
  277.             box.doModal();
  278.             
  279.             ret = false;
  280.         }
  281.         
  282.         try
  283.         {
  284.             file.close();
  285.         }
  286.         catch (IOException e1)
  287.         {
  288.             // internal saving error
  289.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  290.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  291.                 JNotePad.loadString(ResourceIDs.IDS_MSGSAVEERR),
  292.                 box.STOP, UIButtonBar.OK);
  293.             box.doModal();
  294.         }                
  295.         
  296.         // check if file is read only
  297.         File fileObj = getFile();
  298.         if (fileObj.canWrite())
  299.         {
  300.             setReadOnly(false);
  301.         }
  302.         else
  303.         {
  304.             setReadOnly(true);        
  305.         }
  306.         
  307.         // newly loaded files are clean (not dirty)
  308.         setDirtyFlag(false);
  309.  
  310.         // restore the old cursor
  311.         setCursor(iOldCursor);
  312.         
  313.         return ret;
  314.     }
  315.  
  316.     /**
  317.     *    Displays a wait cursor.    
  318.     *    @returns    The int ID of the cursor that used to be displayed
  319.     */
  320.     protected int showWaitCursor()
  321.     {
  322.         // return setCursor(Frame.WAIT_CURSOR);
  323.         return 0;    // we don't show the cursor, becase the this.setCursor does nothing
  324.     }
  325.  
  326.     /**
  327.     *    Sets the cursor.
  328.     *    @param    iCursor    The ID of the cursor to change to
  329.     */
  330.     protected int setCursor(int iCursor)
  331.     {
  332.         /*
  333.         int iOldCursor = parentFrame.getFrame().getCursorType();
  334.  
  335.         parentFrame.getFrame().setCursor(iCursor);
  336.  
  337.         return iOldCursor;
  338.         */
  339.         return 0;
  340.     }
  341.  
  342.     
  343.     // searchReplace is done in parent class and not overridden
  344.     // here
  345.     
  346.     // closeFile is done in parent class, no need to override
  347.     
  348.     // printFile is done in parent class, no need to override
  349.     
  350. }
  351.  
  352.  
  353.