1 16 package net.sf.jftp.gui.hostchooser; 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.GridLayout ; 23 import java.awt.Insets ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.ComponentEvent ; 27 import java.awt.event.ComponentListener ; 28 import java.awt.event.WindowEvent ; 29 import java.awt.event.WindowListener ; 30 import java.io.IOException ; 31 32 import javax.swing.JCheckBox ; 33 import javax.swing.JDialog ; 34 import javax.swing.JLabel ; 35 import javax.swing.JPanel ; 36 import javax.swing.JTextArea ; 37 38 import net.sf.jftp.JFtp; 39 import net.sf.jftp.config.LoadSet; 40 import net.sf.jftp.config.SaveSet; 41 import net.sf.jftp.config.Settings; 42 import net.sf.jftp.gui.base.FtpHost; 43 import net.sf.jftp.gui.framework.HButton; 44 import net.sf.jftp.gui.framework.HFrame; 45 import net.sf.jftp.gui.framework.HPanel; 46 import net.sf.jftp.gui.framework.HPasswordField; 47 import net.sf.jftp.gui.framework.HTextField; 48 import net.sf.jftp.gui.tasks.HostList; 49 import net.sf.jftp.net.FilesystemConnection; 50 import net.sf.jftp.net.FtpConnection; 51 import net.sf.jftp.net.FtpURLConnection; 52 import net.sf.jftp.net.StartConnection; 53 import net.sf.jftp.system.StringUtils; 54 import net.sf.jftp.system.logging.Log; 55 56 57 public class HostChooser extends HFrame implements ActionListener , 58 WindowListener 59 { 60 public HTextField host = new HTextField("Hostname:", "localhost "); 61 public HTextField user = new HTextField("Username:", "anonymous "); 62 63 public HPasswordField pass = new HPasswordField("Password:", 65 "none@nowhere.no"); 66 public HTextField port = new HTextField("Port: ", "21"); 67 public HTextField cwd = new HTextField("Remote: ", Settings.defaultDir); 68 public HTextField lcwd = new HTextField("Local: ", Settings.defaultWorkDir); 69 public HTextField dl = new HTextField("Max. connections: ", "3"); 70 public HTextField crlf = new HTextField("Override server newline: ", "<default>"); 71 private JCheckBox anonBox = new JCheckBox ("Use anonymous login", false); 72 private JCheckBox listBox = new JCheckBox ("LIST compatibility mode", false); 73 private JCheckBox dirBox = new JCheckBox ("Use default directories", 74 Settings.getUseDefaultDir()); 75 private JCheckBox modeBox = new JCheckBox ("Use active Ftp (no need to)", 76 false); 77 private JCheckBox threadBox = new JCheckBox ("Multiple connections", false); 78 private HPanel okP = new HPanel(); 79 private HButton ok = new HButton("Connect"); 80 private HButton backMode = new HButton("Yes"); 81 private HButton frontMode = new HButton("No"); 82 private HFrame h = new HFrame(); 83 private HPanel listP = new HPanel(); 84 private HButton list = new HButton("Choose from or edit list..."); 85 private ComponentListener listener = null; 86 private int mode = 0; 87 private boolean useLocal = false; 88 private boolean ext = Settings.showNewlineOption; 89 90 public HostChooser(ComponentListener l, boolean local) 91 { 92 listener = l; 93 useLocal = local; 94 init(); 97 } 98 99 public HostChooser(ComponentListener l) 100 { 101 listener = l; 102 init(); 103 } 104 105 public HostChooser() 106 { 107 init(); 108 } 109 110 public void init() 111 { 112 setTitle("Ftp Connection..."); 115 setBackground(okP.getBackground()); 116 getContentPane().setLayout(new GridLayout ((ext ? 8 : 7), 2, 5, 3)); 117 118 anonBox.setSelected(false); 119 user.setEnabled(true); 120 pass.text.setEnabled(true); 121 122 LoadSet l = new LoadSet(); 123 String [] login = l.loadSet(Settings.login_def); 124 125 if((login != null) && (login[0] != null)) 126 { 127 host.setText(login[0]); 128 user.setText(login[1]); 129 130 if(login[3] != null) 131 { 132 port.setText(login[3]); 133 } 134 135 if(login[4] != null) 136 { 137 cwd.setText(login[4]); 138 } 139 140 if(login[5] != null) 141 { 142 lcwd.setText(login[5]); 143 } 144 } 145 146 if(Settings.getStorePasswords()) 147 { 148 if(login != null) 149 { 150 pass.setText(login[2]); 151 } 152 } 153 else 154 { 155 pass.setText(""); 156 } 157 158 getContentPane().add(host); 159 getContentPane().add(port); 160 getContentPane().add(anonBox); 161 getContentPane().add(listBox); 162 getContentPane().add(user); 163 getContentPane().add(pass); 164 getContentPane().add(dirBox); 165 getContentPane().add(modeBox); 166 getContentPane().add(lcwd); 167 getContentPane().add(cwd); 168 getContentPane().add(threadBox); 169 getContentPane().add(dl); 170 171 if(ext) { 172 getContentPane().add(crlf); 173 174 JPanel x1 = new JPanel (); 175 x1.setLayout(new BorderLayout (2,2)); 176 JLabel l1 = new JLabel ("Unix: LF, Mac/MVS: CR, Win: CRLF"); 177 l1.setFont(new Font ("Dialog", Font.PLAIN, 10)); 178 JLabel l2 = new JLabel ("Don't change this unless you transfer text only"); 179 l2.setFont(new Font ("Dialog", Font.PLAIN, 10)); 180 x1.add("North", l1); 181 x1.add("South", l2); 182 getContentPane().add(x1); 183 } 184 185 modeBox.setSelected(!Settings.getFtpPasvMode()); 186 threadBox.setSelected(Settings.getEnableMultiThreading()); 187 dirBox.setSelected(Settings.getUseDefaultDir()); 188 anonBox.addActionListener(this); 189 threadBox.addActionListener(this); 190 191 getContentPane().add(okP); 192 okP.add(ok); 193 ok.addActionListener(this); 194 195 getContentPane().add(listP); 196 listP.add(list); 197 list.addActionListener(this); 198 199 dirBox.addActionListener(this); 200 setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 201 202 lcwd.setEnabled(!dirBox.isSelected()); 203 cwd.setEnabled(!dirBox.isSelected()); 204 pass.text.addActionListener(this); 205 206 pack(); 207 setModal(false); 208 setVisible(false); 209 invalidate(); 210 validate(); 211 addWindowListener(this); 212 prepareBackgroundMessage(); 213 } 214 215 public void update() 216 { 217 fixLocation(); 218 setVisible(true); 219 toFront(); 220 host.requestFocus(); 221 } 222 223 public void update(String url) 224 { 225 try 226 { 227 FtpURLConnection uc = new FtpURLConnection(new java.net.URL (url)); 228 FtpConnection con = uc.getFtpConnection(); 229 230 JFtp.statusP.jftp.addConnection(url, con); 231 232 uc.connect(); 236 237 int response = uc.getLoginResponse(); 238 239 if(response != FtpConnection.LOGIN_OK) 240 { 241 setTitle("Wrong password!"); 242 host.setText(uc.getHost()); 243 port.setText(Integer.toString(uc.getPort())); 244 user.setText(uc.getUser()); 245 pass.setText(uc.getPass()); 246 setVisible(true); 247 toFront(); 248 host.requestFocus(); 249 } 250 else 251 { 252 this.dispose(); 253 254 if(listener != null) 255 { 256 listener.componentResized(new ComponentEvent (this, 0)); 257 } 258 259 JFtp.mainFrame.setVisible(true); 260 JFtp.mainFrame.toFront(); 261 } 262 } 263 264 catch(IOException ex) 270 { 271 Log.debug("Error!"); 272 ex.printStackTrace(); 273 } 274 } 275 276 public void actionPerformed(ActionEvent e) 277 { 278 if((e.getSource() == ok) || (e.getSource() == pass.text)) 279 { 280 setCursor(new Cursor (Cursor.WAIT_CURSOR)); 283 284 FtpConnection con = null; 287 JFtp.setHost(host.getText()); 288 289 String htmp = StringUtils.cut(host.getText(), " "); 290 String utmp = StringUtils.cut(user.getText(), " "); 291 String ptmp = StringUtils.cut(pass.getText(), " "); 292 String potmp = StringUtils.cut(port.getText(), " "); 293 294 Settings.setProperty("jftp.ftpPasvMode", !modeBox.isSelected()); 295 Settings.setProperty("jftp.enableMultiThreading", 296 threadBox.isSelected()); 297 Settings.setProperty("jftp.useDefaultDir", dirBox.isSelected()); 298 299 if(listBox.isSelected()) 300 { 301 FtpConnection.LIST = "LIST"; 302 } 303 else 304 { 305 FtpConnection.LIST = "LIST -laL"; 306 } 307 308 309 JFtp.hostinfo.hostname = htmp; 310 JFtp.hostinfo.username = utmp; 311 JFtp.hostinfo.password = ptmp; 312 JFtp.hostinfo.port = potmp; 313 JFtp.hostinfo.type = "ftp"; 314 315 boolean pasv = Settings.getFtpPasvMode(); 316 boolean threads = Settings.getEnableMultiThreading(); 317 318 331 332 int x = Integer.parseInt(dl.getText().trim()); 333 Settings.maxConnections = x; 334 335 Settings.save(); 336 337 String dtmp; 339 String ltmp; 340 341 if(dirBox.isSelected()) 342 { 343 dtmp = Settings.defaultDir; 344 ltmp = Settings.defaultWorkDir; 345 } 346 else 347 { 348 dtmp = cwd.getText(); 349 ltmp = lcwd.getText(); 350 } 351 352 SaveSet s = new SaveSet(Settings.login_def, htmp, utmp, ptmp, 354 potmp, dtmp, ltmp); 355 356 if(JFtp.localDir instanceof FilesystemConnection) 357 { 358 if(!JFtp.localDir.setPath(ltmp)) 359 { 360 if(!JFtp.localDir.setPath(System.getProperty("user.home"))) 361 { 362 JFtp.localDir.setPath("/"); 363 } 364 } 365 } 366 367 int response = StartConnection.startFtpCon(htmp, utmp, ptmp, 368 Integer.parseInt(potmp), 369 dtmp, useLocal, crlf.getText().trim()); 370 371 390 397 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 401 this.dispose(); 402 403 JFtp.mainFrame.setVisible(true); 405 JFtp.mainFrame.toFront(); 406 407 if(listener != null) 408 { 409 listener.componentResized(new ComponentEvent (this, 0)); 410 } 411 412 } 414 else if(e.getSource() == list) 415 { 416 HostList hl = new HostList(this); 417 FtpHost selectedHost = hl.getFtpHost(); 418 419 if(selectedHost == null) 420 { 421 return; 422 } 423 424 host.setText(selectedHost.hostname); 425 pass.setText(selectedHost.password); 426 user.setText(selectedHost.username); 427 port.setText(selectedHost.port); 428 429 Settings.setProperty("jftp.useDefaultDir", true); 430 dirBox.setSelected(Settings.getUseDefaultDir()); 431 lcwd.setEnabled(!dirBox.isSelected()); 432 cwd.setEnabled(!dirBox.isSelected()); 433 } 434 else if(e.getSource() == dirBox) 435 { 436 if(!dirBox.isSelected()) 437 { 438 lcwd.setEnabled(true); 439 cwd.setEnabled(true); 440 } 441 else 442 { 443 lcwd.setEnabled(false); 444 cwd.setEnabled(false); 445 } 446 } 447 else if(e.getSource() == anonBox) 448 { 449 if(!anonBox.isSelected()) 450 { 451 user.setEnabled(true); 452 pass.text.setEnabled(true); 453 } 454 else 455 { 456 user.setText("anonymous"); 457 pass.setText("no@no.no"); 458 user.setEnabled(false); 459 pass.text.setEnabled(false); 460 } 461 } 462 else if(e.getSource() == threadBox) 463 { 464 if(threadBox.isSelected()) 465 { 466 dl.setEnabled(true); 467 } 468 else 469 { 470 dl.setEnabled(false); 471 } 472 } 473 else if(e.getSource() == backMode) 474 { 475 mode = 1; 476 h.setVisible(false); 477 ; 478 } 479 else if(e.getSource() == frontMode) 480 { 481 mode = 2; 482 h.setVisible(false); 483 } 484 } 485 486 495 private void prepareBackgroundMessage() 496 { 497 HPanel p = new HPanel(); 498 p.add(backMode); 499 p.add(frontMode); 500 p.setLayout(new FlowLayout (FlowLayout.CENTER)); 501 502 backMode.addActionListener(this); 503 frontMode.addActionListener(this); 504 505 h.getContentPane().setLayout(new BorderLayout (10, 10)); 506 h.setTitle("Connection failed!"); 507 h.setLocation(150, 200); 508 509 JTextArea text = new JTextArea (); 510 h.getContentPane().add("Center", text); 511 h.getContentPane().add("South", p); 512 text.setText(" ---------------- Output -----------------\n\n" + 513 "The server is busy at the moment.\n\n" + 514 "Do you want JFtp to go to disappear and try to login\n" + 515 "continuously?\n\n" + 516 "(It will show up again when it has initiated a connection)\n\n"); 517 JFtp.log.setText(""); 518 text.setEditable(false); 519 h.pack(); 520 } 521 522 public void windowClosing(WindowEvent e) 523 { 524 this.dispose(); 526 } 527 528 public void windowClosed(WindowEvent e) 529 { 530 } 531 532 public void windowActivated(WindowEvent e) 533 { 534 } 535 536 public void windowDeactivated(WindowEvent e) 537 { 538 } 539 540 public void windowIconified(WindowEvent e) 541 { 542 } 543 544 public void windowDeiconified(WindowEvent e) 545 { 546 } 547 548 public void windowOpened(WindowEvent e) 549 { 550 } 551 552 public Insets getInsets() 553 { 554 Insets std = super.getInsets(); 555 556 return new Insets (std.top + 10, std.left + 10, std.bottom + 10, 557 std.right + 10); 558 } 559 560 public void pause(int time) 561 { 562 try 563 { 564 Thread.sleep(time); 565 } 566 catch(Exception ex) 567 { 568 } 569 } 570 571 private void tryFtpAgain(int response, String htmp, String ptmp, 572 String utmp, String potmp, String dtmp, 573 boolean useLocal) 574 { 575 if((response == FtpConnection.OFFLINE) && Settings.reconnect) 580 { 581 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 583 584 h.setVisible(true); 585 586 while(mode == 0) 587 { 588 pause(10); 589 } 590 591 JFtp.mainFrame.setVisible(false); 592 593 while((response == FtpConnection.OFFLINE) && (mode == 1)) 594 { 595 System.out.print("Server is full, next attempt in "); 596 597 int r = 5; 598 599 for(int i = 0; i < r; r--) 600 { 601 System.out.print("" + r + "-"); 602 603 try 604 { 605 Thread.sleep(1000); 606 } 607 catch(Exception ex) 608 { 609 } 610 } 611 612 System.out.println("0..."); 613 614 621 response = StartConnection.startFtpCon(htmp, utmp, ptmp, 623 Integer.parseInt(potmp), 624 dtmp, useLocal); 625 } 626 627 if(mode == 1) 628 { 629 JFtp.mainFrame.setVisible(true); 630 } 631 else 632 { 633 JFtp.mainFrame.setVisible(false); 635 this.setVisible(true); 636 this.toFront(); 637 638 return; 639 } 640 } 641 else if((response != FtpConnection.LOGIN_OK) || 642 ((response == FtpConnection.OFFLINE) && 643 (!Settings.reconnect))) 644 { 645 setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 646 647 if(useLocal) 649 { 650 JFtp.statusP.jftp.closeCurrentLocalTab(); 651 } 652 else 653 { 654 JFtp.statusP.jftp.closeCurrentTab(); 655 } 656 657 return; 660 } 661 } 662 663 } 665 | Popular Tags |