FileListerpublic class FileLister extends Frame
Fields Summary |
---|
private List | list | private TextField | infoarea | private Panel | buttons | private Button | parent | private Button | quit | private FilenameFilter | filter | private File | cwd | private String[] | entries |
Constructors Summary |
---|
public FileLister(String directory, FilenameFilter filter)
super("File Lister");
this.filter = filter;
list = new List(12, false);
infoarea = new TextField();
infoarea.setEditable(false);
buttons = new Panel();
parent = new Button("Up a Directory");
quit = new Button("Quit");
buttons.add(parent);
buttons.add(quit);
this.add("Center", list);
this.add("South", infoarea);
this.add("North", buttons);
this.resize(550, 350);
this.show();
// list the initial directory.
list_directory(directory);
|
Methods Summary |
---|
public boolean | handleEvent(java.awt.Event e)
if (e.target == quit) System.exit(0);
else if (e.target == parent) {
String parent = cwd.getParent();
if (parent == null) parent = "/"; // Bug workaround
try { list_directory(parent); }
catch (IllegalArgumentException ex) {
infoarea.setText("Already at top");
}
catch (IOException ex) { infoarea.setText("I/O Error"); }
return true;
}
else if (e.target == list) {
// when an item is selected, show its info.
if (e.id == Event.LIST_SELECT) {
try { show_info(entries[((Integer)e.arg).intValue()]); }
catch (IOException ex) { infoarea.setText("I/O Error"); }
}
// When the user double-clicks, change to the selected directory
// or display the selected file.
else if (e.id == Event.ACTION_EVENT) {
try {
String item = new File(cwd, (String)e.arg).getAbsolutePath();
try { list_directory(item); }
catch (IllegalArgumentException ex) {new FileViewer(item);}
}
catch (IOException ex) { infoarea.setText("I/O Error"); }
}
return true;
}
return super.handleEvent(e);
| public void | list_directory(java.lang.String directory)
File dir = new File(directory);
if (!dir.isDirectory())
throw new IllegalArgumentException("FileLister: no such directory");
list.clear();
cwd = dir;
this.setTitle(directory);
entries = cwd.list(filter);
for(int i = 0; i < entries.length; i++)
list.addItem(entries[i]);
| public static void | main(java.lang.String[] args)
FileLister f;
FilenameFilter filter = null;
String directory = null;
for(int i = 0; i < args.length; i++) {
if (args[i].equals("-e")) {
i++;
if (i >= args.length) usage();
filter = new EndsWithFilter(args[i]);
}
else {
if (directory != null) usage(); // Already set
else directory = args[i];
}
}
// if no directory specified, use the current directoy
if (directory == null) directory = System.getProperty("user.dir");
// Create the FileLister object
f = new FileLister(directory, filter);
| public void | show_info(java.lang.String filename)
File f = new File(cwd, filename);
String info;
if (!f.exists())
throw new IllegalArgumentException("FileLister.show_info(): " +
"no such file or directory");
if (f.isDirectory()) info = "Directory: ";
else info = "File: ";
info += filename + " ";
info += (f.canRead()?"read ":" ") +
(f.canWrite()?"write ":" ") +
f.length() + " " +
new java.util.Date(f.lastModified());
infoarea.setText(info);
| public static void | usage()
System.out.println("Usage: java FileLister [directory_name] " +
"[-e file_extension]");
System.exit(0);
|
|