~mathieu.bastian/gephi/geolayout

« back to all changes in this revision

Viewing changes to DirectoryChooser/src/org/netbeans/swing/dirchooser/DirectoryNode.java

  • Committer: Mathieu Bastian
  • Date: 2009-09-13 20:25:12 UTC
  • Revision ID: mathieu.bastian@gmail.com-20090913202512-mb69uewzecob6ohn
Add Netbeans Directory Chooser.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 *
 
41
 * Contributor(s): Soot Phengsy
 
42
 */
 
43
package org.netbeans.swing.dirchooser;
 
44
 
 
45
import java.io.File;
 
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;
 
54
 
 
55
/**
 
56
 * A directory tree node.
 
57
 *
 
58
 * @author Soot Phengsy
 
59
 */
 
60
public class DirectoryNode extends DefaultMutableTreeNode {
 
61
 
 
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;
 
70
 
 
71
    public DirectoryNode(File file) {
 
72
        this(file, true, false, false, false);
 
73
    }
 
74
 
 
75
    public DirectoryNode(File file, boolean allowsChildren) {
 
76
        this(file, allowsChildren, false, false, false);
 
77
    }
 
78
 
 
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;
 
85
    }
 
86
 
 
87
    public boolean isLoaded() {
 
88
        return this.loaded;
 
89
    }
 
90
 
 
91
    public File getFile() {
 
92
        return this.directory;
 
93
    }
 
94
 
 
95
    public void setFile(File file) {
 
96
        setUserObject(file);
 
97
        this.directory = file;
 
98
        this.loaded = false;
 
99
    }
 
100
 
 
101
    public void setSelected(boolean isSelected) {
 
102
        this.isSelected = isSelected;
 
103
    }
 
104
 
 
105
    @Override
 
106
    public boolean isLeaf() {
 
107
        return !this.isDir;
 
108
    }
 
109
 
 
110
    @Override
 
111
    public boolean getAllowsChildren() {
 
112
        return this.isDir;
 
113
    }
 
114
 
 
115
    public boolean isSelected() {
 
116
        return this.isSelected;
 
117
    }
 
118
 
 
119
    public boolean loadChildren(JFileChooser chooser, boolean descend) {
 
120
        //fixed bug #97124
 
121
        if (loaded == false) {
 
122
 
 
123
            ArrayList files = getFiles(chooser);
 
124
 
 
125
            if (files.size() == 0) {
 
126
                return false;
 
127
            }
 
128
 
 
129
            for (int i = 0; i < files.size(); i++) {
 
130
                File child = (File) files.get(i);
 
131
 
 
132
                if (chooser.accept(child)) {
 
133
                    try {
 
134
                        DirectoryNode node = new DirectoryNode(child);
 
135
                        if (descend == false) {
 
136
                            break;
 
137
                        }
 
138
                        add(node);
 
139
                    } catch (NullPointerException t) {
 
140
                        t.printStackTrace();
 
141
                    }
 
142
                }
 
143
            }
 
144
 
 
145
            if (descend == true || (getChildCount() > 0)) {
 
146
                loaded = true;
 
147
            }
 
148
        }
 
149
 
 
150
        return loaded;
 
151
    }
 
152
 
 
153
    private ArrayList getFiles(JFileChooser chooser) {
 
154
        //fixed bug #97124
 
155
        ArrayList list = new ArrayList();
 
156
 
 
157
        // Fix for IZ#116859 [60cat] Node update bug in the "open project" panel while deleting directories
 
158
        if (directory == null || !directory.exists()) {
 
159
            return list;
 
160
        }
 
161
 
 
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()) {
 
168
                    list.add(child);
 
169
                }
 
170
            }
 
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()) {
 
178
                    dirList.add(child);
 
179
                } else {
 
180
                    fileList.add(child);
 
181
                }
 
182
            }
 
183
 
 
184
            Collections.sort(dirList, FILE_NAME_COMPARATOR);
 
185
            Collections.sort(fileList, FILE_NAME_COMPARATOR);
 
186
 
 
187
            list.addAll(dirList);
 
188
            list.addAll(fileList);
 
189
        }
 
190
 
 
191
        return list;
 
192
    }
 
193
 
 
194
    public boolean isNetBeansProject() {
 
195
        return false;
 
196
//        return isNetBeansProject(directory);
 
197
    }
 
198
 
 
199
    /*public static boolean isNetBeansProject (File directory) {
 
200
    boolean retVal = false;
 
201
    if (directory != null) {
 
202
    FileObject fo = convertToValidDir(directory);
 
203
    if (fo != null) {
 
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)
 
208
    } else {
 
209
    retVal = ProjectManager.getDefault().isProject(fo);
 
210
    }
 
211
    }
 
212
    }
 
213
    return retVal;
 
214
    }*/
 
215
    private static FileObject convertToValidDir(File f) {
 
216
        FileObject fo;
 
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
 
220
            // directories.
 
221
            return null;
 
222
        }
 
223
 
 
224
        /**
 
225
         *
 
226
         * ATTENTION: on Windows may occure dir.isDirectory () == dir.isFile () ==
 
227
         *
 
228
         * true then its used testFile instead of dir.
 
229
         *
 
230
         */
 
231
        if (!testFile.isDirectory()) {
 
232
            return null;
 
233
        }
 
234
 
 
235
        fo = FileUtil.toFileObject(FileUtil.normalizeFile(testFile));
 
236
        return fo;
 
237
    }
 
238
 
 
239
    private class DirectoryFilter implements FileFilter {
 
240
 
 
241
        public boolean accept(File f) {
 
242
            return f.isDirectory();
 
243
        }
 
244
 
 
245
        public String getDescription() {
 
246
            return "Directory";
 
247
        }
 
248
    }
 
249
 
 
250
    /** Compares files ignoring case sensitivity */
 
251
    private static class FileNameComparator implements Comparator<File> {
 
252
 
 
253
        public int compare(File f1, File f2) {
 
254
            return String.CASE_INSENSITIVE_ORDER.compare(f1.getName(), f2.getName());
 
255
        }
 
256
    }
 
257
}