📄 userpanel.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.userEditor;import vocal.comm.VPPNoSuchFileException;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.AncestorEvent;import javax.swing.event.AncestorListener;import java.util.Vector;import java.io.IOException;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.WindowListener;import java.awt.event.WindowEvent;import org.w3c.dom.Document;import org.xml.sax.SAXException;/** * Presents the screen which allows the account data (feature configuration, * user info, marshal info) for an end user to be edited. <p> * It lays out a series of panels which implement the DataEditor interface to * actually perform data input, display and validation.<p> * This class is used both from the StandAloneUserEditor when and end user edits * his own account and from the Editor when an administrator manages end user * accounts. When used from StandAloneUserEditor, setShowAdmin(false) is called * and all DataEditors are notified to remove fileds which should only be * accessible to an administrator, not the end user.<p> */public class UserPanel extends JPanel implements Buttons, EditorModes, WindowListener{ private int mode = ADD_NEW_MODE; private JPanel buttonPanel; private JButton ok; private JButton cancel; private JButton changePassword; private UserInfo userInfo; private int lastButtonClicked = BUTTON_NONE; private JDialog passwordScreen = null; private PasswordPanel passwordPanel; private String userName; private AliasPanel aliases; private IdentityInfo identity; private MarshalInfo marshal; private FeatureCallReturn callReturn; private boolean showAdmin = false; private boolean showUser = true; private UserPanel() { super(); setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setBorder(new EmptyBorder(5, 5, 5, 5)); } public UserPanel(final PServerInterface psInterface) throws IOException, VPPNoSuchFileException { this(); passwordPanel = new PasswordPanel(psInterface.getConnection()); userInfo = new UserInfo(); add(userInfo); aliases = new AliasPanel(psInterface); add(aliases); try { identity = new IdentityInfo(psInterface.getMarshalGroups()); add(identity); } catch (SAXException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Could not get list of marshal groups because:\n\n" + e); } try { marshal = new MarshalInfo(psInterface.getMarshalGroups()); add(marshal); } catch (SAXException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this.getTopLevelAncestor(), "Could not get list of marshal groups because:\n\n" + e); }/* try { String[] featureTypes = psInterface.getFeatureTypes(); for (int i = 0; i < featureTypes.length; i++) { if (featureTypes[i].equals("ForwardAllCalls") || featureTypes[i].equals("ForwardNoAnswerBusy") || featureTypes[i].equals("CallBlocking") || featureTypes[i].equals("CallScreening") || featureTypes[i].equals("CallReturn") || featureTypes[i].equals("CallerIdBlocking")) { String[] groupNames = psInterface.getFeatureGroups(featureTypes[i]); Feature panel = Feature.createFeature(featureTypes[i], groupNames, psInterface); if (FeatureCallReturn.class.isInstance(panel)) { callReturn = (FeatureCallReturn) panel; } else { add(panel); System.out.println(panel.getToolTipText()); } } } if (callReturn != null) { add(callReturn); } } catch (SAXException e) { throw new IOException("Could not load list of feature types because: \n" + e); }*/ ok = new JButton("Ok"); // tool tip gets hidden by frame, so leaving it out ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (isAllDataValid()) { lastButtonClicked = UserPanel.BUTTON_OK; // get the frame which contains this panel and hide it UserPanel.this.getTopLevelAncestor().setVisible(false); } } }); cancel = new JButton("Cancel"); // tool tip gets hidden by frame, so leaving it out cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { lastButtonClicked = UserPanel.BUTTON_CANCEL; // get the frame which contains this panel and hide it UserPanel.this.getTopLevelAncestor().setVisible(false); } });/* changePassword = new JButton("Change Password"); changePassword.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (passwordScreen == null) { Container parent = UserPanel.this.getTopLevelAncestor(); if (JDialog.class.isInstance(parent)) { passwordScreen = new JDialog((JDialog) parent, "Change password", false); } else if (JFrame.class.isInstance(parent)) { passwordScreen = new JDialog((JFrame) parent, "Change password", false); } passwordScreen.setContentPane(passwordPanel); passwordScreen.pack(); passwordScreen.setModal(true); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); passwordScreen.setLocation((d.width - passwordScreen.getSize().width) / 2, (d.height - passwordScreen.getSize().height) / 2); } passwordPanel.setUserId(userName); passwordScreen.setVisible(true); } });*/ buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.setBorder(new EmptyBorder(5, 0, 0, 0)); //buttonPanel.add(changePassword); buttonPanel.add(Box.createHorizontalGlue()); buttonPanel.add(ok); buttonPanel.add(Box.createHorizontalStrut(5)); buttonPanel.add(cancel); add(buttonPanel); setMode(this.EDIT_EXISTING_MODE); } private boolean isAllDataValid() { Component[] components = UserPanel.this.getComponents(); for (int i = 0; i < components.length; i++) { if (DataEditor.class.isInstance(components[i])) { if (!((DataEditor) components[i]).isDataValid()) { return false; } } } return true; } public void clear() { // clear all the data fields in all the dataEditors Component[] components = UserPanel.this.getComponents(); for (int i = 0; i < components.length; i++) { if (DataEditor.class.isInstance(components[i])) { ((DataEditor) components[i]).clear(); } } } public void setMode(int newMode) { Component[] components = UserPanel.this.getComponents(); for (int i = 0; i < components.length; i++) { if (DataEditor.class.isInstance(components[i])) { ((DataEditor) components[i]).setMode(newMode); } } if (newMode == ADD_NEW_MODE) { ok.setText("Add"); } else if (newMode == EDIT_EXISTING_MODE) { ok.setText("Ok"); } mode = newMode; } private void showSelectedComponents() { remove(userInfo); remove(marshal); if (callReturn != null) { remove(callReturn); } remove(aliases); Component[] components = getComponents(); if (showAdmin) { add(userInfo, 0); add(marshal, 1); if (callReturn != null) { add(callReturn); } components = getComponents(); if (callReturn != null) { add(callReturn, components.length - 2); } } if (showUser) { if (showAdmin) { add(aliases, 2); } else { add(aliases, 0); } } } public void removeAliasPanel() { remove(aliases); } public void setShowAdmin(boolean show) { Component[] components = getComponents(); showAdmin = show; showSelectedComponents(); components = getComponents(); for (int i = 0; i < components.length; i++) { if (Feature.class.isInstance(components[i])) { ((Feature) components[i]).setShowAdmin(show); } } } public void setShowUser(boolean show) { showUser = show; showSelectedComponents(); Component[] components = getComponents(); for (int i = 0; i < components.length; i++) { if (Feature.class.isInstance(components[i])) { ((Feature) components[i]).setShowUser(show); } } } public void setUserData(Vector user) { userName = (String) user.elementAt(UserTableModel.USER_NAME); // get all the components in this panel Component[] components = UserPanel.this.getComponents(); components = UserPanel.this.getComponents(); for (int id = 0; id <= UserTableModel.MAX_COLUMN_ID; id++) { for (int i = 0; i < components.length; i++) { // if this component was responsible for editing user data if (DataEditor.class.isInstance(components[i])) { DataEditor editor = (DataEditor) components[i]; String dataValue = (String) user.elementAt(id); if (editor.setData(id, dataValue)) { break; } } // end if this is a dataEditor } // end for each component } // end for each data id } public int getLastButtonClicked() { return lastButtonClicked; } public Vector getUserData() { System.out.println("get user data!"); Vector data = new Vector(); for (int i = 0; i <= UserTableModel.MAX_COLUMN_ID; i++) { data.addElement(null); } // get all the components in this panel Component[] components = getComponents(); // find out which components are the ones which can edit user data for (int i = 0; i < components.length; i++) { // if this component was responsible for editing user data if (DataEditor.class.isInstance(components[i])) { // ask it for a data value for each of the columns. If the component // is responsible for editing that data value (and it was changed) // it will return a string. Otherwise it will return null for (int id = 0; id <= UserTableModel.MAX_COLUMN_ID; id++) { String dataElement = null; if (id == UserTableModel.PASSWORD) { if (passwordPanel.getLastButtonClicked() == Buttons.BUTTON_OK) { dataElement = passwordPanel.getPassword(); } passwordPanel.clear(); } else { dataElement = ((DataEditor) components[i]).getData(id); } if (dataElement != null) { data.setElementAt(dataElement, id); } } ((DataEditor) components[i]).clear(); } } return data; } // -----------------implementation of the Windowlistener interface------------- public void windowOpened(WindowEvent e) { // nothing to do } public void windowIconified(WindowEvent e) { // nothing to do } public void windowDeiconified(WindowEvent e) { // nothing to do } public void windowDeactivated(WindowEvent e) { // nothing to do } /** * This should be called when the user presses the "close window" icon it the * title bar of the dialog displaing this panel. * That action should be equivalent to pressing cancel so we clear the editor * and let the last button pressed to cancel. */ public void windowClosing(WindowEvent e) { lastButtonClicked = UserPanel.BUTTON_CANCEL; clear(); } public void windowClosed(WindowEvent e) { // nothing to do } public void windowActivated(WindowEvent e) { // nothing to do }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -