View Javadoc

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  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      private final Command selectCommand_ = new Command( "select", Command.OK, 1 );
65      /***
66       * The cancel command.
67       */
68      private final Command cancelCommand_ = new Command( "cancel", Command.OK, 1 );
69      /***
70       * The create file command.
71       */
72      private final Command createFileCommand_ = new Command( "new file", Command.SCREEN, 1 );
73      /***
74       * The create directory command.
75       */
76      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         super( null, List.IMPLICIT );
103         if( display == null )
104             throw new IllegalArgumentException( "parameter 'display' is null" );
105         if( next == null )
106             throw new IllegalArgumentException( "parameter 'next' is null" );
107         display_ = display;
108         next_ = next;
109         current_ = new Root();
110         visitor_ = new DirectoryVisitor();
111         setCommandListener( this );
112         setSelectCommand( selectCommand_ );
113         addCommand( cancelCommand_ );
114         FileSystemRegistry.addFileSystemListener( this );
115         show();
116     }
117 
118     private void show()
119     {
120         deleteAll();
121         try
122         {
123             visitor_.fill( this, current_ );
124         }
125         catch( Exception e )
126         {
127             error( e.getMessage() );
128         }
129     }
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         isFileCreationAllowed_ = canCreateFiles;
139         checkFileCreation();
140     }
141 
142     private void checkFileCreation()
143     {
144         if( isFileCreationAllowed_ && current_.isWritable() )
145         {
146             addCommand( createFileCommand_ );
147             addCommand( createDirectoryCommand_ );
148         }
149         else
150         {
151             removeCommand( createFileCommand_ );
152             removeCommand( createDirectoryCommand_ );
153         }
154     }
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         listener_ = listener;
164     }
165 
166     /***
167      * {@inheritDoc}
168      */
169     public void commandAction( final Command command, final Displayable displayable )
170     {
171         if( command == selectCommand_ )
172             select( getString( getSelectedIndex() ) );
173         else if( command == cancelCommand_ )
174             display_.setCurrent( next_ );
175         else if( command == createFileCommand_ )
176             createFile( this );
177         else if( command == createDirectoryCommand_ )
178             createDirectory( this );
179     }
180 
181     private void select( final String filename )
182     {
183         current_ = current_.select( filename, listener_ );
184         checkFileCreation();
185         show();
186     }
187 
188     private void createFile( final Displayable next )
189     {
190         final IInputListener listener = new IInputListener()
191         {
192             public void enter( final String value )
193             {
194                 select( value );
195             }
196 
197             public void cancel()
198             {
199                 display_.setCurrent( next );
200             }
201         };
202         display_.setCurrent( new InputDialog( "Save in a new file", "Enter file name", listener ) );
203     }
204 
205     private void createDirectory( final Displayable next )
206     {
207         final IInputListener listener = new IInputListener()
208         {
209             public void enter( final String value )
210             {
211                 try
212                 {
213                     current_.create( value );
214                 }
215                 catch( Exception e )
216                 {
217                     error( e.getMessage() );
218                 }
219                 display_.setCurrent( next );
220                 show();
221             }
222 
223             public void cancel()
224             {
225                 display_.setCurrent( next );
226             }
227         };
228         display_.setCurrent( new InputDialog( "Create a directory", "Enter directory name", listener ) );
229     }
230 
231     /***
232      * {@inheritDoc}
233      */
234     public void rootChanged( final int state, final String rootName )
235     {
236         if( state == FileSystemListener.ROOT_ADDED || current_.isInPath( rootName ) )
237             current_ = new Root();
238         show();
239     }
240 
241     private void error( final String message )
242     {
243         display_.setCurrent( new Alert( "Error", message, null, AlertType.ERROR ), next_ );
244     }
245 }