1 16 package net.sf.jftp.gui.base; 17 18 import java.awt.BorderLayout ; 19 import java.awt.Cursor ; 20 import java.awt.FlowLayout ; 21 import java.awt.Font ; 22 import java.awt.Insets ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.KeyListener ; 27 import java.awt.event.MouseAdapter ; 28 import java.awt.event.MouseEvent ; 29 import java.awt.event.MouseListener ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.io.PrintStream ; 34 import java.lang.reflect.Array ; 35 import java.util.Date ; 36 37 import javax.swing.DefaultListModel ; 38 import javax.swing.ImageIcon ; 39 import javax.swing.JComboBox ; 40 import javax.swing.JEditorPane ; 41 import javax.swing.JLabel ; 42 import javax.swing.JList ; 43 import javax.swing.JMenuItem ; 44 import javax.swing.JOptionPane ; 45 import javax.swing.JPanel ; 46 import javax.swing.JPopupMenu ; 47 import javax.swing.JScrollPane ; 48 import javax.swing.JTextArea ; 49 import javax.swing.JToolBar ; 50 import javax.swing.SwingUtilities ; 51 import javax.swing.event.ListSelectionListener ; 52 53 import net.sf.jftp.JFtp; 54 import net.sf.jftp.config.SaveSet; 55 import net.sf.jftp.config.Settings; 56 import net.sf.jftp.gui.base.dir.DirCanvas; 57 import net.sf.jftp.gui.base.dir.DirCellRenderer; 58 import net.sf.jftp.gui.base.dir.DirComponent; 59 import net.sf.jftp.gui.base.dir.DirEntry; 60 import net.sf.jftp.gui.base.dir.DirLister; 61 import net.sf.jftp.gui.base.dir.DirPanel; 62 import net.sf.jftp.gui.base.dir.TableUtils; 63 import net.sf.jftp.gui.framework.HFrame; 64 import net.sf.jftp.gui.framework.HImage; 65 import net.sf.jftp.gui.framework.HImageButton; 66 import net.sf.jftp.gui.tasks.Creator; 67 import net.sf.jftp.gui.tasks.Displayer; 68 import net.sf.jftp.gui.tasks.PathChanger; 69 import net.sf.jftp.gui.tasks.RemoteCommand; 70 import net.sf.jftp.net.BasicConnection; 71 import net.sf.jftp.net.ConnectionListener; 72 import net.sf.jftp.net.FilesystemConnection; 73 import net.sf.jftp.net.FtpConnection; 74 import net.sf.jftp.net.SftpConnection; 75 import net.sf.jftp.net.SmbConnection; 76 import net.sf.jftp.system.LocalIO; 77 import net.sf.jftp.system.StringUtils; 78 import net.sf.jftp.system.UpdateDaemon; 79 import net.sf.jftp.system.logging.Log; 80 import net.sf.jftp.tools.Shell; 81 82 83 public class RemoteDir extends DirComponent implements ListSelectionListener , 84 ActionListener , 85 ConnectionListener, 86 KeyListener 87 { 88 static final String deleteString = "rm"; 90 static final String mkdirString = "mkdir"; 91 static final String refreshString = "fresh"; 92 static final String cdString = "cd"; 93 static final String cmdString = "cmd"; 94 static final String downloadString = "<-"; 95 static final String uploadString = "->"; 96 static final String queueString = "que"; 97 static final String cdUpString = "cdUp"; 98 static final String rnString = "rn"; 99 HImageButton deleteButton; 100 HImageButton mkdirButton; 101 HImageButton cmdButton; 102 HImageButton refreshButton; 103 HImageButton cdButton; 104 HImageButton uploadButton; 105 HImageButton downloadButton; 106 HImageButton queueButton; 107 HImageButton cdUpButton; 108 HImageButton rnButton; 109 110 private DirCanvas label = new DirCanvas(this); 112 private boolean pathChanged = true; 113 private boolean firstGui = true; 114 private int pos = 0; 115 private JPanel p = new JPanel (); 116 private JToolBar buttonPanel = new JToolBar () 117 { 118 public Insets getInsets() 119 { 120 return new Insets (0, 0, 0, 0); 121 } 122 }; 123 124 private JToolBar currDirPanel = new JToolBar () 126 { 127 public Insets getInsets() 128 { 129 return new Insets (0, 0, 0, 0); 130 } 131 }; 132 133 private DefaultListModel jlm; 134 private JScrollPane jsp = new JScrollPane (jl); 135 private int tmpindex = -1; 136 private HImageButton list = new HImageButton(Settings.listImage, "list", 137 "Show remote listing...", this); 138 private HImageButton transferType = new HImageButton(Settings.typeImage, 139 "type", 140 "Toggle transfer type...", 141 this); 142 private JPopupMenu popupMenu = new JPopupMenu (); 143 private JMenuItem props = new JMenuItem ("Properties"); 144 private DirEntry currentPopup = null; 145 private String sortMode = null; 146 String [] sortTypes = new String [] { "Normal", "Reverse", "Size", "Size/Re" }; 147 private JComboBox sorter = new JComboBox (sortTypes); 148 private boolean dateEnabled = false; 149 150 153 public RemoteDir() 154 { 155 type = "remote"; 156 con = new FilesystemConnection(); 157 con.addConnectionListener(this); 158 159 if(!con.chdir("/")) 160 { 161 con.chdir("C:\\"); 162 } 163 164 setDate(); 165 } 166 167 170 public RemoteDir(String path) 171 { 172 type = "remote"; 173 this.path = path; 174 con = new FilesystemConnection(); 175 con.addConnectionListener(this); 176 con.chdir(path); 177 178 setDate(); 179 } 180 181 184 public void gui_init() 185 { 186 setLayout(new BorderLayout ()); 187 currDirPanel.setFloatable(false); 188 buttonPanel.setFloatable(false); 189 190 FlowLayout f = new FlowLayout (FlowLayout.LEFT); 191 f.setHgap(1); 192 f.setVgap(2); 193 194 buttonPanel.setLayout(f); 195 buttonPanel.setMargin(new Insets (0, 0, 0, 0)); 196 197 props.addActionListener(this); 198 popupMenu.add(props); 199 200 rnButton = new HImageButton(Settings.textFileImage, rnString, 201 "Rename selected file or directory", this); 202 rnButton.setToolTipText("Rename selected"); 203 204 list.setToolTipText("Show remote listing..."); 205 transferType.setToolTipText("Toggle transfer type..."); 206 207 deleteButton = new HImageButton(Settings.deleteImage, deleteString, 208 "Delete selected", this); 209 deleteButton.setToolTipText("Delete selected"); 210 211 mkdirButton = new HImageButton(Settings.mkdirImage, mkdirString, 212 "Create a new directory", this); 213 mkdirButton.setToolTipText("Create directory"); 214 215 refreshButton = new HImageButton(Settings.refreshImage, refreshString, 216 "Refresh current directory", this); 217 refreshButton.setToolTipText("Refresh directory"); 218 refreshButton.setRolloverIcon(new ImageIcon (HImage.getImage(this, Settings.refreshImage2))); 219 refreshButton.setRolloverEnabled(true); 220 221 cdButton = new HImageButton(Settings.cdImage, cdString, 222 "Change directory", this); 223 cdButton.setToolTipText("Change directory"); 224 225 cmdButton = new HImageButton(Settings.cmdImage, cmdString, 226 "Execute remote command", this); 227 cmdButton.setToolTipText("Execute remote command"); 228 229 downloadButton = new HImageButton(Settings.downloadImage, 230 downloadString, "Download selected", 231 this); 232 downloadButton.setToolTipText("Download selected"); 233 234 queueButton = new HImageButton(Settings.downloadImage, queueString, 235 "Queue selected", this); 236 queueButton.setToolTipText("Queue selected"); 237 238 cdUpButton = new HImageButton(Settings.cdUpImage, cdUpString, 239 "Go to Parent Directory", this); 240 cdUpButton.setToolTipText("Go to Parent Directory"); 241 242 setLabel(); 245 label.setSize(getSize().width - 10, 24); 246 currDirPanel.add(label); 247 currDirPanel.setSize(getSize().width - 10, 32); 248 label.setSize(getSize().width - 20, 24); 249 250 p.setLayout(new BorderLayout ()); 251 p.add("North", currDirPanel); 252 253 buttonPanel.add(downloadButton); 254 255 buttonPanel.add(queueButton); 257 258 buttonPanel.add(new JLabel (" ")); 259 260 buttonPanel.add(refreshButton); 263 buttonPanel.add(new JLabel (" ")); 264 buttonPanel.add(rnButton); 265 buttonPanel.add(mkdirButton); 266 buttonPanel.add(cdButton); 267 buttonPanel.add(deleteButton); 268 buttonPanel.add(cdUpButton); 269 buttonPanel.add(new JLabel (" ")); 270 271 buttonPanel.add(cmdButton); 272 buttonPanel.add(list); 273 buttonPanel.add(transferType); 274 275 buttonPanel.add(sorter); 277 278 buttonPanel.setVisible(true); 279 280 buttonPanel.setSize(getSize().width - 10, 32); 281 282 p.add("West", buttonPanel); 283 add("North", p); 284 285 sorter.addActionListener(this); 286 287 jlm = new DefaultListModel (); 289 jl = new JList (jlm); 290 jl.setCellRenderer(new DirCellRenderer()); 291 jl.setVisibleRowCount(Settings.visibleFileRows); 292 jl.setDropTarget(JFtp.statusP.jftp.dropTarget); 293 294 MouseListener mouseListener = new MouseAdapter () 296 { 297 public void mousePressed(MouseEvent e) 298 { 299 if(JFtp.uiBlocked) 300 { 301 return; 302 } 303 304 if(e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e)) 305 { 306 int index = jl.getSelectedIndex() - 1; 307 308 if(index < -1) 309 { 310 return; 311 } 312 313 String tgt = (String ) jl.getSelectedValue().toString(); 314 315 if(index < 0) 316 { 317 } 318 else if((dirEntry == null) || (dirEntry.length < index) || 319 (dirEntry[index] == null)) 320 { 321 return; 322 } 323 else 324 { 325 currentPopup = dirEntry[index]; 326 popupMenu.show(e.getComponent(), e.getX(), e.getY()); 327 } 328 } 329 } 330 331 public void mouseClicked(MouseEvent e) 332 { 333 if(JFtp.uiBlocked) 334 { 335 return; 336 } 337 338 if(Settings.getUseJTableLayout()) { 339 TableUtils.copyTableSelectionsToJList(jl, table); 340 } 341 342 if(e.getClickCount() == 2) 344 { 345 int index = jl.getSelectedIndex() - 1; 347 348 if(index < -1) 350 { 351 return; 352 } 353 354 String tgt = (String ) jl.getSelectedValue().toString(); 355 356 if(index < 0) 357 { 358 if(JFtp.mainFrame != null) 359 { 360 JFtp.mainFrame.setCursor(Cursor.WAIT_CURSOR); 361 } 362 363 con.chdir(path + tgt); 364 365 if(JFtp.mainFrame != null) 366 { 367 JFtp.mainFrame.setCursor(Cursor.DEFAULT_CURSOR); 368 } 369 } 370 else if((dirEntry == null) || (dirEntry.length < index) || 371 (dirEntry[index] == null)) 372 { 373 return; 374 } 375 else if(dirEntry[index].isDirectory()) 376 { 377 con.chdir(path + tgt); 378 } 379 else if(dirEntry[index].isLink()) 380 { 381 if(!con.chdir(path + tgt)) 382 { 383 showContentWindow(path + 384 dirEntry[index].toString(), 385 dirEntry[index]); 386 387 } 389 } 390 else 391 { 392 showContentWindow(path + dirEntry[index].toString(), 393 dirEntry[index]); 394 395 } 397 } 398 } 399 }; 400 401 if(Settings.newTableGui) { 402 jsp = new JScrollPane (table); 403 table.getSelectionModel().addListSelectionListener(this); 404 table.addMouseListener(mouseListener); 405 } 406 else { 407 jsp = new JScrollPane (jl); 408 jl.addListSelectionListener(this); 409 jl.addKeyListener(this); 410 jl.addMouseListener(mouseListener); 411 jl.requestFocus(); 412 } 413 414 415 416 jsp.setSize(getSize().width - 20, getSize().height - 72); 417 add("Center", jsp); 418 jsp.setVisible(true); 419 420 TableUtils.tryToEnableRowSorting(table); 421 422 if(Settings.IS_JAVA_1_6) { 423 buttonPanel.remove(sorter); 425 } 426 427 setVisible(true); 428 } 429 430 public void setViewPort() 431 { 432 } 433 434 private void setLabel() 435 { 436 if(con instanceof FilesystemConnection) 437 { 438 label.setText("Filesystem: " + StringUtils.cutPath(path)); 439 } 440 else if(con instanceof FtpConnection) 441 { 442 label.setText("Ftp: " + StringUtils.cutPath(path)); 443 } 444 else if(con instanceof SftpConnection) 445 { 446 label.setText("Sftp: " + StringUtils.cutPath(path)); 447 } 448 else 449 { 450 label.setText(StringUtils.cutPath(path)); 451 } 452 } 453 454 458 public void gui(boolean fakeInit) 459 { 460 if(firstGui) 461 { 462 gui_init(); 463 firstGui = false; 464 } 465 466 setLabel(); 467 468 if(con instanceof FtpConnection) 469 { 470 list.show(); 471 cmdButton.show(); 472 transferType.show(); 473 } 474 else 475 { 476 list.hide(); 477 cmdButton.hide(); 478 transferType.hide(); 479 } 480 481 if(!fakeInit) 482 { 483 setDirList(false); 484 } 485 486 invalidate(); 488 validate(); 489 490 } 492 493 497 public void setDirList(boolean fakeInit) 498 { 499 jlm = new DefaultListModel (); 500 501 DirEntry dwn = new DirEntry("..", this); 502 dwn.setDirectory(); 503 jlm.addElement(dwn); 504 505 if(!fakeInit) 506 { 507 if(pathChanged) 508 { 509 pathChanged = false; 510 511 DirLister dir = new DirLister(con, sortMode); 513 514 while(!dir.finished) 515 { 516 LocalIO.pause(10); 517 } 518 519 if(dir.isOk()) 520 { 521 length = dir.getLength(); 523 dirEntry = new DirEntry[length]; 524 files = dir.list(); 525 526 String [] fSize = dir.sList(); 527 int[] perms = dir.getPermissions(); 528 529 532 595 596 for(int i = 0; i < length; i++) 598 { 599 if((files == null) || (files[i] == null)) 601 { 602 System.out.println("skipping setDirList, files or files[i] is null!"); 604 605 return; 606 607 } 609 610 dirEntry[i] = new DirEntry(files[i], this); 612 613 if(dirEntry[i] == null) 614 { 615 System.out.println("\nskipping setDirList, dirEntry[i] is null!"); 616 617 return; 618 } 619 620 if(dirEntry[i].file == null) 621 { 622 System.out.println("\nskipping setDirList, dirEntry[i].file is null!"); 623 624 return; 625 } 626 627 if(perms != null) 628 { 629 dirEntry[i].setPermission(perms[i]); 630 } 631 632 if(fSize[i].startsWith("@")) 633 { 634 fSize[i] = fSize[i].substring(1); 635 } 636 637 dirEntry[i].setFileSize(Long.parseLong(fSize[i])); 638 639 if(dirEntry[i].file.endsWith("/")) 640 { 641 dirEntry[i].setDirectory(); 642 } 643 else 644 { 645 dirEntry[i].setFile(); 646 } 647 648 if(dirEntry[i].file.endsWith("###")) 649 { 650 dirEntry[i].setLink(); 651 } 652 653 Object [] d = dir.getDates(); 655 656 if(d != null) 657 { 658 dirEntry[i].setDate((Date ) d[i]); 659 } 660 661 jlm.addElement(dirEntry[i]); 663 } 664 } 665 else 666 { 667 Log.debug("Not a directory: " + path); 668 } 669 } 670 671 } 673 674 jl.setModel(jlm); 675 jl.grabFocus(); 676 jl.setSelectedIndex(0); 677 678 683 update(); 684 } 685 686 689 public void actionPerformed(ActionEvent e) 690 { 691 if(JFtp.uiBlocked) 692 { 693 return; 694 } 695 696 if(e.getActionCommand().equals("rm")) 697 { 698 lock(false); 699 700 if(Settings.getAskToDelete()) 701 { 702 if(!UITool.askToDelete(this)) 703 { 704 unlock(false); 705 706 return; 707 } 708 } 709 710 for(int i = 0; i < length; i++) 711 { 712 if(dirEntry[i].selected) 713 { 714 con.removeFileOrDir(dirEntry[i].file); 715 } 716 } 717 718 unlock(false); 719 fresh(); 720 } 721 else if(e.getActionCommand().equals("mkdir")) 722 { 723 Creator c = new Creator("Create:", con); 724 725 } 728 else if(e.getActionCommand().equals("cmd")) 729 { 730 if(!(con instanceof FtpConnection)) 731 { 732 Log.debug("This feature is for ftp only."); 733 734 return; 735 } 736 737 738 int opt = JOptionPane.showOptionDialog(this, "Would you like to type one command or to open a shell?","Question", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, 739 new ImageIcon (HImage.getImage(this, Settings.helpImage)), new String [] {"Shell","Command", "Cancel"}, "Command"); 740 741 if(opt == 1) { 742 RemoteCommand rc = new RemoteCommand(); 743 } 744 else if(opt == 0) { 745 FtpConnection conn = (FtpConnection) con; 746 Shell s = new Shell(conn.getCommandInputReader(), conn.getCommandOutputStream()); 747 } 748 749 } 751 else if(e.getActionCommand().equals("cd")) 752 { 753 PathChanger pthc = new PathChanger("remote"); 754 755 } 759 else if(e.getActionCommand().equals("fresh")) 760 { 761 fresh(); 762 } 763 else if(e.getActionCommand().equals("->")) 764 { 765 blockedTransfer(-2); 766 } 767 else if(e.getActionCommand().equals("<-")) 768 { 769 blockedTransfer(-2); 770 } 771 else if(e.getActionCommand().equals("list")) 772 { 773 try 774 { 775 if(!(con instanceof FtpConnection)) { 776 Log.debug("Can only list FtpConnection output!"); 777 } 778 779 PrintStream out = new PrintStream (Settings.ls_out); 780 for(int i=0; i<((FtpConnection)con).currentListing.size(); i++) { 781 out.println(((FtpConnection)con).currentListing.get(i)); 782 } 783 out.flush(); 784 out.close(); 785 786 java.net.URL url = new java.io.File (Settings.ls_out).toURL(); 787 Displayer d = new Displayer(url, new Font ("monospaced",Font.PLAIN, 11)); 788 JFtp.desktop.add(d, new Integer (Integer.MAX_VALUE - 13)); 789 } 790 catch(java.net.MalformedURLException ex) 791 { 792 ex.printStackTrace(); 793 Log.debug("ERROR: Malformed URL!"); 794 } 795 catch(FileNotFoundException ex2) 796 { 797 ex2.printStackTrace(); 798 Log.debug("ERROR: File not found!"); 799 } 800 } 801 else if(e.getActionCommand().equals("type") && (!JFtp.uiBlocked)) 802 { 803 if(!(con instanceof FtpConnection)) 804 { 805 Log.debug("You can only set the transfer type for ftp connections."); 806 807 return; 808 } 809 810 FtpConnection c = (FtpConnection) con; 811 String t = c.getTypeNow(); 812 boolean ret = false; 813 814 if(t.equals(FtpConnection.ASCII)) 815 { 816 ret = c.type(FtpConnection.BINARY); 817 } 818 else if(t.equals(FtpConnection.BINARY)) 819 { 820 ret = c.type(FtpConnection.EBCDIC); 821 } 822 823 if(t.equals(FtpConnection.EBCDIC) || 824 (!ret && !t.equals(FtpConnection.L8))) 825 { 826 ret = c.type(FtpConnection.L8); 827 } 828 829 if(!ret) 830 { 831 c.type(FtpConnection.ASCII); 832 Log.debug("Warning: type should be \"I\" if you want to transfer binary files!"); 833 } 834 835 Log.debug("Type is now " + c.getTypeNow()); 836 } 837 838 else if(e.getActionCommand().equals("que")) 839 { 840 if(!(con instanceof FtpConnection)) 841 { 842 Log.debug("Queue supported only for FTP"); 843 844 return; 845 } 846 847 Object [] o = jl.getSelectedValues(); 848 DirEntry[] tmp = new DirEntry[Array.getLength(o)]; 849 850 for(int i = 0; i < Array.getLength(o); i++) 851 { 852 tmp[i] = (DirEntry) o[i]; 853 JFtp.dQueue.addFtp(tmp[i].toString()); 854 } 855 } 856 else if(e.getSource() == props) 857 { 858 JFtp.statusP.jftp.clearLog(); 859 860 int x = currentPopup.getPermission(); 861 String tmp; 862 863 if(x == FtpConnection.R) 864 { 865 tmp = "read only"; 866 } 867 else if(x == FtpConnection.W) 868 { 869 tmp = "read/write"; 870 } 871 else if(x == FtpConnection.DENIED) 872 { 873 tmp = "denied"; 874 } 875 else 876 { 877 tmp = "undefined"; 878 } 879 880 String msg = "File: " + currentPopup.toString() + "\n" + " Size: " + 881 currentPopup.getFileSize() + " raw size: " + 882 currentPopup.getRawSize() + "\n" + " Symlink: " + 883 currentPopup.isLink() + "\n" + " Directory: " + 884 currentPopup.isDirectory() + "\n" + " Permission: " + 885 tmp + "\n"; 886 Log.debug(msg); 887 } 888 else if(e.getSource() == sorter) 889 { 890 sortMode = (String ) sorter.getSelectedItem(); 891 892 if(sortMode.equals("Date")) 893 { 894 Settings.showDateNoSize = true; 895 } 896 else 897 { 898 Settings.showDateNoSize = false; 899 } 900 901 fresh(); 902 } 903 else if(e.getActionCommand().equals("cdUp")) 904 { 905 JFtp.remoteDir.getCon().chdir(".."); 906 } 907 else if(e.getActionCommand().equals("rn")) 908 { 909 Object [] target = jl.getSelectedValues(); 910 911 if((target == null) || (target.length == 0)) 912 { 913 Log.debug("No file selected"); 914 915 return; 916 } 917 else if(target.length > 1) 918 { 919 Log.debug("Too many files selected"); 920 921 return; 922 } 923 924 String val = JOptionPane.showInternalInputDialog(this, 925 "Choose a name..."); 926 927 if(val != null) 928 { 929 if(!con.rename(target[0].toString(), val)) 930 { 931 Log.debug("Rename failed."); 932 } 933 else 934 { 935 Log.debug("Successfully renamed."); 936 fresh(); 937 } 938 } 939 } 940 } 941 942 945 public synchronized void blockedTransfer(int index) 946 { 947 tmpindex = index; 948 949 Runnable r = new Runnable () 950 { 951 public void run() 952 { 953 boolean block = !Settings.getEnableMultiThreading(); 954 955 if(!(con instanceof FtpConnection)) 956 { 957 block = true; 958 } 959 960 if(block) 961 { 962 lock(false); 963 } 964 965 transfer(tmpindex); 966 967 if(block) 968 { 969 JFtp.localDir.fresh(); 970 unlock(false); 971 } 972 } 973 }; 974 975 Thread t = new Thread (r); 976 t.start(); 977 } 978 979 982 public void lock(boolean first) 983 { 984 JFtp.uiBlocked = true; 985 jl.setEnabled(false); 986 987 if(!first) 988 { 989 JFtp.localDir.lock(true); 990 } 991 992 Log.out("ui locked."); 993 } 994 995 998 public void unlock(boolean first) 999 { 1000 JFtp.uiBlocked = false; 1001 jl.setEnabled(true); 1002 1003 if(!first) 1004 { 1005 JFtp.localDir.unlock(true); 1006 } 1007 1008 Log.out("ui unlocked."); 1009 } 1010 1011 1015 public void fresh() 1016 { 1017 Log.out("fresh() called."); 1018 1019 Cursor x = null; 1020 1021 if(JFtp.mainFrame != null) 1022 { 1023 x = JFtp.mainFrame.getCursor(); 1024 JFtp.mainFrame.setCursor(Cursor.WAIT_CURSOR); 1025 } 1026 1027 String i = ""; 1029 int idx = jl.getSelectedIndex(); 1030 1031 if(idx >= 0) 1032 { 1033 Object o = jl.getSelectedValue(); 1034 1035 if(o != null) 1036 { 1037 i = o.toString(); 1038 } 1039 } 1040 1041 con.chdir(path); 1042 1043 if((idx >= 0) && (idx < jl.getModel().getSize())) 1044 { 1045 if(jl.getModel().getElementAt(idx).toString().equals(i)) 1046 { 1047 jl.setSelectedIndex(idx); 1048 } 1049 else 1050 { 1051 jl.setSelectedIndex(0); 1052 } 1053 } 1054 1055 update(); 1056 1057 if((JFtp.mainFrame != null) && (x.getType() != Cursor.WAIT_CURSOR)) 1058 { 1059 JFtp.mainFrame.setCursor(Cursor.DEFAULT_CURSOR); 1060 } 1061 } 1062 1063 1066 public void updateProgress(String file, String type, long bytes) 1067 { 1068 if((dList == null) || (dirEntry == null)) 1069 { 1070 return; 1071 } 1072 1073 boolean flag = false; 1074 1075 if(file.endsWith("/") && (file.length() > 1)) 1076 { 1077 flag = true; 1078 file = file.substring(0, file.lastIndexOf("/")); 1079 } 1080 1081 file = file.substring(file.lastIndexOf("/") + 1); 1082 1083 if(flag) 1084 { 1085 file = file + "/"; 1086 } 1087 1088 long s = 0; 1089 1090 if(JFtp.dList.sizeCache.containsKey(file)) 1091 { 1092 s = ((Long ) JFtp.dList.sizeCache.get(file)).longValue(); 1093 } 1094 else 1095 { 1096 for(int i = 0; i < dirEntry.length; i++) 1097 { 1098 if(dirEntry[i] == null) 1099 { 1100 continue; 1101 } 1102 1103 if(dirEntry[i].toString().equals(file)) 1104 { 1105 s = dirEntry[i].getRawSize(); 1106 JFtp.dList.sizeCache.put(file, new Long (s)); 1107 1108 break; 1109 } 1110 } 1111 1112 if(s <= 0) 1113 { 1114 File f = new File (JFtp.localDir.getPath() + file); 1115 1116 if(f.exists()) 1117 { 1118 s = f.length(); 1119 } 1120 } 1121 } 1122 1123 dList.updateList(file, type, bytes, s); 1124 } 1125 1126 1129 public void connectionInitialized(BasicConnection con) 1130 { 1131 if(con == null) 1132 { 1133 return; 1134 } 1135 1136 setDate(); 1137 1138 Log.out("remote connection initialized"); 1139 } 1140 1141 1144 public void connectionFailed(BasicConnection con, String reason) 1145 { 1146 Log.out("remote connection failed"); 1147 1148 if((Integer.parseInt(reason) == FtpConnection.OFFLINE) && 1149 Settings.reconnect) 1150 { 1151 return; 1152 } 1153 1154 HFrame h = new HFrame(); 1156 h.getContentPane().setLayout(new BorderLayout (10, 10)); 1157 h.setTitle("Connection failed!"); 1158 h.setLocation(150, 200); 1159 1160 JTextArea text = new JTextArea (); 1161 h.getContentPane().add("Center", text); 1162 text.setText(" ---------------- Output -----------------\n" + 1163 JFtp.log.getText()); 1164 JFtp.log.setText(""); 1165 text.setEditable(false); 1166 h.pack(); 1167 h.show(); 1168 } 1169 1170 private void setDate() 1171 { 1172 if(!(con instanceof FtpConnection) && 1173 !(con instanceof FilesystemConnection)) 1174 { 1175 try 1176 { 1177 sorter.removeItem("Date"); 1178 } 1179 catch(Exception ex) 1180 { 1181 } 1182 1183 dateEnabled = false; 1184 1185 return; 1186 } 1187 1188 if((con instanceof FtpConnection) && 1190 (((FtpConnection) con).dateVector.size() > 0)) 1191 { 1192 if(!dateEnabled) 1193 { 1194 sorter.addItem("Date"); 1195 dateEnabled = true; 1196 UpdateDaemon.updateRemoteDirGUI(); 1197 } 1198 } 1199 else if((con instanceof FilesystemConnection) && 1200 (((FilesystemConnection) con).dateVector.size() > 0)) 1201 { 1202 if(!dateEnabled) 1203 { 1204 sorter.addItem("Date"); 1205 dateEnabled = true; 1206 UpdateDaemon.updateRemoteDirGUI(); 1207 } 1208 } 1209 else 1210 { 1211 if(dateEnabled) 1212 { 1213 try 1214 { 1215 sorter.removeItem("Date"); 1216 dateEnabled = false; 1217 Settings.showDateNoSize = false; 1218 UpdateDaemon.updateRemoteDirGUI(); 1219 } 1220 catch(Exception ex) 1221 { 1222 } 1223 } 1224 } 1225 } 1226 1227 1230 public void updateRemoteDirectory(BasicConnection c) 1231 { 1232 if(con == null) 1234 { 1235 return; 1236 } 1237 1238 if((c != con) && !c.hasUploaded && con instanceof FtpConnection) 1239 { 1240 return; 1242 } 1243 1244 setDate(); 1246 1247 if(con instanceof FtpConnection) 1248 { 1249 path = ((FtpConnection) con).getCachedPWD(); 1250 } 1251 else if(con instanceof SmbConnection && !path.startsWith("smb://")) 1252 { 1253 path = c.getPWD(); 1254 } 1255 else 1256 { 1257 path = con.getPWD(); 1258 } 1259 1260 if((c != null) && (c instanceof FtpConnection)) 1262 { 1263 FtpConnection con = (FtpConnection) c; 1264 1265 String tmp = con.getCachedPWD(); 1266 SaveSet s = new SaveSet(Settings.login_def, con.getHost(), 1267 con.getUsername(), con.getPassword(), 1268 Integer.toString(con.getPort()), tmp, 1269 con.getLocalPath()); 1270 } 1271 else if((c != null) && (c instanceof FilesystemConnection)) 1272 { 1273 JFtp.localDir.getCon().setLocalPath(path); 1274 } 1275 1276 pathChanged = true; 1279 gui(false); 1280 1281 UpdateDaemon.updateLog(); 1282 1283 } 1286 1287 1290 public synchronized void transfer() 1291 { 1292 boolean[] bFileSelected = new boolean[dirEntry.length + 1]; 1293 DirEntry[] cacheEntry = new DirEntry[dirEntry.length]; 1294 System.arraycopy(dirEntry, 0, cacheEntry, 0, cacheEntry.length); 1295 1296 for(int i = 0; i < dirEntry.length; i++) 1297 { 1298 bFileSelected[i] = cacheEntry[i].selected; 1299 1300 if(!cacheEntry[i].equals(dirEntry[i])) 1301 { 1302 Log.out("mismatch"); 1303 } 1304 } 1305 1306 for(int i = 0; i < cacheEntry.length; i++) 1307 { 1308 if(bFileSelected[i]) 1309 { 1310 startTransfer(cacheEntry[i]); 1311 } 1312 } 1313 } 1314 1315 1324 public void startTransfer(DirEntry entry) 1325 { 1326 if(con instanceof FtpConnection && 1327 JFtp.localDir.getCon() instanceof FtpConnection) 1328 { 1329 if(entry.isDirectory()) 1330 { 1331 Log.debug("Directory transfer between remote connections is not supported yet!"); 1332 1333 return; 1334 } 1335 1336 Log.out("direct ftp transfer started (download)"); 1337 ((FtpConnection) JFtp.localDir.getCon()).upload(entry.file, 1338 ((FtpConnection) JFtp.remoteDir.getCon()).getDownloadInputStream(path + 1339 entry.file)); 1340 } 1341 else if(con instanceof FtpConnection && 1342 JFtp.localDir.getCon() instanceof FilesystemConnection) 1343 { 1344 int status = checkForExistingFile(entry); 1346 1347 if(status >= 0) 1348 { 1349 1353 1369 long s = entry.getRawSize(); 1370 JFtp.dList.sizeCache.put(entry.file, new Long (s)); 1371 1372 if((entry.getRawSize() < Settings.smallSize) && 1374 !entry.isDirectory()) 1375 { 1376 con.download(entry.file); 1377 } 1378 else 1379 { 1380 con.handleDownload(path + entry.file); 1381 } 1382 } 1383 } 1384 else if(con instanceof FilesystemConnection && 1385 JFtp.localDir.getCon() instanceof FtpConnection) 1386 { 1387 try 1388 { 1389 File f = new File (path + entry.file); 1390 FileInputStream in = new FileInputStream (f); 1391 JFtp.localDir.getCon().setLocalPath(path); 1392 Log.debug(JFtp.localDir.getCon().getPWD()); 1393 ((FtpConnection) JFtp.localDir.getCon()).upload(entry.file, in); 1394 } 1395 catch(FileNotFoundException ex) 1396 { 1397 Log.debug("Error: File not found: " + path + entry.file); 1398 } 1399 } 1400 else if(con instanceof FilesystemConnection && 1401 JFtp.localDir.getCon() instanceof FilesystemConnection) 1402 { 1403 con.download(path + entry.file); 1404 JFtp.localDir.actionPerformed(con, ""); 1405 } 1406 else if(JFtp.localDir.getCon() instanceof FilesystemConnection) 1407 { 1408 con.handleDownload(entry.file); 1410 JFtp.localDir.actionPerformed(con, ""); 1411 } 1412 else 1413 { 1414 if(entry.isDirectory()) 1415 { 1416 Log.debug("Directory transfer between remote connections is not supported yet!"); 1417 1418 return; 1419 } 1420 1421 Log.out("direct transfer started (download)"); 1422 JFtp.localDir.getCon().upload(entry.file, 1423 JFtp.remoteDir.getCon() 1424 .getDownloadInputStream(path + 1425 entry.file)); 1426 JFtp.localDir.actionPerformed(con, "FRESH"); 1427 } 1428 } 1429 1430 1433 public void transfer(int i) 1434 { 1435 if(i == -2) 1436 { 1437 transfer(); 1438 1439 return; 1440 } 1441 else if(dirEntry[i].selected) 1442 { 1443 startTransfer(dirEntry[i]); 1444 } 1445 } 1446 1447 1450 private int checkForExistingFile(DirEntry dirEntry) 1451 { 1452 File f = new File (JFtp.localDir.getPath() + dirEntry.file); 1453 1454 if(f.exists() && Settings.enableResuming && Settings.askToResume) 1455 { 1456 ResumeDialog r = new ResumeDialog(dirEntry); 1458 return -1; 1459 } 1460 1461 return 1; 1462 } 1463 1464 1467 public void actionFinished(BasicConnection c) 1468 { 1469 JFtp.localDir.actionPerformed(c, "LOWFRESH"); 1470 1471 if(c instanceof FtpConnection) 1472 { 1473 if(((FtpConnection) c).hasUploaded) 1474 { 1475 Log.out("actionFinished called by upload: " + c); 1476 1477 UpdateDaemon.updateRemoteDir(); 1479 } 1480 1481 Log.out("actionFinished called by download: " + c); 1482 } 1483 else 1484 { 1485 Log.out("actionFinished called by: " + c); 1486 1487 UpdateDaemon.updateRemoteDir(); 1489 } 1490 1491 UpdateDaemon.updateLog(); 1493 } 1494 1495 1498 public void actionPerformed(Object target, String msg) 1499 { 1500 if(msg.equals(type)) 1501 { 1502 UpdateDaemon.updateRemoteDirGUI(); 1503 1504 } 1507 else if(msg.equals("FRESH")) 1508 { 1509 UpdateDaemon.updateRemoteDir(); 1510 1511 } 1513 1514 UpdateDaemon.updateLog(); 1517 } 1518 1519 1522 public void showContentWindow(String url, DirEntry d) 1523 { 1524 try 1525 { 1526 if(d.getRawSize() > 200000) 1527 { 1528 Log.debug("File is too big - 200kb is the maximum, sorry."); 1529 1530 return; 1531 } 1532 1533 String path = JFtp.localDir.getPath(); 1534 1535 if(!path.endsWith("/")) 1536 { 1537 path = path + "/"; 1538 } 1539 1540 if(!new File (path + StringUtils.getFile(url)).exists()) 1541 { 1542 con.download(url); 1543 } 1544 else 1545 { 1546 Log.debug("\nRemote file must be downloaded to be viewed and\n" + 1547 " you already have a local copy present, pleasen rename it\n" + 1548 " and try again."); 1549 1550 return; 1551 } 1552 1553 File file = new File (JFtp.localDir.getPath() + 1554 StringUtils.getFile(url)); 1555 1556 if(!file.exists()) 1557 { 1558 Log.debug("File not found: " + JFtp.localDir.getPath() + 1559 StringUtils.getFile(url)); 1560 } 1561 1562 HFrame f = new HFrame(); 1563 f.setTitle(url); 1564 1565 JEditorPane pane = new JEditorPane ("file://" + 1566 file.getAbsolutePath()); 1567 1568 if(!pane.getEditorKit().getContentType().equals("text/html") && 1569 !pane.getEditorKit().getContentType().equals("text/rtf")) 1570 { 1571 if(!pane.getEditorKit().getContentType().equals("text/plain")) 1572 { 1573 Log.debug("Nothing to do with this filetype - use the buttons if you want to transfer files."); 1574 1575 return; 1576 } 1577 1578 pane.setEditable(false); 1579 } 1580 1581 JScrollPane jsp = new JScrollPane (pane); 1582 1583 f.getContentPane().setLayout(new BorderLayout ()); 1584 f.getContentPane().add("Center", jsp); 1585 f.setModal(false); 1586 f.setLocation(100, 100); 1587 f.setSize(600, 400); 1588 1589 f.show(); 1591 1592 dList.fresh(); 1593 JFtp.localDir.getCon().removeFileOrDir(StringUtils.getFile(url)); 1594 JFtp.localDir.fresh(); 1595 } 1596 catch(Exception ex) 1597 { 1598 Log.debug("File error: " + ex); 1599 } 1600 } 1601 1602 public void keyPressed(KeyEvent e) 1603 { 1604 if(e.getKeyCode() == KeyEvent.VK_ENTER) 1605 { 1606 Object o = jl.getSelectedValue(); 1607 1608 if(o == null) 1609 { 1610 return; 1611 } 1612 1613 String tmp = ((DirEntry) o).toString(); 1614 1615 if(tmp.endsWith("/")) 1616 { 1617 con.chdir(tmp); 1618 } 1619 else 1620 { 1621 showContentWindow(path + tmp, (DirEntry) o); 1622 } 1623 } 1624 else if(e.getKeyCode() == KeyEvent.VK_SPACE) 1625 { 1626 int x = ((DirPanel) JFtp.localDir).jl.getSelectedIndex(); 1627 1628 if(x == -1) 1629 { 1630 x = 0; 1631 } 1632 1633 ((DirPanel) JFtp.localDir).jl.grabFocus(); 1634 ((DirPanel) JFtp.localDir).jl.setSelectedIndex(x); 1635 } 1636 } 1637 1638 public void keyReleased(KeyEvent e) 1639 { 1640 } 1641 1642 public void keyTyped(KeyEvent e) 1643 { 1644 } 1645} 1646 | Popular Tags |