1 2 24 25 package org.enhydra.tool.archive.wizard; 27 28 import org.enhydra.tool.common.PathHandle; 30 import org.enhydra.tool.common.ExtensionFilter; 31 import org.enhydra.tool.common.SwingUtil; 32 33 import java.awt.*; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.ActionListener ; 37 import java.beans.Beans ; 38 import java.io.File ; 39 import javax.swing.*; 40 41 public class OneColumnFileSelector extends RefPanel { 43 private JScrollPane scrollPane = null; 44 private JList list = null; 45 private JButton buttonAdd = null; 46 private JButton buttonRemove = null; 47 private GridBagLayout layoutMain = null; 48 private LocalButtonListener buttonListener = null; 49 private String [] extensions = { 50 "jar" 51 }; 52 private String title = "Library"; 53 54 public OneColumnFileSelector() { 56 try { 57 jbInit(); 58 pmInit(); 59 } catch (Exception ex) { 60 ex.printStackTrace(); 61 } 62 } 63 64 public String [] getSelections() { 65 int size = list.getModel().getSize(); 66 String [] selects = new String [size]; 67 68 for (int i = 0; i < size; i++) { 69 selects[i] = list.getModel().getElementAt(i).toString(); 70 } 71 return selects; 72 } 73 74 protected void setTitle(String t) { 75 title = t; 76 } 77 78 protected String getTitle() { 79 return title; 80 } 81 82 protected void setExtensions(String [] exts) { 83 extensions = exts; 84 } 85 86 protected String [] getExtensions() { 87 return extensions; 88 } 89 90 protected void setSelections(String [] files) { 91 getListModel().clear(); 92 for (int i = 0; i < files.length; i++) { 93 getListModel().addElement(files[i]); 94 } 95 } 96 97 protected JScrollPane getScrollPane() { 98 return scrollPane; 99 } 100 101 private void pmInit() { 103 buttonListener = new LocalButtonListener(); 104 buttonAdd.addActionListener(buttonListener); 105 buttonRemove.addActionListener(buttonListener); 106 list.setModel(new DefaultListModel()); 107 } 108 109 private void jbInit() throws Exception { 110 scrollPane = 111 (JScrollPane) Beans.instantiate(getClass().getClassLoader(), 112 JScrollPane.class.getName()); 113 list = (JList) Beans.instantiate(getClass().getClassLoader(), 114 JList.class.getName()); 115 buttonAdd = (JButton) Beans.instantiate(getClass().getClassLoader(), 116 JButton.class.getName()); 117 buttonRemove = 118 (JButton) Beans.instantiate(getClass().getClassLoader(), 119 JButton.class.getName()); 120 layoutMain = 121 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 122 GridBagLayout.class.getName()); 123 buttonAdd.setPreferredSize(new Dimension(75, 27)); 124 buttonAdd.setMargin(new Insets(2, 10, 2, 10)); 125 buttonAdd.setText("Add..."); 126 buttonRemove.setPreferredSize(new Dimension(75, 27)); 127 buttonRemove.setMargin(new Insets(2, 10, 2, 10)); 128 buttonRemove.setText("Remove"); 129 scrollPane.getViewport().add(list, null); 130 this.setLayout(layoutMain); 131 this.add(scrollPane, 132 new GridBagConstraints(0, 0, 1, 2, 0.8, 0.8, 133 GridBagConstraints.CENTER, 134 GridBagConstraints.BOTH, 135 new Insets(5, 5, 0, 5), 0, 0)); 136 this.add(buttonAdd, 137 new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1, 138 GridBagConstraints.SOUTH, 139 GridBagConstraints.NONE, 140 new Insets(10, 5, 10, 5), 20, 0)); 141 this.add(buttonRemove, 142 new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1, 143 GridBagConstraints.NORTH, 144 GridBagConstraints.NONE, 145 new Insets(10, 5, 10, 5), 20, 0)); 146 } 147 148 private void addChoice() { 149 ExtensionFilter filter = null; 150 File choice = null; 151 PathHandle path = null; 152 153 filter = new ExtensionFilter(); 154 filter.setDescriptionTitle(getTitle()); 155 for (int i = 0; i < getExtensions().length; i++) { 156 filter.addExtension(getExtensions()[i]); 157 } 158 choice = SwingUtil.getFileChoice(this, getWorkingRoot(), filter, 159 "Select a " 160 + getTitle().toLowerCase() 161 + " to add"); 162 path = PathHandle.createPathHandle(choice); 163 if (path.isFile()) { 164 addToModel(path); 165 } 166 } 167 168 protected void addToModel(PathHandle path) { 169 if (!getListModel().contains(path.getPath())) { 170 getListModel().addElement(path.getPath()); 171 } 172 } 173 174 private DefaultListModel getListModel() { 175 return (DefaultListModel) list.getModel(); 176 } 177 178 protected void removeSelection() { 179 int index = list.getSelectedIndex(); 180 181 if ((index > -1) && (index < getListModel().size())) { 182 getListModel().remove(index); 183 } 184 } 185 186 private class LocalButtonListener implements ActionListener { 187 public void actionPerformed(ActionEvent event) { 188 Object source = event.getSource(); 189 190 if (source == buttonAdd) { 191 addChoice(); 192 } else if (source == buttonRemove) { 193 removeSelection(); 194 } 195 } 196 197 } 198 } 199 | Popular Tags |