|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DirectoryChooser.java | 16.7% | 60.7% | 53.8% | 53.2% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | ||
| 38 | package edu.rice.cs.util.swing; | |
| 39 | ||
| 40 | import java.io.File; | |
| 41 | ||
| 42 | import javax.swing.JFileChooser; | |
| 43 | import javax.swing.filechooser.FileFilter; | |
| 44 | ||
| 45 | import java.awt.Component; | |
| 46 | ||
| 47 | public class DirectoryChooser extends JFileChooser { | |
| 48 | ||
| 49 | /** GUI component that owns the dialog (if any) for this directory chooser. */ | |
| 50 | protected Component _owner; | |
| 51 | ||
| 52 | /** File system root for chooser */ | |
| 53 | protected File _root; | |
| 54 | ||
| 55 | /** Creates a DirectoryChooser rooted at file system root, allowing only a single selection. */ | |
| 56 | 38 | public DirectoryChooser() { this(null, null, false, false); } |
| 57 | ||
| 58 | /** Creates a DirectoryChooser rooted at the file system root, allowing only single selection. */ | |
| 59 | 114 | public DirectoryChooser(Component owner) { this(owner, null, false, false); } |
| 60 | ||
| 61 | /** Creates a DirectoryChooser rooted at the file system root, allowing multiple selection as specified. | |
| 62 | * @param allowMultiple whether to allow multiple selection | |
| 63 | */ | |
| 64 | 0 | public DirectoryChooser(Component owner, boolean allowMultiple) { this(owner, null, allowMultiple, false); } |
| 65 | ||
| 66 | /** Creates a DirectoryChooser with the given root, allowing only a single selection. | |
| 67 | * @param root the root directory to display in the tree | |
| 68 | */ | |
| 69 | 0 | public DirectoryChooser(Component owner, File root) { this(owner, root, false, false); } |
| 70 | ||
| 71 | /** Creates a DirectoryChooser with the given root, allowing multiple selections as specified. | |
| 72 | * @param root the root directory to display in the tree. If null, then show entire file system | |
| 73 | * @param allowMultiple whether to allow multiple selection | |
| 74 | */ | |
| 75 | 152 | public DirectoryChooser(Component owner, File root, boolean allowMultiple, boolean showHidden) { |
| 76 | /* This super call sets current directory to root if it is valid directory, root.parentFile() if it is a valid | |
| 77 | * non-directory file, and the system default otherwise. */ | |
| 78 | 152 | super(root); |
| 79 | 152 | _init(owner, root, allowMultiple, showHidden); |
| 80 | } | |
| 81 | ||
| 82 | /*---------- INITIALIZATION METHODS ----------*/ | |
| 83 | ||
| 84 | /** Sets up the GUI components of the dialog */ | |
| 85 | 152 | private void _init(Component owner, final File root, boolean allowMultiple, boolean showHidden) { |
| 86 | ||
| 87 | ||
| 88 | // if (root != null && root.exists()) { | |
| 89 | // setFileView(new FileView() { | |
| 90 | // public Boolean isTraversable(File f) { | |
| 91 | // return Boolean.valueOf(f.isDirectory() && FileOps.inFileTree(f, root)); | |
| 92 | // }}); | |
| 93 | // } | |
| 94 | ||
| 95 | 152 | _owner = owner; |
| 96 | 152 | _root = root; // may be null |
| 97 | 152 | if (root != null) { |
| 98 | 0 | if (! root.exists()) _root = null; |
| 99 | 0 | else if (! root.isDirectory()) _root = root.getParentFile(); |
| 100 | } | |
| 101 | ||
| 102 | 152 | setMultiSelectionEnabled(allowMultiple); |
| 103 | 152 | setFileHidingEnabled(! showHidden); |
| 104 | 152 | setFileSelectionMode(DIRECTORIES_ONLY); |
| 105 | 152 | setDialogType(CUSTOM_DIALOG); |
| 106 | 152 | setApproveButtonText("Select"); |
| 107 | 152 | setAcceptAllFileFilterUsed(false); |
| 108 | 152 | setFileFilter(new FileFilter() { |
| 109 | 6286 | public boolean accept(File f) { return true; } |
| 110 | 152 | public String getDescription() { return "All Folders"; } |
| 111 | }); | |
| 112 | } | |
| 113 | ||
| 114 | 0 | public int showDialog(File initialSelection) { |
| 115 | 0 | setCurrentDirectory(initialSelection); |
| 116 | 0 | return showDialog(_owner, null); // null means leave the approve button text unchanged |
| 117 | } | |
| 118 | ||
| 119 | /** Set the owner of this DirectoryChooser. */ | |
| 120 | 38 | public void setOwner(Component 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 | 0 | 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 | 0 | public File[] 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 | 0 | public File 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 | } |
|
||||||||||