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

  1. //
  2. // (c) 1998 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.             //file.writeBytes("\n");            
  124.         }
  125.         catch (IOException e)
  126.         {
  127.             // can't write to disk
  128.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  129.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  130.                 JNotePad.loadString(ResourceIDs.IDS_MSGNOSAVE),
  131.                 box.STOP, UIButtonBar.OK);
  132.             box.doModal();
  133.             
  134.             ret = false;
  135.         }
  136.         
  137.         try
  138.         {
  139.             file.close();
  140.         }
  141.         catch (IOException e1)
  142.         {
  143.         }
  144.         
  145.         // newly saved files are clean (not dirty)
  146.         setDirtyFlag(false);
  147.  
  148.         // restore the old cursor
  149.         setCursor(iOldCursor);
  150.         
  151.         return ret;
  152.     }
  153.     
  154.     /**
  155.     *    Checks if the file uses CRLF to separate lines or CRs only. 
  156.     *    Returns the next string in the file and sets the bIsCRLF member variable. Note that
  157.     *    this string has a carriage return appended to it.
  158.     */
  159.     private String checkIfCRLF(DataInputStream file)
  160.     {
  161.         StringBuffer buffer = new StringBuffer(85);
  162.         byte[] b = new byte[1];
  163.         boolean bHitCR = false;
  164.         
  165.         bIsCRLF = false;
  166.  
  167.         try
  168.         {
  169.             // read file until CR hit. Set a flag and stop reading when that happens.
  170.             int iNumRead = 0;
  171.             b[0] = 0;
  172.             while (iNumRead > -1)
  173.             {        
  174.                 iNumRead = file.read(b);
  175.                 
  176.                 if (b[0] == '\r')
  177.                 {
  178.                     bHitCR = true;
  179.                     iNumRead = -2;
  180.                 }
  181.                 else
  182.                 {
  183.                     buffer.append((char)b[0]);
  184.                 }                
  185.             } 
  186.  
  187.             // if we've hit a CR
  188.             if (bHitCR == true)
  189.             {
  190.                 // read next char. If this fails, b[0] will be unchanged, which is fine.
  191.                 // we only want to do something special if b[0] is \n (i.e. the next char
  192.                 // is LF) and if this hits EOF, b[0] will remain what it was. 
  193.                 file.read(b);
  194.                 
  195.                 // append a carriage return 
  196.                 buffer.append('\n');
  197.                 if (b[0] == '\n')
  198.                 {
  199.                     // CR followed by a LF. Clearly we're using CRLFs in this file.
  200.                     bIsCRLF = true;                    
  201.                 }
  202.                 else
  203.                 {
  204.                     // not using CRLF in this class. Append the character we read.
  205.                     bIsCRLF = false;                    
  206.                     buffer.append((char)b[0]);
  207.                 }                
  208.             }
  209.  
  210.         } 
  211.         catch (IOException e)
  212.         {
  213.             return null; // !!
  214.         }
  215.  
  216.         return buffer.toString();
  217.             
  218.     }
  219.  
  220.     /**
  221.     *    Opens a file from disk and loads it into
  222.     *    the edit control. Assumes filename has already been set by setFileName(). Part of IFileOperationTarget. 
  223.     */
  224.     public boolean openFile()
  225.     {        
  226.         DataInputStream file = null;
  227.         
  228.         try 
  229.         {
  230.             file = new DataInputStream(new FileInputStream(getFileLocation()));
  231.         }
  232.         catch (IOException e)
  233.         {
  234.             // file not found
  235.             UIMessageBox box = new UIMessageBox(new UIFrame(),
  236.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  237.                 JNotePad.loadString(ResourceIDs.IDS_MSGFILENOTFOUND)+getFileLocation(),
  238.                 box.STOP, UIButtonBar.OK);
  239.             box.doModal();
  240.             
  241.             return false;
  242.         }
  243.         
  244.         // display a wait cursor while we're waiting
  245.         int iOldCursor = showWaitCursor();
  246.  
  247.         boolean ret = true;                
  248.         
  249.         // check if we should read files with CR or CRLF at end of the lines.
  250.         String line = checkIfCRLF(file);
  251.         editControl.setText("");
  252.         editControl.appendText(line);
  253.         
  254.         try 
  255.         {                    
  256.             line = file.readLine();            
  257.  
  258.             while (line != null)
  259.             {
  260.                 editControl.appendText(line+"\n");
  261.                 line = file.readLine();
  262.             } 
  263.             
  264.         }
  265.         catch (IOException e)
  266.         {
  267.             // file read error
  268.             UIMessageBox box = new UIMessageBox(new UIFrame(),
  269.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  270.                 JNotePad.loadString(ResourceIDs.IDS_MSGFILEREAD),
  271.                 box.STOP, UIButtonBar.OK);
  272.             box.doModal();
  273.             
  274.             ret = false;
  275.         }
  276.         
  277.         try
  278.         {
  279.             file.close();
  280.         }
  281.         catch (IOException e1)
  282.         {
  283.             // internal saving error
  284.             UIMessageBox box = new UIMessageBox(new UIFrame(), 
  285.                 JNotePad.loadString(ResourceIDs.IDS_MSGTITLE),
  286.                 JNotePad.loadString(ResourceIDs.IDS_MSGSAVEERR),
  287.                 box.STOP, UIButtonBar.OK);
  288.             box.doModal();
  289.         }                
  290.         
  291.         // check if file is read only
  292.         File fileObj = getFile();
  293.         if (fileObj.canWrite())
  294.         {
  295.             setReadOnly(false);
  296.         }
  297.         else
  298.         {
  299.             setReadOnly(true);        
  300.         }
  301.         
  302.         // newly loaded files are clean (not dirty)
  303.         setDirtyFlag(false);
  304.  
  305.         // restore the old cursor
  306.         setCursor(iOldCursor);
  307.         
  308.         return ret;
  309.     }
  310.  
  311.     /**
  312.     *    Displays a wait cursor.    
  313.     *    @returns    The int ID of the cursor that used to be displayed
  314.     */
  315.     protected int showWaitCursor()
  316.     {
  317.         // return setCursor(Frame.WAIT_CURSOR);
  318.         return 0;    // we don't show the cursor, becase the this.setCursor does nothing
  319.     }
  320.  
  321.     /**
  322.     *    Sets the cursor.
  323.     *    @param    iCursor    The ID of the cursor to change to
  324.     */
  325.     protected int setCursor(int iCursor)
  326.     {
  327.         /*
  328.         int iOldCursor = parentFrame.getFrame().getCursorType();
  329.  
  330.         parentFrame.getFrame().setCursor(iCursor);
  331.  
  332.         return iOldCursor;
  333.         */
  334.         return 0;
  335.     }
  336.  
  337.     
  338.     // searchReplace is done in parent class and not overridden
  339.     // here
  340.     
  341.     // closeFile is done in parent class, no need to override
  342.     
  343.     // printFile is done in parent class, no need to override
  344.     
  345. }
  346.  
  347.  
  348.