KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jftp > gui > tasks > LastConnections


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
17 //TODO: Having capacity passed to these methods is redundant, take it out
18
//*** (should it be in GUI dir?)
19
package net.sf.jftp.gui.tasks;
20
21 import net.sf.jftp.JFtp;
22
23 //***
24
import net.sf.jftp.config.*;
25
26 import java.awt.*;
27 import java.awt.event.*;
28
29 import java.io.*;
30
31 import java.lang.*;
32
33 import java.util.*;
34
35 import javax.swing.*;
36
37
38 public class LastConnections
39 {
40     public static String JavaDoc SENTINEL = new String JavaDoc("********************");
41     private static JFtp jftp;
42
43     //*** changed this so that JFtp object is passed to it and
44
//initialized
45
public LastConnections(JFtp jftp)
46     {
47         this.jftp = jftp;
48
49         //init();
50
}
51
52     //writeToFile: This code is called when modifications are done
53
//being made (should it be private and called from inside
54
//this class?)
55
//maybe I should return a boolean value stating whether or not it
56
//succeeded
57
//SHOULD THIS BE PRIVATE?
58
//public static void writeToFile(String[] a, int capacity) {
59
public static void writeToFile(String JavaDoc[][] a, int capacity)
60     {
61         try
62         {
63             File f1 = new File(Settings.appHomeDir);
64             f1.mkdir();
65
66             File f2 = new File(Settings.last_cons);
67             f2.createNewFile();
68
69             FileOutputStream fos;
70             PrintStream out;
71
72             fos = new FileOutputStream(Settings.last_cons);
73             out = new PrintStream(fos);
74
75             //String[] lastCons = new String[capacity];
76
//lastCons = readFromFile(capacity);
77
for(int i = 0; i < capacity; i++)
78             {
79                 //out.println(a[i]);
80
int j = 0;
81                 out.println(a[i][j]);
82
83                 while((j < JFtp.CAPACITY) && !(a[i][j].equals(SENTINEL)))
84                 {
85                     j++;
86                     out.println(a[i][j]);
87
88                     //retVal[i][j] = raf.readLine();
89
//System.out.println();
90
}
91
92                 //System.out.println(a[i]);
93
}
94
95             /*
96             RandomAccessFile raf =
97                     new RandomAccessFile(f2, "rw");
98
99             for (int i = 0; i < capacity; i++) {
100
101                     raf.writeChars(a[i] + "\n");
102
103
104             }
105             */

106             jftp.updateMenuBar();
107         }
108         catch(Exception JavaDoc e)
109         {
110             e.printStackTrace();
111         }
112     }
113
114     //writeToFile
115
public static String JavaDoc[][] readFromFile(int capacity)
116     {
117         //MAKE THIS 2D
118
//String[] retVal = new String[capacity];
119
String JavaDoc[][] retVal = new String JavaDoc[capacity][JFtp.CONNECTION_DATA_LENGTH];
120
121         //if ((f.exists()))
122
// System.out.println("not found");
123
try
124         {
125             File f1 = new File(Settings.appHomeDir);
126             f1.mkdir();
127
128             File f2 = new File(Settings.last_cons);
129
130             //File f;
131
//f = init(capacity);
132
if(!f2.exists())
133             {
134                 init(capacity);
135             }
136
137             /*
138             FileReader fr = new FileReader(Settings.last_cons);
139
140             BufferedReader breader = new BufferedReader(fr);
141
142             for (int i=0; i<capacity; i++) {
143
144                    retVal[i] = breader.readLine();
145             }
146             */

147             RandomAccessFile raf = new RandomAccessFile(f2, "r");
148
149             //BUGFIX 1.40: determine if old style of file is
150
// being used
151
//THIS MAY MAKE THIS SECTION LESS TIME-EFFICIENT
152
String JavaDoc[] oldValues = new String JavaDoc[capacity];
153             String JavaDoc firstSection = new String JavaDoc("");
154             boolean oldVersion = true;
155
156             //System.out.println(capacity);
157
for(int i = 0; i < capacity; i++)
158             {
159                 if(capacity < JFtp.CAPACITY)
160                 {
161                     oldVersion = false;
162
163                     break;
164                 }
165
166                 oldValues[i] = raf.readLine();
167                 firstSection = oldValues[i].substring(0, 3);
168
169                 //System.out.print(i);
170
//System.out.print(firstSection);
171
//System.out.println("end");
172
if(!(firstSection.equals("FTP")) &&
173                        !(firstSection.equals("SFT")) &&
174                        !(firstSection.equals("SMB")) &&
175                        !(firstSection.equals("NFS")) &&
176                        !(firstSection.equals("nul")))
177                 {
178                     //System.out.println("###");
179
oldVersion = false;
180                 }
181
182                 if(!oldVersion)
183                 {
184                     //System.out.println("!!!");
185
break;
186                 }
187             }
188
189             //for
190
raf = new RandomAccessFile(f2, "r");
191
192             if(oldVersion)
193             {
194                 //System.out.println("old file detected");
195
for(int i = 0; i < capacity; i++)
196                 {
197                     oldValues[i] = raf.readLine();
198                 }
199
200                 changeFile(oldValues);
201             }
202
203             //reset to read start of file
204
raf = new RandomAccessFile(f2, "r");
205
206             for(int i = 0; i < capacity; i++)
207             {
208                 int j = 0;
209                 retVal[i][j] = raf.readLine();
210
211                 //System.out.print("--->");
212
//System.out.println(retVal[i][j]);
213
while((j < JFtp.CONNECTION_DATA_LENGTH) &&
214                           !(retVal[i][j].equals(SENTINEL)))
215                 {
216                     j++;
217                     retVal[i][j] = raf.readLine();
218
219                     //System.out.print("--->");
220
//System.out.println(retVal[i][j]);
221
//j++;
222
}
223
224                 //while
225
//retVal[i][j+1] = raf.readLine();
226
}
227
228             //for
229
}
230         catch(Exception JavaDoc ex)
231         {
232             ex.printStackTrace();
233         }
234
235         return retVal;
236     }
237
238     //readFromFile
239
public static String JavaDoc[][] prepend(String JavaDoc[] newString, int capacity,
240                                      boolean newConnection)
241     {
242         //BUGFIX: 2D
243
//String[] lastCons = new String[capacity];
244
String JavaDoc[][] lastCons = new String JavaDoc[capacity][JFtp.CONNECTION_DATA_LENGTH];
245
246         lastCons = readFromFile(capacity);
247
248         for(int i = 0; i < capacity; i++)
249         {
250             int j = 0;
251
252             while(!(lastCons[i][j].equals(SENTINEL)))
253             {
254                 //temp[j] = lastCons[i][j];
255
//lastCons[i][j] = newString[j];
256
//System.out.println(lastCons[i][j]);
257
j++;
258             }
259
260             //System.out.println(lastCons[i][j]);
261
}
262
263         //BUGFIX: now need array to represent 1st connection data
264
//String temp = new String("");
265
String JavaDoc[] temp = new String JavaDoc[JFtp.CONNECTION_DATA_LENGTH];
266
267         int j = 0;
268
269         //is temp necessary?
270
//System.out.println("++++++++++++++++++++");
271
while(!(lastCons[0][j].equals(SENTINEL)))
272         {
273             //while (!(newString[j].equals(SENTINEL))) {
274
temp[j] = lastCons[0][j];
275
276             //lastCons[0][j] = newString[j];
277
//System.out.println(lastCons[0][j]);
278
//if (lastCons[0][j].equals(SENTINEL))
279
//break;
280
j++;
281         }
282
283         temp[j] = SENTINEL;
284
285         j = 0;
286
287         while(!(newString[j].equals(SENTINEL)))
288         {
289             lastCons[0][j] = newString[j];
290             j++;
291         }
292
293         lastCons[0][j] = SENTINEL;
294
295         //take out any data that would be "left over" after sentinel
296
//is this necessary?
297
j++;
298
299         while(j < JFtp.CONNECTION_DATA_LENGTH)
300         {
301             lastCons[0][j] = "";
302             j++;
303         }
304
305         //System.out.println("-----------------");
306
for(int i = 0; i < capacity; i++)
307         {
308             //while (!(lastCons[j].equals(SENTINEL))) {
309
//newString = temp;
310
if((i + 1) != capacity)
311             {
312                 //System.out.print("-->");
313
j = 0;
314
315                 //System.out.println(temp[j]);
316
//shift temp data into newString
317
while(!(temp[j].equals(SENTINEL)))
318                 {
319                     newString[j] = temp[j];
320
321                     //System.out.println(newString[j]);
322
j++;
323                 }
324
325                 newString[j] = SENTINEL;
326
327                 //System.out.println("$$$$$$$$$$$$$");
328
//store lastCons data in temp again
329
j = 0;
330
331                 //while (!(temp[j].equals(SENTINEL))) {
332
while(!(lastCons[i + 1][j].equals(SENTINEL)))
333                 {
334                     //newString[j] = temp[j];
335
temp[j] = lastCons[i + 1][j];
336
337                     //temp[j] = lastCons[i + 1][j];
338
//lastCons[i+1][j] = newString[j];
339
//j++;
340
//System.out.println(lastCons[i+1][j]);
341
//System.out.println(temp[j]);
342
//if (temp[j].equals(SENTINEL))
343
//if (lastCons[i][j].equals(SENTINEL))
344
//break;
345
//temp[j] = lastCons[i+1][j];
346
j++;
347                 }
348
349                 //while
350
temp[j] = SENTINEL;
351
352                 //System.out.println("+-+-+-+-+-+-");
353
//then, make adjustments to lastCons
354
j = 0;
355
356                 //while (!(lastCons[i+1][j].equals(SENTINEL))) {
357
while(!(newString[j].equals(SENTINEL)))
358                 {
359                     lastCons[i + 1][j] = newString[j];
360
361                     //System.out.println(lastCons[i+1][j]);
362
j++;
363                 }
364
365                 lastCons[i + 1][j] = SENTINEL;
366
367                 //j=0;
368

369                 /*
370                 while (!(lastCons[i][j].equals(SENTINEL))) {
371
372                         temp[j] = lastCons[i+1][j];
373                         j++;
374
375                 }
376                 */

377
378                 //lastCons[i+1][j] = SENTINEL;
379
}
380
381             //if
382
}
383
384         //for
385
for(int i = 0; i < capacity; i++)
386         {
387             j = 0;
388
389             while(!lastCons[i][j].equals(SENTINEL))
390             {
391                 //System.out.println(lastCons[i][j]);
392
if(lastCons[i][j].equals(SENTINEL))
393                 {
394                     break;
395                 }
396
397                 j++;
398             }
399
400             //System.out.println(lastCons[i][j]);
401
}
402
403         //***
404
if(newConnection)
405         {
406             writeToFile(lastCons, capacity);
407         }
408
409         return lastCons;
410     }
411
412     //prepend
413
public static String JavaDoc[][] moveToFront(int position, int capacity)
414     {
415         //make these 2D
416
//String[] lastCons = new String[capacity];
417
//String[] newLastCons = new String[capacity];
418
String JavaDoc[][] lastCons = new String JavaDoc[capacity][JFtp.CONNECTION_DATA_LENGTH];
419         String JavaDoc[][] newLastCons = new String JavaDoc[capacity][JFtp.CONNECTION_DATA_LENGTH];
420
421         //System.out.print("--------:");
422
//System.out.println(capacity);
423
lastCons = readFromFile(capacity);
424
425         //String temp = new String("");
426
String JavaDoc[] temp = new String JavaDoc[JFtp.CONNECTION_DATA_LENGTH];
427
428         //String[] lastCons = new String[capacity];
429
int j = 0;
430         temp[j] = lastCons[position][j];
431
432         while(!(lastCons[position][j].equals(SENTINEL)))
433         {
434             j++;
435             temp[j] = lastCons[position][j];
436         }
437
438         j = 0;
439
440         //System.out.println("START");
441
while(!(lastCons[position][j].equals(SENTINEL)))
442         {
443             //System.out.println(lastCons[position][j]);
444
j++;
445         }
446
447         //System.out.println("END");
448
//possible bugfix code?
449
/*
450         for (int i=0; i<JFtp.CONNECTION_DATA_LENGTH; i++) {
451                 temp[i] = lastCons[position][i];
452         }
453         */

454         newLastCons = prepend(temp, position + 1, false);
455
456         for(int i = 0; i <= position; i++)
457         {
458             j = 0;
459
460             //while (!(lastCons[position][i].equals(SENTINEL))) {
461
//while (!(lastCons[i][j].equals(SENTINEL))) {
462
while(!(newLastCons[i][j].equals(SENTINEL)))
463             {
464                 //j++;
465
//temp[i] = lastCons[position][i];
466
//System.out.println(i);
467
//System.out.println(j);
468
//System.out.println(newLastCons[i][j]);
469
lastCons[i][j] = newLastCons[i][j];
470                 j++;
471             }
472
473             lastCons[i][j] = SENTINEL;
474         }
475
476         //for
477
writeToFile(lastCons, capacity);
478
479         return lastCons;
480     }
481
482     //moveToFront
483
public static int findString(String JavaDoc[] findVal, int capacity)
484     {
485         //BUGFIX: 2D
486
//String[] lastCons = new String[capacity];
487
String JavaDoc[][] lastCons = new String JavaDoc[capacity][JFtp.CONNECTION_DATA_LENGTH];
488
489         lastCons = readFromFile(capacity);
490
491         for(int i = 0; i < capacity; i++)
492         {
493             int j = 0;
494
495             while((j < JFtp.CAPACITY) && findVal[j].equals(lastCons[i][j]) &&
496                       !(lastCons[i][j].equals(SENTINEL)) &&
497                       !(findVal[j].equals(SENTINEL)))
498             {
499                 //System.out.println("start ");
500
//System.out.print(lastCons[i][j]);
501
//System.out.print(findVal[j]);
502
//System.out.println("end");
503
j++;
504
505                 //if (findVal.equals(lastCons[i]))
506
//return i;
507
}
508
509             if(findVal[j].equals(lastCons[i][j]))
510             {
511                 //System.out.println("test");
512
//System.out.println(lastCons[i][j]);
513
//System.out.println(findVal[j]);
514
return i;
515             }
516             else
517             {
518                 //System.out.println("test2");
519
//System.out.println(lastCons[i][j]);
520
//System.out.println(findVal[j]);
521
//System.out.println("NO");
522
}
523         }
524
525         //if not found, return -1
526
return -1;
527     }
528
529     //findString
530

531     /*
532     //unnecessary?
533     public static String[] swap(String a[], int pos1, int pos2) {
534
535             String temp = new String("");
536
537             temp = a[pos2];
538             a[pos2] = a[pos1];
539             a[pos1] = temp;
540
541             return a;
542
543     } //swap
544     */

545     private static void init(int capacity)
546     {
547         //File f = new File(Settings.last_cons);
548
try
549         {
550             FileOutputStream fos;
551             PrintStream out;
552
553             fos = new FileOutputStream(Settings.last_cons);
554             out = new PrintStream(fos);
555
556             for(int i = 0; i < capacity; i++)
557             {
558                 out.println("null");
559                 out.println(SENTINEL);
560             }
561
562             fos.close();
563         }
564         catch(Exception JavaDoc e)
565         {
566             e.printStackTrace();
567         }
568
569         //return f;
570
}
571
572     //init
573
private static void changeFile(String JavaDoc[] oldValues)
574     {
575         StringTokenizer tokens;
576
577         String JavaDoc[][] newData = new String JavaDoc[JFtp.CAPACITY][JFtp.CONNECTION_DATA_LENGTH];
578
579         //this assumes that capacity will not change
580
//should this be set to 9 instead of JFtp.CAPACITY?
581
for(int i = 0; i < JFtp.CAPACITY; i++)
582         {
583             //System.out.println(oldValues[i]);
584
tokens = new StringTokenizer(oldValues[i], " ");
585
586             int j = 0;
587
588             while((tokens.hasMoreTokens()))
589             {
590                 newData[i][j] = tokens.nextToken();
591
592                 //System.out.println(newData[i][j]);
593
j++;
594             }
595
596             newData[i][j] = SENTINEL;
597
598             //System.out.print("---->");
599
//System.out.println(newData[i][0]);
600
//System.out.println(j);
601
//for backwards compatibility with versions that
602
//don't have SFTP port remembered: enter port 22
603
//(which is default) there
604
if(newData[i][0].equals("SFTP") && (j == 5))
605             {
606                 String JavaDoc temp = new String JavaDoc("");
607                 String JavaDoc temp2 = new String JavaDoc("");
608
609                 temp = newData[i][4];
610                 newData[i][4] = "22";
611
612                 temp2 = newData[i][5];
613
614                 newData[i][5] = temp;
615
616                 //temp2 = newData[i][5];
617
//newData[i][6] = temp2;
618
//newData[i][7] = SENTINEL;
619
newData[i][6] = SENTINEL;
620
621                 /*
622                 for (int j=5; j++; j<7) {
623
624                         temp = newData[i][j];
625                         newData[i][j] = newData[i][j-1];
626
627                 } */

628             }
629
630             //if
631
}
632
633         //for
634
writeToFile(newData, JFtp.CAPACITY);
635     }
636
637     //changeFile
638
}
639
640
641 //LastConnections
642
Popular Tags