📄 addressbook.java
字号:
/** * JHylaFax - A java client for HylaFAX. * * Copyright (C) 2005 by Steffen Pingel <steffenp@gmx.de> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package net.sf.jhylafax.addressbook;import static net.sf.jhylafax.JHylaFAX.i18n;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Insets;import java.awt.datatransfer.Transferable;import java.awt.event.ActionEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import java.util.StringTokenizer;import javax.swing.Action;import javax.swing.Icon;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.JToolBar;import javax.swing.JTree;import javax.swing.ListSelectionModel;import javax.swing.WindowConstants;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.table.AbstractTableModel;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreePath;import net.sf.jhylafax.JHylaFAX;import net.sf.jhylafax.LocaleChangeListener;import net.sf.jhylafax.Settings;import net.wimpi.pim.Pim;import net.wimpi.pim.contact.db.ContactDatabase;import net.wimpi.pim.contact.db.ContactGroup;import net.wimpi.pim.contact.facades.SimpleContact;import net.wimpi.pim.contact.io.ContactMarshaller;import net.wimpi.pim.contact.io.ContactUnmarshaller;import net.wimpi.pim.contact.model.Contact;import net.wimpi.pim.factory.ContactIOFactory;import net.wimpi.pim.factory.ContactModelFactory;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.xnap.commons.gui.Builder;import org.xnap.commons.gui.ColoredTable;import org.xnap.commons.gui.Dialogs;import org.xnap.commons.gui.EraseTextFieldAction;import org.xnap.commons.gui.ErrorDialog;import org.xnap.commons.gui.ToolBarButton;import org.xnap.commons.gui.action.AbstractXNapAction;import org.xnap.commons.gui.table.StringCellRenderer;import org.xnap.commons.gui.table.TableLayoutManager;import org.xnap.commons.gui.table.TableSorter;import org.xnap.commons.gui.util.DoubleClickListener;import org.xnap.commons.gui.util.GUIHelper;import org.xnap.commons.gui.util.IconHelper;import org.xnap.commons.gui.util.PopupListener;import org.xnap.commons.io.FileExtensionFilter;import org.xnap.commons.settings.SettingStore;import com.jgoodies.forms.builder.DefaultFormBuilder;import com.jgoodies.forms.layout.FormLayout;public class AddressBook extends JFrame implements ListSelectionListener, LocaleChangeListener { private final static Log logger = LogFactory.getLog(AddressBook.class); private static final String[] DEFAULT_CONTACT_TABLE_COLUMNS = new String[] { "displayName", "company", "faxNumber" }; /** * @param args */ public static void main(String[] args) { final AddressBook app = new AddressBook(); app.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); app.restoreLayout(new SettingStore(Settings.backstore)); try { File file = JHylaFAX.getAddressBookFile(); if (file.exists()) { app.load(file); } } catch (Exception e) { ErrorDialog.showError(null, i18n.tr("Could not load address book"), i18n.tr("JHylaFAX Error"), e); } app.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { try { File file = JHylaFAX.getAddressBookFile(); app.store(file); } catch (Exception e) { ErrorDialog.showError(null, i18n.tr("Could not store address book"), i18n.tr("JHylaFAX Error"), e); } } }); app.setVisible(true); } private JMenu addressBookMenu; private JTree addressBookTree; private DefaultTreeModel addressBookTreeModel; private CloseAction closeAction; private JTable contactTable; private AddressTableModel contactTableModel; private DeleteAction deleteAction; private Action doubleClickAction; private EditAction editAction; private ExportAction exportAction; private FilterAction filterAction; private ImportAction importAction; private ContactCollection localAddressBook; private JToolBar mainToolBar; private NewAction newAction; private DefaultMutableTreeNode rootNode; private JTextField searchTextField; private JSplitPane horizontalSplitPane; private JLabel filterLabel; private TableLayoutManager contactTableLayoutManager; public AddressBook() { ContactModelFactory cmf = Pim.getContactModelFactory(); JHylaFAX.initializeToolkit(); initialize(); ContactDatabase contactDatabase = Pim.getContactDBFactory().createContactDatabase(); // initialize tree content DefaultMutableTreeNode localAddressBookTreeNode = new DefaultMutableTreeNode(); localAddressBook = new ContactCollection(contactDatabase, i18n.tr("Local Address Book")); localAddressBookTreeNode.setUserObject(localAddressBook); rootNode.add(localAddressBookTreeNode); addressBookTree.expandPath(new TreePath(rootNode)); addressBookTree.setSelectionRow(0); updateActions(); } public void exportContacts(File file, SimpleContact[] contacts) throws IOException { FileOutputStream out = new FileOutputStream(file); try { ContactIOFactory factory = Pim.getContactIOFactory(); ContactMarshaller marshaller = factory.createContactMarshaller(); marshaller.setEncoding("UTF-8"); for (SimpleContact contact : contacts) { marshaller.marshallContact(out, contact.getContact()); } } finally { out.close(); } } public SimpleContact[] getAlllContacts() { TableSorter sorter = (TableSorter)contactTable.getModel(); SimpleContact[] result = new SimpleContact[sorter.getRowCount()]; for (int i = 0; i < result.length; i++) { int row = sorter.mapToIndex(i); result[i] = contactTableModel.getContact(row); } return result; } public ContactCollection getSelectedContactCollection() { DefaultMutableTreeNode node = (DefaultMutableTreeNode)addressBookTree.getSelectionPath().getLastPathComponent(); return (ContactCollection)node.getUserObject(); } public SimpleContact[] getSelectedContacts() { int[] rows = contactTable.getSelectedRows(); if (rows.length == 0) { return new SimpleContact[0]; } SimpleContact[] result = new SimpleContact[rows.length]; for (int i = 0; i < rows.length; i++) { int row = ((TableSorter)contactTable.getModel()).mapToIndex(rows[i]); result[i] = contactTableModel.getContact(row); } return result; } public SimpleContact[] importVCardContacts(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { ContactIOFactory factory = Pim.getContactIOFactory(); ContactUnmarshaller unmarshaller = factory.createContactUnmarshaller(); unmarshaller.setEncoding("UTF-8"); Contact[] contacts = unmarshaller.unmarshallContacts(in); if (contacts != null) { SimpleContact[] result = new SimpleContact[contacts.length]; for (int i = 0; i < contacts.length; i++) { SimpleContact contact = new SimpleContact(contacts[i]); getSelectedContactCollection().add(contact); result[i] = contact; } return result; } return new SimpleContact[0]; } finally { in.close(); } } public SimpleContact[] importCSVContacts(File file, String separator) throws IOException { BufferedReader in = new BufferedReader(new FileReader(file)); try { List<SimpleContact> contacts = new ArrayList<SimpleContact>(); String line; while ((line = in.readLine()) != null) { SimpleContact contact = new SimpleContact(); StringTokenizer t = new StringTokenizer(line, separator); if (t.hasMoreTokens()) contact.setFirstname(t.nextToken()); if (t.hasMoreTokens()) contact.setLastname(t.nextToken()); if (t.hasMoreTokens()) contact.setFaxNumber(t.nextToken()); if (t.hasMoreTokens()) contact.setCompany(t.nextToken()); getSelectedContactCollection().add(contact); contacts.add(contact); } return contacts.toArray(new SimpleContact[0]); } finally { in.close(); } } private void initialize() { initializeActions(); initializeShortCuts(); initializeMenuBar(); initializeContent(); initializeToolBar(); updateLabels(); } private void initializeActions() { newAction = new NewAction(); editAction = new EditAction(); deleteAction = new DeleteAction(); importAction = new ImportAction(); exportAction = new ExportAction(); closeAction = new CloseAction(); filterAction = new FilterAction(); } private void initializeContent() { horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); horizontalSplitPane.setBorder(GUIHelper.createEmptyBorder(5)); rootNode = new DefaultMutableTreeNode(); addressBookTreeModel = new DefaultTreeModel(rootNode); addressBookTree = new JTree(addressBookTreeModel); addressBookTree.setRootVisible(false); addressBookTree.setCellRenderer(new ContactCollectionCellRenderer()); horizontalSplitPane.add(new JScrollPane(addressBookTree)); JPanel contactPanel = new JPanel(); contactPanel.setLayout(new BorderLayout(0, 10)); horizontalSplitPane.add(contactPanel); DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout("min, 3dlu, min, 3dlu, pref:grow, 3dlu, min", "")); contactPanel.add(builder.getPanel(), BorderLayout.NORTH); searchTextField = new JTextField(10); EraseTextFieldAction eraseAction = new EraseTextFieldAction(searchTextField) { public void actionPerformed(ActionEvent event) { super.actionPerformed(event); filterAction.actionPerformed(event); }; }; builder.append(new TabTitleButton(eraseAction)); filterLabel = new JLabel(); builder.append(filterLabel); builder.append(searchTextField); GUIHelper.bindEnterKey(searchTextField, filterAction); builder.append(Builder.createButton(filterAction)); JPopupMenu tablePopupMenu = new JPopupMenu(); tablePopupMenu.add(Builder.createMenuItem(newAction)); tablePopupMenu.addSeparator(); tablePopupMenu.add(Builder.createMenuItem(editAction)); tablePopupMenu.addSeparator(); tablePopupMenu.add(Builder.createMenuItem(deleteAction)); contactTableModel = new AddressTableModel(); TableSorter sorter = new TableSorter(contactTableModel); contactTable = new ColoredTable(sorter); contactTableLayoutManager = new TableLayoutManager(contactTable); contactTableLayoutManager.addColumnProperties("displayName", "", 180, true); contactTableLayoutManager.addColumnProperties("company", "", 80, true); contactTableLayoutManager.addColumnProperties("faxNumber", "", 60, true); contactTableLayoutManager.initializeTableLayout(); contactPanel.add(new JScrollPane(contactTable), BorderLayout.CENTER); contactTable.setShowVerticalLines(true); contactTable.setShowHorizontalLines(false); contactTable.setAutoCreateColumnsFromModel(true); contactTable.setIntercellSpacing(new java.awt.Dimension(2, 1)); contactTable.setBounds(0, 0, 50, 50); contactTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); contactTable.getSelectionModel().addListSelectionListener(this); contactTable.addMouseListener(new PopupListener(tablePopupMenu)); contactTable.addMouseListener(new DoubleClickListener(new TableDoubleClickAction())); contactTable.setTransferHandler(new ContactTransferHandler()); contactTable.setDragEnabled(true); contactTable.setDefaultRenderer(String.class, new StringCellRenderer()); getContentPane().setLayout(new BorderLayout()); getContentPane().add(horizontalSplitPane, BorderLayout.CENTER); } private void initializeMenuBar() { JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); addressBookMenu = new JMenu(); menuBar.add(addressBookMenu); addressBookMenu.add(Builder.createMenuItem(newAction)); addressBookMenu.addSeparator(); addressBookMenu.add(Builder.createMenuItem(editAction)); addressBookMenu.addSeparator(); addressBookMenu.add(Builder.createMenuItem(deleteAction)); addressBookMenu.addSeparator(); addressBookMenu.add(Builder.createMenuItem(importAction)); addressBookMenu.add(Builder.createMenuItem(exportAction)); addressBookMenu.addSeparator(); addressBookMenu.add(Builder.createMenuItem(closeAction)); } private void initializeShortCuts() { } private void initializeToolBar() { mainToolBar = new JToolBar(); //mainToolBar.setBorderPainted(false); //mainToolBar.setRollover(true); getContentPane().add(mainToolBar, BorderLayout.NORTH); mainToolBar.add(Builder.createToolBarButton(newAction)); mainToolBar.addSeparator(); mainToolBar.add(Builder.createToolBarButton(editAction)); mainToolBar.add(Builder.createToolBarButton(deleteAction)); } public void load(File file) throws IOException, ClassNotFoundException { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); try { ContactDatabase contactDatabase = (ContactDatabase)in.readObject(); localAddressBook.setDatabase(contactDatabase); } finally { in.close(); } } public void saveLayout(SettingStore store) { contactTableLayoutManager.saveLayout(store, "addressbook.contact"); store.saveWindow("addressbook.window", this); store.saveSplitPane("addressbook.horizontalSplit", horizontalSplitPane); } public void restoreLayout(SettingStore store) { contactTableLayoutManager.restoreLayout(store, "addressbook.contact"); store.restoreWindow("addressbook.window", 40, 40, 550, 300, this); store.restoreSplitPane("addressbook.horizontalSplit", 150, horizontalSplitPane); } public void setDoubleClickAction(Action doubleClickAction) { this.doubleClickAction = doubleClickAction; } public void store(File file) throws IOException { if (!localAddressBook.isChanged()) { logger.info("Address book unchanged"); return; } ObjectOutputStream in = new ObjectOutputStream(new FileOutputStream(file)); try { in.writeObject(localAddressBook.getDatabase()); logger.info("Stored address book in " + file.getAbsolutePath()); } finally { in.close(); } } public void updateLabels() { setTitle(i18n.tr("JHylaFAX Address Book")); addressBookMenu.setText(i18n.tr("Address Book")); newAction.putValue(Action.NAME, i18n.tr("New Contact...")); editAction.putValue(Action.NAME, i18n.tr("Edit Contact...")); deleteAction.putValue(Action.NAME, i18n.tr("Delete Contact")); importAction.putValue(Action.NAME, i18n.tr("Import...")); exportAction.putValue(Action.NAME, i18n.tr("Export...")); closeAction.putValue(Action.NAME, i18n.tr("Close")); filterLabel.setText(i18n.tr("Search for")); filterAction.putValue(Action.NAME, i18n.tr("Go"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -