KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > DirectoryChooser


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34
35 package edu.rice.cs.util.swing;
36
37 import edu.rice.cs.util.FileOps;
38 import edu.rice.cs.util.swing.Utilities;
39
40 import java.io.File JavaDoc;
41
42 import javax.swing.JFileChooser JavaDoc;
43 import javax.swing.filechooser.FileFilter JavaDoc;
44 import javax.swing.filechooser.FileView JavaDoc;
45
46 import java.awt.Component JavaDoc;
47
48 public class DirectoryChooser extends JFileChooser JavaDoc {
49   
50   /** GUI component that owns the dialog (if any) for this directory chooser. */
51   protected Component JavaDoc _owner;
52   
53   /** File system root for chooser */
54   protected File JavaDoc _root;
55   
56   /** Creates a DirectoryChooser rooted at file system root, allowing only a single selection. */
57   public DirectoryChooser() { this(null, null, false, false); }
58   
59   /** Creates a DirectoryChooser rooted at the file system root, allowing only single selection. */
60   public DirectoryChooser(Component JavaDoc owner) { this(owner, null, false, false); }
61   
62   /** Creates a DirectoryChooser rooted at the file system root, allowing multiple selection as specified.
63    * @param allowMultiple whether to allow multiple selection
64    */

65   public DirectoryChooser(Component JavaDoc owner, boolean allowMultiple) { this(owner, null, allowMultiple, false); }
66   
67   /** Creates a DirectoryChooser with the given root, allowing only a single selection.
68    * @param root the root directory to display in the tree
69    */

70   public DirectoryChooser(Component JavaDoc owner, File JavaDoc root) { this(owner, root, false, false); }
71   
72   /** Creates a DirectoryChooser with the given root, allowing multiple selections as specified.
73    * @param root the root directory to display in the tree. If null, then show entire file system
74    * @param allowMultiple whether to allow multiple selection
75    */

76   public DirectoryChooser(Component JavaDoc owner, File JavaDoc root, boolean allowMultiple, boolean showHidden) {
77     /* This super call sets current directory to root if it is valid directory, root.parentFile() if it is a valid
78      * non-directory file, and the system default otherwise. */

79     super(root);
80     _init(owner, root, allowMultiple, showHidden);
81   }
82   
83   /*---------- INITIALIZATION METHODS ----------*/
84     
85   /** Sets up the GUI components of the dialog */
86   private void _init(Component JavaDoc owner, final File JavaDoc root, boolean allowMultiple, boolean showHidden) {
87     
88     
89 // if (root != null && root.exists()) {
90
// setFileView(new FileView() {
91
// public Boolean isTraversable(File f) {
92
// return Boolean.valueOf(f.isDirectory() && FileOps.inFileTree(f, root));
93
// }});
94
// }
95

96     _owner = owner;
97     _root = root; // may be null
98
if (root != null) {
99       if (! root.exists()) _root = null;
100       else if (! root.isDirectory()) _root = root.getParentFile();
101     }
102
103     setMultiSelectionEnabled(allowMultiple);
104     setFileHidingEnabled(! showHidden);
105     setFileSelectionMode(DIRECTORIES_ONLY);
106     setDialogType(CUSTOM_DIALOG);
107     setApproveButtonText("Select");
108     setFileFilter(new FileFilter JavaDoc() {
109       public boolean accept(File JavaDoc f) { return true; }
110       public String JavaDoc getDescription() { return "All Folders"; }
111     });
112   }
113   
114   public int showDialog(File JavaDoc initialSelection) {
115     setCurrentDirectory(initialSelection);
116     return showDialog(_owner, null); // null means leave the approve button text unchanged
117
}
118   
119   /** Set the owner of this DirectoryChooser. */
120   public void setOwner(Component JavaDoc owner) { _owner = owner; }
121   
122   /** Shows the dialog with the same selection as the last time the dialog was shown. If this is the first time it is
123    * shown, then the root is selected.
124    */

125   public int showDialog() { return showDialog(_owner, null); }
126   
127   /** returns which directories were selected in the tree
128    * @return an array of files for the selected directories
129    */

130   public File JavaDoc[] getSelectedDirectories() { return getSelectedFiles(); }
131   
132   /** returns which directory was selected in the tree
133    * @return the file for the selected directory, null if none selected
134    */

135   public File JavaDoc getSelectedDirectory() { return getSelectedFile(); }
136   
137 // public boolean isTraversable(File f) {
138
// if (_root == null) return super.isTraversable(f);
139
// Utilities.show("isTraversable(" + f + ") called; _root = " + _root);
140
// return f.isDirectory() && FileOps.inFileTree(f, _root);
141
// }
142
}
143
Popular Tags