001: /*
002: * This program is free software; you can redistribute it and/or
003: * modify it under the terms of the GNU General Public License
004: * as published by the Free Software Foundation; either version 2
005: * of the License, or (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011:
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package net.sf.jftp.gui.base.dir;
017:
018: import net.sf.jftp.*;
019: import net.sf.jftp.config.Settings;
020: import net.sf.jftp.gui.framework.*;
021: import net.sf.jftp.net.*;
022: import net.sf.jftp.system.LocalIO;
023: import net.sf.jftp.util.*;
024:
025: import java.awt.event.*;
026:
027: import java.io.*;
028:
029: import java.net.*;
030:
031: import java.text.*;
032:
033: import java.util.*;
034:
035: public class DirLister implements ActionListener {
036: private int length;
037: private String[] files;
038: private String[] sizes;
039: private int[] perms;
040: private boolean isDirectory = true;
041: public boolean finished = false;
042: private BasicConnection con;
043: private String sortMode = null;
044: private Date[] dates = null;
045:
046: public DirLister(BasicConnection con) //String type)
047: {
048: this .con = con;
049: init();
050: }
051:
052: public DirLister(BasicConnection con, String sortMode) //String type)
053: {
054: this .con = con;
055: this .sortMode = sortMode;
056: init();
057: }
058:
059: public DirLister(BasicConnection con, String sortMode, boolean hide) //String type)
060: {
061: this .con = con;
062: this .sortMode = sortMode;
063: init();
064:
065: int cnt = files.length;
066:
067: if (hide) {
068: for (int i = 0; i < files.length; i++) {
069: if (files[i].startsWith(".")
070: && !files[i].startsWith("..")) {
071: files[i] = null;
072: cnt--;
073: }
074: }
075:
076: String newFiles[] = new String[cnt];
077: String newSizes[] = new String[cnt];
078: int newPerms[] = new int[cnt];
079:
080: int idx = 0;
081: for (int i = 0; i < files.length; i++) {
082: if (files[i] == null) {
083: continue;
084: } else {
085: newFiles[idx] = files[i];
086: newSizes[idx] = sizes[i];
087: newPerms[idx] = perms[i];
088: idx++;
089: }
090: }
091:
092: files = newFiles;
093: sizes = newSizes;
094: perms = newPerms;
095: length = files.length;
096: }
097: }
098:
099: public void init() {
100: try {
101: String outfile = Settings.ls_out;
102:
103: //BasicConnection con = JFtp.getControlConnection();
104: con.list();
105: files = con.sortLs();
106: sizes = con.sortSize();
107:
108: //Log.debug("sizes: " + sizes.length);
109: /*
110: for(int i=0; i<files.length; i++)
111: {
112: Log.out("parser: "+files[i]+":"+sizes[i]);
113: }
114: */
115: length = files.length;
116: perms = con.getPermissions();
117: isDirectory = true;
118:
119: /*
120: for(int i=0; i<files.length; i++)
121: {
122: if((con instanceof FtpConnection) && sortMode.equals("Date"))
123: {
124: Vector v = ((FtpConnection)con).dateVector;
125: if(v.size() > 0) Log.out(files[i]+":"+v.elementAt(i));
126: }
127: }*/
128: if (sortMode != null) {
129: if (!sortMode.equals("Date")) {
130: //Log.out("0"+sortMode);
131: sortFirst();
132: }
133:
134: sort(sortMode);
135: } else if (sortMode == null) {
136: //Log.out("1"+sortMode);
137: sortFirst();
138: }
139: } catch (Exception ex) {
140: ex.printStackTrace();
141: isDirectory = false;
142: }
143:
144: finished = true;
145: }
146:
147: private void sort(String type) {
148: Vector fv = new Vector();
149: Vector sv = new Vector();
150: Vector pv = new Vector();
151:
152: if (type.equals("Reverse")) {
153: for (int i = 0; i < length; i++) {
154: fv.add(files[i]);
155: sv.add(sizes[i]);
156:
157: if (perms != null) {
158: pv.add(new Integer(perms[i]));
159: }
160: }
161:
162: Collections.sort(fv, Collections.reverseOrder());
163:
164: // Collections.sort(sv, Collections.reverseOrder());
165: // if(perms != null) Collections.sort(pv, Collections.reverseOrder());
166: Object[] filesTmp = fv.toArray();
167: Object[] sizesTmp = sv.toArray();
168: Object[] permsTmp = null;
169:
170: if (perms != null) {
171: permsTmp = pv.toArray();
172: }
173:
174: for (int i = 0; i < length; i++) {
175: files[i] = (String) filesTmp[i];
176: sizes[i] = (String) sizesTmp[length - i - 1];
177:
178: if (perms != null) {
179: perms[i] = ((Integer) permsTmp[length - i - 1])
180: .intValue();
181: }
182: }
183: } else if (type.startsWith("Size")) {
184: int cnt = 0;
185: Hashtable processed = new Hashtable();
186: boolean reverse = false;
187:
188: if (type.endsWith("/Re")) {
189: reverse = true;
190: }
191:
192: while (cnt < length) {
193: int idx = 0;
194: double current = 0;
195:
196: if (reverse) {
197: current = Double.MAX_VALUE;
198: }
199:
200: for (int i = 0; i < length; i++) {
201: if (processed.containsKey("" + i)) {
202: continue;
203: }
204:
205: int si = Integer.parseInt(sizes[i]);
206:
207: if (!reverse && (si >= current)) {
208: //Log.out(sizes[i]+"/"+i);
209: idx = i;
210: current = si;
211: } else if (reverse && (si <= current)) {
212: idx = i;
213: current = si;
214: }
215: }
216:
217: processed.put("" + idx, "" + sizes[idx]);
218: fv.add(files[idx]);
219: sv.add(sizes[idx]);
220:
221: //System.out.println(files[idx]+":"+sizes[idx]+":"+idx);
222: if (perms != null) {
223: pv.add(new Integer(perms[idx]));
224: }
225:
226: cnt++;
227: }
228:
229: for (int i = 0; i < length; i++) {
230: files[i] = (String) fv.elementAt(i);
231: sizes[i] = (String) sv.elementAt(i);
232:
233: if (perms != null) {
234: perms[i] = ((Integer) pv.elementAt(i)).intValue();
235: }
236: }
237: } else if (type.equals("Date")) {
238: String style = "ftp";
239:
240: //TODO: may be slow
241: if (!(con instanceof FtpConnection)
242: || (((FtpConnection) con).dateVector == null)
243: || (((FtpConnection) con).dateVector.size() < 1)) {
244: if (!(con instanceof FilesystemConnection)
245: || (((FilesystemConnection) con).dateVector == null)
246: || (((FilesystemConnection) con).dateVector
247: .size() < 1)) {
248: } else {
249: style = "file";
250: }
251: }
252:
253: Object[] date = null;
254:
255: if (style.equals("ftp")) {
256: date = ((FtpConnection) con).dateVector.toArray();
257:
258: /*
259: for(int v=0; v<date.length; v++)
260: {
261: System.out.println(files[v]+":"+((Date)date[v]).toString());
262: }
263: */
264: } else {
265: date = ((FilesystemConnection) con).dateVector
266: .toArray();
267: }
268:
269: for (int j = 0; j < date.length; j++) {
270: for (int i = 0; i < date.length; i++) {
271: Date x = (Date) date[i];
272:
273: if (i == (date.length - 1)) {
274: break;
275: }
276:
277: if (comp(x, (Date) date[i + 1])) {
278: //Log.debug("switch");
279: Date swp = (Date) date[i + 1];
280: date[i + 1] = x;
281: date[i] = swp;
282:
283: String s1 = files[i + 1];
284: String s2 = files[i];
285: files[i] = s1;
286: files[i + 1] = s2;
287:
288: //if(files[i].startsWith(".cross")) Log.out(files[i]+" / "+ ((Date)date[i]).toString());
289: s1 = sizes[i + 1];
290: s2 = sizes[i];
291: sizes[i] = s1;
292: sizes[i + 1] = s2;
293:
294: int s3 = perms[i + 1];
295: int s4 = perms[i];
296: perms[i] = s3;
297: perms[i + 1] = s4;
298: }
299: }
300: }
301:
302: dates = new Date[date.length];
303:
304: for (int i = 0; i < dates.length; i++) {
305: dates[i] = (Date) date[i];
306: }
307:
308: /*
309: try
310: {
311: for(int k=0; k<date.length; k++)
312: {
313: Log.out("+++ " + date[k].toString());
314: }
315: }
316: catch(Exception ex)
317: {
318: ex.printStackTrace();
319: }
320: */
321: } else if (type.equals("Normal")) {
322: // already done.
323: }
324: }
325:
326: private boolean comp(Date one, Date two) {
327: Calendar c = new GregorianCalendar();
328: c.clear();
329: c.setTime(one);
330:
331: Calendar c2 = new GregorianCalendar();
332: c2.clear();
333: c2.setTime(two);
334:
335: if (c.getTime().compareTo(c2.getTime()) > 0) // c.compareTo(c2) > 0)
336: {
337: return true;
338: } else {
339: return false;
340: }
341: }
342:
343: public void sortFirst() {
344: String[] tmpx = new String[length];
345:
346: for (int x = 0; x < length; x++) {
347: if (perms != null) {
348: tmpx[x] = files[x] + "@@@" + sizes[x] + "@@@"
349: + perms[x];
350:
351: //Log.debug(tmpx[x]);
352: } else {
353: tmpx[x] = files[x] + "@@@" + sizes[x];
354: }
355: }
356:
357: LocalIO.sortStrings(tmpx);
358:
359: for (int y = 0; y < length; y++) {
360: files[y] = tmpx[y].substring(0, tmpx[y].indexOf("@@@"));
361:
362: String tmp = tmpx[y].substring(tmpx[y].indexOf("@@@") + 3);
363: sizes[y] = tmp.substring(0, tmp.lastIndexOf("@@@"));
364:
365: if (perms != null) {
366: perms[y] = Integer.parseInt(tmpx[y].substring(tmpx[y]
367: .lastIndexOf("@@@") + 3));
368: }
369: }
370: }
371:
372: public void actionPerformed(ActionEvent e) {
373: }
374:
375: public boolean isOk() {
376: return isDirectory;
377: }
378:
379: public int getLength() {
380: return length;
381: }
382:
383: public String[] list() {
384: return files;
385: }
386:
387: public String[] sList() {
388: return sizes;
389: }
390:
391: public int[] getPermissions() {
392: return perms;
393: }
394:
395: public Date[] getDates() {
396: return dates;
397: }
398: }
|