1 30 package com.genimen.djeneric.tools.modeler.dialogs; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.MouseEvent ; 36 import java.util.ArrayList ; 37 import java.util.Collections ; 38 import java.util.Comparator ; 39 import java.util.List ; 40 41 import javax.swing.DefaultComboBoxModel ; 42 import javax.swing.JButton ; 43 import javax.swing.JLabel ; 44 import javax.swing.JList ; 45 import javax.swing.JPanel ; 46 import javax.swing.JScrollPane ; 47 import javax.swing.ListCellRenderer ; 48 import javax.swing.ListModel ; 49 import javax.swing.ListSelectionModel ; 50 51 import com.genimen.djeneric.language.Messages; 52 import com.genimen.djeneric.tools.modeler.ModelEditor; 53 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 54 import com.genimen.djeneric.util.DjLogger; 55 import com.genimen.djeneric.util.DjStringComparator; 56 57 public class SelectionPanel extends JPanel 58 { 59 private static final long serialVersionUID = 1L; 60 BorderLayout borderLayout1 = new BorderLayout (); 61 JScrollPane jScrollPane1 = new JScrollPane (); 62 JScrollPane jScrollPane2 = new JScrollPane (); 63 JList _lstSelected = new JList (); 64 JList _lstAvailable = new JList (); 65 JPanel jPanel1 = new JPanel (); 66 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 67 JButton _butAdd = new JButton (); 68 JButton _butRemove = new JButton (); 69 JPanel jPanel2 = new JPanel (); 70 BorderLayout borderLayout2 = new BorderLayout (); 71 JLabel jLabel1 = new JLabel (); 72 JPanel jPanel3 = new JPanel (); 73 BorderLayout borderLayout3 = new BorderLayout (); 74 JLabel jLabel2 = new JLabel (); 75 JLabel jLabel3 = new JLabel (); 76 JLabel jLabel4 = new JLabel (); 77 JButton _butRemoveAll = new JButton (); 78 JButton _butAddAll = new JButton (); 79 List _available = new ArrayList (); 80 Comparator _customComparator = null; 81 82 public SelectionPanel() 83 { 84 try 85 { 86 jbInit(); 87 } 88 catch (Exception ex) 89 { 90 DjLogger.log(ex); 91 } 92 } 93 94 public void setCellRenderer(ListCellRenderer cellRenderer) 95 { 96 _lstSelected.setCellRenderer(cellRenderer); 97 _lstAvailable.setCellRenderer(cellRenderer); 98 } 99 100 public Comparator getComparator() 101 { 102 if (_customComparator == null) _customComparator = new DjStringComparator(false); 103 return _customComparator; 104 } 105 106 public void setComparator(Comparator comp) 107 { 108 _customComparator = comp; 109 } 110 111 public void setAvailable(List available) 112 { 113 Collections.sort(available, getComparator()); 114 _available = available; 115 116 selectNone(); 117 } 118 119 public void selectNone() 120 { 121 _lstSelected.setModel(new DefaultComboBoxModel ()); 122 _lstAvailable.setModel(new DefaultComboBoxModel (_available.toArray())); 123 } 124 125 public void selectAll() 126 { 127 _lstAvailable.setModel(new DefaultComboBoxModel ()); 128 _lstSelected.setModel(new DefaultComboBoxModel (_available.toArray())); 129 } 130 131 public void setSelection(ArrayList sels) 132 { 133 ArrayList avs = new ArrayList (); 134 avs.addAll(_available); 135 avs.removeAll(sels); 136 Collections.sort(avs, getComparator()); 137 Collections.sort(sels, getComparator()); 138 _lstAvailable.setModel(new DefaultComboBoxModel (avs.toArray())); 139 _lstSelected.setModel(new DefaultComboBoxModel (sels.toArray())); 140 } 141 142 void jbInit() throws Exception 143 { 144 this.setLayout(borderLayout1); 145 jScrollPane1.setPreferredSize(new Dimension (250, 131)); 146 jScrollPane2.setPreferredSize(new Dimension (250, 131)); 147 jPanel1.setLayout(verticalFlowLayout1); 148 _butAdd.setPreferredSize(new Dimension (24, 24)); 149 _butAdd.addActionListener(new SelectionPanel__butAdd_actionAdapter(this)); 150 verticalFlowLayout1.setHorizontalFill(false); 151 _butRemove.setPreferredSize(new Dimension (24, 24)); 152 _butRemove.addActionListener(new SelectionPanel__butRemove_actionAdapter(this)); 153 _lstSelected.addMouseListener(new SelectionPanel_lstSelected_mouseAdapter(this)); 154 _lstAvailable.addMouseListener(new SelectionPanel_lstAvailable_mouseAdapter(this)); 155 jPanel2.setLayout(borderLayout2); 156 jLabel1.setText(Messages.getString("SelectionPanel.Available")); jPanel3.setLayout(borderLayout3); 158 jLabel2.setText(Messages.getString("SelectionPanel.Selected")); jLabel3.setText(" "); _lstAvailable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 161 jLabel4.setText(" "); _butRemoveAll.setPreferredSize(new Dimension (24, 24)); 163 _butRemoveAll.addActionListener(new SelectionPanel__butRemoveAll_actionAdapter(this)); 164 _butAddAll.setPreferredSize(new Dimension (24, 24)); 165 _butAddAll.addActionListener(new SelectionPanel__butAddAll_actionAdapter(this)); 166 this.add(jPanel1, BorderLayout.CENTER); 167 jPanel1.add(jLabel3, null); 168 jPanel1.add(_butAdd, null); 169 jPanel1.add(_butRemove, null); 170 jPanel1.add(jLabel4, null); 171 jPanel1.add(_butAddAll, null); 172 jPanel1.add(_butRemoveAll, null); 173 this.add(jPanel2, BorderLayout.WEST); 174 jPanel2.add(jScrollPane1, BorderLayout.CENTER); 175 jPanel2.add(jLabel1, BorderLayout.NORTH); 176 this.add(jPanel3, BorderLayout.EAST); 177 jPanel3.add(jScrollPane2, BorderLayout.CENTER); 178 jPanel3.add(jLabel2, BorderLayout.NORTH); 179 jScrollPane2.getViewport().add(_lstSelected, null); 180 jScrollPane1.getViewport().add(_lstAvailable, null); 181 182 _butAdd.setIcon(ModelEditor.getImageIcon("add.gif")); _butRemove.setIcon(ModelEditor.getImageIcon("remove.gif")); _butAddAll.setIcon(ModelEditor.getImageIcon("addall.gif")); _butRemoveAll.setIcon(ModelEditor.getImageIcon("removeall.gif")); 187 } 188 189 void lstSelected_mouseClicked(MouseEvent e) 190 { 191 if (e.getClickCount() > 1) moveSelected(_lstSelected, _lstAvailable); 192 } 193 194 void lstAvailable_mouseClicked(MouseEvent e) 195 { 196 if (e.getClickCount() > 1) moveSelected(_lstAvailable, _lstSelected); 197 } 198 199 protected void moveSelected(JList from, JList to) 200 { 201 DefaultComboBoxModel fromModel = (DefaultComboBoxModel ) from.getModel(); 202 DefaultComboBoxModel toModel = (DefaultComboBoxModel ) to.getModel(); 203 204 Object [] sels = from.getSelectedValues(); 205 for (int i = 0; i < sels.length; i++) 206 { 207 fromModel.removeElement(sels[i]); 208 toModel.addElement(sels[i]); 209 } 210 211 ArrayList sortthis = new ArrayList (); 212 213 for (int i = 0; i < toModel.getSize(); i++) 214 { 215 sortthis.add(toModel.getElementAt(i)); 216 } 217 Collections.sort(sortthis, getComparator()); 218 to.setModel(new DefaultComboBoxModel (sortthis.toArray())); 219 } 220 221 void _butAdd_actionPerformed(ActionEvent e) 222 { 223 moveSelected(_lstAvailable, _lstSelected); 224 } 225 226 void _butRemove_actionPerformed(ActionEvent e) 227 { 228 moveSelected(_lstSelected, _lstAvailable); 229 } 230 231 void _butAddAll_actionPerformed(ActionEvent e) 232 { 233 selectAll(); 234 } 235 236 void _butRemoveAll_actionPerformed(ActionEvent e) 237 { 238 selectNone(); 239 } 240 241 public ArrayList getSelection() 242 { 243 ListModel model = _lstSelected.getModel(); 244 245 ArrayList result = new ArrayList (); 246 for (int i = 0; i < model.getSize(); i++) 247 result.add(model.getElementAt(i)); 248 return result; 249 } 250 251 public ArrayList getNegatedSelection() 252 { 253 ListModel model = _lstAvailable.getModel(); 254 255 ArrayList result = new ArrayList (); 256 for (int i = 0; i < model.getSize(); i++) 257 result.add(model.getElementAt(i)); 258 return result; 259 } 260 } 261 262 class SelectionPanel_lstSelected_mouseAdapter extends java.awt.event.MouseAdapter 263 { 264 SelectionPanel adaptee; 265 266 SelectionPanel_lstSelected_mouseAdapter(SelectionPanel adaptee) 267 { 268 this.adaptee = adaptee; 269 } 270 271 public void mouseClicked(MouseEvent e) 272 { 273 adaptee.lstSelected_mouseClicked(e); 274 } 275 } 276 277 class SelectionPanel_lstAvailable_mouseAdapter extends java.awt.event.MouseAdapter 278 { 279 SelectionPanel adaptee; 280 281 SelectionPanel_lstAvailable_mouseAdapter(SelectionPanel adaptee) 282 { 283 this.adaptee = adaptee; 284 } 285 286 public void mouseClicked(MouseEvent e) 287 { 288 adaptee.lstAvailable_mouseClicked(e); 289 } 290 } 291 292 class SelectionPanel__butAdd_actionAdapter implements java.awt.event.ActionListener 293 { 294 SelectionPanel adaptee; 295 296 SelectionPanel__butAdd_actionAdapter(SelectionPanel adaptee) 297 { 298 this.adaptee = adaptee; 299 } 300 301 public void actionPerformed(ActionEvent e) 302 { 303 adaptee._butAdd_actionPerformed(e); 304 } 305 } 306 307 class SelectionPanel__butRemove_actionAdapter implements java.awt.event.ActionListener 308 { 309 SelectionPanel adaptee; 310 311 SelectionPanel__butRemove_actionAdapter(SelectionPanel adaptee) 312 { 313 this.adaptee = adaptee; 314 } 315 316 public void actionPerformed(ActionEvent e) 317 { 318 adaptee._butRemove_actionPerformed(e); 319 } 320 } 321 322 class SelectionPanel__butAddAll_actionAdapter implements java.awt.event.ActionListener 323 { 324 SelectionPanel adaptee; 325 326 SelectionPanel__butAddAll_actionAdapter(SelectionPanel adaptee) 327 { 328 this.adaptee = adaptee; 329 } 330 331 public void actionPerformed(ActionEvent e) 332 { 333 adaptee._butAddAll_actionPerformed(e); 334 } 335 } 336 337 class SelectionPanel__butRemoveAll_actionAdapter implements java.awt.event.ActionListener 338 { 339 SelectionPanel adaptee; 340 341 SelectionPanel__butRemoveAll_actionAdapter(SelectionPanel adaptee) 342 { 343 this.adaptee = adaptee; 344 } 345 346 public void actionPerformed(ActionEvent e) 347 { 348 adaptee._butRemoveAll_actionPerformed(e); 349 } 350 } | Popular Tags |