1 19 20 package org.openide.loaders; 21 22 import java.util.Date ; 23 import java.util.Enumeration ; 24 import org.openide.filesystems.FileObject; 25 import org.openide.nodes.Node; 26 27 31 class FolderComparator extends DataFolder.SortMode { 32 33 public static final int NONE = 0; 34 public static final int NAMES = 1; 35 public static final int CLASS = 2; 36 39 public static final int FOLDER_NAMES = 3; 40 41 public static final int LAST_MODIFIED = 4; 42 43 public static final int SIZE = 5; 44 45 46 47 private int mode; 48 49 51 public FolderComparator () { 52 this (FOLDER_NAMES); 53 } 54 55 58 public FolderComparator (int mode) { 59 this.mode = mode; 60 } 61 62 65 public int compare (Object o1, Object o2) { 66 DataObject obj1; 67 DataObject obj2; 68 69 if (o1 instanceof Node) { 70 obj1 = (DataObject)((Node)o1).getCookie (DataObject.class); 71 obj2 = (DataObject)((Node)o2).getCookie (DataObject.class); 72 } else { 73 obj1 = (DataObject)o1; 74 obj2 = (DataObject)o2; 75 } 76 77 switch (mode) { 78 case NONE: 79 return 0; 80 case NAMES: 81 return compareNames (obj1, obj2); 82 case CLASS: 83 return compareClass (obj1, obj2); 84 case FOLDER_NAMES: 85 return compareFoldersFirst (obj1, obj2); 86 case LAST_MODIFIED: 87 return compareLastModified(obj1, obj2); 88 case SIZE: 89 return compareSize(obj1, obj2); 90 default: 91 assert false : mode; 92 return 0; 93 } 94 } 95 96 97 98 private int compareNames (DataObject obj1, DataObject obj2) { 99 int part = obj1.getName().compareTo(obj2.getName()); 102 return part != 0 ? part : 103 obj1.getPrimaryFile().getExt().compareTo(obj2.getPrimaryFile().getExt()); 104 } 105 106 107 private int compareFoldersFirst (DataObject obj1, DataObject obj2) { 108 if (obj1.getClass () != obj2.getClass ()) { 109 if (obj1 instanceof DataFolder) { 111 return -1; 112 } 113 if (obj2 instanceof DataFolder) { 114 return 1; 115 } 116 } 117 118 return compareNames(obj1, obj2); 120 } 121 122 123 private int compareClass (DataObject obj1, DataObject obj2) { 124 Class <?> c1 = obj1.getClass (); 125 Class <?> c2 = obj2.getClass (); 126 127 if (c1 == c2) { 128 return compareNames(obj1, obj2); 129 } 130 131 DataLoaderPool dlp = DataLoaderPool.getDefault(); 133 final Enumeration loaders = dlp.allLoaders (); 134 135 while (loaders.hasMoreElements ()) { 137 Class <? extends DataObject> clazz = ((DataLoader) (loaders.nextElement ())).getRepresentationClass (); 138 139 if (clazz == DataObject.class) continue; 144 145 boolean r1 = clazz.isAssignableFrom (c1); 146 boolean r2 = clazz.isAssignableFrom (c2); 147 148 if (r1 && r2) return compareNames(obj1, obj2); 149 if (r1) return -1; 150 if (r2) return 1; 151 } 152 return compareNames(obj1, obj2); 153 } 154 155 158 private static int compareLastModified(DataObject obj1, DataObject obj2) { 159 if (obj1 instanceof DataFolder) { 160 if (obj2 instanceof DataFolder) { 161 return obj1.getName().compareTo(obj2.getName()); 162 } else { 163 return -1; 164 } 165 } else { 166 if (obj2 instanceof DataFolder) { 167 return 1; 168 } else { 169 FileObject fo1 = obj1.getPrimaryFile(); 170 FileObject fo2 = obj2.getPrimaryFile(); 171 Date d1 = fo1.lastModified(); 172 Date d2 = fo2.lastModified(); 173 if (d1.after(d2)) { 174 return -1; 175 } else if (d2.after(d1)) { 176 return 1; 177 } else { 178 return fo1.getNameExt().compareTo(fo2.getNameExt()); 179 } 180 } 181 } 182 } 183 184 187 private static int compareSize(DataObject obj1, DataObject obj2) { 188 if (obj1 instanceof DataFolder) { 189 if (obj2 instanceof DataFolder) { 190 return obj1.getName().compareTo(obj2.getName()); 191 } else { 192 return -1; 193 } 194 } else { 195 if (obj2 instanceof DataFolder) { 196 return 1; 197 } else { 198 FileObject fo1 = obj1.getPrimaryFile(); 199 FileObject fo2 = obj2.getPrimaryFile(); 200 long s1 = fo1.getSize(); 201 long s2 = fo2.getSize(); 202 if (s1 > s2) { 203 return -1; 204 } else if (s2 > s1) { 205 return 1; 206 } else { 207 return fo1.getNameExt().compareTo(fo2.getNameExt()); 208 } 209 } 210 } 211 } 212 213 } 214 | Popular Tags |