View Javadoc
1   package org.argeo.eclipse.ui.fs;
2   
3   import java.io.IOException;
4   import java.nio.file.DirectoryIteratorException;
5   import java.nio.file.DirectoryStream;
6   import java.nio.file.Files;
7   import java.nio.file.Path;
8   import java.util.ArrayList;
9   import java.util.Arrays;
10  import java.util.List;
11  
12  /** Centralise additional utilitary methods to manage Java7 NIO files */
13  public class FsUiUtils {
14  
15  	/**
16  	 * thanks to
17  	 * http://programming.guide/java/formatting-byte-size-to-human-readable-format.html
18  	 */
19  	public static String humanReadableByteCount(long bytes, boolean si) {
20  		int unit = si ? 1000 : 1024;
21  		if (bytes < unit)
22  			return bytes + " B";
23  		int exp = (int) (Math.log(bytes) / Math.log(unit));
24  		String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
25  		return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
26  	}
27  
28  	public static Path[] getChildren(Path parent, String filter, boolean showHiddenItems, boolean folderFirst,
29  			String orderProperty, boolean reverseOrder) {
30  		if (!Files.isDirectory(parent))
31  			return null;
32  		List<Pair> pairs = new ArrayList<>();
33  		try (DirectoryStream<Path> stream = Files.newDirectoryStream(parent, filter)) {
34  			loop: for (Path entry : stream) {
35  				if (!showHiddenItems)
36  					if (Files.isHidden(entry))
37  						continue loop;
38  				switch (orderProperty) {
39  				case FsUiConstants.PROPERTY_SIZE:
40  					if (folderFirst)
41  						pairs.add(new LPair(entry, Files.size(entry), Files.isDirectory(entry)));
42  					else
43  						pairs.add(new LPair(entry, Files.size(entry)));
44  					break;
45  				case FsUiConstants.PROPERTY_LAST_MODIFIED:
46  					if (folderFirst)
47  						pairs.add(new LPair(entry, Files.getLastModifiedTime(entry).toMillis(),
48  								Files.isDirectory(entry)));
49  					else
50  						pairs.add(new LPair(entry, Files.getLastModifiedTime(entry).toMillis()));
51  					break;
52  				case FsUiConstants.PROPERTY_NAME:
53  					if (folderFirst)
54  						pairs.add(new SPair(entry, entry.getFileName().toString(), Files.isDirectory(entry)));
55  					else
56  						pairs.add(new SPair(entry, entry.getFileName().toString()));
57  					break;
58  				default:
59  					throw new FsUiException("Unable to prepare sort for property " + orderProperty);
60  				}
61  			}
62  			Pair[] rows = pairs.toArray(new Pair[0]);
63  			Arrays.sort(rows);
64  			Path[] results = new Path[rows.length];
65  			if (reverseOrder) {
66  				int j = rows.length - 1;
67  				for (int i = 0; i < rows.length; i++)
68  					results[i] = rows[j - i].p;
69  			} else
70  				for (int i = 0; i < rows.length; i++)
71  					results[i] = rows[i].p;
72  			return results;
73  		} catch (IOException | DirectoryIteratorException e) {
74  			throw new FsUiException("Unable to filter " + parent + " children with filter " + filter, e);
75  		}
76  	}
77  
78  	static abstract class Pair implements Comparable<Object> {
79  		Path p;
80  		Boolean i;
81  	};
82  
83  	static class LPair extends Pair {
84  		long v;
85  
86  		public LPair(Path path, long propValue) {
87  			p = path;
88  			v = propValue;
89  		}
90  
91  		public LPair(Path path, long propValue, boolean isDir) {
92  			p = path;
93  			v = propValue;
94  			i = isDir;
95  		}
96  
97  		public int compareTo(Object o) {
98  			if (i != null) {
99  				Boolean j = ((LPair) o).i;
100 				if (i.booleanValue() != j.booleanValue())
101 					return i.booleanValue() ? -1 : 1;
102 			}
103 			long u = ((LPair) o).v;
104 			return v < u ? -1 : v == u ? 0 : 1;
105 		}
106 	};
107 
108 	static class SPair extends Pair {
109 		String v;
110 
111 		public SPair(Path path, String propValue) {
112 			p = path;
113 			v = propValue;
114 		}
115 
116 		public SPair(Path path, String propValue, boolean isDir) {
117 			p = path;
118 			v = propValue;
119 			i = isDir;
120 		}
121 
122 		public int compareTo(Object o) {
123 			if (i != null) {
124 				Boolean j = ((SPair) o).i;
125 				if (i.booleanValue() != j.booleanValue())
126 					return i.booleanValue() ? -1 : 1;
127 			}
128 			String u = ((SPair) o).v;
129 			return v.compareTo(u);
130 		}
131 	};
132 }