KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > base > dir > DirLister


1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

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 JavaDoc[] files;
40     private String JavaDoc[] sizes;
41     private int[] perms;
42     private boolean isDirectory = true;
43     public boolean finished = false;
44     private BasicConnection con;
45     private String JavaDoc sortMode = null;
46     private Date[] dates = null;
47
48     public DirLister(BasicConnection con) //String type)
49
{
50         this.con = con;
51         init();
52     }
53
54     public DirLister(BasicConnection con, String JavaDoc sortMode) //String type)
55
{
56         this.con = con;
57         this.sortMode = sortMode;
58         init();
59     }
60     
61     public DirLister(BasicConnection con, String JavaDoc sortMode, boolean hide) //String type)
62
{
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 JavaDoc newFiles[] = new String JavaDoc[cnt];
78             String JavaDoc newSizes[] = new String JavaDoc[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 JavaDoc outfile = Settings.ls_out;
106
107             //BasicConnection con = JFtp.getControlConnection();
108
con.list();
109             files = con.sortLs();
110             sizes = con.sortSize();
111
112             //Log.debug("sizes: " + sizes.length);
113
/*
114             for(int i=0; i<files.length; i++)
115             {
116                     Log.out("parser: "+files[i]+":"+sizes[i]);
117             }
118             */

119             length = files.length;
120             perms = con.getPermissions();
121             isDirectory = true;
122
123             /*
124             for(int i=0; i<files.length; i++)
125             {
126                     if((con instanceof FtpConnection) && sortMode.equals("Date"))
127                 {
128                         Vector v = ((FtpConnection)con).dateVector;
129                         if(v.size() > 0) Log.out(files[i]+":"+v.elementAt(i));
130                 }
131             }*/

132             if(sortMode != null)
133             {
134                 if(!sortMode.equals("Date"))
135                 {
136                     //Log.out("0"+sortMode);
137
sortFirst();
138                 }
139
140                 sort(sortMode);
141             }
142             else if(sortMode == null)
143             {
144                 //Log.out("1"+sortMode);
145
sortFirst();
146             }
147         }
148         catch(Exception JavaDoc ex)
149         {
150             ex.printStackTrace();
151             isDirectory = false;
152         }
153
154         finished = true;
155     }
156
157     private void sort(String JavaDoc 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 JavaDoc(perms[i]));
173                 }
174             }
175
176             Collections.sort(fv, Collections.reverseOrder());
177
178             // Collections.sort(sv, Collections.reverseOrder());
179
// if(perms != null) Collections.sort(pv, Collections.reverseOrder());
180
Object JavaDoc[] filesTmp = fv.toArray();
181             Object JavaDoc[] sizesTmp = sv.toArray();
182             Object JavaDoc[] 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 JavaDoc) filesTmp[i];
192                 sizes[i] = (String JavaDoc) sizesTmp[length - i - 1];
193
194                 if(perms != null)
195                 {
196                     perms[i] = ((Integer JavaDoc) 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                         //Log.out(sizes[i]+"/"+i);
233
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                 //System.out.println(files[idx]+":"+sizes[idx]+":"+idx);
248
if(perms != null)
249                 {
250                     pv.add(new Integer JavaDoc(perms[idx]));
251                 }
252
253                 cnt++;
254             }
255
256             for(int i = 0; i < length; i++)
257             {
258                 files[i] = (String JavaDoc) fv.elementAt(i);
259                 sizes[i] = (String JavaDoc) sv.elementAt(i);
260
261                 if(perms != null)
262                 {
263                     perms[i] = ((Integer JavaDoc) pv.elementAt(i)).intValue();
264                 }
265             }
266         }
267         else if(type.equals("Date"))
268         {
269             String JavaDoc style = "ftp";
270
271             //TODO: may be slow
272
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 JavaDoc[] date = null;
288
289             if(style.equals("ftp"))
290             {
291                 date = ((FtpConnection) con).dateVector.toArray();
292
293                 /*
294                         for(int v=0; v<date.length; v++)
295                         {
296                                 System.out.println(files[v]+":"+((Date)date[v]).toString());
297                         }
298                 */

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                         //Log.debug("switch");
319
Date swp = (Date) date[i + 1];
320                         date[i + 1] = x;
321                         date[i] = swp;
322
323                         String JavaDoc s1 = files[i + 1];
324                         String JavaDoc s2 = files[i];
325                         files[i] = s1;
326                         files[i + 1] = s2;
327
328                         //if(files[i].startsWith(".cross")) Log.out(files[i]+" / "+ ((Date)date[i]).toString());
329
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             /*
350             try
351             {
352             for(int k=0; k<date.length; k++)
353             {
354                     Log.out("+++ " + date[k].toString());
355             }
356             }
357             catch(Exception ex)
358             {
359                     ex.printStackTrace();
360             }
361             */

362         }
363         else if(type.equals("Normal"))
364         {
365             // already done.
366
}
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) // c.compareTo(c2) > 0)
380
{
381             return true;
382         }
383         else
384         {
385             return false;
386         }
387     }
388
389     public void sortFirst()
390     {
391         String JavaDoc[] tmpx = new String JavaDoc[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                 //Log.debug(tmpx[x]);
400
}
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 JavaDoc 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 JavaDoc[] list()
439     {
440         return files;
441     }
442
443     public String JavaDoc[] 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