📄 adminacctmanagerpanel.java
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */package vocal.pw;import vocal.comm.VPPTransactionWrapper;import vocal.comm.VPPException;import vocal.comm.VPPNoSuchFileException;import vocal.userEditor.PServerInterface;import vocal.pw.CreateAdminAccountPanel;import vocal.userEditor.Buttons;import vocal.userEditor.EditorModes;import vocal.data.XMLUtils;import javax.swing.*;import javax.swing.table.TableColumn;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.InputEvent;import java.awt.event.MouseEvent;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Dimension;import java.awt.Toolkit;import java.util.Vector;import java.io.FileNotFoundException;import java.io.IOException;import org.w3c.dom.Document;import org.xml.sax.SAXException;/** * Displays a pannel which shows a table of all the administrative accounts * on the provisioning server and what type of access (administrator and/or * technician) each allows. <p> * Uses the CreateAdminAccountPanel to ceate or edit the accounts. */public class AdminAcctManagerPanel extends JPanel{ JTable table; AdminTableModel model; JScrollPane scroller; PServerInterface connection; private JDialog createAdminAcct = null; private CreateAdminAccountPanel createAdminAcctPanel = null; private JPopupMenu popupMenu; private JButton exit; public AdminAcctManagerPanel(boolean isStandalone) { model = new AdminTableModel(); table = new JTable(); table.setModel(model); scroller = new JScrollPane(table); setLayout(new BorderLayout()); add(scroller, BorderLayout.CENTER); exit = new JButton("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); JButton reload = new JButton("Reload all"); reload.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int rows = model.getRowCount(); for (int i = 0; i < rows; i++) { model.deleteAdminAt(0); } init(); } }); JPanel temp = new JPanel(); temp.setLayout(new BoxLayout(temp, BoxLayout.X_AXIS)); temp.add(reload); temp.add(Box.createHorizontalGlue()); if (isStandalone) { temp.add(exit); } add(temp, BorderLayout.SOUTH); } public AdminAcctManagerPanel(VPPTransactionWrapper con, boolean isStandAlone) { this(isStandAlone); setConnection(con); } public void setConnection(VPPTransactionWrapper con) { connection = new PServerInterface(con); } public void init() { try { // find out what are the admin accounts on the system String[] adminNames = null; try { adminNames = connection.getAllAdminNames(); } catch (VPPNoSuchFileException e1) { return; } for (int i = 0; i < adminNames.length; i++) { // for each name, get the account file Document dom = null; try { dom = connection.getAdminNamed(adminNames[i]); } catch (VPPException e2) { VPPTransactionWrapper.showVPPException(e2, "Failed to get Admin account " + adminNames[i]); return; } // change the dom to a vector Vector vector = model.convertAdminToVector(dom); // add the vector to the model model.addAdmin(vector); } // update the table model.fireTableDataChanged(); } catch (FileNotFoundException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Could not load list of administrators because: \n\n" + e); } catch (SAXException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Could not parse xml file because: \n\n" + e); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Could not parse xml file because: \n\n" + e); } createPopupMenu(); } private void createAdminAccountEdior() { Container parent = this.getTopLevelAncestor(); if (JDialog.class.isInstance(parent)) { createAdminAcct = new JDialog((JDialog) parent, "Create administrative account", false); } else if (JFrame.class.isInstance(parent)) { createAdminAcct = new JDialog((JFrame) parent, "Create administrative account", false); } else // if this is running in an applet, can't use it as parent of JDialog { createAdminAcct = new JDialog((JFrame) null, "Create administrative account", false); } createAdminAcctPanel = new CreateAdminAccountPanel(connection.getConnection()); createAdminAcct.setContentPane(createAdminAcctPanel); createAdminAcct.pack(); createAdminAcct.setModal(true); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); createAdminAcct.setLocation((d.width - createAdminAcct.getSize().width) / 2, (d.height - createAdminAcct.getSize().height) / 2); } private void createPopupMenu() { popupMenu = new JPopupMenu(); AbstractAction action = new AbstractAction("Edit") { public void actionPerformed(ActionEvent e) { if (createAdminAcct == null) { createAdminAccountEdior(); } // if no rows are selected, there's nothing to edit if (table.getSelectedRowCount() == 0) { return; } int row = table.getSelectedRow(); for (int i = 0; i < model.MAX_ID; i++) { String data = model.getValueAt(row, i).toString(); createAdminAcctPanel.setData(data, i); } createAdminAcctPanel.setMode(EditorModes.EDIT_EXISTING_MODE); createAdminAcct.setVisible(true); int lastButton = createAdminAcctPanel.getLastButtonClicked(); if (lastButton == Buttons.BUTTON_OK) { String xml = createAdminAcctPanel.getAdminXml(); try { Vector vector = model.convertAdminToVector(xml); for (int i = 0; i < vector.size(); i++) { model.setValueAt(vector.elementAt(i), row, i); } model.fireTableDataChanged(); } catch (Exception ex) { System.out.println("There was an error parsing the xml generated for the new administrator"); ex.printStackTrace(); JOptionPane.showMessageDialog(AdminAcctManagerPanel.this, "There was an error parsing the " + "xml for the generated administrator because: \n\n" + ex, "ERROR", JOptionPane.ERROR_MESSAGE); } } } }; popupMenu.add(action); action = new AbstractAction("Add") { public void actionPerformed(ActionEvent e) { if (createAdminAcct == null) { createAdminAccountEdior(); } createAdminAcctPanel.setMode(EditorModes.ADD_NEW_MODE); createAdminAcct.setVisible(true); int lastButton = createAdminAcctPanel.getLastButtonClicked(); if (lastButton == Buttons.BUTTON_OK) { String xml = createAdminAcctPanel.getAdminXml(); try { Vector vector = model.convertAdminToVector(xml); model.addAdmin(vector); model.fireTableDataChanged(); } catch (Exception ex) { System.out.println("There was an error parsing the xml generated for the new administrator"); ex.printStackTrace(); JOptionPane.showMessageDialog(AdminAcctManagerPanel.this, "There was an error parsing the " + "xml for the generated administrator because: \n\n" + ex, "ERROR", JOptionPane.ERROR_MESSAGE); } } } }; popupMenu.add(action); action = new AbstractAction("Delete") { public void actionPerformed(ActionEvent e) { while (table.getSelectedRowCount() > 0) { int row = table.getSelectedRow(); // WHY does this not update automatically?? table.getSelectionModel().removeIndexInterval(row, row); String name = model.deleteAdminAt(row); try { connection.deleteAdmin(name); } catch (VPPNoSuchFileException ex) { ex.printStackTrace(); VPPTransactionWrapper.showVPPException(ex, "Attempted to delete file that does not exist: " + name); } model.fireTableDataChanged(); } } }; popupMenu.add(action); RowSelectionListener listener = new RowSelectionListener(); table.addMouseListener(listener); scroller.addMouseListener(listener); } private class RowSelectionListener extends MouseAdapter { public void mouseReleased(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { // This is still not right. Needs more work. The problem is that the // e.getx method does not return the correct value once the table has // been scrolled. int width = AdminAcctManagerPanel.this.getWidth(); int modx = e.getX() % width; int height = AdminAcctManagerPanel.this.getHeight(); int mody = e.getY() % height; popupMenu.show(AdminAcctManagerPanel.this, modx, mody); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -