1 16 package net.sf.jftp.gui.tasks; 17 18 import net.sf.jftp.config.*; 19 import net.sf.jftp.gui.base.FtpHost; 20 import net.sf.jftp.gui.framework.*; 21 import net.sf.jftp.system.StringUtils; 22 import net.sf.jftp.util.*; 23 24 import java.awt.*; 25 import java.awt.event.*; 26 27 import java.io.File ; 28 29 import javax.swing.*; 30 import javax.swing.event.ListSelectionEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 33 34 public class HostList extends JDialog 35 { 36 private String promptHost = " Host : "; 37 private String promptUser = " User : "; 38 private String promptPass = " Password : "; 39 private String promptName = " Name : "; 40 private String promptPort = " Port : "; 41 private String promptButtonCancel = "Cancel"; 42 private String promptButtonOk = " Ok "; 43 private String promptButtonSave = " Apply "; 44 private String promptButtonNew = " New "; 45 private String promptButtonDelete = "Delete"; 46 private String promptDialogTitle = " J-FTP Host Selection "; 47 48 private JPanel jpHostInfo; 51 private JTextField jtfHost; 52 private JTextField jtfUser; 53 private JPasswordField jtfPass; 54 private JTextField jtfName; 55 private JTextField jtfPort; 56 private JLabel jlHost; 57 private JLabel jlUser; 58 private JLabel jlPass; 59 private JLabel jlName; 60 private JLabel jlPort; 61 private JSplitPane jsplitpane; 62 private JScrollPane jscrollpane; 63 private JPanel jpbuttons; 64 private JButton jbsave; 65 private JButton jbcancel; 66 private JButton jbok; 67 private JButton jbnew; 68 private JButton jbdelete; 69 private JList hostList; 70 private DefaultListModel hostListModel; 71 72 75 private FtpHost selectedHostInfo = null; 76 77 84 public HostList(JDialog parent) 85 { 86 super(parent); 87 setTitle(promptDialogTitle); 88 init(); 89 setSize(600, 300); 90 } 91 92 95 protected void initListeners() 96 { 97 hostList.addListSelectionListener(new ListSelectionListener () 98 { 99 public void valueChanged(ListSelectionEvent lse) 100 { 101 onSelectHost(); 102 } 103 }); 104 jbsave.addActionListener(new ActionListener() 105 { 106 public void actionPerformed(ActionEvent ae) 107 { 108 onSave(); 109 } 110 }); 111 jbok.addActionListener(new ActionListener() 112 { 113 public void actionPerformed(ActionEvent ae) 114 { 115 onOk(); 116 } 117 }); 118 jbcancel.addActionListener(new ActionListener() 119 { 120 public void actionPerformed(ActionEvent ae) 121 { 122 onCancel(); 123 } 124 }); 125 jbnew.addActionListener(new ActionListener() 126 { 127 public void actionPerformed(ActionEvent ae) 128 { 129 onNew(); 130 } 131 }); 132 jbdelete.addActionListener(new ActionListener() 133 { 134 public void actionPerformed(ActionEvent ae) 135 { 136 onDelete(); 137 } 138 }); 139 } 140 141 147 public FtpHost getFtpHost() 148 { 149 selectedHostInfo = null; 150 setVisible(true); 151 152 return selectedHostInfo; 153 } 154 155 158 protected void init() 159 { 160 this.initPrompts(); 161 this.initHostInfoPanel(); 162 this.initButtonPanel(); 163 this.initHostListFrame(); 164 this.loadHostList(); 165 initListeners(); 166 167 if(hostListModel.size() > 0) 168 { 169 hostList.setSelectedIndex(0); 170 } 171 else 172 { 173 updateHostInfoPanel(); 174 } 175 176 selectedHostInfo = getSelected(); 177 setModal(true); 178 } 179 180 185 protected void initPrompts() 186 { 187 } 189 190 193 protected void initButtonPanel() 194 { 195 jpbuttons = new JPanel(); 196 jpbuttons.setLayout(new FlowLayout(FlowLayout.RIGHT)); 197 jbcancel = new JButton(promptButtonCancel); 198 jbok = new JButton(promptButtonOk); 199 jbsave = new JButton(promptButtonSave); 200 jbnew = new JButton(promptButtonNew); 201 jbdelete = new JButton(promptButtonDelete); 202 jpbuttons.add(jbsave); 203 jpbuttons.add(jbok); 204 jpbuttons.add(jbcancel); 205 } 206 207 210 protected void initHostInfoPanel() 211 { 212 jtfHost = new JTextField(20); 213 jtfUser = new JTextField(20); 214 jtfPass = new JPasswordField(20); 215 jtfName = new JTextField(20); 216 jtfPort = new JTextField(20); 217 jlHost = new JLabel(promptHost); 218 jlUser = new JLabel(promptUser); 219 jlPass = new JLabel(promptPass); 220 jlName = new JLabel(promptName); 221 jlPort = new JLabel(promptPort); 222 223 jpHostInfo = new JPanel(); 224 225 GridBagLayout gbl = new GridBagLayout(); 226 jpHostInfo.setLayout(gbl); 227 228 GridBagConstraints gbc = new GridBagConstraints(); 229 gbc.gridx = 0; 230 gbc.gridy = 0; 231 gbc.weightx = 0.0; 232 gbc.weighty = 0.0; 233 gbc.anchor = gbc.NORTHWEST; 234 gbc.fill = gbc.HORIZONTAL; 235 236 gbl.setConstraints(jlName, gbc); 237 238 gbc.gridy = 1; 239 gbl.setConstraints(jlHost, gbc); 240 241 gbc.gridy = 2; 242 gbl.setConstraints(jlUser, gbc); 243 244 gbc.gridy = 3; 245 gbl.setConstraints(jlPass, gbc); 246 247 gbc.gridy = 4; 248 gbl.setConstraints(jlPort, gbc); 249 250 gbc.gridy = 0; 251 gbc.gridx = 1; 252 gbc.weightx = 1.0; 253 gbl.setConstraints(jtfName, gbc); 254 255 gbc.gridy = 1; 256 gbl.setConstraints(jtfHost, gbc); 257 258 gbc.gridy = 2; 259 gbl.setConstraints(jtfUser, gbc); 260 261 gbc.gridy = 3; 262 gbl.setConstraints(jtfPass, gbc); 263 264 gbc.gridy = 4; 265 gbl.setConstraints(jtfPort, gbc); 266 267 jpHostInfo.add(jlName); 268 jpHostInfo.add(jlHost); 269 jpHostInfo.add(jlUser); 270 jpHostInfo.add(jlPass); 271 jpHostInfo.add(jlPort); 272 jpHostInfo.add(jtfName); 273 jpHostInfo.add(jtfHost); 274 jpHostInfo.add(jtfUser); 275 jpHostInfo.add(jtfPass); 276 jpHostInfo.add(jtfPort); 277 } 278 279 282 protected void initHostListFrame() 283 { 284 hostListModel = new DefaultListModel(); 285 hostList = new JList(hostListModel); 286 jscrollpane = new JScrollPane(hostList); 287 288 JPanel jptempleft = new JPanel(new BorderLayout()); 289 jptempleft.add(jscrollpane, BorderLayout.CENTER); 290 291 JPanel jptempbutt = new JPanel(new FlowLayout()); 292 jptempbutt.add(jbnew); 293 jptempbutt.add(jbdelete); 294 jptempleft.add(jptempbutt, BorderLayout.SOUTH); 295 296 JPanel jptemp = new JPanel(new BorderLayout()); 297 jptemp.add(jpbuttons, BorderLayout.SOUTH); 298 jptemp.add(jpHostInfo, BorderLayout.CENTER); 299 300 jsplitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jptempleft, 301 jptemp); 302 getContentPane().add(jsplitpane); 303 } 304 305 308 protected void loadHostList() 309 { 310 int i = 0; 313 314 while(i >= 0) 315 { 316 String filename = Settings.login.concat(String.valueOf(i)); 317 String [] host_info = LoadSet.loadSet(filename); 318 319 if((host_info == null) || (host_info.length == 1)) 320 { 321 i = -1; 323 324 continue; 325 } 326 327 FtpHost ftpHost = new FtpHost(); 328 329 try 330 { 331 ftpHost.hostname = host_info[0]; 332 ftpHost.username = host_info[1]; 333 ftpHost.password = host_info[2]; 334 ftpHost.name = host_info[3]; 335 ftpHost.port = host_info[4]; 336 } 337 catch(ArrayIndexOutOfBoundsException aioobe) 338 { 339 } 341 342 hostListModel.addElement(ftpHost); 343 i++; 344 } 345 } 346 347 public void onSelectHost() 348 { 349 updateHostInfoObject(); 351 selectedHostInfo = getSelected(); 352 updateHostInfoPanel(); 353 } 354 355 358 public void onDelete() 359 { 360 Object selected = hostList.getSelectedValue(); 361 hostListModel.removeElement(selected); 362 selectedHostInfo = null; 363 364 if(hostListModel.size() > 0) 365 { 366 hostList.setSelectedIndex(0); 367 } 368 else 369 { 370 updateHostInfoPanel(); 371 } 372 373 onSave(); 374 hostList.repaint(); 375 } 376 377 380 public void onSave() 381 { 382 updateHostInfoObject(); 383 384 int i = 0; 386 387 while(true) 388 { 389 File f = new File (Settings.login.concat(String.valueOf(i))); 390 391 if(f.exists()) 392 { 393 f.delete(); 394 i++; 395 } 396 else 397 { 398 break; 399 } 400 } 401 402 int len = hostListModel.size(); 403 404 for(i = 0; i < len; i++) 405 { 406 FtpHost ftphost = (FtpHost) hostListModel.elementAt(i); 407 String htmp = StringUtils.cut(ftphost.hostname, " "); 408 String utmp = StringUtils.cut(ftphost.username, " "); 409 String ptmp = StringUtils.cut(ftphost.password, " "); 410 String ntmp = StringUtils.cut(ftphost.name, " "); 411 String ttmp = StringUtils.cut(ftphost.port, " "); 412 SaveSet s = new SaveSet(Settings.login.concat(String.valueOf(i)), 413 htmp, utmp, ptmp, ntmp, ttmp); 414 } 415 416 hostList.repaint(); 417 } 418 419 422 public void onOk() 423 { 424 selectedHostInfo = getSelected(); 425 onSave(); 426 dispose(); 427 } 428 429 432 public void onCancel() 433 { 434 selectedHostInfo = null; 435 dispose(); 436 } 437 438 441 public void onNew() 442 { 443 FtpHost ftpHost = new FtpHost(); 444 ftpHost.name = "undefined"; 445 ftpHost.username = "undefined"; 446 ftpHost.hostname = "undefined"; 447 ftpHost.password = "undefined"; 448 ftpHost.port = "21"; 449 hostListModel.addElement(ftpHost); 450 hostList.setSelectedValue(ftpHost, true); 451 selectedHostInfo = ftpHost; 452 } 453 454 457 private FtpHost getSelected() 458 { 459 int sel = hostList.getSelectedIndex(); 460 461 if((sel < 0) || (sel > (hostListModel.size() - 1))) 462 { 463 return null; 464 } 465 else 466 { 467 return (FtpHost) hostListModel.elementAt(hostList.getSelectedIndex()); 468 } 469 } 470 471 476 private void updateHostInfoPanel() 477 { 478 if(selectedHostInfo == null) 479 { 480 jtfName.setText(""); 481 jtfUser.setText(""); 482 jtfPass.setText(""); 483 jtfHost.setText(""); 484 jtfPort.setText(""); 485 jtfName.setEnabled(false); 486 jtfUser.setEnabled(false); 487 jtfHost.setEnabled(false); 488 jtfPass.setEnabled(false); 489 jtfPort.setEnabled(false); 490 } 491 else 492 { 493 jtfName.setEnabled(true); 494 jtfUser.setEnabled(true); 495 jtfHost.setEnabled(true); 496 jtfPass.setEnabled(true); 497 jtfPort.setEnabled(true); 498 jtfName.setText(selectedHostInfo.name); 499 jtfUser.setText(selectedHostInfo.username); 500 jtfPass.setText(selectedHostInfo.password); 501 jtfHost.setText(selectedHostInfo.hostname); 502 jtfPort.setText(selectedHostInfo.port); 503 } 504 } 505 506 510 private void updateHostInfoObject() 511 { 512 if(selectedHostInfo == null) 513 { 514 return; 515 } 516 517 selectedHostInfo.hostname = jtfHost.getText(); 518 selectedHostInfo.name = jtfName.getText(); 519 selectedHostInfo.username = jtfUser.getText(); 520 selectedHostInfo.password = new String (jtfPass.getPassword()); 521 selectedHostInfo.port = jtfPort.getText(); 522 } 523 } 524 | Popular Tags |