1 23 package org.enhydra.kelp.common.swing; 24 25 import org.enhydra.kelp.common.event.SelectionPanelEvent; 27 import org.enhydra.kelp.common.event.SelectionPanelListener; 28 import org.enhydra.kelp.common.node.OtterFileNode; 29 30 import java.awt.event.ActionEvent ; 32 import java.awt.event.ActionListener ; 33 import java.io.File ; 34 import java.util.ResourceBundle ; 35 import java.util.ArrayList ; 36 import java.util.Arrays ; 37 import javax.swing.JButton ; 38 import javax.swing.JCheckBox ; 39 import javax.swing.JLabel ; 40 import javax.swing.JList ; 41 import javax.swing.JPanel ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.DefaultListModel ; 44 import java.awt.*; 45 import java.beans.*; 46 47 public class FileNodeSelectionPanel extends JPanel { 49 50 public static ResourceBundle res = 52 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); 54 private final String SELECT = ">"; 56 private final String SELECT_ALL = ">>"; 57 private final String REMOVE = "<"; 58 private final String REMOVE_ALL = "<<"; 59 60 private JLabel labelAvailable; 62 private JLabel labelSelected; 63 private OtterFileNode[] nodes = new OtterFileNode[0]; 64 private LocalButtonListener buttonListener = null; 65 private JScrollPane scrollAvailable; 66 private JScrollPane scrollSelected; 67 private JList listAvailable; 68 private JList listSelected; 69 private JButton buttonAdd; 70 private JButton buttonAddAll; 71 private JButton buttonRemove; 72 private JButton buttonRemoveAll; 73 private GridBagLayout layoutSelect; 74 private JPanel panelSelect; 75 private JPanel panelOption; 76 private DefaultListModel modelAvailable = new DefaultListModel (); 77 private DefaultListModel modelSelected = new DefaultListModel (); 78 private GridBagLayout layoutOption; 79 private JCheckBox checkShowFull; 80 private SelectionPanelListener[] selectionPanelListeners = 81 new SelectionPanelListener[0]; 82 private GridBagLayout layoutMain; 83 84 88 public FileNodeSelectionPanel() { 89 try { 90 jbInit(); 91 pmInit(); 92 } catch (Exception e) { 93 e.printStackTrace(); 94 } 95 } 96 97 public void setEnabled(boolean enable) { 99 buttonAdd.setEnabled(enable); 100 buttonAddAll.setEnabled(enable); 101 buttonRemove.setEnabled(enable); 102 buttonRemoveAll.setEnabled(enable); 103 listAvailable.setEnabled(enable); 104 listSelected.setEnabled(enable); 105 checkShowFull.setEnabled(enable); 106 super.setEnabled(enable); 107 } 108 109 public void clearAll() { 110 buttonAdd.removeActionListener(buttonListener); 111 buttonAddAll.removeActionListener(buttonListener); 112 buttonRemove.removeActionListener(buttonListener); 113 buttonRemoveAll.removeActionListener(buttonListener); 114 checkShowFull.removeActionListener(buttonListener); 115 listAvailable.setModel(new DefaultListModel ()); 116 listSelected.setModel(new DefaultListModel ()); 117 removeAll(); 118 modelAvailable = null; 119 modelSelected = null; 120 nodes = null; 121 selectionPanelListeners = null; 122 buttonListener = null; 123 } 124 125 public synchronized void addSelectionPanelListener(SelectionPanelListener l) { 127 ArrayList list = null; 128 129 list = new ArrayList (Arrays.asList(selectionPanelListeners)); 130 if (!list.contains(l)) { 131 list.add(l); 132 list.trimToSize(); 133 selectionPanelListeners = new SelectionPanelListener[list.size()]; 134 selectionPanelListeners = 135 (SelectionPanelListener[]) list.toArray(selectionPanelListeners); 136 } 137 list.clear(); 138 } 139 140 public synchronized void removeSelectionPanelListener(SelectionPanelListener l) { 141 ArrayList list = null; 142 143 list = new ArrayList (Arrays.asList(selectionPanelListeners)); 144 if (list.contains(l)) { 145 list.remove(l); 146 list.trimToSize(); 147 selectionPanelListeners = new SelectionPanelListener[list.size()]; 148 selectionPanelListeners = 149 (SelectionPanelListener[]) list.toArray(selectionPanelListeners); 150 } 151 list.clear(); 152 } 153 154 160 public OtterFileNode[] getNodes() { 161 return nodes; 162 } 163 164 170 public void setNodes(OtterFileNode[] n) { 171 nodes = n; 172 fillLists(); 173 fireSelectionEvent(); 174 } 175 176 public String [] getSelectText() { 177 String [] s = new String [2]; 178 179 s[0] = labelAvailable.getText(); 180 s[1] = labelSelected.getText(); 181 return s; 182 } 183 184 public void setSelectText(String [] s) { 185 labelAvailable.setText(s[0]); 186 labelSelected.setText(s[1]); 187 } 188 189 protected void fillLists() { 193 int count = getNodes().length; 194 OtterFileNode source = null; 195 196 if (getModelSelected().getSize() > 0) { 197 getModelSelected().clear(); 198 } 199 if (getModelAvailable().getSize() > 0) { 200 getModelAvailable().clear(); 201 } 202 for (int i = 0; i < count; i++) { 203 LocalListNode listNode = null; 204 205 source = getNodes()[i]; 206 listNode = new LocalListNode(source.getFilePath()); 207 if (source.isSelected()) { 208 getModelSelected().addElement(listNode); 209 } else { 210 getModelAvailable().addElement(listNode); 211 } 212 } 213 } 214 215 protected DefaultListModel getModelSelected() { 216 return modelSelected; 217 } 218 219 protected DefaultListModel getModelAvailable() { 220 return modelAvailable; 221 } 222 223 private void pmInit() { 227 buttonListener = new LocalButtonListener(); 228 buttonAdd.addActionListener(buttonListener); 229 buttonAddAll.addActionListener(buttonListener); 230 buttonRemove.addActionListener(buttonListener); 231 buttonRemoveAll.addActionListener(buttonListener); 232 checkShowFull.addActionListener(buttonListener); 233 checkShowFull.setSelected(false); 234 } 235 236 private void jbInit() throws Exception { 237 layoutSelect = 238 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 239 GridBagLayout.class.getName()); 240 panelSelect = (JPanel ) Beans.instantiate(getClass().getClassLoader(), 241 JPanel .class.getName()); 242 panelOption = (JPanel ) Beans.instantiate(getClass().getClassLoader(), 243 JPanel .class.getName()); 244 scrollAvailable = 245 (JScrollPane ) Beans.instantiate(getClass().getClassLoader(), 246 JScrollPane .class.getName()); 247 scrollSelected = 248 (JScrollPane ) Beans.instantiate(getClass().getClassLoader(), 249 JScrollPane .class.getName()); 250 labelAvailable = 251 (JLabel ) Beans.instantiate(getClass().getClassLoader(), 252 JLabel .class.getName()); 253 labelSelected = 254 (JLabel ) Beans.instantiate(getClass().getClassLoader(), 255 JLabel .class.getName()); 256 listAvailable = (JList ) Beans.instantiate(getClass().getClassLoader(), 257 JList .class.getName()); 258 listSelected = (JList ) Beans.instantiate(getClass().getClassLoader(), 259 JList .class.getName()); 260 buttonAdd = (JButton ) Beans.instantiate(getClass().getClassLoader(), 261 JButton .class.getName()); 262 buttonAddAll = 263 (JButton ) Beans.instantiate(getClass().getClassLoader(), 264 JButton .class.getName()); 265 buttonRemove = 266 (JButton ) Beans.instantiate(getClass().getClassLoader(), 267 JButton .class.getName()); 268 buttonRemoveAll = 269 (JButton ) Beans.instantiate(getClass().getClassLoader(), 270 JButton .class.getName()); 271 layoutOption = 272 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 273 GridBagLayout.class.getName()); 274 checkShowFull = 275 (JCheckBox ) Beans.instantiate(getClass().getClassLoader(), 276 JCheckBox .class.getName()); 277 layoutMain = 278 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 279 GridBagLayout.class.getName()); 280 listAvailable.setModel(modelAvailable); 281 listSelected.setModel(modelSelected); 282 labelAvailable.setText(res.getString("Available_Pages")); 283 labelSelected.setText(res.getString("Selected_Pages")); 284 buttonAdd.setMargin(new Insets(1, 10, 1, 10)); 285 buttonAdd.setText(SELECT); 286 buttonAddAll.setMargin(new Insets(1, 10, 1, 10)); 287 buttonAddAll.setText(SELECT_ALL); 288 buttonRemove.setMargin(new Insets(1, 10, 1, 10)); 289 buttonRemove.setText(REMOVE); 290 buttonRemoveAll.setMargin(new Insets(1, 10, 1, 10)); 291 buttonRemoveAll.setText(REMOVE_ALL); 292 checkShowFull.setText(res.getString("Show_full")); 293 scrollAvailable.getViewport().add(listAvailable); 294 scrollSelected.getViewport().add(listSelected); 295 panelSelect.setLayout(layoutSelect); 296 panelSelect.add(labelAvailable, 297 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 298 GridBagConstraints.WEST, 299 GridBagConstraints.HORIZONTAL, 300 new Insets(5, 5, 2, 5), 0, 0)); 301 panelSelect.add(scrollAvailable, 302 new GridBagConstraints(0, 1, 1, 4, 0.4, 0.0, 303 GridBagConstraints.WEST, 304 GridBagConstraints.BOTH, 305 new Insets(3, 3, 5, 3), 0, 0)); 306 panelSelect.add(buttonAdd, 307 new GridBagConstraints(1, 1, 1, 1, 0.1, 0.0, 308 GridBagConstraints.CENTER, 309 GridBagConstraints.NONE, 310 new Insets(8, 2, 3, 2), 14, 311 0)); 312 panelSelect.add(buttonAddAll, 313 new GridBagConstraints(1, 2, 1, 1, 0.1, 0.0, 314 GridBagConstraints.CENTER, 315 GridBagConstraints.NONE, 316 new Insets(3, 2, 3, 2), 6, 0)); 317 panelSelect.add(buttonRemove, 318 new GridBagConstraints(1, 3, 1, 1, 0.1, 0.0, 319 GridBagConstraints.CENTER, 320 GridBagConstraints.NONE, 321 new Insets(3, 2, 3, 2), 14, 322 0)); 323 panelSelect.add(buttonRemoveAll, 324 new GridBagConstraints(1, 4, 1, 1, 0.1, 0.0, 325 GridBagConstraints.CENTER, 326 GridBagConstraints.NONE, 327 new Insets(3, 2, 8, 2), 6, 0)); 328 panelSelect.add(labelSelected, 329 new GridBagConstraints(2, 0, 1, 1, 0.1, 0.0, 330 GridBagConstraints.WEST, 331 GridBagConstraints.HORIZONTAL, 332 new Insets(5, 5, 2, 5), 0, 0)); 333 panelSelect.add(scrollSelected, 334 new GridBagConstraints(2, 1, 1, 4, 0.4, 0.0, 335 GridBagConstraints.EAST, 336 GridBagConstraints.BOTH, 337 new Insets(3, 3, 5, 3), 0, 0)); 338 panelOption.setLayout(layoutOption); 339 panelOption.add(checkShowFull, 340 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 341 GridBagConstraints.WEST, 342 GridBagConstraints.HORIZONTAL, 343 new Insets(5, 5, 0, 5), 0, 0)); 344 this.setLayout(layoutMain); 345 this.add(panelSelect, 346 new GridBagConstraints(0, 0, 1, 1, 0.1, 0.0, 347 GridBagConstraints.CENTER, 348 GridBagConstraints.BOTH, 349 new Insets(0, 0, 0, 0), 0, 0)); 350 this.add(panelOption, 351 new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0, 352 GridBagConstraints.CENTER, 353 GridBagConstraints.HORIZONTAL, 354 new Insets(0, 0, 0, 0), 0, 0)); 355 } 356 357 private void addSelected() { 358 Object [] elements = listAvailable.getSelectedValues(); 359 360 for (int i = 0; i < elements.length; i++) { 361 LocalListNode listNode = (LocalListNode) elements[i]; 362 363 modelSelected.addElement(listNode); 364 modelAvailable.removeElement(listNode); 365 for (int j = 0; j < getNodes().length; j++) { 366 if (getNodes()[j].getFilePath().equals(listNode.getFullname())) { 367 getNodes()[j].setSelected(true); 368 } 369 } 370 } 371 fireSelectionEvent(); 372 } 373 374 private void addAllElements() { 375 for (int i = 0; i < modelAvailable.size(); i++) { 376 modelSelected.addElement(modelAvailable.elementAt(i)); 377 } 378 modelAvailable.removeAllElements(); 379 for (int i = 0; i < getNodes().length; i++) { 380 getNodes()[i].setSelected(true); 381 } 382 fireSelectionEvent(); 383 } 384 385 private void removeSelected() { 386 Object [] elements = listSelected.getSelectedValues(); 387 388 for (int i = 0; i < elements.length; i++) { 389 LocalListNode listNode = (LocalListNode) elements[i]; 390 391 modelAvailable.addElement(listNode); 392 modelSelected.removeElement(listNode); 393 for (int j = 0; j < getNodes().length; j++) { 394 if (getNodes()[j].getFilePath().equals(listNode.getFullname())) { 395 getNodes()[j].setSelected(false); 396 } 397 } 398 } 399 fireSelectionEvent(); 400 } 401 402 private void removeAllElements() { 403 for (int i = 0; i < modelSelected.size(); i++) { 404 modelAvailable.addElement(modelSelected.elementAt(i)); 405 } 406 modelSelected.removeAllElements(); 407 for (int i = 0; i < getNodes().length; i++) { 408 getNodes()[i].setSelected(false); 409 } 410 fireSelectionEvent(); 411 } 412 413 private void fireSelectionEvent() { 414 SelectionPanelEvent event = null; 415 416 event = new SelectionPanelEvent(this, modelAvailable, modelSelected); 417 for (int i = 0; i < selectionPanelListeners.length; i++) { 418 selectionPanelListeners[i].onSelection(event); 419 } 420 } 421 422 private class LocalButtonListener implements ActionListener { 423 public void actionPerformed(ActionEvent event) { 424 Object source = event.getSource(); 425 426 if (source == buttonAdd) { 427 addSelected(); 428 } else if (source == buttonAddAll) { 429 addAllElements(); 430 } else if (source == buttonRemove) { 431 removeSelected(); 432 } else if (source == buttonRemoveAll) { 433 removeAllElements(); 434 } else if (source == checkShowFull) { 435 checkShowFull.isSelected(); 436 fillLists(); 437 } 438 } 439 440 } 441 442 private class LocalListNode { 444 private String full = new String (); 445 446 public LocalListNode(String fullName) { 447 full = fullName; 448 } 449 450 public String toString() { 451 String s = full; 452 453 if (!checkShowFull.isSelected()) { 454 File f = new File (full); 455 456 s = f.getName(); 457 } 458 return s; 459 } 460 461 public String getFullname() { 462 return full; 463 } 464 465 } 466 } 467 | Popular Tags |