📄 admintablemodel.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 javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.swing.table.AbstractTableModel;import java.util.Vector;import org.w3c.dom.Document;import org.xml.sax.InputSource;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import java.io.IOException;import java.io.StringReader;import org.xml.sax.SAXException;public class AdminTableModel extends AbstractTableModel{ private Vector data; public static final int LOGIN_ID = 0; public static final int ADMIN_ACCESS = 1; public static final int TECH_ACCESS = 2; public static final int PASSWORD = 3; public static final int MAX_ID = PASSWORD; private DocumentBuilder parser; private static final String[] columnNames = new String[] { "Login id", "Admin access", "Tech access" }; public AdminTableModel() { data = new Vector(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { parser = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } } /** * Takes a string xml representation of an administrator account and coverts * to a vector. Calls the <code>convertAdminToVector (Document)</code> method * to perform the conversion.<p> * @see Vector convertAdminToVector (Document) convertAdminToVector */ public Vector convertAdminToVector(String xml) throws IOException, SAXException { Document dom = parser.parse(new InputSource(new StringReader(xml))); return convertAdminToVector(dom); } /** * Takes a dom representation of an administrator account xml file and * converts the contents into a vector. <p> * The elements in the vector are LOGIN_ID, ADMIN_ACCESS, TECH_ACCESS, and * PASSWORD as defined by the identifiers in this file. The values are gotten * by searching the dom. * @param the account dom * @return the account vector */ public Vector convertAdminToVector(Document admin) { // Create a blank vector with enought elements in it to hold all the data Vector adminVector = new Vector(MAX_ID); for (int i = 0; i <= MAX_ID; i++) { adminVector.addElement("Not loaded from xml"); } // copy the data from the dom to the vector // get the administrator's name/login id NodeList ids = admin.getElementsByTagName("name"); if (ids != null) { if (ids.getLength() > 0) { String id = ids.item(0).getFirstChild().getNodeValue(); adminVector.setElementAt(id, this.LOGIN_ID); } else { System.out.println("---ERROR--- parsing admin xml file: no name element"); } } else { System.out.println("---ERROR--- parsing admin xml file: name element is null"); } // get the password NodeList pws = admin.getElementsByTagName("password"); if (pws != null) { if (pws.getLength() > 0) { String pw = pws.item(0).getFirstChild().getNodeValue(); adminVector.setElementAt(pw, this.PASSWORD); } else { System.out.println("---ERROR--- parsing admin xml file: no password element"); } } else { System.out.println("---ERROR--- parsing admin xml file: password element is null"); } // get the access levels NodeList accessLevels = admin.getElementsByTagName("accessLevel"); if (accessLevels != null) { if (accessLevels.getLength() > 0) { for (int i = 0; i < accessLevels.getLength(); i++) { String levelName = ((Element) accessLevels.item(i)).getAttribute("type"); String enabled = ((Element) accessLevels.item(i)).getAttribute("enabled"); if (levelName.equals("administrator")) { adminVector.setElementAt(enabled, this.ADMIN_ACCESS); } else if (levelName.equals("technician")) { adminVector.setElementAt(enabled, this.TECH_ACCESS); } } } else { System.out.println("---ERROR--- parsing admin xml file: no accessLevel element"); } } else { System.out.println("---ERROR--- parsing admin xml file: accessLevel element is null"); } return adminVector; } public void addAdmin(Vector admin) { data.addElement(admin); } public Object getValueAt(int row, int column) { Vector admin = (Vector) data.elementAt(row); String value = (String) admin.elementAt(column); if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) { Boolean boolValue = Boolean.valueOf(value); return boolValue; } return admin.elementAt(column); } public Class getColumnClass(int columnIndex) { if (data.size() == 0) { return Object.class; } Vector admin = (Vector) data.elementAt(0); Object dataElement = admin.elementAt(columnIndex); if (dataElement.equals("true") || dataElement.equals("false")) { return Boolean.class; } else { return dataElement.getClass(); } } public int getRowCount() { return data.size(); } public int getColumnCount() { return columnNames.length; } public String getColumnName(int index) { return columnNames[index]; } public String deleteAdminAt(int row) { Vector admin = (Vector) data.elementAt(row); String name = (String) admin.elementAt(LOGIN_ID); data.remove(row); return name; } public void setValueAt(Object aValue, int rowIndex, int columnIndex) { Vector admin = (Vector) data.elementAt(rowIndex); admin.setElementAt(aValue, columnIndex); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -