2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - DIGITEO - Calixte DENIZET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.ui_data.filebrowser;
19 import java.util.Comparator;
20 import java.util.TreeSet;
21 import java.util.regex.Pattern;
22 import javax.swing.Icon;
23 import javax.swing.SortOrder;
24 import org.scilab.modules.commons.CommonFileUtils;
25 import org.scilab.modules.commons.ScilabConstants;
26 import org.scilab.modules.ui_data.utils.UiDataMessages;
29 * Handle a File as a node in the JTree
31 * @author Calixte DENIZET
33 public class FileNode {
35 public static final int NAMEORDER = 1;
36 public static final int SIZEORDER = 2;
37 public static final int DATEORDER = 3;
38 public static final int TYPEORDER = 4;
40 private static final File userHomeDir = new File(ScilabConstants.USERHOME);
41 private static final File SCIDIR = new File(System.getenv("SCI"));
43 private static Comparator<FileNode> comparator = FileComparators.ASCENDING_ALPHA;
47 protected FileNode[] children;
48 protected long lastModified;
49 protected boolean isFile;
50 protected boolean canRead;
51 protected boolean canWrite;
52 protected boolean isEmpty;
53 protected boolean userHome;
54 protected boolean sci;
55 protected long length;
56 protected String name;
57 protected String extension;
58 private int order = 1;
65 * @param file the file in this node
67 public FileNode(File file, int position) {
68 this(file, file.getName(), file.canRead(), file.isFile());
69 this.position = position;
72 public FileNode(File file, String name, boolean canRead, boolean isFile) {
76 this.canRead = canRead;
77 this.canWrite = file.canWrite();
78 this.lastModified = file.lastModified();
79 this.userHome = file.equals(userHomeDir);
81 this.length = file.length();
83 this.extension = FileUtils.getFileExtension(file);
84 this.sci = file.equals(SCIDIR);
85 this.isEmpty = isFile || CommonFileUtils.isEmptyDirectory(file.getAbsolutePath()) == 1;
88 public int getPosition() {
92 /** @param pat the pattern to filter the files */
93 public void setFilter(Pattern pat) {
99 * @param order the order to use to sort the file. If order is positive, then ascending order is
102 public void setOrder(int order) {
106 comparator = FileComparators.ASCENDING_ALPHA;
109 comparator = FileComparators.DESCENDING_ALPHA;
112 comparator = FileComparators.ASCENDING_TYPE;
115 comparator = FileComparators.DESCENDING_TYPE;
118 comparator = FileComparators.ASCENDING_DATE;
121 comparator = FileComparators.DESCENDING_DATE;
124 comparator = FileComparators.ASCENDING_SIZE;
127 comparator = FileComparators.DESCENDING_SIZE;
133 * @param order the order to use to sort the file.
134 * @param ascending if true, the ascending order is used
136 public void setOrder(int order, boolean ascending) {
137 int sign = ascending ? +1 : -1;
138 setOrder(sign * order);
141 /** @return the used order */
142 public SortOrder getOrder() {
143 return order > 0 ? SortOrder.ASCENDING : SortOrder.DESCENDING;
147 * Sort a column according to the natural order for its.
149 * @param nameColumn the column name
151 public void toggleSortOrder(String nameColumn) {
152 if (nameColumn.equals(UiDataMessages.NAME_COLUMN)) {
153 if (Math.abs(order) == NAMEORDER) {
158 } else if (nameColumn.equals(UiDataMessages.TYPE_COLUMN)) {
159 if (Math.abs(order) == TYPEORDER) {
164 } else if (nameColumn.equals(UiDataMessages.SIZE_COLUMN)) {
165 if (Math.abs(order) == SIZEORDER) {
170 } else if (nameColumn.equals(UiDataMessages.LASTMODIF_COLUMN)) {
171 if (Math.abs(order) == DATEORDER) {
182 * @param order the order to use
183 * @param files the files to order
184 * @return the ordered FileNodes
186 protected void orderFiles() {
187 if (children != null) {
188 TreeSet<FileNode> set = new TreeSet<FileNode>(comparator);
189 for (FileNode fn : children) {
193 children = set.toArray(children);
194 for (int i = 0; i < children.length; i++) {
195 children[i].position = i;
200 /** Returns the string to be used to display this leaf in the JTree. */
201 public String toString() {
202 String name = file.getName();
203 if (name.isEmpty()) {
204 name = file.toString();
209 /** @return the file associated with this node */
210 public File getFile() {
214 /** @return true if this represents the user-home directory */
215 public boolean isUserHome() {
219 /** @return true if this represents the SCI directory */
220 public boolean isSCI() {
224 /** @return the last modified time for this file */
225 public long getLastModified() {
229 /** @return the icon associated with this file */
230 public Icon getIcon() {
232 icon = FileUtils.getIconForFile(file);
238 /** @return true if the file is not a directory or if it is an empty one */
239 public boolean isLeaf() {
243 /** @return the number of files in the directory representated by this file */
244 public int getChildrenCount() {
246 synchronized (file) {
247 if (children == null) {
248 children = listFiles();
253 if (children != null) {
254 return children.length;
260 /** @return the children FileNode of this FileNode */
261 protected Object[] getChildren() {
262 if (children == null && !isEmpty) {
263 children = listFiles();
269 public FileNode[] getRawChildren() {
273 public FileNode[] listFiles() {
274 String[] filesName = file.list();
275 if (filesName != null) {
276 TreeSet<FileNode> nodes = new TreeSet<FileNode>(comparator);
277 for (String fileName : filesName) {
278 File f = new File(file, fileName);
279 if (pat != null && !pat.matcher(fileName).matches()) {
285 boolean canRead = f.canRead();
289 boolean isFile = f.isFile();
290 if (!isFile && !f.isDirectory()) {
293 nodes.add(new FileNode(f, fileName, canRead, isFile));
296 FileNode[] fnodes = new FileNode[nodes.size()];
297 fnodes = nodes.toArray(fnodes);
299 for (int i = 0; i < fnodes.length; i++) {
300 fnodes[i].position = i;
309 /** Reset children only */
310 public void resetChildren() {
312 isEmpty = isFile || CommonFileUtils.isEmptyDirectory(file.getAbsolutePath()) == 1;
316 public boolean equals(Object o) {
317 return (o instanceof FileNode) && ((FileNode) o).file.equals(file);
321 public int hashCode() {
322 return file.hashCode();