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

  1. //
  2. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. //
  4. // import java.awt.*;
  5. import com.ms.ui.*;
  6. import com.ms.ui.event.*;
  7. import java.io.File;
  8.  
  9. /**
  10. *    An abstract base class for FileControls. FileControls encapsulate a file
  11. *    object and supports the IFileOperationTargetExt interface (i.e. DiskFileControl
  12. *    and ClipboardFileControl). Allows one to display and edit a file
  13. *    in a control. Supports interfaces for file load/save and editing. File
  14. *    printing and searching is supported here, as well as file bookkeeping, 
  15. *    and everything else is delegated to descendant classes.
  16. *
  17. *    @version    1.0, 7/15/97
  18. */
  19.  
  20. public abstract class FileControl implements IFileOperationTargetExt
  21. {
  22.     
  23.     /**
  24.     *    Edit control attached to this file.    This edit control displays the file's contents.
  25.     */
  26.     protected ITextOperationTargetExt editControl;
  27.     
  28.     /**
  29.      * The file, from which you can get the filename and the path.
  30.      */
  31.     protected File file;
  32.     
  33.     /**
  34.     *    Parent frame - used for displaying dialogs
  35.     */
  36.     protected UIFrame parentFrame;
  37.     
  38.     
  39.     /**
  40.     *    dirty flag - true if file was modified since it was last opened or saved
  41.     */
  42.     private boolean bDirty;
  43.     
  44.     /**
  45.     *    read only flag - true if the file cannot be written to
  46.     */
  47.     private boolean bReadOnly;
  48.     
  49.     /**
  50.     *    The find replace dialog box.    
  51.     */
  52.     protected JNoteFindReplaceDialog findReplaceDialog;
  53.     
  54.     /**
  55.     *    Creates a new FileControl.
  56.     *
  57.     *    @param    control    Edit control to display the file's contents
  58.     *    @param    parent    Parent frame
  59.     */
  60.     public FileControl(ITextOperationTargetExt control, UIFrame parent)
  61.     {
  62.         editControl = control;
  63.         parentFrame = parent;
  64.         setFile(null,null);
  65.  
  66.         findReplaceDialog = new JNoteFindReplaceDialog(parentFrame, 
  67.                                     JNotePad.loadString(ResourceIDs.IDS_FINDREPLACE),
  68.                                     false, editControl);
  69.  
  70.         bReadOnly = false;
  71.         bDirty = false;
  72.     }
  73.     
  74.     /**
  75.     *    Public init function - called to load a file into the control. Sets variables and loads the file.
  76.     *
  77.     *    @param    filepath    Path of file to load.
  78.     *    @param    filename    Name of file to load.
  79.     */
  80.     public boolean init(String filepath, String filename)
  81.     {
  82.         setFile(filepath, filename);
  83.  
  84.         // if we're passed a file name, open the file.
  85.         if (getFileName() != null)
  86.             return openFile();
  87.  
  88.         return true;
  89.     }        
  90.     
  91.     /**
  92.     *    Returns the edit control associated with this file.
  93.     */
  94.     public ITextOperationTargetExt getEditControl()
  95.     {
  96.         return editControl;
  97.     }
  98.     
  99.     /**
  100.     *    Sets the filename and filepath of the file associated with this control.
  101.     *
  102.     *    @param    path    Path of file to load
  103.     *    @param    name    Name of file to load
  104.     */
  105.     public void setFile(String path, String name)
  106.     {
  107.         if( (path==null) && (name==null) )
  108.         {
  109.             file=null;
  110.             return;
  111.         }
  112.  
  113.         if ((path == null) && (name != null))
  114.         {
  115.             int iIndex = name.lastIndexOf('\\');
  116.             if (iIndex > -1)
  117.             {
  118.                 path= name.substring(0, iIndex+1);
  119.                 name = name.substring(iIndex+1);
  120.             }
  121.             else
  122.             {
  123.                 path = ".\\";
  124.             }
  125.         }
  126.  
  127.         file=new File(path, name);
  128.     }
  129.     
  130.     /**
  131.     *    Returns the path of the file.
  132.     */
  133.     public String getFilePath()
  134.     {
  135.         if(file!=null)
  136.             return file.getPath();
  137.         return null;
  138.     }
  139.     
  140.     /**
  141.     *    Returns the name of the file.
  142.     */
  143.     public String getFileName()
  144.     {
  145.         if(file!=null)
  146.             return file.getName();
  147.         return null;
  148.     }
  149.  
  150.     /**
  151.     *    Returns the location of the file.
  152.     */
  153.     public String getFileLocation()
  154.     {
  155.         if(file!=null)
  156.             return file.getAbsolutePath();
  157.         return null;
  158.     }
  159.  
  160.     public void setFile(File f)
  161.     {
  162.         file=f;
  163.     }
  164.  
  165.     public File getFile()
  166.     {
  167.         return file;
  168.     }
  169.     
  170.     /**
  171.     *    Returns the filename extension. Returns an empty string if there is
  172.     *    no extension and null if there is no filename defined.
  173.     */
  174.     public String getFileExtension()
  175.     {
  176.         if (getFileName() == null)
  177.             return null;
  178.         
  179.         String sFileName=getFileName();
  180.  
  181.         int iDotIndex = sFileName.lastIndexOf('.');
  182.         
  183.         if ((iDotIndex < 0) || (iDotIndex == sFileName.length() - 1))
  184.             return "";
  185.         
  186.         return sFileName.substring(iDotIndex+1);
  187.     }
  188.     
  189.     
  190.     /**
  191.     *    Returns the filename that should be displayed on the screen (on a tab,
  192.     *    for example). If the filename is null, returns "(untitled)". Adds
  193.     *    dirty bit indicators and read-only indicators prefixed onto the name.
  194.     */
  195.     // !! expand this function out a bit
  196.     public String getDisplayFileName()
  197.     {
  198.         if (getFileName() == null)
  199.             return JNotePad.loadString(ResourceIDs.IDS_UNTITLEDFILE);
  200.         
  201.         String sPrefix="";
  202.         
  203.         if (isReadOnly() )
  204.             sPrefix+="[RO] ";
  205.         
  206.         if (isDirty() )
  207.             sPrefix+="* ";
  208.         
  209.         // append a file state prefix to the filename and return it.
  210.         return sPrefix+getFileName();
  211.     }
  212.     
  213.     
  214.     /**
  215.     *    Sets the file's read only state.
  216.     *
  217.     *    @param    ro    true if the file is to be marked read only, false if it is
  218.     *                to be writable.
  219.     */
  220.     public void setReadOnly(boolean ro)
  221.     {
  222.         bReadOnly = ro;
  223.         editControl.setReadOnly(ro);
  224.     }
  225.     
  226.     /**
  227.     *    Returns the read only state of the file.
  228.     */
  229.     public boolean isReadOnly()
  230.     {
  231.         return bReadOnly;
  232.     }
  233.     
  234.     /**
  235.     *    Returns the contents of the edit control associated with this file.
  236.     *    This is the contents of the file.
  237.     */
  238.     public String getText()
  239.     {
  240.         return editControl.getText();
  241.     }
  242.     
  243.     // implements some IFileOperationTarget methods that are
  244.     // common to all files. Override these if you want
  245.     
  246.     /**
  247.     *    Opens up a JNoteFindReplaceDialog on the
  248.     *    text, which handles all searching and replacing in the file. Part of the IFileOperationTarget interface. 
  249.     *
  250.     *    @param    isFind    true if a find dialog is to be opened, false if a replace
  251.     *                    dialog is to be opened.
  252.     */
  253.     public void searchReplace(boolean isFind)
  254.     {                                
  255.         int iStyle = (isFind ? UIFindReplaceDialog.FIND : UIFindReplaceDialog.REPLACE);
  256.         findReplaceDialog.startNewSearch();
  257.         findReplaceDialog.display(iStyle);            
  258.     }
  259.     
  260.     
  261.     /**
  262.     *    Closes the file. Returns true if the
  263.     *    file was closed successfully, false if not. This method does nothing.
  264.     *    Part of the IFileOperationTarget interface. 
  265.     */
  266.     public boolean closeFile()
  267.     {
  268.         return true;
  269.     }
  270.     
  271.     /**
  272.     *    Prints the file. This method does nothing. Part of the IFileOperationTarget interface.
  273.     */
  274.     public void printFile()
  275.     {
  276.     }
  277.     
  278.     /**
  279.     *    Called when a command is executed which is not
  280.     *    in the CommandFeature class. Use this to handle any application-specific
  281.     *    commands that need to be processed. This method does nothing;
  282.     *    override it to provide custom command functionality. Part of the IFileOperationTarget interface
  283.     *
  284.     *    @returns    false, indicating that the command was not processed here.
  285.     *
  286.     *    @param    command    The name of the command being processed. 
  287.     *    @param    evt    The event which is being processed.
  288.     */
  289.     public boolean otherCommand(String command, UIActionEvent evt)
  290.     {
  291.         return false;
  292.     }
  293.     
  294.     /**
  295.     *    Called when the file object has been
  296.     *    selected, in a tab view for example. This method does nothing. 
  297.     */
  298.     public void gotFocus()
  299.     {
  300.         // default is not to care
  301.     }
  302.     
  303.     /**
  304.     *    Called to ask whether this file can be
  305.     *    saved to disk. In this class we return true. Part of the IFileOperationTargetExt interface.
  306.     */
  307.     public boolean shouldSave()
  308.     {
  309.         return true;
  310.     }
  311.     
  312.     /**
  313.     *    Sets the dirty flag.
  314.     *
  315.     *    @param    b    true if the file is to be made dirty, false if it is to be made clean
  316.     */
  317.     public void setDirtyFlag(boolean b)
  318.     {
  319.         bDirty = b;
  320.     }
  321.     
  322.     /**
  323.     *    Returns whether the file is dirty or not.
  324.     */
  325.     public boolean isDirty()
  326.     {
  327.         return bDirty;
  328.     }
  329.     
  330.     /**
  331.     *    Returns the name of the image file we should use to display next to the
  332.     *    filename (in a tab, for example). This is a small picture which represents
  333.     *    the type of file graphically. This class returns an image of a blank file.
  334.     */
  335.     public String getImageName()
  336.     {
  337.         return "Images\\new.gif";
  338.     }
  339. }
  340.