📄 grouppanel.java
字号:
//You can redistribute this software and/or modify it under the terms of
//the Ozone Library License version 1 published by ozone-db.org.
//
//The original code and portions created by SMB are
//Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package org.ozoneDB.adminGui.feature.account.groups;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.ozoneDB.adminGui.feature.account.Account;
import org.ozoneDB.adminGui.feature.account.AccountItem;
import org.ozoneDB.adminGui.feature.account.users.UserItem;
import org.ozoneDB.adminGui.feature.account.users.UserAccount;
import org.ozoneDB.adminGui.widget.TitledPanel;
import org.ozoneDB.adminGui.main.AdminGui;
//#############################################################################
/**
* This class is used to manage the group list panel.
*
* @author <p align=center>Ibsen Ramos-Bonilla
* <br>Copyright © 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
*
* @version 1.0
*/
//#############################################################################
public class GroupPanel extends TitledPanel implements TreeSelectionListener {
/** Handle to the Groups information instance. */
private GroupAccount groupAccount;
//private UserAccount userAccount;
/** The group list table. */
private JTree groupTree = null;
/** Table model for the group tree. */
private GroupTreeModel groupTreeModel = null;
/** Scroll panel that holds the account list table. */
private JScrollPane scrollPane;
/** Tracks the last account record selected. */
private String lastGroup = "";
/** Group tree popup menu. */
protected JPopupMenu menuPopup = new JPopupMenu();
/** Group tree remove menu item. */
protected JMenuItem itemRemoveGroup = new JMenuItem("Remove");
/** Group tree create group menu item. */
protected JMenuItem itemCreateGroup = new JMenuItem("Create Group");
/** Group tree assign account menu item. */
protected JMenuItem itemAssignUser = new JMenuItem("Assign User");
/** Group tree unassign account menu item. */
protected JMenuItem itemUnassignUser = new JMenuItem("Unassign");
/**
* The default constructor.
*
*/
public GroupPanel() {
super("Groups");
//this.userAccount = userAccount;
init();
//load the table
this.groupAccount.list();
}
/**
* This method initializes the account panel.
*/
private void init() {
//set the account account
this.groupAccount = new GroupAccount(this);
//add menu listeners
this.itemRemoveGroup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
groupAccount.remove();
}
});
this.itemCreateGroup.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
groupAccount.create();
}
});
this.itemUnassignUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
groupAccount.unassign();
}
});
this.itemAssignUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
groupAccount.assign();
}
});
//set the account tree
this.groupTreeModel = new GroupTreeModel();
this.groupTree = new JTree(this.groupTreeModel);
this.groupTree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
this.groupTree.setShowsRootHandles(true);
this.groupTree.setCellRenderer(new GroupTreeRenderer());
this.groupTree.addTreeSelectionListener(this);
//add a mouse event for the right mouse button
this.groupTree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
//clicked with right mouse button
if ((me.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
rightSelectTreeNode(me.getComponent(), me.getX(),
me.getY());
}
}
});
//add the controls to the panel
scrollPane = new JScrollPane(groupTree);
this.add(scrollPane, BorderLayout.CENTER);
//add the popup menu items for this tree
this.menuPopup.add(this.itemAssignUser);
this.menuPopup.add(this.itemCreateGroup);
this.menuPopup.add(this.itemUnassignUser);
this.menuPopup.add(this.itemRemoveGroup);
}
/**
* This method returns a handle to the account panel group information.
*
* @return GroupAccount - the groups information instance.
*/
public GroupAccount getGroups() {
return this.groupAccount;
}
/**
* This method returns a handle to the group tree.
*
* @return JTree - the group tree instance.
*/
public JTree getTree() {
return this.groupTree;
}
/**
* This method returns a handle to the group panel tree model.
*
* @return GroupTableModel - handle to the group tree model.
*/
public GroupTreeModel getTreeModel() {
return this.groupTreeModel;
}
/**
* This method checks when a node has been selected and enables/disables
* the tree buttons accordingly.
*
* @param event - a tree selection event.
*/
public void valueChanged(TreeSelectionEvent event) {
//check if we have a valid node
Object selectedNode = this.groupTree.getLastSelectedPathComponent();
if (selectedNode != null) {
int nodeType = -99;
//retrieve the node type
if (selectedNode instanceof UserItem)
nodeType = Account.NODE_IS_USER;
else if (selectedNode instanceof GroupItem)
nodeType = Account.NODE_IS_GROUP;
else if (selectedNode instanceof AccountItem)
nodeType = Account.NODE_IS_ACCOUNT;
//set the menu switches (add, select, research)
switch (nodeType) {
case Account.NODE_IS_USER:
setOptions(false, false, true, false);
break;
case Account.NODE_IS_GROUP:
setOptions(true, false, false, true);
break;
case Account.NODE_IS_ACCOUNT:
setOptions(false, true, false, false);
break;
default:
setOptions(false, false, false, false);
break;
}
}
//not a valid node, turn off all menus
else
setOptions(false, false, false, false);
}
/**
* This method enables/disables the toolbar buttons and popup menus
* accordingly.
*
* @param add - enables/disables the add option.
* @param grp - enables/disables the new group option.
* @param rvu - enables/disables the remove account option.
* @param rvg - enables/disables the remove group option.
*/
public void setOptions(boolean add, boolean grp, boolean rvu, boolean rvg) {
//enable/disable popup menus
this.itemAssignUser.setVisible(add);
this.itemCreateGroup.setVisible(grp);
this.itemRemoveGroup.setVisible(rvg);
this.itemUnassignUser.setVisible(rvu);
}
/**
* This method selects a tree node by using the mouse right button.
*
* @param component - the component holding the tree.
* @param x - the horizontal coordinate into the tree
* @param y - the vertical coordinate into the tree
*/
private void rightSelectTreeNode(Component component, int x, int y) {
TreePath path = this.groupTree.getPathForLocation(x, y);
if (path != null) {
this.groupTree.setSelectionPath(path);
this.menuPopup.show(component, x, y);
}
}
/**
* This method refreshes the tree.
*/
public void refreshTree() {
//save the current tree path
/*TreePath selPath = null;
try {
selPath = this.groupTree.getSelectionPath();
}
catch(Exception e) {
selPath = null;
}*/
//notify the change
this.groupTreeModel.fireTreeStructureChanged();
//set the path again
/*if(selPath != null) {
this.groupTree.expandPath(selPath);
this.groupTree.setSelectionPath(selPath);
this.groupTree.scrollPathToVisible(selPath);
}*/
}
/**
* This internal method fetches all the account names from the user account.
*
* @return Vector - a list of account names.
*/
protected Vector getAllUsers() {
return UserAccount.allUsersNames();
}
} //--------------------------------- E O F -----------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -