import java.io.*; import java.awt.*; import java.awt.event.*; import java.util.*; import utilities.gui.*; public class DirLister extends Panel implements ActionListener, Runnable { protected String filePath; protected Vector fileList; protected TextField pathTextField; protected ActionListener fNameListenerChain; protected static int treeDepth; //------------------------------------------------------------------- // DirLister Constructor //------------------------------------------------------------------- public DirLister() { Label prompt = new Label("Select Directory:"); pathTextField = new TextField("", 40); pathTextField.addActionListener(this); add(prompt); add(pathTextField); fNameListenerChain = null; } //------------------------------------------------------------------- // DirLister Thread body //------------------------------------------------------------------- public void run() { fileList = new Vector(); System.out.println("Scanning directory: "+ filePath); scanDir(filePath); // transmit all filenames to client bean Enumeration fileIterator = fileList.elements(); while ( fileIterator.hasMoreElements() ) { String filename = (String) fileIterator.nextElement(); fNameListenerChain.actionPerformed( new FilenameActionEvent(this, filename) ); } // finally, terminate list of filenames with a null String fNameListenerChain.actionPerformed( new FilenameActionEvent(this, null) ); // re-enable GUI to allow user input. setFrameCursor(this, Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); pathTextField.setEnabled(true); } //------------------------------------------------------------------- // Recursive directory scanner //------------------------------------------------------------------- protected void scanDir (String dir) { String dirEntries[]; // list of dir entries (file|dir) String filename; File path, fpath; treeDepth++; // going down... path = new File(dir); dirEntries = path.list(); // create list of files in this dir for (int i=0; i < dirEntries.length; i++) { filename = dir + File.separator + dirEntries[i]; fpath = new File(filename); if ( fpath.isDirectory() ) { scanDir(fpath.getPath()); // recursively descend dir tree } else { fileList.addElement(fpath.getPath()); } } treeDepth--; // and going up... } //------------------------------------------------------------------- // ActionListener interface method //------------------------------------------------------------------- public void actionPerformed(ActionEvent actionEvent) { Component source; // for Self-Test only: if ( actionEvent instanceof FilenameActionEvent ) { String filename = ((FilenameActionEvent) actionEvent).getFilename(); System.out.println("File: " + filename ); } else { source = (Component) actionEvent.getSource(); if ( source instanceof TextField ) { filePath = pathTextField.getText(); startListing(filePath); } } } //------------------------------------------------------------------- // Start the file listing process as a separate Thread. //------------------------------------------------------------------- public boolean startListing(String path) { File tentative; tentative = new File(path); if ( tentative.exists()) { if (tentative.isDirectory()) { // disable GUI to block user input until thread is done. setFrameCursor(this, Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); pathTextField.setEnabled(false); // start up filename event firing thread. new Thread(this).start(); return true; } else { DialogKit.ok( "'"+ path +"' Is not a directory."); } } else { DialogKit.ok( "'"+ path +"' Does not exist."); } return false; } //------------------------------------------------------------------- // ActionListener management methods //------------------------------------------------------------------- public void addActionListener(ActionListener listener) { fNameListenerChain = AWTEventMulticaster.add(fNameListenerChain, listener); } public void removeActionListener(ActionListener listener) { fNameListenerChain = AWTEventMulticaster.remove(fNameListenerChain, listener); } //------------------------------------------------------------------- // Set this Component's parent Frame pointer to some Cursor type. //------------------------------------------------------------------- public static void setFrameCursor(Component component, Cursor pointerType) { Component parent; parent = component; while ( parent != null ) { if ( parent instanceof Frame ) { parent.setCursor(pointerType); break; } parent = parent.getParent(); } } //------------------------------------------------------------------- // Self-test //------------------------------------------------------------------- public static void main (String[] args) { Frame window = new Frame("DirLister Self-Test"); DirLister dirLister = new DirLister(); dirLister.addActionListener(dirLister); // let DirLister dump own dir list window.add("Center", dirLister); window.pack(); window.setVisible(true); } } // End of Class DirLister