1 package org.ozoneDB.adminGui.feature.account.groups; 8 9 import java.awt.BorderLayout ; 10 import java.awt.Component ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.awt.event.MouseAdapter ; 14 import java.awt.event.MouseEvent ; 15 import java.util.Vector ; 16 import javax.swing.event.TreeSelectionListener ; 17 import javax.swing.event.TreeSelectionEvent ; 18 import javax.swing.JMenuItem ; 19 import javax.swing.JPopupMenu ; 20 import javax.swing.JScrollPane ; 21 import javax.swing.JTree ; 22 import javax.swing.tree.TreePath ; 23 import javax.swing.tree.TreeSelectionModel ; 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 42 44 public class GroupPanel extends TitledPanel implements TreeSelectionListener { 45 46 47 private GroupAccount groupAccount; 48 49 51 52 private JTree groupTree = null; 53 54 private GroupTreeModel groupTreeModel = null; 55 56 57 private JScrollPane scrollPane; 58 59 private String lastGroup = ""; 60 61 protected JPopupMenu menuPopup = new JPopupMenu (); 62 63 protected JMenuItem itemRemoveGroup = new JMenuItem ("Remove"); 64 65 protected JMenuItem itemCreateGroup = new JMenuItem ("Create Group"); 66 67 protected JMenuItem itemAssignUser = new JMenuItem ("Assign User"); 68 69 protected JMenuItem itemUnassignUser = new JMenuItem ("Unassign"); 70 71 72 76 public GroupPanel() { 77 super("Groups"); 78 init(); 80 81 this.groupAccount.list(); 83 } 84 85 88 private void init() { 89 this.groupAccount = new GroupAccount(this); 91 92 this.itemRemoveGroup.addActionListener(new ActionListener () { 94 public void actionPerformed(ActionEvent e) { 95 groupAccount.remove(); 96 } 97 }); 98 99 this.itemCreateGroup.addActionListener(new ActionListener () { 100 public void actionPerformed(ActionEvent e) { 101 groupAccount.create(); 102 } 103 }); 104 105 this.itemUnassignUser.addActionListener(new ActionListener () { 106 public void actionPerformed(ActionEvent e) { 107 groupAccount.unassign(); 108 } 109 }); 110 111 this.itemAssignUser.addActionListener(new ActionListener () { 112 public void actionPerformed(ActionEvent e) { 113 groupAccount.assign(); 114 } 115 }); 116 117 this.groupTreeModel = new GroupTreeModel(); 119 this.groupTree = new JTree (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 this.groupTree.addMouseListener(new MouseAdapter () { 128 public void mousePressed(MouseEvent me) { 129 if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) { 131 rightSelectTreeNode(me.getComponent(), me.getX(), 132 me.getY()); 133 } 134 } 135 }); 136 137 scrollPane = new JScrollPane (groupTree); 139 this.add(scrollPane, BorderLayout.CENTER); 140 141 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 153 public GroupAccount getGroups() { 154 return this.groupAccount; 155 } 156 157 162 public JTree getTree() { 163 return this.groupTree; 164 } 165 166 171 public GroupTreeModel getTreeModel() { 172 return this.groupTreeModel; 173 } 174 175 181 public void valueChanged(TreeSelectionEvent event) { 182 Object selectedNode = this.groupTree.getLastSelectedPathComponent(); 184 185 if (selectedNode != null) { 186 int nodeType = -99; 187 188 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 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 else 215 setOptions(false, false, false, false); 216 } 217 218 227 public void setOptions(boolean add, boolean grp, boolean rvu, boolean rvg) { 228 this.itemAssignUser.setVisible(add); 230 this.itemCreateGroup.setVisible(grp); 231 this.itemRemoveGroup.setVisible(rvg); 232 this.itemUnassignUser.setVisible(rvu); 233 } 234 235 242 private void rightSelectTreeNode(Component component, int x, int y) { 243 TreePath 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 254 public void refreshTree() { 255 264 265 this.groupTreeModel.fireTreeStructureChanged(); 267 268 274 } 275 276 281 protected Vector getAllUsers() { 282 return UserAccount.allUsersNames(); 283 } 284 285 286 } 288 289 | Popular Tags |