Wednesday, May 4, 2011

DirectoryService


Listing 1:

package filechooser;
import java.io.File;
import java.util.List;

public interface DirectoryService {
  public final static int TYPE_LEAGUE = 0;
  public final static int TYPE_TEAM = 1;
  public final static int TYPE_PLAYER = 2;

/**
 * Returns the children of the parent directory.
 */
  public File[] getChildren(File aDir);

/**
 * Returns the root (in this case, Baseball).
 */
  public File getRoot();

/**
 * Returns the type (for example, Player).
 */
  public int getType(File aFile);

/**
 * Returns whether the file is traversable.
 */
  public boolean isTraversable(File aFile);

/**
 * Creates a new folder in the containing directory.
 */
  public File createNewFolder(File aContainingDir);

/**
 * Determines if the file should be displayed
 * based on the current filter.
 */
  public boolean acceptFilter(File aFile,
                          String aCurrentFilter);
}


Listing 2:

package filechooser;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * This class provides a view into a generic
 * directory service.
 */
public class DirectoryServiceFileSystemView
                          extends FileSystemView {

  private DirectoryService directoryService;

  public DirectoryServiceFileSystemView(
                          DirectoryService
                          aDirectoryService){

    this.directoryService = aDirectoryService;
  }

/**
 * Creates a new folder with a default folder name.
 * This method calls into the currently opened
 * directory service to obtain the directory service
 * system specific way to create a new folder.
 */
  public File createNewFolder(File aContainingDir)
                              throws IOException {
    File f = directoryService.
                  createNewFolder(aContainingDir);
    return f;
  }

/**
 * Returns all root partitions on this system.
 */
  public File[] getRoots() {
    File[] arrFiles = new File[1];
    arrFiles[0] = directoryService.getRoot();
    return arrFiles;
  }

/**
 * Returns whether a file is hidden or not.
 * In the current system, there is no concept of
 * a hidden file.
 */
  public boolean isHiddenFile(File f) {
    return false;
  }

/**
 * Determines if the file is a root partition.
 */
  public boolean isRoot(File f) {
    File root = directoryService.getRoot();
    String rootName = root.getName();
    String fName = f.getName();
    return (rootName.equals(fName));
  }

/**
 * Returns the user's home directory.
 * The concept of a user's home directory
 * does not directly map to the Directory Service,
 * so, as a result, the root is always returned.
 */
  public File getHomeDirectory() {
    return directoryService.getRoot();
  }

/**
 * Gets the list of files. Invoked internally
 * by the file chooser when the directory
 * is changed.
 */
  public File[] getFiles(File dir,
                         boolean useFileHiding){
    File[] arrFiles =
        directoryService.getChildren(dir);
    return arrFiles;
  }
}

No comments:

Post a Comment