📄 aliaspanel.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/>. * *//** * The component of the user interface which displays aliases. <p> * This contains a table with a single column of aliases and a popup menu and * assoicated listenera and acctions which may be used to add or remove items * in the table. */package vocal.userEditor;import vocal.comm.VPPNoSuchFileException;import javax.swing.*;import javax.swing.border.TitledBorder;import javax.swing.table.DefaultTableModel;import java.util.StringTokenizer;import java.util.Vector;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;public class AliasPanel extends JPanel implements DataEditor{ private JTable aliasList; private DefaultTableModel model; private JScrollPane scroller; private JPopupMenu menu; private PServerInterface psInterface; private Vector previousData = new Vector(); public AliasPanel(PServerInterface con) { psInterface = con; model = new DefaultTableModel(new String[] { "" }, 0); aliasList = new JTable(model); aliasList.setPreferredScrollableViewportSize(new Dimension(this.getWidth(), 50)); scroller = new JScrollPane(aliasList); scroller.setToolTipText("right-click to edit list of your secondary phone numbers"); setBorder(new TitledBorder("Aliases")); this.setLayout(new BorderLayout()); add(scroller, BorderLayout.CENTER); createPopupMenu(); } public void setMode(int newMode) { // nothing to do so far } public boolean setData(int id, String data) { switch (id) { case UserTableModel.ALIASES: { StringTokenizer tokenizer = new StringTokenizer(data, ";", false); String alias; while (tokenizer.hasMoreTokens()) { alias = tokenizer.nextToken(); model.addRow(new String[] { alias }); } previousData = new Vector(); for (int i = 0; i < model.getDataVector().size(); i++) { previousData.addElement(((Vector) model.getDataVector().elementAt(i)).elementAt(0)); } return true; } } return false; } public String getData(int id) { switch (id) { case UserTableModel.ALIASES: { // if the user did not hit enter in the cell after they typed the alias // in, whatever they type in will not be saved unless this method is // called. if (aliasList.isEditing()) { aliasList.getCellEditor().stopCellEditing(); } Vector newData = new Vector(); for (int i = 0; i < model.getRowCount(); i++) { Object newAlias = model.getValueAt(i, 0); // don't allow the same alias to be added twice // && // don't allow a blank line to be an alias if ((!newData.contains(newAlias)) && (!newAlias.equals(""))) { newData.add(model.getValueAt(i, 0)); } } // find aliases which have been removed (those that are in the old list // but not in the new list) for (int i = 0; i < previousData.size(); i++) { // the alias is on the old list but not the new one -> it has been // removed if (!newData.contains(previousData.elementAt(i))) { String alias = (String) previousData.elementAt(i); try { psInterface.deleteAliasFile(alias); } catch (VPPNoSuchFileException ex) { // if this, happens there is something pretty odd going on ex.printStackTrace(); JOptionPane.showMessageDialog(AliasPanel.this.getTopLevelAncestor(), "Could not remove the alias " + alias + " because it does not exist. \n\n Yeah, I know that's really odd."); } } } String aliases = ""; /** * for (int row=0; row<model.getRowCount(); row++) * { * //if the user put a blank line in the table, don't add this as an * //alias * if (!model.getValueAt(row,0).equals("")) * { * aliases += model.getValueAt(row,0) + ";"; * } * } */ for (int i = 0; i < newData.size(); i++) { aliases += newData.elementAt(i).toString() + ";"; } return aliases; } // end case ALIASES } // end switch return null; } private void createPopupMenu() { menu = new JPopupMenu(); AbstractAction action = new AbstractAction("Add") { public void actionPerformed(ActionEvent e) { model.insertRow(0, new String[] { "" }); aliasList.setEditingRow(0); model.fireTableDataChanged(); } }; menu.add(action); action = new AbstractAction("Remove") { public void actionPerformed(ActionEvent e) { while (aliasList.getSelectedRowCount() > 0) { int row = aliasList.getSelectedRow(); String alias = (String) model.getValueAt(row, 0); model.removeRow(row); aliasList.getSelectionModel().removeIndexInterval(row, row); } model.fireTableDataChanged(); } }; menu.add(action); class AliasTableListener extends MouseAdapter { public void mouseReleased(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) { AliasPanel.this.menu.show(AliasPanel.this, e.getX(), e.getY()); } } } AliasTableListener listener = new AliasTableListener(); aliasList.addMouseListener(listener); scroller.addMouseListener(listener); } public void clear() { if (aliasList.isEditing()) { aliasList.getCellEditor().cancelCellEditing(); } if (model.getRowCount() > 0) { int num = model.getRowCount(); for (int i = 0; i < num; i++) { model.removeRow(0); } } } public boolean isDataValid() { return true; // no validating fields avaliable for this component yet }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -