Coverage Report - palmed.file.FileBrowser

Classes in this Package Line Coverage Branch Coverage Complexity
FileBrowser
0% 
0% 
1,8

 1  
 /**
 2  
  * Redistribution  and use  in source  and binary  forms, with  or without
 3  
  * modification, are permitted provided  that the following conditions are
 4  
  * met :
 5  
  *
 6  
  * . Redistributions  of  source  code  must  retain  the  above copyright
 7  
  *   notice, this list of conditions and the following disclaimer.
 8  
  *
 9  
  * . Redistributions in  binary form  must reproduce  the above  copyright
 10  
  *   notice, this list of conditions  and the following disclaimer in  the
 11  
  *   documentation and/or other materials provided with the distribution.
 12  
  *
 13  
  * . The name of the author may not be used to endorse or promote products
 14  
  *   derived from this software without specific prior written permission.
 15  
  *
 16  
  * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
 17  
  * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
 18  
  * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
 19  
  * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
 20  
  * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
 21  
  * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
 22  
  * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
 23  
  * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
 24  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 25  
  * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
 26  
  * POSSIBILITY OF SUCH DAMAGE.
 27  
  *
 28  
  * $Id: FileBrowser.java 359 2005-12-23 14:26:53Z mat007 $
 29  
  */
 30  
 
 31  
 package palmed.file;
 32  
 
 33  
 import javax.microedition.io.file.FileSystemListener;
 34  
 import javax.microedition.io.file.FileSystemRegistry;
 35  
 import javax.microedition.lcdui.Alert;
 36  
 import javax.microedition.lcdui.AlertType;
 37  
 import javax.microedition.lcdui.Command;
 38  
 import javax.microedition.lcdui.CommandListener;
 39  
 import javax.microedition.lcdui.Display;
 40  
 import javax.microedition.lcdui.Displayable;
 41  
 import javax.microedition.lcdui.List;
 42  
 import palmed.ui.IInputListener;
 43  
 import palmed.ui.InputDialog;
 44  
 
 45  
 /**
 46  
  * This class implements a file browser.
 47  
  *
 48  
  * @author Mathieu Champlon
 49  
  * @version $Revision: 359 $ $Date: 2005-12-23 23:26:53 +0900 (ven., 23 déc. 2005) $
 50  
  */
 51  0
 public final class FileBrowser extends List implements CommandListener, FileSystemListener
 52  
 {
 53  
     /**
 54  
      * The display.
 55  
      */
 56  
     private final Display display_;
 57  
     /**
 58  
      * The displayable to show when closing the browser.
 59  
      */
 60  
     private final Displayable next_;
 61  
     /**
 62  
      * The file list select command.
 63  
      */
 64  0
     private final Command selectCommand_ = new Command( "select", Command.OK, 1 );
 65  
     /**
 66  
      * The cancel command.
 67  
      */
 68  0
     private final Command cancelCommand_ = new Command( "cancel", Command.OK, 1 );
 69  
     /**
 70  
      * The create file command.
 71  
      */
 72  0
     private final Command createFileCommand_ = new Command( "new file", Command.SCREEN, 1 );
 73  
     /**
 74  
      * The create directory command.
 75  
      */
 76  0
     private final Command createDirectoryCommand_ = new Command( "new directory", Command.SCREEN, 2 );
 77  
     /**
 78  
      * The file browser listener.
 79  
      */
 80  
     private IFileBrowserListener listener_;
 81  
     /**
 82  
      * Current directory.
 83  
      */
 84  
     private IDirectory current_;
 85  
     /**
 86  
      * Whether file creation should be enabled.
 87  
      */
 88  
     private boolean isFileCreationAllowed_;
 89  
     /**
 90  
      * The directory visitor.
 91  
      */
 92  
     private final DirectoryVisitor visitor_;
 93  
 
 94  
     /**
 95  
      * Create a file browser.
 96  
      *
 97  
      * @param display the display
 98  
      * @param next the displayable to display when closing the file browser
 99  
      */
 100  
     public FileBrowser( final Display display, final Displayable next )
 101  
     {
 102  0
         super( null, List.IMPLICIT );
 103  0
         if( display == null )
 104  0
             throw new IllegalArgumentException( "parameter 'display' is null" );
 105  0
         if( next == null )
 106  0
             throw new IllegalArgumentException( "parameter 'next' is null" );
 107  0
         display_ = display;
 108  0
         next_ = next;
 109  0
         current_ = new Root();
 110  0
         visitor_ = new DirectoryVisitor();
 111  0
         setCommandListener( this );
 112  0
         setSelectCommand( selectCommand_ );
 113  0
         addCommand( cancelCommand_ );
 114  0
         FileSystemRegistry.addFileSystemListener( this );
 115  0
         show();
 116  0
     }
 117  
 
 118  
     private void show()
 119  
     {
 120  0
         deleteAll();
 121  
         try
 122  
         {
 123  0
             visitor_.fill( this, current_ );
 124  
         }
 125  0
         catch( Exception e )
 126  
         {
 127  0
             error( e.getMessage() );
 128  0
         }
 129  0
     }
 130  
 
 131  
     /**
 132  
      * Add or remove the possibility to create a new file and select it.
 133  
      *
 134  
      * @param canCreateFiles whether file creation is allowed or not
 135  
      */
 136  
     public void allowCreation( final boolean canCreateFiles )
 137  
     {
 138  0
         isFileCreationAllowed_ = canCreateFiles;
 139  0
         checkFileCreation();
 140  0
     }
 141  
 
 142  
     private void checkFileCreation()
 143  
     {
 144  0
         if( isFileCreationAllowed_ && current_.isWritable() )
 145  
         {
 146  0
             addCommand( createFileCommand_ );
 147  0
             addCommand( createDirectoryCommand_ );
 148  0
         }
 149  
         else
 150  
         {
 151  0
             removeCommand( createFileCommand_ );
 152  0
             removeCommand( createDirectoryCommand_ );
 153  
         }
 154  0
     }
 155  
 
 156  
     /**
 157  
      * Set or change the file browser listener.
 158  
      *
 159  
      * @param listener the file browser listener
 160  
      */
 161  
     public void setListener( final IFileBrowserListener listener )
 162  
     {
 163  0
         listener_ = listener;
 164  0
     }
 165  
 
 166  
     /**
 167  
      * {@inheritDoc}
 168  
      */
 169  
     public void commandAction( final Command command, final Displayable displayable )
 170  
     {
 171  0
         if( command == selectCommand_ )
 172  0
             select( getString( getSelectedIndex() ) );
 173  0
         else if( command == cancelCommand_ )
 174  0
             display_.setCurrent( next_ );
 175  0
         else if( command == createFileCommand_ )
 176  0
             createFile( this );
 177  0
         else if( command == createDirectoryCommand_ )
 178  0
             createDirectory( this );
 179  0
     }
 180  
 
 181  
     private void select( final String filename )
 182  
     {
 183  0
         current_ = current_.select( filename, listener_ );
 184  0
         checkFileCreation();
 185  0
         show();
 186  0
     }
 187  
 
 188  
     private void createFile( final Displayable next )
 189  
     {
 190  0
         final IInputListener listener = new IInputListener()
 191  
         {
 192  
             public void enter( final String value )
 193  
             {
 194  0
                 select( value );
 195  0
             }
 196  
 
 197  0
             public void cancel()
 198  
             {
 199  0
                 display_.setCurrent( next );
 200  0
             }
 201  
         };
 202  0
         display_.setCurrent( new InputDialog( "Save in a new file", "Enter file name", listener ) );
 203  0
     }
 204  
 
 205  
     private void createDirectory( final Displayable next )
 206  
     {
 207  0
         final IInputListener listener = new IInputListener()
 208  
         {
 209  
             public void enter( final String value )
 210  
             {
 211  
                 try
 212  
                 {
 213  0
                     current_.create( value );
 214  
                 }
 215  0
                 catch( Exception e )
 216  
                 {
 217  0
                     error( e.getMessage() );
 218  0
                 }
 219  0
                 display_.setCurrent( next );
 220  0
                 show();
 221  0
             }
 222  
 
 223  0
             public void cancel()
 224  
             {
 225  0
                 display_.setCurrent( next );
 226  0
             }
 227  
         };
 228  0
         display_.setCurrent( new InputDialog( "Create a directory", "Enter directory name", listener ) );
 229  0
     }
 230  
 
 231  
     /**
 232  
      * {@inheritDoc}
 233  
      */
 234  
     public void rootChanged( final int state, final String rootName )
 235  
     {
 236  0
         if( state == FileSystemListener.ROOT_ADDED || current_.isInPath( rootName ) )
 237  0
             current_ = new Root();
 238  0
         show();
 239  0
     }
 240  
 
 241  
     private void error( final String message )
 242  
     {
 243  0
         display_.setCurrent( new Alert( "Error", message, null, AlertType.ERROR ), next_ );
 244  0
     }
 245  
 }