📄 usereditor.java
字号:
/*
Copyright 2005 Matthew J. Battey
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
This software implements a Java application to manage a SAFMQ server.
Created on May 26, 2005
*/
package com.safmq.manager;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import com.safmq.*;
/**
* @author matt
*/
public class UserEditor extends JComponent implements ChangeListener {
User user = null;
JCheckBox modUsers = new JCheckBox("Add/Modify Users");
JCheckBox modGroups = new JCheckBox("Add/Modify Groups");
JCheckBox modQueues = new JCheckBox("Add/Modify Queues");
JList groupsList = new JList(new DefaultListModel());
Action reloadAction = null;
Action applyAction = null;
Action setPasswordAction = null;
Action addGroupsAction = null;
Action deleteGroupAction = null;
static Icon icon = null;
static {
try {
icon = new ImageIcon(UserEditor.class.getResource("images/user.gif"));
} catch (Exception e) {
}
}
UserEditor(User user) {
JLabel l;
this.user = user;
loadData();
reloadAction = new SimpleAction("Reload",new Integer(KeyEvent.VK_R),false) {
public void actionPerformed(ActionEvent e) {
doReload();
}
};
applyAction = new SimpleAction("Apply",new Integer(KeyEvent.VK_A),false) {
public void actionPerformed(ActionEvent e) {
doApply();
}
};
setPasswordAction = new SimpleAction("Set Password",new Integer(KeyEvent.VK_S),true) {
public void actionPerformed(ActionEvent e) {
doSetPassword();
}
};
addGroupsAction = new SimpleAction("Groups",new Integer(KeyEvent.VK_G), true) {
public void actionPerformed(ActionEvent e) {
doAddGroups();
}
};
deleteGroupAction = new SimpleAction("Delete Group", (Integer)null, true) {
public void actionPerformed(ActionEvent e) {
doDeleteGroup();
}
};
JButton reload = new JButton(reloadAction);
JButton apply = new JButton(applyAction);
JButton setPassword = new JButton(setPasswordAction);
JButton addGroups = new JButton(addGroupsAction);
modUsers.addChangeListener(this);
modGroups.addChangeListener(this);
modQueues.addChangeListener(this);
GridBagLayout g = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
Insets origInsets = c.insets;
this.setLayout(g);
// Setup the panels ///////////////////////////////////////////////////
JPanel iconPanel = new JPanel();
c.insets = new Insets(5,5,5,5);
c.gridwidth = GridBagConstraints.RELATIVE;
c.fill = GridBagConstraints.NONE;
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.NORTHWEST;
this.add(iconPanel,c);
JPanel dataPanel = new JPanel();
c.insets = origInsets;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
JScrollPane jsp;
this.add(jsp = new JScrollPane(dataPanel),c);
jsp.setBorder(BorderFactory.createEmptyBorder());
JPanel buttonPanel = new JPanel();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
this.add(buttonPanel,c);
///////////////////////////////////////////////////////////////////////
// Setup te icon panel ////////////////////////////////////////////////
iconPanel.setLayout(g = new GridBagLayout());
if (icon == null)
icon = UIManager.getIcon("OptionPane.questionIcon");
l = new JLabel(icon);
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.NONE;
c.weightx=1;
c.weighty=1;
iconPanel.add(l,c);
///////////////////////////////////////////////////////////////////////
// Setup te button panel ////////////////////////////////////////////////
buttonPanel.setLayout(g = new GridBagLayout());
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(5,0,5,20);
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
c.weightx = 1;
c.weighty = 0;
buttonPanel.add(setPassword,c);
c.insets = new Insets(5,0,5,10);
c.weightx = 0;
c.gridwidth = 1;
buttonPanel.add(addGroups,c);
c.gridwidth = GridBagConstraints.RELATIVE;
buttonPanel.add(reload,c);
c.gridwidth = GridBagConstraints.REMAINDER;
buttonPanel.add(apply,c);
///////////////////////////////////////////////////////////////////////
// Setup the data panel ///////////////////////////////////////////////
dataPanel.setLayout(g = new GridBagLayout());
c.insets = origInsets;
c.anchor = GridBagConstraints.NORTHWEST;
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.weighty = 0;
dataPanel.add(new JLabel("<html><font size=5><b>Login: </b>"+user.getLogin()),c);
dataPanel.add(new JLabel("<html><font size=5><b>Description: </b>"+user.getTheDescription()),c);
dataPanel.add(modUsers,c);
dataPanel.add(modGroups,c);
dataPanel.add(modQueues,c);
c.insets = new Insets(5,0,0,0);
dataPanel.add(new JLabel("Groups:"),c);
c.insets = new Insets(0,0,0,10);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
groupsList.setVisibleRowCount(-1);
groupsList.setLayoutOrientation(JList.VERTICAL_WRAP);
groupsList.setCellRenderer(new IconListRenderer());
dataPanel.add(new JScrollPane(groupsList),c);
groupsList.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0),deleteGroupAction);
groupsList.getActionMap().put(deleteGroupAction,deleteGroupAction);
///////////////////////////////////////////////////////////////////////
}
void loadData() {
MQConnection con = user.getServer().getCon();
if (con != null) {
ActorPermissions perms = new ActorPermissions();
Vector groups = new Vector();
int error = con.UserGetPermissions(user.getLogin(),perms);
if (error == Safmq.EC_NOERROR || error == Safmq.EC_DOESNOTEXIST) {
error = con.UserGetGroups(user.getLogin(),groups);
if (error == Safmq.EC_NOERROR) {
modUsers.setSelected(perms.getModifyusers());
modGroups.setSelected(perms.getModifygroups());
modQueues.setSelected(perms.getModifyqueues());
DefaultListModel model = (DefaultListModel)groupsList.getModel();
model.removeAllElements();
for(int x=0;x<groups.size();x++)
model.addElement(new Group(groups.get(x).toString(),null));
} else if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to load group membership for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
}
} else if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to load permissions for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
}
}
}
void doReload() {
loadData();
reloadAction.setEnabled(false);
applyAction.setEnabled(false);
}
void doApply() {
int error;
MQConnection con = user.getServer().getCon();
if (con != null) {
error = con.UserSetPermissions(user.getLogin(),modQueues.isSelected(),modUsers.isSelected(),modGroups.isSelected());
if (error == Safmq.EC_NOERROR) {
Vector groups = new Vector();
error = con.UserGetGroups(user.getLogin(),groups);
if (error == Safmq.EC_NOERROR) {
int x;
DefaultListModel model = (DefaultListModel)groupsList.getModel();
for (x=0;x<groups.size();x++) {
error = con.GroupDeleteUser((String)groups.get(x),user.getLogin());
if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
return;
} else if (error != Safmq.EC_NOERROR) {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to store group membership for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
doReload();
return;
}
}
for (x=0;x<model.size();x++) {
error = con.GroupAddUser(((Group)model.get(x)).getName(),user.getLogin());
if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
return;
} else if (error != Safmq.EC_NOERROR) {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to store group membership for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
doReload();
return;
}
}
applyAction.setEnabled(false);
reloadAction.setEnabled(false);
} else if(error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to store permissions for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
}
} else if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to store permissions for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
doReload();
}
}
reloadAction.setEnabled(false);
applyAction.setEnabled(false);
}
void doSetPassword() {
PasswordChangeDialog dlg = new PasswordChangeDialog(user.getLogin());
dlg.show();
if (dlg.isGood()) {
MQConnection con = user.getServer().getCon();
if (con != null) {
int error = con.SetPassword(user.getLogin(),dlg.getPassword());
if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else if (error != Safmq.EC_NOERROR) {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to change the password for \""+user.getLogin()+"\".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
}
}
}
}
void doAddGroups() {
MQConnection con = user.getServer().getCon();
if (con != null) {
Vector groups = new Vector();
int error = con.EnumerateGroups(groups);
if (error == Safmq.EC_NOERROR) {
DefaultListModel model = (DefaultListModel)groupsList.getModel();
int x;
for(x=0; x<model.size(); x++)
groups.remove(((Group)model.get(x)).getName());
Vector g = new Vector();
for(x=0;x<groups.size();x++)
g.add(new Group((String)groups.get(x),null));
MultiChoiceDialog dlg = new MultiChoiceDialog("Please choose the groups to add", "Add Groups", g.toArray(), new IconListRenderer());
dlg.show();
if (dlg.isGood()) {
Object choices[] = dlg.getChoices();
for(x=0;x<choices.length; x++)
model.addElement(choices[x]);
reloadAction.setEnabled(true);
applyAction.setEnabled(true);
}
} else if (error == Safmq.EC_NETWORKERROR) {
Manager.getInstance().handleNetworkError(user.getServer());
} else {
JOptionPane.showMessageDialog(Manager.getInstance(),"An error occured attempting to list groups for sfamq://"+user.getServer().getName()+".\n\n"+
Safmq.errorDecode(error),"Edit Users", JOptionPane.ERROR_MESSAGE);
}
}
}
void doDeleteGroup() {
int sel = groupsList.getSelectedIndex();
if (sel >= 0) {
DefaultListModel model = (DefaultListModel)groupsList.getModel();
model.remove(sel);
reloadAction.setEnabled(true);
applyAction.setEnabled(true);
}
}
/**
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
public void stateChanged(ChangeEvent e) {
reloadAction.setEnabled(true);
applyAction.setEnabled(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -