1 16 package net.sf.jftp.gui.base; 17 18 import net.sf.jftp.*; 19 import net.sf.jftp.config.*; 20 import net.sf.jftp.gui.framework.*; 21 import net.sf.jftp.net.*; 22 import net.sf.jftp.system.StringUtils; 23 import net.sf.jftp.system.logging.Log; 24 import net.sf.jftp.util.*; 25 26 import java.awt.*; 27 import java.awt.event.*; 28 29 import java.io.*; 30 31 import java.util.*; 32 33 import javax.swing.*; 34 import javax.swing.JComponent .*; 35 import javax.swing.event.*; 36 37 38 public class DownloadQueue extends HPanel implements ActionListener 39 { 40 private static final String SEP = "--> "; 41 42 int NumRetry = 5; 44 45 private DefaultListModel liststr = new DefaultListModel(); 47 private JList list = new JList(liststr); 48 private ArrayList queue = new ArrayList(); 49 private queueDownloader thread = new queueDownloader(); 50 private QueueRecord lastDownload; 51 private BasicConnection con; 52 53 private HImageButton start = new HImageButton(Settings.resumeImage, 55 "start", 56 "Start queue download...", 57 this); 58 private HImageButton stop = new HImageButton(Settings.pauseImage, "stop", 59 "Stop queue download...", this); 60 private HImageButton save = new HImageButton(Settings.saveImage, "save", 61 "Save queue list to file...", 62 this); 63 private HImageButton load = new HImageButton(Settings.cdImage, "load", 64 "Load queue list from...", this); 65 private HImageButton up = new HImageButton(Settings.downloadImage, "up", 66 "Change order of queue", this); 67 private HImageButton down = new HImageButton(Settings.uploadImage, "down", 68 "Change order of queue", this); 69 private HImageButton delete = new HImageButton(Settings.deleteImage, "del", 70 "Delete item in queue", this); 71 72 private boolean isThere = false; 75 private boolean downloading = false; 76 private ConnectionHandler handler = new ConnectionHandler(); 77 private JLabel statuslabel; 78 79 public DownloadQueue() 80 { 81 setLayout(new BorderLayout()); 82 83 HPanel cmdP = new HPanel(); 85 86 cmdP.add(start); 87 88 cmdP.add(stop); 89 90 cmdP.add(new JLabel(" ")); 91 92 cmdP.add(up); 93 94 cmdP.add(down); 95 96 cmdP.add(delete); 97 98 cmdP.add(new JLabel(" ")); 99 100 cmdP.add(save); 101 102 cmdP.add(load); 103 104 start.setSize(24, 24); 105 start.setBorder(true); 106 stop.setSize(24, 24); 107 stop.setBorder(true); 108 up.setSize(24, 24); 109 up.setBorder(true); 110 down.setSize(24, 24); 111 down.setBorder(true); 112 delete.setSize(24, 24); 113 delete.setBorder(true); 114 save.setSize(24, 24); 115 save.setBorder(true); 116 load.setSize(24, 24); 117 load.setBorder(true); 118 119 JScrollPane dP = new JScrollPane(list); 120 121 add(cmdP, BorderLayout.SOUTH); 124 add(dP, BorderLayout.CENTER); 125 126 HPanel status = new HPanel(); 127 statuslabel = new JLabel(""); 128 statuslabel.setSize(100, 100); 129 130 status.add(statuslabel); 131 add(status, BorderLayout.NORTH); 132 133 start.setToolTipText("Start queue download..."); 135 stop.setToolTipText("Stop queue download..."); 136 save.setToolTipText("Save queue list to file..."); 137 load.setToolTipText("Load queue list from..."); 138 up.setToolTipText("Change order of queue"); 139 down.setToolTipText("Change order of queue"); 140 delete.setToolTipText("Delete item in queue"); 141 142 } 144 145 150 public void addFtp(String file) 151 { 152 Log.debug("Remote File" + JFtp.remoteDir.getPath() + file + 153 "Local File" + JFtp.localDir.getPath() + file + "HostName" + 154 JFtp.hostinfo.hostname); 155 156 QueueRecord rec = new QueueRecord(); 157 rec.type = "ftp"; 158 rec.hostname = JFtp.hostinfo.hostname; 159 rec.username = JFtp.hostinfo.username; 160 rec.password = JFtp.hostinfo.password; 161 rec.port = JFtp.hostinfo.port; 162 rec.local = JFtp.localDir.getPath(); 163 rec.remote = JFtp.remoteDir.getPath(); 164 rec.file = file; 165 queue.add(rec); 166 liststr.addElement(rec.hostname + " : " + rec.remote + rec.file); 167 } 168 169 public void actionPerformed(ActionEvent e) 170 { 171 if(e.getActionCommand().equals("start")) 172 { 173 if(thread != null) 174 { 175 if(!thread.isAlive()) 176 { 177 thread = new queueDownloader(); 178 thread.start(); 179 180 } 182 } 183 else 184 { 185 thread = new queueDownloader(); 186 thread.start(); 187 188 } 190 } 191 192 else if(e.getActionCommand().equals("stop")) 193 { 194 if(thread != null) 195 { 196 thread.block = true; 197 198 FtpConnection ftpcon = (FtpConnection) con; 202 DataConnection dcon = ftpcon.getDataConnection(); 203 dcon.getCon().work = false; 204 205 try 206 { 207 dcon.sock.close(); 208 } 209 catch(Exception ex) 210 { 211 ex.printStackTrace(); 212 } 213 } 214 } 215 216 else if(e.getActionCommand().equals("down")) 217 { 218 if(list.getSelectedIndex() != -1) 219 { 220 int a = list.getSelectedIndex(); 221 int b = a + 1; 222 223 QueueRecord qa = (QueueRecord) queue.get(b); 224 queue.remove(b); 225 queue.add(a, qa); 226 227 String sa = (String ) liststr.get(b); 228 liststr.remove(b); 229 liststr.add(a, sa); 230 231 list.setSelectedIndex(b); 232 } 233 } 234 else if(e.getActionCommand().equals("up")) 235 { 236 if(list.getSelectedIndex() != -1) 237 { 238 int a = list.getSelectedIndex() - 1; 239 int b = a + 1; 240 241 QueueRecord qa = (QueueRecord) queue.get(b); 242 queue.remove(b); 243 queue.add(a, qa); 244 245 String sa = (String ) liststr.get(b); 246 liststr.remove(b); 247 liststr.add(a, sa); 248 249 list.setSelectedIndex(a); 250 } 251 } 252 else if(e.getActionCommand().equals("del")) 253 { 254 if(list.getSelectedIndex() != -1) 255 { 256 queue.remove(list.getSelectedIndex()); 257 liststr.remove(list.getSelectedIndex()); 258 list.setSelectedIndex(0); 259 } 260 } 261 262 else if(e.getActionCommand().equals("save")) 263 { 264 JFileChooser chooser = new JFileChooser(); 265 chooser.setDialogTitle("Save file"); 266 chooser.setDialogType(JFileChooser.SAVE_DIALOG); 267 268 int returnVal = chooser.showSaveDialog(new JDialog()); 269 270 if(returnVal == JFileChooser.APPROVE_OPTION) 271 { 272 File f = chooser.getSelectedFile(); 273 saveList(f); 274 } 275 } 276 277 else if(e.getActionCommand().equals("load")) 278 { 279 JFileChooser chooser = new JFileChooser(); 280 chooser.setDialogTitle("Open file"); 281 chooser.setDialogType(JFileChooser.OPEN_DIALOG); 282 283 int returnVal = chooser.showOpenDialog(new JDialog()); 284 285 if(returnVal == JFileChooser.APPROVE_OPTION) 286 { 287 File f = chooser.getSelectedFile(); 288 loadList(f); 289 } 290 } 291 } 292 293 private void saveList(File file) 294 { 295 try 296 { 297 if(file.exists()) 298 { 299 file.delete(); 300 } 301 302 FileOutputStream fos; 303 PrintStream f = new PrintStream((fos = new FileOutputStream(file))); 304 305 for(int i = 0; i <= queue.size(); i++) 306 { 307 QueueRecord rec = (QueueRecord) queue.get(i); 308 f.println(rec.type); 309 f.println(rec.hostname); 310 f.println(rec.username); 311 f.println(rec.password); 312 f.println(rec.port); 313 f.println(rec.file); 314 f.println(rec.local); 315 f.println(rec.remote); 316 f.println(rec.localip); 317 f.println(rec.domain); 318 } 319 320 fos.close(); 321 } 322 catch(Exception ex) 323 { 324 ex.printStackTrace(); 325 } 326 } 327 328 private void loadList(File file) 329 { 330 try 331 { 332 BufferedReader breader = new BufferedReader(new FileReader(file)); 333 boolean Eof = false; 334 queue.clear(); 335 liststr.clear(); 336 337 while(!Eof) 338 { 339 QueueRecord rec = new QueueRecord(); 340 rec.type = breader.readLine(); 341 rec.hostname = breader.readLine(); 342 rec.username = breader.readLine(); 343 rec.password = breader.readLine(); 344 rec.port = breader.readLine(); 345 rec.file = breader.readLine(); 346 rec.local = breader.readLine(); 347 rec.remote = breader.readLine(); 348 rec.localip = breader.readLine(); 349 rec.domain = breader.readLine(); 350 351 if(rec.hostname == null) 352 { 353 Eof = true; 354 } 355 else 356 { 357 queue.add(rec); 358 liststr.addElement(rec.hostname + " : " + rec.remote + 359 rec.file); 360 } 361 } 362 } 363 catch(Exception ex) 364 { 365 } 366 } 367 368 public void debug(String msg) 371 { 372 System.out.println(msg); 373 } 374 375 public void debugRaw(String msg) 377 { 378 System.out.print(msg); 379 } 380 381 public void debug(String msg, Throwable throwable) 383 { 384 } 385 386 public void warn(String msg) 387 { 388 } 389 390 public void warn(String msg, Throwable throwable) 391 { 392 } 393 394 public void error(String msg) 395 { 396 } 397 398 public void error(String msg, Throwable throwable) 399 { 400 } 401 402 public void info(String msg) 403 { 404 } 405 406 public void info(String msg, Throwable throwable) 407 { 408 } 409 410 public void fatal(String msg) 411 { 412 } 413 414 public void fatal(String msg, Throwable throwable) 415 { 416 } 417 418 class QueueRecord 419 { 420 public String type; 421 422 public String hostname; 424 public String username; 425 public String password; 426 public String port; 427 public String file; 428 429 public String local; 431 432 public String remote; 434 435 public String localip; 437 public String domain; 438 439 public QueueRecord() 440 { 441 } 442 } 443 444 class queueDownloader extends Thread implements ConnectionListener 445 { 446 public boolean block = false; 447 public boolean connected = false; 448 public QueueRecord last; 449 private FtpConnection conFtp; 450 451 public void run() 452 { 453 try 454 { 455 while((queue.size() >= 1) && !block) 456 { 457 QueueRecord rec = (QueueRecord) queue.get(0); 458 last = rec; 459 460 if(!connected) 461 { 462 con = new FtpConnection(rec.hostname, 463 Integer.parseInt(rec.port), "/"); 464 conFtp = (FtpConnection) con; 465 conFtp.addConnectionListener(this); 466 conFtp.setConnectionHandler(handler); 467 conFtp.login(rec.username, rec.password); 468 connected = true; 469 } 470 471 conFtp.chdirNoRefresh(rec.remote); 472 473 conFtp.setLocalPath(rec.local); 474 475 int i = 0; 476 477 while(!isThere) 478 { 479 i++; 480 481 if(i > NumRetry) 482 { 483 return; 484 } 485 486 try 487 { 488 Thread.sleep(10); 489 } 490 catch(Exception ex) 491 { 492 ex.printStackTrace(); 493 } 494 } 495 496 conFtp.download(rec.file); 497 498 statuslabel.setText(""); 499 500 if(queue.size() >= 1) 501 { 502 rec = (QueueRecord) queue.get(0); 503 504 if((rec.hostname.compareTo(last.hostname) == 0) && 505 (rec.port.compareTo(last.port) == 0) && 506 (rec.username.compareTo(last.username) == 0) && 507 (rec.password.compareTo(last.password) == 0)) 508 { 509 connected = true; 510 } 511 else 512 { 513 conFtp.disconnect(); 514 connected = false; 515 } 516 } 517 else 518 { 519 conFtp.disconnect(); 520 connected = false; 521 } 522 523 Thread.sleep(100); 524 } 525 } 526 catch(InterruptedException e) 527 { 528 } 529 530 if(connected) 531 { 532 con.disconnect(); 533 } 534 } 535 536 public void updateRemoteDirectory(BasicConnection con) 539 { 540 } 541 542 public void connectionInitialized(BasicConnection con) 544 { 545 isThere = true; 546 } 547 548 public void updateProgress(String file, String type, long bytes) 550 { 551 String strtmp; 552 553 if(StringUtils.getFile(file).compareTo("") == 0) 554 { 555 strtmp = "directory " + file; 556 } 557 else 558 { 559 strtmp = "file " + StringUtils.getFile(file); 560 } 561 562 statuslabel.setText("Downloading " + strtmp + " - kbyte " + 563 (bytes / 1024)); 564 565 String tmp; 566 567 if(type.length() >= 10) 568 { 569 tmp = type.substring(0, 9); 570 } 571 else 572 { 573 tmp = " "; 574 } 575 576 if(((type.compareTo(DataConnection.FINISHED) == 0) || 577 (tmp.compareTo(DataConnection.DFINISHED) == 0)) && !block) 578 { 579 queue.remove(0); 580 liststr.remove(0); 581 } 582 } 583 584 public void connectionFailed(BasicConnection con, String why) 586 { 587 System.out.println("connection failed!"); 588 } 589 590 public void actionFinished(BasicConnection con) 592 { 593 } 594 } 595 } 596 | Popular Tags |