1 2 24 package org.enhydra.tool.archive.wizard; 25 26 import org.enhydra.tool.archive.ArchiveException; 28 import org.enhydra.tool.archive.ClassFilter; 29 import org.enhydra.tool.common.PathHandle; 30 import org.enhydra.tool.common.SwingUtil; 31 32 import java.awt.event.ActionEvent ; 34 import java.awt.event.ActionListener ; 35 import java.io.File ; 36 import java.util.ResourceBundle ; 37 import java.util.Enumeration ; 38 import java.util.ArrayList ; 39 import java.util.Vector ; 40 import java.awt.*; 41 import javax.swing.*; 42 import java.beans.*; 43 44 49 public class TwoColumnFileSelector extends JPanel { 50 51 private final String SELECT = ">"; 53 private final String SELECT_ALL = ">>"; 54 private final String REMOVE = "<"; 55 private final String REMOVE_ALL = "<<"; 56 57 private boolean defaultSelect = false; 59 private JLabel labelAvailable = null; 60 private JLabel labelSelected = null; 61 private JScrollPane scrollAvailable = null; 62 private JScrollPane scrollSelected = null; 63 private JList listAvailable = null; 64 private JList listSelected = null; 65 private JButton buttonAdd = null; 66 private JButton buttonAddAll = null; 67 private JButton buttonRemove = null; 68 private JButton buttonRemoveAll = null; 69 private GridBagLayout layoutSelect = null; 70 private JPanel panelSelect = null; 71 private JPanel panelOption = null; 72 private BorderLayout layoutMain = null; 73 private GridBagLayout layoutOption = null; 74 private JCheckBox checkShowFull = null; 75 private JLabel labelRoot = null; 76 private JTextField textRoot = null; 77 private JButton buttonRoot = null; 78 private LocalButtonListener buttonListener = null; 79 private DefaultListModel modelAvailable = new DefaultListModel(); 80 private DefaultListModel modelSelected = new DefaultListModel(); 81 private ClassFilter filter = null; 82 83 87 public TwoColumnFileSelector() { 88 try { 89 jbInit(); 90 pmInit(); 91 } catch (Exception e) { 92 e.printStackTrace(); 93 } 94 } 95 96 protected void validatePlan() throws ArchiveException { 97 String [] selects = new String [0]; 98 File file = null; 99 100 if (getRootFile() == null || (!getRootFile().isDirectory())) { 101 throw new ArchiveException("Invalid root"); 102 } 103 selects = getSelections(); 104 for (int i = 0; i < selects.length; i++) { 105 file = new File (selects[i]); 106 if (!file.isFile()) { 107 throw new ArchiveException("Invalid selection: " 108 + file.getAbsolutePath()); 109 } 110 } 111 } 112 113 protected void setFilter(ClassFilter f) { 114 filter = f; 115 } 116 117 protected ClassFilter getFilter() { 118 return filter; 119 } 120 121 protected void setDefaultSelect(boolean b) { 122 defaultSelect = b; 123 } 124 125 protected boolean isDefaultSelect() { 126 return defaultSelect; 127 } 128 129 private void clearLists() { 130 if (getModelSelected().getSize() > 0) { 131 getModelSelected().clear(); 132 } 133 if (getModelAvailable().getSize() > 0) { 134 getModelAvailable().clear(); 135 } 136 } 137 138 142 protected void fillFrom(File cursor) throws ListOverflow { 143 File [] children = new File [0]; 144 145 if (getModelAvailable().size() + getModelSelected().size() > 2500) { 146 throw new ListOverflow(); 147 } else if (cursor == null) { 148 children = null; 149 } else { 150 if (getFilter() == null) { 151 children = cursor.listFiles(); 152 } else { 153 getFilter().setRoot(getRoot()); 154 children = cursor.listFiles(getFilter()); 155 } 156 } 157 if (children == null) { 158 159 } else { 161 for (int i = 0; i < children.length; i++) { 162 if (children[i].isDirectory()) { 163 fillFrom(children[i]); 164 } else if (children[i].isFile()) { 165 LocalListNode listNode = null; 166 167 listNode = 168 new LocalListNode(children[i].getAbsolutePath()); 169 if (isDefaultSelect()) { 170 getModelSelected().addElement(listNode); 171 } else { 172 getModelAvailable().addElement(listNode); 173 } 174 } 175 } 176 } 177 } 178 179 182 188 protected DefaultListModel getModelSelected() { 189 return modelSelected; 190 } 191 192 198 protected DefaultListModel getModelAvailable() { 199 return modelAvailable; 200 } 201 202 208 public String [] getSelections() { 209 String [] selects = new String [0]; 210 Enumeration enumeration = null; 211 ArrayList list = new ArrayList (); 212 213 enumeration = getModelSelected().elements(); 214 while (enumeration.hasMoreElements()) { 215 LocalListNode next = (LocalListNode) enumeration.nextElement(); 216 217 if (next.getFullname().trim().length() > 0) { 218 list.add(next.getFullname()); 219 } 220 } 221 list.trimToSize(); 222 selects = new String [list.size()]; 223 selects = (String []) list.toArray(selects); 224 return selects; 225 } 226 227 public void setSelections(File [] selectFiles) { 228 String [] selects = new String [0]; 229 230 if (selectFiles != null) { 231 selects = new String [selectFiles.length]; 232 for (int i = 0; i < selects.length; i++) { 233 selects[i] = selectFiles[i].getAbsolutePath(); 234 } 235 } 236 setSelections(selects); 237 } 238 239 public void setSelections(String [] selects) { 240 setDefaultSelect(false); 241 clearLists(); 242 fillFromRoot(); 243 for (int i = 0; i < selects.length; i++) { 244 LocalListNode node = null; 245 246 node = new LocalListNode(selects[i]); 247 if (!getModelSelected().contains(node)) { 248 getModelSelected().addElement(node); 249 } 250 if (getModelAvailable().contains(node)) { 251 getModelAvailable().removeElement(node); 252 } 253 } 254 } 255 256 public void setRoot(String path) { 257 PathHandle root = null; 258 259 root = PathHandle.createPathHandle(path); 260 if (root.isDirectory()) { 261 textRoot.setText(root.getPath()); 262 } else { 263 textRoot.setText(new String ()); 264 } 265 textRoot.setToolTipText(textRoot.getText()); 266 clearLists(); 267 fillFromRoot(); 268 } 269 270 public String getRoot() { 271 String root = null; 272 PathHandle path = null; 273 274 path = PathHandle.createPathHandle(textRoot.getText()); 275 if (path.isDirectory()) { 276 root = path.getPath(); 277 } 278 return root; 279 } 280 281 public File getRootFile() { 282 String root = getRoot(); 283 File file = null; 284 285 if (root == null) { 286 287 } else { 289 file = new File (root); 290 } 291 return file; 292 } 293 294 private void fillFromRoot() { 295 try { 296 fillFrom(getRootFile()); 297 } catch (ListOverflow e) { 298 JOptionPane.showMessageDialog(this, 299 "Reached maximum files for root: 2500", 300 "Archive Wizard Warning", 301 JOptionPane.WARNING_MESSAGE); 302 } 303 } 304 305 private void pmInit() { 306 buttonListener = new LocalButtonListener(); 307 buttonRoot.addActionListener(buttonListener); 308 buttonAdd.addActionListener(buttonListener); 309 buttonAddAll.addActionListener(buttonListener); 310 buttonRemove.addActionListener(buttonListener); 311 buttonRemoveAll.addActionListener(buttonListener); 312 checkShowFull.addActionListener(buttonListener); 313 checkShowFull.setSelected(false); 314 textRoot.setText(new String ()); 315 } 316 317 private void jbInit() throws Exception { 318 layoutSelect = 319 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 320 GridBagLayout.class.getName()); 321 panelSelect = (JPanel) Beans.instantiate(getClass().getClassLoader(), 322 JPanel.class.getName()); 323 panelOption = (JPanel) Beans.instantiate(getClass().getClassLoader(), 324 JPanel.class.getName()); 325 scrollAvailable = 326 (JScrollPane) Beans.instantiate(getClass().getClassLoader(), 327 JScrollPane.class.getName()); 328 scrollSelected = 329 (JScrollPane) Beans.instantiate(getClass().getClassLoader(), 330 JScrollPane.class.getName()); 331 labelAvailable = 332 (JLabel) Beans.instantiate(getClass().getClassLoader(), 333 JLabel.class.getName()); 334 labelSelected = 335 (JLabel) Beans.instantiate(getClass().getClassLoader(), 336 JLabel.class.getName()); 337 listAvailable = (JList) Beans.instantiate(getClass().getClassLoader(), 338 JList.class.getName()); 339 listSelected = (JList) Beans.instantiate(getClass().getClassLoader(), 340 JList.class.getName()); 341 buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(), 342 JButton.class.getName()); 343 buttonAddAll = 344 (JButton) Beans.instantiate(getClass().getClassLoader(), 345 JButton.class.getName()); 346 buttonRemove = 347 (JButton) Beans.instantiate(getClass().getClassLoader(), 348 JButton.class.getName()); 349 buttonRemoveAll = 350 (JButton) Beans.instantiate(getClass().getClassLoader(), 351 JButton.class.getName()); 352 layoutMain = 353 (BorderLayout) Beans.instantiate(getClass().getClassLoader(), 354 BorderLayout.class.getName()); 355 layoutOption = 356 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 357 GridBagLayout.class.getName()); 358 checkShowFull = 359 (JCheckBox) Beans.instantiate(getClass().getClassLoader(), 360 JCheckBox.class.getName()); 361 labelRoot = (JLabel) Beans.instantiate(getClass().getClassLoader(), 362 JLabel.class.getName()); 363 textRoot = (JTextField) Beans.instantiate(getClass().getClassLoader(), 364 JTextField.class.getName()); 365 buttonRoot = (JButton) Beans.instantiate(getClass().getClassLoader(), 366 JButton.class.getName()); 367 listAvailable.setModel(modelAvailable); 368 listSelected.setModel(modelSelected); 369 labelAvailable.setText("Available"); 370 labelSelected.setText("Selected"); 371 buttonAdd.setMaximumSize(new Dimension(40, 27)); 372 buttonAdd.setMinimumSize(new Dimension(40, 27)); 373 buttonAdd.setPreferredSize(new Dimension(40, 27)); 374 buttonAdd.setToolTipText("Select"); 375 buttonAdd.setMargin(new Insets(2, 2, 2, 2)); 376 buttonAdd.setText(SELECT); 377 buttonAddAll.setMaximumSize(new Dimension(40, 27)); 378 buttonAddAll.setMinimumSize(new Dimension(40, 27)); 379 buttonAddAll.setPreferredSize(new Dimension(40, 27)); 380 buttonAddAll.setToolTipText("Select All"); 381 buttonAddAll.setMargin(new Insets(2, 2, 2, 2)); 382 buttonAddAll.setText(SELECT_ALL); 383 buttonRemove.setMaximumSize(new Dimension(40, 27)); 384 buttonRemove.setMinimumSize(new Dimension(40, 27)); 385 buttonRemove.setPreferredSize(new Dimension(40, 27)); 386 buttonRemove.setToolTipText("Deselect"); 387 buttonRemove.setMargin(new Insets(2, 2, 2, 2)); 388 buttonRemove.setText(REMOVE); 389 buttonRemoveAll.setMaximumSize(new Dimension(40, 27)); 390 buttonRemoveAll.setMinimumSize(new Dimension(40, 27)); 391 buttonRemoveAll.setPreferredSize(new Dimension(40, 27)); 392 buttonRemoveAll.setToolTipText("Deselect All"); 393 buttonRemoveAll.setMargin(new Insets(2, 2, 2, 2)); 394 buttonRemoveAll.setText(REMOVE_ALL); 395 scrollAvailable.setMaximumSize(new Dimension(300, 300)); 396 scrollAvailable.setPreferredSize(new Dimension(250, 250)); 397 scrollSelected.setMaximumSize(new Dimension(300, 300)); 398 scrollSelected.setPreferredSize(new Dimension(250, 250)); 399 checkShowFull.setText("Show paths"); 400 labelRoot.setText("Root:"); 401 textRoot.setEnabled(false); 402 textRoot.setEditable(false); 403 textRoot.setText("jTextField1"); 404 buttonRoot.setMargin(new Insets(2, 6, 2, 6)); 405 buttonRoot.setText("..."); 406 scrollAvailable.getViewport().add(listAvailable); 407 scrollSelected.getViewport().add(listSelected); 408 panelSelect.setLayout(layoutSelect); 409 panelSelect.add(labelAvailable, 410 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.1, 411 GridBagConstraints.WEST, 412 GridBagConstraints.HORIZONTAL, 413 new Insets(5, 5, 2, 5), 0, 0)); 414 panelSelect.add(scrollAvailable, 415 new GridBagConstraints(0, 1, 1, 4, 0.2, 0.2, 416 GridBagConstraints.WEST, 417 GridBagConstraints.BOTH, 418 new Insets(2, 0, 2, 2), 100, 419 150)); 420 panelSelect.add(buttonAdd, 421 new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1, 422 GridBagConstraints.CENTER, 423 GridBagConstraints.NONE, 424 new Insets(8, 0, 4, 0), 10, 425 0)); 426 panelSelect.add(buttonAddAll, 427 new GridBagConstraints(1, 2, 1, 1, 0.1, 0.1, 428 GridBagConstraints.CENTER, 429 GridBagConstraints.NONE, 430 new Insets(4, 0, 4, 0), 10, 431 0)); 432 panelSelect.add(buttonRemove, 433 new GridBagConstraints(1, 3, 1, 1, 0.1, 0.1, 434 GridBagConstraints.SOUTH, 435 GridBagConstraints.NONE, 436 new Insets(4, 0, 4, 0), 10, 437 0)); 438 panelSelect.add(buttonRemoveAll, 439 new GridBagConstraints(1, 4, 1, 1, 0.1, 0.1, 440 GridBagConstraints.CENTER, 441 GridBagConstraints.NONE, 442 new Insets(4, 0, 8, 0), 10, 443 0)); 444 panelSelect.add(labelSelected, 445 new GridBagConstraints(2, 0, 1, 1, 0.1, 0.1, 446 GridBagConstraints.WEST, 447 GridBagConstraints.HORIZONTAL, 448 new Insets(5, 5, 2, 5), 0, 0)); 449 panelSelect.add(scrollSelected, 450 new GridBagConstraints(2, 1, 1, 4, 0.2, 0.2, 451 GridBagConstraints.EAST, 452 GridBagConstraints.BOTH, 453 new Insets(2, 2, 2, 0), 100, 454 150)); 455 panelOption.setLayout(layoutOption); 456 panelOption.add(labelRoot, 457 new GridBagConstraints(0, 1, 1, 1, 0.1, 0.1, 458 GridBagConstraints.WEST, 459 GridBagConstraints.NONE, 460 new Insets(5, 15, 5, 0), 0, 461 0)); 462 panelOption.add(checkShowFull, 463 new GridBagConstraints(0, 0, 3, 1, 0.5, 0.5, 464 GridBagConstraints.WEST, 465 GridBagConstraints.HORIZONTAL, 466 new Insets(5, 15, 5, 5), 0, 467 0)); 468 panelOption.add(textRoot, 469 new GridBagConstraints(1, 1, 1, 1, 0.8, 0.8, 470 GridBagConstraints.CENTER, 471 GridBagConstraints.HORIZONTAL, 472 new Insets(5, 1, 5, 1), 0, 0)); 473 panelOption.add(buttonRoot, 474 new GridBagConstraints(2, 1, 1, 1, 0.1, 0.1, 475 GridBagConstraints.CENTER, 476 GridBagConstraints.NONE, 477 new Insets(5, 1, 5, 1), 0, 0)); 478 this.setLayout(layoutMain); 479 this.setMinimumSize(new Dimension(400, 200)); 480 this.setPreferredSize(new Dimension(620, 300)); 481 this.add(panelSelect, BorderLayout.CENTER); 482 this.add(panelOption, BorderLayout.SOUTH); 483 } 484 485 private void addSelected() { 486 Object [] elements = listAvailable.getSelectedValues(); 487 488 for (int i = 0; i < elements.length; i++) { 489 LocalListNode listNode = (LocalListNode) elements[i]; 490 491 modelSelected.addElement(listNode); 492 modelAvailable.removeElement(listNode); 493 } 494 } 495 496 private void refreshLists() { 497 int size = modelAvailable.getSize(); 498 499 for (int i = 0; i < size; i++) { 500 modelAvailable.setElementAt(modelAvailable.getElementAt(i), i); 501 } 502 size = modelSelected.getSize(); 503 for (int i = 0; i < size; i++) { 504 modelSelected.setElementAt(modelSelected.getElementAt(i), i); 505 } 506 } 507 508 private void addAllElements() { 509 for (int i = 0; i < modelAvailable.size(); i++) { 510 modelSelected.addElement(modelAvailable.elementAt(i)); 511 } 512 modelAvailable.removeAllElements(); 513 } 514 515 private void removeSelected() { 516 Object [] elements = listSelected.getSelectedValues(); 517 518 for (int i = 0; i < elements.length; i++) { 519 LocalListNode listNode = (LocalListNode) elements[i]; 520 521 modelAvailable.addElement(listNode); 522 modelSelected.removeElement(listNode); 523 } 524 } 525 526 private void removeAllElements() { 527 for (int i = 0; i < modelSelected.size(); i++) { 528 modelAvailable.addElement(modelSelected.elementAt(i)); 529 } 530 modelSelected.removeAllElements(); 531 } 532 533 private void selectRoot() { 534 File choice = null; 535 536 choice = SwingUtil.getDirectoryChoice(this, textRoot.getText(), 537 "Select root directory"); 538 if (choice == null) { 539 540 } else { 542 setRoot(choice.getAbsolutePath()); 543 } 544 } 545 546 private class LocalButtonListener implements ActionListener { 548 public void actionPerformed(ActionEvent event) { 549 Object source = event.getSource(); 550 551 if (source == buttonAdd) { 552 addSelected(); 553 } else if (source == buttonAddAll) { 554 addAllElements(); 555 } else if (source == buttonRemove) { 556 removeSelected(); 557 } else if (source == buttonRemoveAll) { 558 removeAllElements(); 559 } else if (source == checkShowFull) { 560 refreshLists(); 561 } else if (source == buttonRoot) { 562 selectRoot(); 563 } 564 } 565 566 } 567 568 private class LocalListNode { 570 private String full = new String (); 571 572 public LocalListNode(String fullName) { 573 full = PathHandle.createPathString(fullName); 574 } 575 576 public String toString() { 577 String s = full; 578 579 if (!checkShowFull.isSelected()) { 580 File f = new File (full); 581 582 s = f.getName(); 583 } 584 return s; 585 } 586 587 public String getFullname() { 588 return full; 589 } 590 591 public boolean equals(Object in) { 592 boolean eq = false; 593 594 if (in instanceof LocalListNode) { 595 LocalListNode comp = (LocalListNode) in; 596 597 eq = comp.getFullname().equals(getFullname()); 598 } 599 return eq; 600 } 601 602 } 603 private class ListOverflow extends Exception {} 604 } 605 | Popular Tags |