001: /*
002: * @(#)FolderChooser.java 10/9/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import com.jidesoft.plaf.LookAndFeelFactory;
009: import com.jidesoft.plaf.UIDefaultsLookup;
010:
011: import javax.swing.*;
012: import javax.swing.filechooser.FileSystemView;
013: import java.io.File;
014: import java.util.List;
015:
016: /**
017: * <code>FolderChooser</code> provides a simple mechanism for the user to
018: * choose a folder.
019: * <p/>
020: * In addition to supporting the basic folder choosing function, it also supports create new folder, delete an existing
021: * folder. Another useful feature is recent list. It allows you to set a list of recent selected folders so that user
022: * can choose them directly inatead of navigating to it in the file system tree.
023: * <p/>
024: * The following code pops up a folder chooser for user to choose a folder.
025: * <pre>
026: * FolderChooser chooser = new FolderChooser();
027: * int returnVal = chooser.showOpenDialog(parent);
028: * if(returnVal == FolderChooser.APPROVE_OPTION) {
029: * System.out.println("You chose to open this file: " +
030: * chooser.getSelectedFile().getName());
031: * }
032: * </pre>
033: */
034: public class FolderChooser extends JFileChooser {
035:
036: private static final String uiClassID = "FolderChooserUI";
037:
038: private List<String> _recentList;
039:
040: public final static String PROPERTY_RECENTLIST = "recentList";
041:
042: public FolderChooser() {
043: }
044:
045: public FolderChooser(String currentDirectoryPath) {
046: super (currentDirectoryPath);
047: }
048:
049: public FolderChooser(File currentDirectory) {
050: super (currentDirectory);
051: }
052:
053: public FolderChooser(FileSystemView fsv) {
054: super (fsv);
055: }
056:
057: public FolderChooser(File currentDirectory, FileSystemView fsv) {
058: super (currentDirectory, fsv);
059: }
060:
061: public FolderChooser(String currentDirectoryPath, FileSystemView fsv) {
062: super (currentDirectoryPath, fsv);
063: }
064:
065: /**
066: * Gets recent selected folder list. The element in the list is {@link File}.
067: *
068: * @return the recent selected folder list.
069: */
070: public List<String> getRecentList() {
071: return _recentList;
072: }
073:
074: /**
075: * Sets the recent folder list. The element in the list should be {@link File}.
076: * Property change event on {@link FolderChooser#PROPERTY_RECENTLIST} will be fired when recent folder list is changed.
077: *
078: * @param recentList the recent folder list.
079: */
080: public void setRecentList(List<String> recentList) {
081: List<String> old = _recentList;
082: _recentList = recentList;
083: firePropertyChange(PROPERTY_RECENTLIST, old, recentList);
084: }
085:
086: /**
087: * Resets the UI property to a value from the current look and
088: * feel.
089: *
090: * @see JComponent#updateUI
091: */
092: @Override
093: public void updateUI() {
094: if (UIDefaultsLookup.get(uiClassID) == null) {
095: LookAndFeelFactory.installJideExtension();
096: }
097: setUI(UIManager.getUI(this ));
098: }
099:
100: /**
101: * Returns a string that specifies the name of the L&F class
102: * that renders this component.
103: *
104: * @return the string "FolderChooserUI"
105: * @see JComponent#getUIClassID
106: * @see UIDefaults#getUI
107: */
108: @Override
109: public String getUIClassID() {
110: return uiClassID;
111: }
112:
113: // we have to remove these two overridden method because it causes problem in
114: // JFileChooser's setSelectedFile method where setCurrentDirectory
115: // is called with selected file's parent folder.
116:
117: // /**
118: // * Current directory concept doesn't make sense in the case of FolderChooser. So we
119: // * override this method of JFileChooser and delegate to {@link #setSelectedFile(java.io.File)}.
120: // *
121: // * @param dir
122: // */
123: // public void setCurrentDirectory(File dir) {
124: // super.setSelectedFile(dir);
125: // }
126: //
127: // /**
128: // * Current directory concept doesn't make sense in the case of FolderChooser. So we
129: // * override this method of JFileChooser and delegate to {@link #getSelectedFile()}.
130: // *
131: // * @return the selected folder.
132: // */
133: // public File getCurrentDirectory() {
134: // return super.getSelectedFile();
135: // }
136: }
|