custommessages.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 582 行 · 第 1/2 页

JAVA
582
字号
/** * $Revision: $ * $Date: $ * * Copyright (C) 2006 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Lesser Public License (LGPL), * a copy of which is included in this distribution. */package org.jivesoftware.spark.ui.status;import com.thoughtworks.xstream.XStream;import org.jivesoftware.resource.Res;import org.jivesoftware.smack.packet.Presence;import org.jivesoftware.spark.SparkManager;import org.jivesoftware.spark.component.JiveTreeCellRenderer;import org.jivesoftware.spark.component.JiveTreeNode;import org.jivesoftware.spark.component.Tree;import org.jivesoftware.spark.component.renderer.ListIconRenderer;import org.jivesoftware.spark.util.ModelUtil;import org.jivesoftware.spark.util.ResourceUtils;import org.jivesoftware.spark.util.log.Log;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ImageIcon;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.TreePath;import java.awt.BorderLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class CustomMessages {    private static File customMessages = new File(SparkManager.getUserDirectory(), "custom_messages.xml");    private static XStream xstream = new XStream();    private CustomMessages() {    }    static {        xstream.alias("custom-items", List.class);        xstream.alias("custom-status", CustomStatusItem.class);    }    // Handle Custom Messages    public static List load() {        List list = null;        if (customMessages.exists()) {            try {                list = (List)xstream.fromXML(new FileReader(customMessages));            }            catch (Exception e) {                xstream.alias("list", List.class);                xstream.alias("com.jivesoftware.workspaces.CustomStatusItem", CustomStatusItem.class);                try {                    list = (List)xstream.fromXML(new FileReader(customMessages));                }                catch (Exception e1) {                    Log.error(e1);                }            }        }        if (list == null) {            list = new ArrayList();        }        return list;    }    public static void save(List list) {        xstream.alias("custom-items", List.class);        xstream.alias("custom-status", CustomStatusItem.class);        try {            xstream.toXML(list, new FileWriter(customMessages));        }        catch (IOException e) {            Log.error("Could not save custom messages.", e);        }    }    public static void addCustomMessage() {        CustomStatus status = new CustomStatus();        status.invoke(null);    }    public static void editCustomMessages() {        final JiveTreeNode rootNode = new JiveTreeNode("Custom Messages");        final Tree tree = new Tree(rootNode);        tree.setCellRenderer(new JiveTreeCellRenderer());        final List customItems = load();        final StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();        Iterator statusItems = statusBar.getStatusList().iterator();        while (statusItems.hasNext()) {            StatusItem item = (StatusItem)statusItems.next();            JiveTreeNode node = new JiveTreeNode(item.getText(), false, item.getIcon());            Iterator cMessages = customItems.iterator();            node.setAllowsChildren(true);            while (cMessages.hasNext()) {                CustomStatusItem csi = (CustomStatusItem)cMessages.next();                if (csi.getType().equals(item.getText())) {                    JiveTreeNode subNode = new JiveTreeNode(csi.getStatus(), false);                    node.add(subNode);                }            }            rootNode.add(node);        }        final JScrollPane pane = new JScrollPane(tree);        // The user should only be able to close this dialog.        Object[] options = {Res.getString("close")};        final JOptionPane optionPane = new JOptionPane(pane, JOptionPane.PLAIN_MESSAGE,                JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);        JPanel mainPanel = new JPanel();        mainPanel.setLayout(new BorderLayout());        mainPanel.add(optionPane, BorderLayout.CENTER);        final JDialog optionsDialog = new JDialog(SparkManager.getMainWindow(), Res.getString("title.edit.custom.message"), true);        optionsDialog.setContentPane(mainPanel);        optionsDialog.pack();        optionsDialog.setLocationRelativeTo(SparkManager.getMainWindow());        optionPane.addPropertyChangeListener(new PropertyChangeListener() {            public void propertyChange(PropertyChangeEvent propertyChangeEvent) {                String value = (String)optionPane.getValue();                if (Res.getString("close").equals(value)) {                    optionsDialog.setVisible(false);                    return;                }            }        });        for (int i = 0; i <= tree.getRowCount(); i++) {            tree.expandPath(tree.getPathForRow(i));        }        tree.addMouseListener(new MouseAdapter() {            public void mousePressed(MouseEvent mouseEvent) {                checkPopup(mouseEvent);            }            public void mouseReleased(MouseEvent mouseEvent) {                checkPopup(mouseEvent);            }            public void checkPopup(MouseEvent event) {                if (!event.isPopupTrigger()) {                    return;                }                TreePath path = tree.getPathForLocation(event.getX(), event.getY());                if (path != null) {                    tree.setSelectionPath(path);                }                final JiveTreeNode selectedNode = (JiveTreeNode)tree.getLastSelectedPathComponent();                if (selectedNode == null || selectedNode.getParent() == null) {                    return;                }                else if (selectedNode.getParent() == rootNode) {                    JPopupMenu popup = new JPopupMenu();                    Action addAction = new AbstractAction() {                        public void actionPerformed(ActionEvent actionEvent) {                            CustomStatus status = new CustomStatus();                            String type = (String)selectedNode.getUserObject();                            status.invoke(type);                            reloadTree(rootNode, tree);                        }                    };                    addAction.putValue(Action.NAME, Res.getString("menuitem.add"));                    popup.add(addAction);                    popup.show(tree, event.getX(), event.getY());                    return;                }                final JiveTreeNode parentNode = (JiveTreeNode)selectedNode.getParent();                final String messageStatus = (String)selectedNode.getUserObject();                final String messageType = (String)parentNode.getUserObject();                if (event.isPopupTrigger()) {                    JPopupMenu popup = new JPopupMenu();                    Action deleteAction = new AbstractAction() {                        public void actionPerformed(ActionEvent actionEvent) {                            List list = new ArrayList();                            Iterator iter = customItems.iterator();                            while (iter.hasNext()) {                                CustomStatusItem item = (CustomStatusItem)iter.next();                                if (item.getType().equals(messageType) && item.getStatus().equals(messageStatus)) {                                }                                else {                                    list.add(item);                                }                            }                            parentNode.remove(selectedNode);                            DefaultTreeModel model = (DefaultTreeModel)tree.getModel();                            model.nodeStructureChanged(parentNode);                            save(list);                        }                    };                    deleteAction.putValue(Action.NAME, Res.getString("menuitem.delete"));                    popup.add(deleteAction);                    Action editAction = new AbstractAction() {                        public void actionPerformed(ActionEvent actionEvent) {                            List newItems = load();                            Iterator iter = newItems.iterator();                            while (iter.hasNext()) {                                CustomStatusItem item = (CustomStatusItem)iter.next();                                if (item.getType().equals(messageType) && item.getStatus().equals(messageStatus)) {                                    CustomStatus customStatus = new CustomStatus();                                    customStatus.showEditDialog(item);                                    // Reload tree.                                    reloadTree(rootNode, tree);                                    break;                                }                            }                        }                    };                    editAction.putValue(Action.NAME, Res.getString("menuitem.edit"));                    popup.add(editAction);                    popup.show(tree, event.getX(), event.getY());                }            }        });        optionsDialog.setVisible(true);        optionsDialog.toFront();        optionsDialog.requestFocus();    }    private static void reloadTree(JiveTreeNode rootNode, Tree tree) {        StatusBar statusBar = SparkManager.getWorkspace().getStatusBar();        rootNode.removeAllChildren();        Iterator statusItems = statusBar.getStatusList().iterator();        while (statusItems.hasNext()) {            StatusItem statusItem = (StatusItem)statusItems.next();            JiveTreeNode node = new JiveTreeNode(statusItem.getText(), false, statusItem.getIcon());            List newItems = load();            Iterator cMessages = newItems.iterator();            node.setAllowsChildren(true);            while (cMessages.hasNext()) {                CustomStatusItem csi = (CustomStatusItem)cMessages.next();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?