📄 dialplantablemodel.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.ui;import javax.swing.table.AbstractTableModel;import org.w3c.dom.Node;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.w3c.dom.Document;import java.util.Vector;/** * Used to drive the data of the Dial Plan table. A Dial Plan table is used * in DialPlanScreen. */public class DialPlanTableModel extends AbstractTableModel{ /** * Used to create new Element objects */ private Document document; /** * The root of the dial plan DOM Tree */ private Element rootElement; /** * Internal storage of data */ private Vector data = new Vector(); /** * Internal backup of data (used when 'cancel' clicked) */ private Vector dataBackup; /** * an optional list of names for each column in the table */ private String columnNames[] = null; /** * An internal class to represent one row of data */ private class KeyMultiValue { String key; Vector values = new Vector(); } /** * * @return the table data as a DOM. Return is the root element */ public Element getData() { Element keyElement; Element contactElement; KeyMultiValue kmv; Vector contacts; deleteAllChildNodes(rootElement); for (int i = 0; i < data.size(); i++) { kmv = (KeyMultiValue) data.get(i); keyElement = document.createElement("key"); keyElement.setAttribute("value", kmv.key); keyElement.setAttribute("index", String.valueOf(i)); rootElement.appendChild(keyElement); for (int j = 0; j < kmv.values.size(); j++) { contactElement = document.createElement("contact"); contactElement.setAttribute("value", (String) kmv.values.get(j)); keyElement.appendChild(contactElement); } } return rootElement; } /** * Sets the data of the table * @param newNode the root of the DOM tree for dial plan data */ public void setData(Element newNode) { data.clear(); // populate internal data storage if necessary if (data.size() == 0) { NodeList parentNodes = newNode.getElementsByTagName("key"); Element parentNode; NodeList childNodes; KeyMultiValue kmv; data.clear(); for (int i = 0; i < parentNodes.getLength(); i++) { kmv = new KeyMultiValue(); data.add(kmv); parentNode = (Element) parentNodes.item(i); kmv.key = parentNode.getAttribute("value"); childNodes = parentNode.getElementsByTagName("contact"); for (int j = 0; j < childNodes.getLength(); j++) { kmv.values.add(((Element) childNodes.item(j)).getAttribute("value")); } } dataBackup = (Vector) data.clone(); document = newNode.getOwnerDocument(); rootElement = newNode; // We have already copied all the data into a vector, // now delete the DOM structure (free memory!) //deleteAllChildNodes(rootElement); fireTableDataChanged(); } } /** * Called when the the user clicks 'Ok' */ public void dataSaved() { dataBackup = (Vector) data.clone(); // DOM structure saved. Now delete to save memory. //deleteAllChildNodes(rootElement); } /** * Delete all the child nodes of the supplied element */ private void deleteAllChildNodes(Element element) { NodeList nodesToRemove = element.getChildNodes(); // Must copy nodes into a Vector first because if you cannot delete nodes // all in one go. If you do, the tree is changing as you delete nodes, // and therefore, for some reason, you miss some nodes. Vector nodesToRemoveVec = new Vector(); for (int i = 0; i < nodesToRemove.getLength(); i++) { nodesToRemoveVec.add(nodesToRemove.item(i)); } for (int i = 0; i < nodesToRemoveVec.size(); i++) { element.removeChild((Node) nodesToRemoveVec.get(i)); } } /** * Revert to the backup data. Called when the user clicks 'Cancel' */ public void revertData() { data = (Vector) dataBackup.clone(); fireTableDataChanged(); } /** * Delete a dial plan entry, effectively a row of data */ public void deleteEntry(int row) { data.remove(row); fireTableRowsDeleted(row, row); } /** * Add a new dial plan entry, effectively one new row of data * @param newKey string key * @param newContacts a Vector of string contacts * @param index the index of where to insert data */ public void addNewEntry(String newKey, Vector newContacts, int index) { KeyMultiValue kmv = new KeyMultiValue(); kmv.key = newKey; kmv.values = newContacts; data.add(index, kmv); fireTableRowsInserted(index, index); } /** * Column 0 gets the index * Column 1 gets the key value * Column 2 gets the contact values. A Vector. * @param row * @param column * * @return the object at the position of row and column. */ public Object getValueAt(int row, int column) { switch (column) { case 0: return String.valueOf(row); case 1: return ((KeyMultiValue) data.elementAt(row)).key; case 2: return ((KeyMultiValue) data.elementAt(row)).values; default: throw new ArrayIndexOutOfBoundsException("Column argument " + column + " out of bounds"); } } /** * */ public Class getColumnClass(int column) { // return String.class; switch (column) { case 0: case 1: return String.class; case 2: return Vector.class; default: throw new ArrayIndexOutOfBoundsException("Column argument " + column + " out of bounds"); } } /** * */ public int getColumnCount() { if (columnNames == null) { return 0; } return columnNames.length; } /** * */ public int getRowCount() { return data.size(); } /** * */ public void setColumnNames(String names[]) { columnNames = names; } /** * */ public String getColumnName(int column) { if ((columnNames == null) || (column > columnNames.length)) { return ""; } return columnNames[column]; } /** * Always returns false. */ public boolean isCellEditable(int row, int column) { return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -