KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > modeler > dialogs > SelectionPanel


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.modeler.dialogs;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.MouseEvent JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.Comparator JavaDoc;
39 import java.util.List JavaDoc;
40
41 import javax.swing.DefaultComboBoxModel JavaDoc;
42 import javax.swing.JButton JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JList JavaDoc;
45 import javax.swing.JPanel JavaDoc;
46 import javax.swing.JScrollPane JavaDoc;
47 import javax.swing.ListCellRenderer JavaDoc;
48 import javax.swing.ListModel JavaDoc;
49 import javax.swing.ListSelectionModel JavaDoc;
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 JavaDoc
58 {
59   private static final long serialVersionUID = 1L;
60   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
61   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
62   JScrollPane JavaDoc jScrollPane2 = new JScrollPane JavaDoc();
63   JList JavaDoc _lstSelected = new JList JavaDoc();
64   JList JavaDoc _lstAvailable = new JList JavaDoc();
65   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
66   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
67   JButton JavaDoc _butAdd = new JButton JavaDoc();
68   JButton JavaDoc _butRemove = new JButton JavaDoc();
69   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
70   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
71   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
72   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
73   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
74   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
75   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
76   JLabel JavaDoc jLabel4 = new JLabel JavaDoc();
77   JButton JavaDoc _butRemoveAll = new JButton JavaDoc();
78   JButton JavaDoc _butAddAll = new JButton JavaDoc();
79   List JavaDoc _available = new ArrayList JavaDoc();
80   Comparator JavaDoc _customComparator = null;
81
82   public SelectionPanel()
83   {
84     try
85     {
86       jbInit();
87     }
88     catch (Exception JavaDoc ex)
89     {
90       DjLogger.log(ex);
91     }
92   }
93
94   public void setCellRenderer(ListCellRenderer JavaDoc cellRenderer)
95   {
96     _lstSelected.setCellRenderer(cellRenderer);
97     _lstAvailable.setCellRenderer(cellRenderer);
98   }
99
100   public Comparator JavaDoc getComparator()
101   {
102     if (_customComparator == null) _customComparator = new DjStringComparator(false);
103     return _customComparator;
104   }
105
106   public void setComparator(Comparator JavaDoc comp)
107   {
108     _customComparator = comp;
109   }
110
111   public void setAvailable(List JavaDoc 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 JavaDoc());
122     _lstAvailable.setModel(new DefaultComboBoxModel JavaDoc(_available.toArray()));
123   }
124
125   public void selectAll()
126   {
127     _lstAvailable.setModel(new DefaultComboBoxModel JavaDoc());
128     _lstSelected.setModel(new DefaultComboBoxModel JavaDoc(_available.toArray()));
129   }
130
131   public void setSelection(ArrayList JavaDoc sels)
132   {
133     ArrayList JavaDoc avs = new ArrayList JavaDoc();
134     avs.addAll(_available);
135     avs.removeAll(sels);
136     Collections.sort(avs, getComparator());
137     Collections.sort(sels, getComparator());
138     _lstAvailable.setModel(new DefaultComboBoxModel JavaDoc(avs.toArray()));
139     _lstSelected.setModel(new DefaultComboBoxModel JavaDoc(sels.toArray()));
140   }
141
142   void jbInit() throws Exception JavaDoc
143   {
144     this.setLayout(borderLayout1);
145     jScrollPane1.setPreferredSize(new Dimension JavaDoc(250, 131));
146     jScrollPane2.setPreferredSize(new Dimension JavaDoc(250, 131));
147     jPanel1.setLayout(verticalFlowLayout1);
148     _butAdd.setPreferredSize(new Dimension JavaDoc(24, 24));
149     _butAdd.addActionListener(new SelectionPanel__butAdd_actionAdapter(this));
150     verticalFlowLayout1.setHorizontalFill(false);
151     _butRemove.setPreferredSize(new Dimension JavaDoc(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")); //$NON-NLS-1$
157
jPanel3.setLayout(borderLayout3);
158     jLabel2.setText(Messages.getString("SelectionPanel.Selected")); //$NON-NLS-1$
159
jLabel3.setText(" "); //$NON-NLS-1$
160
_lstAvailable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
161     jLabel4.setText(" "); //$NON-NLS-1$
162
_butRemoveAll.setPreferredSize(new Dimension JavaDoc(24, 24));
163     _butRemoveAll.addActionListener(new SelectionPanel__butRemoveAll_actionAdapter(this));
164     _butAddAll.setPreferredSize(new Dimension JavaDoc(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")); //$NON-NLS-1$
183
_butRemove.setIcon(ModelEditor.getImageIcon("remove.gif")); //$NON-NLS-1$
184
_butAddAll.setIcon(ModelEditor.getImageIcon("addall.gif")); //$NON-NLS-1$
185
_butRemoveAll.setIcon(ModelEditor.getImageIcon("removeall.gif")); //$NON-NLS-1$
186

187   }
188
189   void lstSelected_mouseClicked(MouseEvent JavaDoc e)
190   {
191     if (e.getClickCount() > 1) moveSelected(_lstSelected, _lstAvailable);
192   }
193
194   void lstAvailable_mouseClicked(MouseEvent JavaDoc e)
195   {
196     if (e.getClickCount() > 1) moveSelected(_lstAvailable, _lstSelected);
197   }
198
199   protected void moveSelected(JList JavaDoc from, JList JavaDoc to)
200   {
201     DefaultComboBoxModel JavaDoc fromModel = (DefaultComboBoxModel JavaDoc) from.getModel();
202     DefaultComboBoxModel JavaDoc toModel = (DefaultComboBoxModel JavaDoc) to.getModel();
203
204     Object JavaDoc[] 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 JavaDoc sortthis = new ArrayList JavaDoc();
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 JavaDoc(sortthis.toArray()));
219   }
220
221   void _butAdd_actionPerformed(ActionEvent JavaDoc e)
222   {
223     moveSelected(_lstAvailable, _lstSelected);
224   }
225
226   void _butRemove_actionPerformed(ActionEvent JavaDoc e)
227   {
228     moveSelected(_lstSelected, _lstAvailable);
229   }
230
231   void _butAddAll_actionPerformed(ActionEvent JavaDoc e)
232   {
233     selectAll();
234   }
235
236   void _butRemoveAll_actionPerformed(ActionEvent JavaDoc e)
237   {
238     selectNone();
239   }
240
241   public ArrayList JavaDoc getSelection()
242   {
243     ListModel JavaDoc model = _lstSelected.getModel();
244
245     ArrayList JavaDoc result = new ArrayList JavaDoc();
246     for (int i = 0; i < model.getSize(); i++)
247       result.add(model.getElementAt(i));
248     return result;
249   }
250
251   public ArrayList JavaDoc getNegatedSelection()
252   {
253     ListModel JavaDoc model = _lstAvailable.getModel();
254
255     ArrayList JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc
263 {
264   SelectionPanel adaptee;
265
266   SelectionPanel_lstSelected_mouseAdapter(SelectionPanel adaptee)
267   {
268     this.adaptee = adaptee;
269   }
270
271   public void mouseClicked(MouseEvent JavaDoc e)
272   {
273     adaptee.lstSelected_mouseClicked(e);
274   }
275 }
276
277 class SelectionPanel_lstAvailable_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
278 {
279   SelectionPanel adaptee;
280
281   SelectionPanel_lstAvailable_mouseAdapter(SelectionPanel adaptee)
282   {
283     this.adaptee = adaptee;
284   }
285
286   public void mouseClicked(MouseEvent JavaDoc e)
287   {
288     adaptee.lstAvailable_mouseClicked(e);
289   }
290 }
291
292 class SelectionPanel__butAdd_actionAdapter implements java.awt.event.ActionListener JavaDoc
293 {
294   SelectionPanel adaptee;
295
296   SelectionPanel__butAdd_actionAdapter(SelectionPanel adaptee)
297   {
298     this.adaptee = adaptee;
299   }
300
301   public void actionPerformed(ActionEvent JavaDoc e)
302   {
303     adaptee._butAdd_actionPerformed(e);
304   }
305 }
306
307 class SelectionPanel__butRemove_actionAdapter implements java.awt.event.ActionListener JavaDoc
308 {
309   SelectionPanel adaptee;
310
311   SelectionPanel__butRemove_actionAdapter(SelectionPanel adaptee)
312   {
313     this.adaptee = adaptee;
314   }
315
316   public void actionPerformed(ActionEvent JavaDoc e)
317   {
318     adaptee._butRemove_actionPerformed(e);
319   }
320 }
321
322 class SelectionPanel__butAddAll_actionAdapter implements java.awt.event.ActionListener JavaDoc
323 {
324   SelectionPanel adaptee;
325
326   SelectionPanel__butAddAll_actionAdapter(SelectionPanel adaptee)
327   {
328     this.adaptee = adaptee;
329   }
330
331   public void actionPerformed(ActionEvent JavaDoc e)
332   {
333     adaptee._butAddAll_actionPerformed(e);
334   }
335 }
336
337 class SelectionPanel__butRemoveAll_actionAdapter implements java.awt.event.ActionListener JavaDoc
338 {
339   SelectionPanel adaptee;
340
341   SelectionPanel__butRemoveAll_actionAdapter(SelectionPanel adaptee)
342   {
343     this.adaptee = adaptee;
344   }
345
346   public void actionPerformed(ActionEvent JavaDoc e)
347   {
348     adaptee._butRemoveAll_actionPerformed(e);
349   }
350 }
Popular Tags