KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > client > widgets > UserSelector


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.client.widgets;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.swing.*;
27
28 import org.lucane.client.Client;
29 import org.lucane.client.util.Translation;
30 import org.lucane.common.concepts.GroupConcept;
31 import org.lucane.common.concepts.UserConcept;
32
33
34 /**
35  * A List dialog to select users
36  */

37 public class UserSelector extends JDialog
38   implements ActionListener, MouseListener
39 {
40   private JButton btOK;
41   private JButton btSelectAll;
42   private JButton btCancel;
43   private JComboBox cmbGroups;
44   private JList lstUsers;
45   private DefaultListModel lstModel;
46   private JPanel pnlButton;
47   private JPanel pnlEast;
48   private boolean accept;
49   
50   private ArrayList JavaDoc groups;
51   private boolean recursive;
52
53   /**
54    * Creates a new object.
55    *
56    * @param owner the base JFrame (can be null)
57    * @param title the title of the dialog box
58    */

59   public UserSelector(JFrame owner, String JavaDoc title)
60   {
61     super(owner, title, true);
62     btOK = new JButton(Translation.tr("ok"), Client.getImageIcon("ok.png"));
63     btSelectAll = new JButton(Translation.tr("all"), Client.getImageIcon("select-all-users.png"));
64     btCancel = new JButton(Translation.tr("cancel"), Client.getImageIcon("cancel.png"));
65     lstModel = new DefaultListModel();
66     lstUsers = new JList(lstModel);
67     cmbGroups = new JComboBox();
68     pnlEast = new JPanel(new BorderLayout());
69     pnlButton = new JPanel(new GridLayout(3, 0));
70
71     pnlButton.add(btOK);
72     pnlButton.add(btSelectAll);
73     pnlButton.add(btCancel);
74     
75     pnlEast.add(pnlButton, BorderLayout.NORTH);
76     
77     btOK.addActionListener(this);
78     btSelectAll.addActionListener(this);
79     btCancel.addActionListener(this);
80     cmbGroups.addActionListener(this);
81     lstUsers.addMouseListener(this);
82     
83     setRecursive(false);
84     
85     this.setSize(400, 200);
86     this.getContentPane().setLayout(new BorderLayout(3, 3));
87     this.getContentPane().add(cmbGroups, BorderLayout.NORTH);
88     this.getContentPane().add(new JScrollPane(lstUsers), BorderLayout.CENTER);
89     this.getContentPane().add(pnlEast, BorderLayout.EAST);
90   }
91   
92   public void setGroups(ArrayList JavaDoc groups)
93   {
94     this.groups = groups;
95     
96     Iterator JavaDoc i = groups.iterator();
97     while(i.hasNext())
98         {
99             GroupConcept group = (GroupConcept)i.next();
100             if(group.getUsers().hasNext())
101                 cmbGroups.addItem(group);
102             else if(recursive)
103             {
104                 ArrayList JavaDoc tmpusers = new ArrayList JavaDoc();
105                 ArrayList JavaDoc tmpgroups = new ArrayList JavaDoc();
106                 recurseGroups(group, tmpusers, tmpgroups);
107                 if(tmpusers.size() > 0)
108                     cmbGroups.addItem(group);
109             }
110         }
111   }
112   
113   public void setRecursive(boolean recursive)
114   {
115     this.recursive = recursive;
116     this.btSelectAll.setVisible(recursive);
117   }
118   
119   
120   /**
121    * Get the selected item index
122    *
123    * @return the selected index
124    */

125   public Object JavaDoc selectItem()
126   {
127     lstUsers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
128     pnlButton.remove(btSelectAll);
129     this.show();
130
131     if(accept)
132       return lstUsers.getSelectedValue();
133     else
134       return null;
135   }
136
137   /**
138    * Get the selected items
139    *
140    * @return the selected items
141    */

142   public Object JavaDoc[] selectItems()
143   {
144     lstUsers.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
145     this.show();
146
147     if(accept)
148         return lstUsers.getSelectedValues();
149     else
150         return null;
151   }
152
153   /**
154    * A button has been clicked (oh my god!)
155    *
156    * @param ev yet another event...
157    */

158   public void actionPerformed(ActionEvent ev)
159   {
160     if(ev.getSource() == btOK)
161     {
162       accept = true;
163       this.dispose();
164     }
165     else if(ev.getSource() == btSelectAll)
166     {
167       lstUsers.setSelectionInterval(0, lstUsers.getModel().getSize()-1);
168       accept = true;
169       this.dispose();
170     }
171     else if(ev.getSource() == btCancel)
172     {
173       accept = false;
174       this.dispose();
175     }
176     else if(ev.getSource() == cmbGroups)
177     {
178         GroupConcept group = (GroupConcept)cmbGroups.getSelectedItem();
179         if(recursive)
180             showRecursiveGroup(group);
181         else
182             showGroup(group);
183     }
184   }
185
186   private void showGroup(GroupConcept group)
187   {
188     lstModel.clear();
189     Iterator JavaDoc users = group.getUsers();
190     while(users.hasNext())
191         lstModel.addElement(users.next());
192   }
193
194   private void showRecursiveGroup(GroupConcept group)
195   {
196     lstModel.clear();
197     ArrayList JavaDoc users = new ArrayList JavaDoc();
198     ArrayList JavaDoc groups = new ArrayList JavaDoc();
199     
200     recurseGroups(group, users, groups);
201     Iterator JavaDoc i = users.iterator();
202     while(i.hasNext())
203         lstModel.addElement(i.next());
204   }
205   
206   private void recurseGroups(GroupConcept group, ArrayList JavaDoc users, ArrayList JavaDoc groups)
207   {
208     groups.add(group);
209     
210     Iterator JavaDoc i = group.getUsers();
211     while(i.hasNext())
212     {
213         UserConcept user = (UserConcept)i.next();
214         if(!users.contains(user))
215             users.add(user);
216     }
217     
218     
219     i = this.groups.iterator();
220     while(i.hasNext())
221     {
222         GroupConcept g = (GroupConcept)i.next();
223         if(g.hasParent(group) && !groups.contains(g))
224             recurseGroups(g, users, groups);
225     }
226   }
227   
228   //-- mouse listener
229

230     public void mouseEntered(MouseEvent e) {}
231     public void mouseExited(MouseEvent e) {}
232     public void mousePressed(MouseEvent e) {}
233     public void mouseReleased(MouseEvent e) {}
234     public void mouseClicked(MouseEvent e)
235     {
236         if(e.getClickCount() > 1 && lstUsers.getSelectedIndex() >= 0)
237         {
238             accept = true;
239             this.dispose();
240         }
241     }
242 }
Popular Tags