1 21 package proguard.gui; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.util.List ; 26 import java.util.*; 27 28 import javax.swing.*; 29 import javax.swing.event.*; 30 31 38 abstract class ListPanel extends JPanel 39 { 40 protected DefaultListModel listModel = new DefaultListModel(); 41 protected JList list = new JList(listModel); 42 43 protected int firstSelectionButton = 2; 44 45 46 protected ListPanel() 47 { 48 GridBagLayout layout = new GridBagLayout(); 49 setLayout(layout); 50 51 GridBagConstraints listConstraints = new GridBagConstraints(); 52 listConstraints.gridheight = GridBagConstraints.REMAINDER; 53 listConstraints.fill = GridBagConstraints.BOTH; 54 listConstraints.weightx = 1.0; 55 listConstraints.weighty = 1.0; 56 listConstraints.anchor = GridBagConstraints.NORTHWEST; 57 listConstraints.insets = new Insets(0, 2, 0, 2); 58 59 list.addListSelectionListener(new ListSelectionListener() 62 { 63 public void valueChanged(ListSelectionEvent e) 64 { 65 enableSelectionButtons(); 66 } 67 }); 68 69 add(new JScrollPane(list), listConstraints); 70 71 } 80 81 82 protected void addRemoveButton() 83 { 84 JButton removeButton = new JButton(GUIResources.getMessage("remove")); 85 removeButton.addActionListener(new ActionListener() 86 { 87 public void actionPerformed(ActionEvent e) 88 { 89 removeElementsAt(list.getSelectedIndices()); 91 } 92 }); 93 94 addButton(removeButton); 95 } 96 97 98 protected void addUpButton() 99 { 100 JButton upButton = new JButton(GUIResources.getMessage("moveUp")); 101 upButton.addActionListener(new ActionListener() 102 { 103 public void actionPerformed(ActionEvent e) 104 { 105 int[] selectedIndices = list.getSelectedIndices(); 106 if (selectedIndices.length > 0 && 107 selectedIndices[0] > 0) 108 { 109 moveElementsAt(selectedIndices, -1); 111 } 112 } 113 }); 114 115 addButton(upButton); 116 } 117 118 119 protected void addDownButton() 120 { 121 JButton downButton = new JButton(GUIResources.getMessage("moveDown")); 122 downButton.addActionListener(new ActionListener() 123 { 124 public void actionPerformed(ActionEvent e) 125 { 126 int[] selectedIndices = list.getSelectedIndices(); 127 if (selectedIndices.length > 0 && 128 selectedIndices[selectedIndices.length-1] < listModel.getSize()-1) 129 { 130 moveElementsAt(selectedIndices, 1); 132 } 133 } 134 }); 135 136 addButton(downButton); 137 } 138 139 140 146 public void addCopyToPanelButton(String buttonText, 147 final ListPanel panel) 148 { 149 JButton moveButton = new JButton(buttonText); 150 moveButton.addActionListener(new ActionListener() 151 { 152 public void actionPerformed(ActionEvent e) 153 { 154 int[] selectedIndices = list.getSelectedIndices(); 155 Object [] selectedElements = list.getSelectedValues(); 156 157 removeElementsAt(selectedIndices); 159 160 panel.addElements(selectedElements); 162 } 163 }); 164 165 addButton(moveButton); 166 } 167 168 169 protected void addButton(JButton button) 170 { 171 GridBagConstraints buttonConstraints = new GridBagConstraints(); 172 buttonConstraints.gridwidth = GridBagConstraints.REMAINDER; 173 buttonConstraints.fill = GridBagConstraints.HORIZONTAL; 174 buttonConstraints.anchor = GridBagConstraints.NORTHWEST; 175 buttonConstraints.insets = new Insets(0, 2, 0, 2); 176 177 add(button, buttonConstraints); 178 } 179 180 181 184 public List getButtons() 185 { 186 List list = new ArrayList(getComponentCount()-1); 187 188 for (int index = 1; index < getComponentCount(); index++) 190 { 191 list.add(getComponent(index)); 192 } 193 194 return list; 195 } 196 197 198 protected void addElement(Object element) 199 { 200 listModel.addElement(element); 201 202 list.setSelectedIndex(listModel.size() - 1); 204 } 205 206 207 protected void addElements(Object [] elements) 208 { 209 for (int index = 0; index < elements.length; index++) 211 { 212 listModel.addElement(elements[index]); 213 } 214 215 int[] selectedIndices = new int[elements.length]; 217 for (int index = 0; index < selectedIndices.length; index++) 218 { 219 selectedIndices[index] = 220 listModel.size() - selectedIndices.length + index; 221 } 222 list.setSelectedIndices(selectedIndices); 223 } 224 225 226 protected void moveElementsAt(int[] indices, int offset) 227 { 228 Object [] selectedElements = list.getSelectedValues(); 230 231 removeElementsAt(indices); 233 234 for (int index = 0; index < indices.length; index++) 236 { 237 indices[index] += offset; 238 } 239 240 insertElementsAt(selectedElements, indices); 242 } 243 244 245 protected void insertElementsAt(Object [] elements, int[] indices) 246 { 247 for (int index = 0; index < elements.length; index++) 248 { 249 listModel.insertElementAt(elements[index], indices[index]); 250 } 251 252 list.setSelectedIndices(indices); 254 } 255 256 257 protected void setElementAt(Object element, int index) 258 { 259 listModel.setElementAt(element, index); 260 261 list.setSelectedIndex(index); 263 } 264 265 266 protected void setElementsAt(Object [] elements, int[] indices) 267 { 268 for (int index = 0; index < elements.length; index++) 269 { 270 listModel.setElementAt(elements[index], indices[index]); 271 } 272 273 list.setSelectedIndices(indices); 275 } 276 277 278 protected void removeElementsAt(int[] indices) 279 { 280 for (int index = indices.length - 1; index >= 0; index--) 281 { 282 listModel.removeElementAt(indices[index]); 283 } 284 285 list.clearSelection(); 287 288 enableSelectionButtons(); 291 } 292 293 294 protected void removeAllElements() 295 { 296 listModel.removeAllElements(); 297 298 enableSelectionButtons(); 301 } 302 303 304 307 protected void enableSelectionButtons() 308 { 309 boolean selected = !list.isSelectionEmpty(); 310 311 for (int index = firstSelectionButton; index < getComponentCount(); index++) 313 { 314 getComponent(index).setEnabled(selected); 315 } 316 } 317 } 318 | Popular Tags |