1 16 package net.sf.jftp.gui.base.dir; 17 18 import net.sf.jftp.*; 19 import net.sf.jftp.config.Settings; 20 import net.sf.jftp.gui.framework.*; 21 import net.sf.jftp.net.*; 22 import net.sf.jftp.system.LocalIO; 23 import net.sf.jftp.util.*; 24 25 import java.awt.event.*; 26 27 import java.io.*; 28 29 import java.net.*; 30 31 import java.text.*; 32 33 import java.util.*; 34 35 36 public class DirLister implements ActionListener 37 { 38 private int length; 39 private String [] files; 40 private String [] sizes; 41 private int[] perms; 42 private boolean isDirectory = true; 43 public boolean finished = false; 44 private BasicConnection con; 45 private String sortMode = null; 46 private Date[] dates = null; 47 48 public DirLister(BasicConnection con) { 50 this.con = con; 51 init(); 52 } 53 54 public DirLister(BasicConnection con, String sortMode) { 56 this.con = con; 57 this.sortMode = sortMode; 58 init(); 59 } 60 61 public DirLister(BasicConnection con, String sortMode, boolean hide) { 63 this.con = con; 64 this.sortMode = sortMode; 65 init(); 66 67 int cnt = files.length; 68 69 if(hide) { 70 for(int i=0; i<files.length; i++) { 71 if(files[i].startsWith(".") && !files[i].startsWith("..")) { 72 files[i] = null; 73 cnt--; 74 } 75 } 76 77 String newFiles[] = new String [cnt]; 78 String newSizes[] = new String [cnt]; 79 int newPerms[] = new int[cnt]; 80 81 int idx = 0; 82 for(int i=0; i<files.length; i++) { 83 if(files[i] == null) { 84 continue; 85 } 86 else { 87 newFiles[idx] = files[i]; 88 newSizes[idx] = sizes[i]; 89 newPerms[idx] = perms[i]; 90 idx++; 91 } 92 } 93 94 files = newFiles; 95 sizes = newSizes; 96 perms = newPerms; 97 length = files.length; 98 } 99 } 100 101 public void init() 102 { 103 try 104 { 105 String outfile = Settings.ls_out; 106 107 con.list(); 109 files = con.sortLs(); 110 sizes = con.sortSize(); 111 112 119 length = files.length; 120 perms = con.getPermissions(); 121 isDirectory = true; 122 123 132 if(sortMode != null) 133 { 134 if(!sortMode.equals("Date")) 135 { 136 sortFirst(); 138 } 139 140 sort(sortMode); 141 } 142 else if(sortMode == null) 143 { 144 sortFirst(); 146 } 147 } 148 catch(Exception ex) 149 { 150 ex.printStackTrace(); 151 isDirectory = false; 152 } 153 154 finished = true; 155 } 156 157 private void sort(String type) 158 { 159 Vector fv = new Vector(); 160 Vector sv = new Vector(); 161 Vector pv = new Vector(); 162 163 if(type.equals("Reverse")) 164 { 165 for(int i = 0; i < length; i++) 166 { 167 fv.add(files[i]); 168 sv.add(sizes[i]); 169 170 if(perms != null) 171 { 172 pv.add(new Integer (perms[i])); 173 } 174 } 175 176 Collections.sort(fv, Collections.reverseOrder()); 177 178 Object [] filesTmp = fv.toArray(); 181 Object [] sizesTmp = sv.toArray(); 182 Object [] permsTmp = null; 183 184 if(perms != null) 185 { 186 permsTmp = pv.toArray(); 187 } 188 189 for(int i = 0; i < length; i++) 190 { 191 files[i] = (String ) filesTmp[i]; 192 sizes[i] = (String ) sizesTmp[length - i - 1]; 193 194 if(perms != null) 195 { 196 perms[i] = ((Integer ) permsTmp[length - i - 1]).intValue(); 197 } 198 } 199 } 200 else if(type.startsWith("Size")) 201 { 202 int cnt = 0; 203 Hashtable processed = new Hashtable(); 204 boolean reverse = false; 205 206 if(type.endsWith("/Re")) 207 { 208 reverse = true; 209 } 210 211 while(cnt < length) 212 { 213 int idx = 0; 214 double current = 0; 215 216 if(reverse) 217 { 218 current = Double.MAX_VALUE; 219 } 220 221 for(int i = 0; i < length; i++) 222 { 223 if(processed.containsKey("" + i)) 224 { 225 continue; 226 } 227 228 int si = Integer.parseInt(sizes[i]); 229 230 if(!reverse && (si >= current)) 231 { 232 idx = i; 234 current = si; 235 } 236 else if(reverse && (si <= current)) 237 { 238 idx = i; 239 current = si; 240 } 241 } 242 243 processed.put("" + idx, "" + sizes[idx]); 244 fv.add(files[idx]); 245 sv.add(sizes[idx]); 246 247 if(perms != null) 249 { 250 pv.add(new Integer (perms[idx])); 251 } 252 253 cnt++; 254 } 255 256 for(int i = 0; i < length; i++) 257 { 258 files[i] = (String ) fv.elementAt(i); 259 sizes[i] = (String ) sv.elementAt(i); 260 261 if(perms != null) 262 { 263 perms[i] = ((Integer ) pv.elementAt(i)).intValue(); 264 } 265 } 266 } 267 else if(type.equals("Date")) 268 { 269 String style = "ftp"; 270 271 if(!(con instanceof FtpConnection) || 273 (((FtpConnection) con).dateVector == null) || 274 (((FtpConnection) con).dateVector.size() < 1)) 275 { 276 if(!(con instanceof FilesystemConnection) || 277 (((FilesystemConnection) con).dateVector == null) || 278 (((FilesystemConnection) con).dateVector.size() < 1)) 279 { 280 } 281 else 282 { 283 style = "file"; 284 } 285 } 286 287 Object [] date = null; 288 289 if(style.equals("ftp")) 290 { 291 date = ((FtpConnection) con).dateVector.toArray(); 292 293 299 } 300 else 301 { 302 date = ((FilesystemConnection) con).dateVector.toArray(); 303 } 304 305 for(int j = 0; j < date.length; j++) 306 { 307 for(int i = 0; i < date.length; i++) 308 { 309 Date x = (Date) date[i]; 310 311 if(i == (date.length - 1)) 312 { 313 break; 314 } 315 316 if(comp(x, (Date) date[i + 1])) 317 { 318 Date swp = (Date) date[i + 1]; 320 date[i + 1] = x; 321 date[i] = swp; 322 323 String s1 = files[i + 1]; 324 String s2 = files[i]; 325 files[i] = s1; 326 files[i + 1] = s2; 327 328 s1 = sizes[i + 1]; 330 s2 = sizes[i]; 331 sizes[i] = s1; 332 sizes[i + 1] = s2; 333 334 int s3 = perms[i + 1]; 335 int s4 = perms[i]; 336 perms[i] = s3; 337 perms[i + 1] = s4; 338 } 339 } 340 } 341 342 dates = new Date[date.length]; 343 344 for(int i = 0; i < dates.length; i++) 345 { 346 dates[i] = (Date) date[i]; 347 } 348 349 362 } 363 else if(type.equals("Normal")) 364 { 365 } 367 } 368 369 private boolean comp(Date one, Date two) 370 { 371 Calendar c = new GregorianCalendar(); 372 c.clear(); 373 c.setTime(one); 374 375 Calendar c2 = new GregorianCalendar(); 376 c2.clear(); 377 c2.setTime(two); 378 379 if(c.getTime().compareTo(c2.getTime()) > 0) { 381 return true; 382 } 383 else 384 { 385 return false; 386 } 387 } 388 389 public void sortFirst() 390 { 391 String [] tmpx = new String [length]; 392 393 for(int x = 0; x < length; x++) 394 { 395 if(perms != null) 396 { 397 tmpx[x] = files[x] + "@@@" + sizes[x] + "@@@" + perms[x]; 398 399 } 401 else 402 { 403 tmpx[x] = files[x] + "@@@" + sizes[x]; 404 } 405 } 406 407 LocalIO.sortStrings(tmpx); 408 409 for(int y = 0; y < length; y++) 410 { 411 files[y] = tmpx[y].substring(0, tmpx[y].indexOf("@@@")); 412 413 String tmp = tmpx[y].substring(tmpx[y].indexOf("@@@") + 3); 414 sizes[y] = tmp.substring(0, tmp.lastIndexOf("@@@")); 415 416 if(perms != null) 417 { 418 perms[y] = Integer.parseInt(tmpx[y].substring(tmpx[y].lastIndexOf("@@@") + 419 3)); 420 } 421 } 422 } 423 424 public void actionPerformed(ActionEvent e) 425 { 426 } 427 428 public boolean isOk() 429 { 430 return isDirectory; 431 } 432 433 public int getLength() 434 { 435 return length; 436 } 437 438 public String [] list() 439 { 440 return files; 441 } 442 443 public String [] sList() 444 { 445 return sizes; 446 } 447 448 public int[] getPermissions() 449 { 450 return perms; 451 } 452 453 public Date[] getDates() 454 { 455 return dates; 456 } 457 } 458 | Popular Tags |