KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > feature > account > groups > GroupPanel


1 //You can redistribute this software and/or modify it under the terms of
2
//the Ozone Library License version 1 published by ozone-db.org.
3
//
4
//The original code and portions created by SMB are
5
//Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package org.ozoneDB.adminGui.feature.account.groups;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.awt.event.MouseAdapter JavaDoc;
14 import java.awt.event.MouseEvent JavaDoc;
15 import java.util.Vector JavaDoc;
16 import javax.swing.event.TreeSelectionListener JavaDoc;
17 import javax.swing.event.TreeSelectionEvent JavaDoc;
18 import javax.swing.JMenuItem JavaDoc;
19 import javax.swing.JPopupMenu JavaDoc;
20 import javax.swing.JScrollPane JavaDoc;
21 import javax.swing.JTree JavaDoc;
22 import javax.swing.tree.TreePath JavaDoc;
23 import javax.swing.tree.TreeSelectionModel JavaDoc;
24
25 import org.ozoneDB.adminGui.feature.account.Account;
26 import org.ozoneDB.adminGui.feature.account.AccountItem;
27 import org.ozoneDB.adminGui.feature.account.users.UserItem;
28 import org.ozoneDB.adminGui.feature.account.users.UserAccount;
29 import org.ozoneDB.adminGui.widget.TitledPanel;
30 import org.ozoneDB.adminGui.main.AdminGui;
31
32
33 //#############################################################################
34
/**
35  * This class is used to manage the group list panel.
36  *
37  * @author <p align=center>Ibsen Ramos-Bonilla
38  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
39  *
40  * @version 1.0
41  */

42 //#############################################################################
43

44 public class GroupPanel extends TitledPanel implements TreeSelectionListener JavaDoc {
45
46     /** Handle to the Groups information instance. */
47     private GroupAccount groupAccount;
48
49     //private UserAccount userAccount;
50

51     /** The group list table. */
52     private JTree JavaDoc groupTree = null;
53     /** Table model for the group tree. */
54     private GroupTreeModel groupTreeModel = null;
55
56     /** Scroll panel that holds the account list table. */
57     private JScrollPane JavaDoc scrollPane;
58     /** Tracks the last account record selected. */
59     private String JavaDoc lastGroup = "";
60     /** Group tree popup menu. */
61     protected JPopupMenu JavaDoc menuPopup = new JPopupMenu JavaDoc();
62     /** Group tree remove menu item. */
63     protected JMenuItem JavaDoc itemRemoveGroup = new JMenuItem JavaDoc("Remove");
64     /** Group tree create group menu item. */
65     protected JMenuItem JavaDoc itemCreateGroup = new JMenuItem JavaDoc("Create Group");
66     /** Group tree assign account menu item. */
67     protected JMenuItem JavaDoc itemAssignUser = new JMenuItem JavaDoc("Assign User");
68     /** Group tree unassign account menu item. */
69     protected JMenuItem JavaDoc itemUnassignUser = new JMenuItem JavaDoc("Unassign");
70
71
72     /**
73      * The default constructor.
74      *
75      */

76     public GroupPanel() {
77         super("Groups");
78         //this.userAccount = userAccount;
79
init();
80
81         //load the table
82
this.groupAccount.list();
83     }
84
85     /**
86      * This method initializes the account panel.
87      */

88     private void init() {
89         //set the account account
90
this.groupAccount = new GroupAccount(this);
91
92         //add menu listeners
93
this.itemRemoveGroup.addActionListener(new ActionListener JavaDoc() {
94             public void actionPerformed(ActionEvent JavaDoc e) {
95                 groupAccount.remove();
96             }
97         });
98
99         this.itemCreateGroup.addActionListener(new ActionListener JavaDoc() {
100             public void actionPerformed(ActionEvent JavaDoc e) {
101                 groupAccount.create();
102             }
103         });
104
105         this.itemUnassignUser.addActionListener(new ActionListener JavaDoc() {
106             public void actionPerformed(ActionEvent JavaDoc e) {
107                 groupAccount.unassign();
108             }
109         });
110
111         this.itemAssignUser.addActionListener(new ActionListener JavaDoc() {
112             public void actionPerformed(ActionEvent JavaDoc e) {
113                 groupAccount.assign();
114             }
115         });
116
117         //set the account tree
118
this.groupTreeModel = new GroupTreeModel();
119         this.groupTree = new JTree JavaDoc(this.groupTreeModel);
120         this.groupTree.getSelectionModel().setSelectionMode(
121                 TreeSelectionModel.SINGLE_TREE_SELECTION);
122         this.groupTree.setShowsRootHandles(true);
123         this.groupTree.setCellRenderer(new GroupTreeRenderer());
124         this.groupTree.addTreeSelectionListener(this);
125
126         //add a mouse event for the right mouse button
127
this.groupTree.addMouseListener(new MouseAdapter JavaDoc() {
128             public void mousePressed(MouseEvent JavaDoc me) {
129                 //clicked with right mouse button
130
if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
131                     rightSelectTreeNode(me.getComponent(), me.getX(),
132                             me.getY());
133                 }
134             }
135         });
136
137         //add the controls to the panel
138
scrollPane = new JScrollPane JavaDoc(groupTree);
139         this.add(scrollPane, BorderLayout.CENTER);
140
141         //add the popup menu items for this tree
142
this.menuPopup.add(this.itemAssignUser);
143         this.menuPopup.add(this.itemCreateGroup);
144         this.menuPopup.add(this.itemUnassignUser);
145         this.menuPopup.add(this.itemRemoveGroup);
146     }
147
148     /**
149      * This method returns a handle to the account panel group information.
150      *
151      * @return GroupAccount - the groups information instance.
152      */

153     public GroupAccount getGroups() {
154         return this.groupAccount;
155     }
156
157     /**
158      * This method returns a handle to the group tree.
159      *
160      * @return JTree - the group tree instance.
161      */

162     public JTree JavaDoc getTree() {
163         return this.groupTree;
164     }
165
166     /**
167      * This method returns a handle to the group panel tree model.
168      *
169      * @return GroupTableModel - handle to the group tree model.
170      */

171     public GroupTreeModel getTreeModel() {
172         return this.groupTreeModel;
173     }
174
175     /**
176      * This method checks when a node has been selected and enables/disables
177      * the tree buttons accordingly.
178      *
179      * @param event - a tree selection event.
180      */

181     public void valueChanged(TreeSelectionEvent JavaDoc event) {
182         //check if we have a valid node
183
Object JavaDoc selectedNode = this.groupTree.getLastSelectedPathComponent();
184
185         if (selectedNode != null) {
186             int nodeType = -99;
187
188             //retrieve the node type
189
if (selectedNode instanceof UserItem)
190                 nodeType = Account.NODE_IS_USER;
191             else if (selectedNode instanceof GroupItem)
192                 nodeType = Account.NODE_IS_GROUP;
193             else if (selectedNode instanceof AccountItem)
194                 nodeType = Account.NODE_IS_ACCOUNT;
195
196             //set the menu switches (add, select, research)
197
switch (nodeType) {
198                 case Account.NODE_IS_USER:
199                     setOptions(false, false, true, false);
200                     break;
201                 case Account.NODE_IS_GROUP:
202                     setOptions(true, false, false, true);
203                     break;
204                 case Account.NODE_IS_ACCOUNT:
205                     setOptions(false, true, false, false);
206                     break;
207                 default:
208                     setOptions(false, false, false, false);
209                     break;
210             }
211         }
212
213         //not a valid node, turn off all menus
214
else
215             setOptions(false, false, false, false);
216     }
217
218     /**
219      * This method enables/disables the toolbar buttons and popup menus
220      * accordingly.
221      *
222      * @param add - enables/disables the add option.
223      * @param grp - enables/disables the new group option.
224      * @param rvu - enables/disables the remove account option.
225      * @param rvg - enables/disables the remove group option.
226      */

227     public void setOptions(boolean add, boolean grp, boolean rvu, boolean rvg) {
228         //enable/disable popup menus
229
this.itemAssignUser.setVisible(add);
230         this.itemCreateGroup.setVisible(grp);
231         this.itemRemoveGroup.setVisible(rvg);
232         this.itemUnassignUser.setVisible(rvu);
233     }
234
235     /**
236      * This method selects a tree node by using the mouse right button.
237      *
238      * @param component - the component holding the tree.
239      * @param x - the horizontal coordinate into the tree
240      * @param y - the vertical coordinate into the tree
241      */

242     private void rightSelectTreeNode(Component JavaDoc component, int x, int y) {
243         TreePath JavaDoc path = this.groupTree.getPathForLocation(x, y);
244
245         if (path != null) {
246             this.groupTree.setSelectionPath(path);
247             this.menuPopup.show(component, x, y);
248         }
249     }
250
251     /**
252      * This method refreshes the tree.
253      */

254     public void refreshTree() {
255         //save the current tree path
256
/*TreePath selPath = null;
257
258         try {
259             selPath = this.groupTree.getSelectionPath();
260         }
261         catch(Exception e) {
262             selPath = null;
263         }*/

264
265         //notify the change
266
this.groupTreeModel.fireTreeStructureChanged();
267
268         //set the path again
269
/*if(selPath != null) {
270             this.groupTree.expandPath(selPath);
271             this.groupTree.setSelectionPath(selPath);
272             this.groupTree.scrollPathToVisible(selPath);
273         }*/

274     }
275
276     /**
277      * This internal method fetches all the account names from the user account.
278      *
279      * @return Vector - a list of account names.
280      */

281     protected Vector JavaDoc getAllUsers() {
282         return UserAccount.allUsersNames();
283     }
284
285
286 } //--------------------------------- E O F -----------------------------------
287

288
289
Popular Tags