2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
* Contributor(s): Soot Phengsy
43
package org.netbeans.swing.dirchooser;
46
import java.io.FileFilter;
47
import java.util.ArrayList;
48
import java.util.Collections;
49
import java.util.Comparator;
50
import javax.swing.JFileChooser;
51
import javax.swing.tree.DefaultMutableTreeNode;
52
import org.openide.filesystems.FileObject;
53
import org.openide.filesystems.FileUtil;
56
* A directory tree node.
58
* @author Soot Phengsy
60
public class DirectoryNode extends DefaultMutableTreeNode {
62
public final static int SINGLE_SELECTION = 0;
63
public final static int DIG_IN_SELECTION = 4;
64
/** case insensitive file name's comparator */
65
static final FileNameComparator FILE_NAME_COMPARATOR = new FileNameComparator();
66
private File directory;
67
private boolean isDir;
68
private boolean loaded;
69
private boolean isSelected;
71
public DirectoryNode(File file) {
72
this(file, true, false, false, false);
75
public DirectoryNode(File file, boolean allowsChildren) {
76
this(file, allowsChildren, false, false, false);
79
public DirectoryNode(File file, boolean allowsChildren, boolean isSelected,
80
boolean isChecked, boolean isEditable) {
81
super(file, allowsChildren);
82
this.directory = file;
83
this.isDir = directory.isDirectory();
84
this.isSelected = isSelected;
87
public boolean isLoaded() {
91
public File getFile() {
92
return this.directory;
95
public void setFile(File file) {
97
this.directory = file;
101
public void setSelected(boolean isSelected) {
102
this.isSelected = isSelected;
106
public boolean isLeaf() {
111
public boolean getAllowsChildren() {
115
public boolean isSelected() {
116
return this.isSelected;
119
public boolean loadChildren(JFileChooser chooser, boolean descend) {
121
if (loaded == false) {
123
ArrayList files = getFiles(chooser);
125
if (files.size() == 0) {
129
for (int i = 0; i < files.size(); i++) {
130
File child = (File) files.get(i);
132
if (chooser.accept(child)) {
134
DirectoryNode node = new DirectoryNode(child);
135
if (descend == false) {
139
} catch (NullPointerException t) {
145
if (descend == true || (getChildCount() > 0)) {
153
private ArrayList getFiles(JFileChooser chooser) {
155
ArrayList list = new ArrayList();
157
// Fix for IZ#116859 [60cat] Node update bug in the "open project" panel while deleting directories
158
if (directory == null || !directory.exists()) {
162
File[] files = chooser.getFileSystemView().getFiles(directory, chooser.isFileHidingEnabled());
163
int mode = chooser.getFileSelectionMode();
164
if (mode == JFileChooser.DIRECTORIES_ONLY) {
165
for (int i = 0; i < files.length; i++) {
166
File child = files[i];
167
if (child.isDirectory()) {
171
Collections.sort(list, FILE_NAME_COMPARATOR);
172
} else if (mode == JFileChooser.FILES_AND_DIRECTORIES || mode == JFileChooser.FILES_ONLY) {
173
ArrayList dirList = new ArrayList();
174
ArrayList fileList = new ArrayList();
175
for (int i = 0; i < files.length; i++) {
176
File child = files[i];
177
if (child.isDirectory()) {
184
Collections.sort(dirList, FILE_NAME_COMPARATOR);
185
Collections.sort(fileList, FILE_NAME_COMPARATOR);
187
list.addAll(dirList);
188
list.addAll(fileList);
194
public boolean isNetBeansProject() {
196
// return isNetBeansProject(directory);
199
/*public static boolean isNetBeansProject (File directory) {
200
boolean retVal = false;
201
if (directory != null) {
202
FileObject fo = convertToValidDir(directory);
204
if (Utilities.isUnix() && fo.getParent() != null
205
&& fo.getParent().getParent() == null) {
206
retVal = false; // Ignore all subfolders of / on unixes
207
// (e.g. /net, /proc)
209
retVal = ProjectManager.getDefault().isProject(fo);
215
private static FileObject convertToValidDir(File f) {
217
File testFile = new File(f.getPath());
218
if (testFile == null || testFile.getParent() == null) {
219
// BTW this means that roots of file systems can't be project
226
* ATTENTION: on Windows may occure dir.isDirectory () == dir.isFile () ==
228
* true then its used testFile instead of dir.
231
if (!testFile.isDirectory()) {
235
fo = FileUtil.toFileObject(FileUtil.normalizeFile(testFile));
239
private class DirectoryFilter implements FileFilter {
241
public boolean accept(File f) {
242
return f.isDirectory();
245
public String getDescription() {
250
/** Compares files ignoring case sensitivity */
251
private static class FileNameComparator implements Comparator<File> {
253
public int compare(File f1, File f2) {
254
return String.CASE_INSENSITIVE_ORDER.compare(f1.getName(), f2.getName());