| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 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 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
0 |
public final class FileBrowser extends List implements CommandListener, FileSystemListener |
| 52 |
|
{ |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private final Display display_; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
private final Displayable next_; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
0 |
private final Command selectCommand_ = new Command( "select", Command.OK, 1 ); |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
0 |
private final Command cancelCommand_ = new Command( "cancel", Command.OK, 1 ); |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
0 |
private final Command createFileCommand_ = new Command( "new file", Command.SCREEN, 1 ); |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
0 |
private final Command createDirectoryCommand_ = new Command( "new directory", Command.SCREEN, 2 ); |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
private IFileBrowserListener listener_; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
private IDirectory current_; |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
private boolean isFileCreationAllowed_; |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
private final DirectoryVisitor visitor_; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 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 |
|
|
| 133 |
|
|
| 134 |
|
|
| 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 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
public void setListener( final IFileBrowserListener listener ) |
| 162 |
|
{ |
| 163 |
0 |
listener_ = listener; |
| 164 |
0 |
} |
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 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 |
|
|
| 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 |
|
} |