⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settingpanel.java

📁 该系统是一个基于p2p的即时聊天系统
💻 JAVA
字号:
/* * @(#) SettingPanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.admin;//导入核心Java类库import java.awt.Insets;import java.awt.Component;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.util.List;import javax.swing.JTree;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JButton;import javax.swing.JSplitPane;import javax.swing.JScrollPane;import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeCellRenderer;import javax.swing.tree.DefaultTreeSelectionModel;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;//导入自定义Java类库import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.SBChanger;import hws.item.smart.panel.function.admin.misc.UserViewPanel;import hws.item.smart.action.admin.setting.StopSettingAction;import hws.item.smart.action.admin.setting.StartSettingAction;import hws.item.smart.action.admin.setting.RestartSettingAction;import hws.item.smart.utility.admin.SettingService;import hws.item.smart.utility.admin.XMLAccessor;import hws.item.smart.utility.share.ShareInfo;import hws.item.smart.utility.share.SharesInfo;//导入第三方Java类库import org.jdom.Element;/** * 设置服务面板 * * @version 0.1 2005-08-25 * @author Hwerz */public class SettingPanel extends JPanel {    /*------------------------------------------------------------------------*     *                                属性定义                                *     *------------------------------------------------------------------------*/    /**     * 文件树根节点     */    private static final DefaultMutableTreeNode FILE_ROOT =        new DefaultMutableTreeNode("上传文件");    /**     * 该类自身的一个静态引用     */    private static SettingPanel panel;    /**     * 用户视图面板     */    private UserViewPanel1 userViewPanel;    /**     * 文件视图面板     */    private FileViewPanel fileViewPanel;    /*------------------------------------------------------------------------*     *                                构造函数                                *     *------------------------------------------------------------------------*/    /**     * 构造函数为私有,这样在整个运行过程中该类就只能有一个实例     */    private SettingPanel() {        super(new GridBagLayout());        //启动设置服务        SettingService.getInstance().start();        //工具栏面板        GridBagConstraints constraints = new GridBagConstraints(            //gridx, gridy            0, 0,            //gridwidth, gridheight            1, 1,            //weightx, weighty            1.0, 0.0,            //anchor            GridBagConstraints.NORTH,            //fill            GridBagConstraints.HORIZONTAL,            //insets            new Insets(5, 0, 0, 0),            //ipadx, ipady            0, 0);        add(new Toolbar(), constraints);        //分割条面板        fileViewPanel = new FileViewPanel();        userViewPanel = new UserViewPanel1();        JSplitPane spliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false,            userViewPanel, fileViewPanel);        spliter.setOneTouchExpandable(true);        spliter.setDividerLocation(200);        constraints.gridy = 1;        constraints.weighty = 1.0;        constraints.fill = GridBagConstraints.BOTH;        constraints.insets = new Insets(5, 5, 5, 5);        add(spliter, constraints);    }    /*------------------------------------------------------------------------*     *                                公共方法                                *     *------------------------------------------------------------------------*/    /**     * 对该类提供的一个全局访问点,用来实例化该对象     *     * @return 该类唯一的一个实例     */    public static SettingPanel getInstance() {        if (panel == null) {            panel = new SettingPanel();        }        return panel;    }    /**     * 刷新     */    public void refresh() {        userViewPanel.reloadUsers();    }    /*------------------------------------------------------------------------*     *                                 内部类                                 *     *------------------------------------------------------------------------*/    /**     * 工具栏面板     */    class Toolbar extends JPanel {        /**         * Create a new instance of this class         */        public Toolbar() {            super(new FlowLayout(FlowLayout.CENTER, 5, 0));            //启动服务            JButton button = new JButton(StartSettingAction.getInstance());            button.setIcon(ImageShop.START_IMAGEICON);            button.addMouseListener(new SBChanger(                StartSettingAction.getInstance().getHintInfo(), false));            add(button);            //停止服务            button = new JButton(StopSettingAction.getInstance());            button.setIcon(ImageShop.STOP_IMAGEICON);            button.addMouseListener(new SBChanger(                StopSettingAction.getInstance().getHintInfo(), false));            add(button);            //重启服务            button = new JButton(RestartSettingAction.getInstance());            button.setIcon(ImageShop.RESTART_IMAGEICON);            button.addMouseListener(new SBChanger(                RestartSettingAction.getInstance().getHintInfo(), false));            add(button);        }    }    /**     * 用户视图面板     */    class UserViewPanel1 extends UserViewPanel        implements TreeSelectionListener {        /**         * Create a new instance of this class         */        public UserViewPanel1() {            super();            addTreeSelectionListener(this);        }        /**         * 实现TreeSelectionListener接口的方法         *         * @param event TreeSelectionEvent对象         */        public void valueChanged(TreeSelectionEvent event) {            DefaultMutableTreeNode node = (DefaultMutableTreeNode) event                .getPath().getLastPathComponent();            if (node == getRootNode()) {                fileViewPanel.reloadFiles(null);            } else {                fileViewPanel.reloadFiles(node.toString());            }        }    }    /**     * 文件视图面板     */    class FileViewPanel extends JPanel {        /**         * 文件树的视图         */        private JTree fileTree;        /**         * 文件树的模型         */        private DefaultTreeModel fileModel;        /**         * Create a new instance of this class         */        public FileViewPanel() {            super(new GridBagLayout());            //文件视图标签            GridBagConstraints constraints = new GridBagConstraints(                //gridx, gridy                0, 0,                //gridwidth, gridheight                1, 1,                //weightx, weighty                0.0, 0.0,                //anchor                GridBagConstraints.NORTHWEST,                //fill                GridBagConstraints.NONE,                //insets                new Insets(0, 5, 0, 0),                //ipadx, ipady                0, 0);            add(new JLabel("文件视图"), constraints);            //树型组件            fileModel = new DefaultTreeModel(FILE_ROOT);            fileTree = new JTree(fileModel);            setTreeCellRenderer();            setTree();            JScrollPane scroller = new JScrollPane(fileTree,                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);            constraints.gridy = 1;            constraints.weightx = 1.0;            constraints.weighty = 1.0;            constraints.fill = GridBagConstraints.BOTH;            constraints.insets = new Insets(0, 0, 0, 0);            add(scroller, constraints);        }        /**         * 重载文件         *         * @param id 指定的用户         */        public void reloadFiles(String id) {            unloadFiles();            if (id != null) {                loadFiles(id);            }        }        /**         * 装载文件         *         * @param id 指定的用户         */        private void loadFiles(String id) {            Element shares = XMLAccessor.getSharesElement(id);            List s = SharesInfo.sharesElement2SharesInfo(shares).getAllShares();            for (int i = 0; i < s.size(); i++) {                ShareInfo share = (ShareInfo) s.get(i);                DefaultMutableTreeNode file =                    new DefaultMutableTreeNode(share.getFile());                fileModel.insertNodeInto(file, FILE_ROOT, i);                List friends = share.getFriends().getAllFriends();                for (int j = 0; j < friends.size(); j++) {                    DefaultMutableTreeNode friend =                        new DefaultMutableTreeNode(friends.get(j));                    fileModel.insertNodeInto(friend, file, j);                }            }            fileTree.expandRow(0);            fileTree.setSelectionRow(0);        }        /**         * 卸载文件         */        private void unloadFiles() {            int count = FILE_ROOT.getChildCount();            for (int i = 0; i < count; i++) {                fileModel.removeNodeFromParent(                    (DefaultMutableTreeNode) FILE_ROOT.getChildAt(0));            }        }        /**         * 设置树型组件的附加选项         */        private void setTree() {            DefaultTreeSelectionModel model = new DefaultTreeSelectionModel();            model.setSelectionMode(model.SINGLE_TREE_SELECTION);            fileTree.setSelectionModel(model);            fileTree.setSelectionRow(0);            fileTree.expandRow(0);        }        /**         * 设置树型组件的单元格渲染器         */        private void setTreeCellRenderer() {            final DefaultTreeCellRenderer renderer1 =                (DefaultTreeCellRenderer) fileTree.getCellRenderer();            DefaultTreeCellRenderer renderer2 = new DefaultTreeCellRenderer() {                public Component getTreeCellRendererComponent(JTree tree,                    Object value, boolean selected, boolean expanded,                    boolean leaf, int row, boolean hasFocus) {                    Component c = renderer1.getTreeCellRendererComponent(tree,                        value, selected, expanded, leaf, row, hasFocus);                    JLabel label = (JLabel) c;                    if (value == FILE_ROOT) {                        label.setIcon(ImageShop.SHARE_ROOT_IMAGEICON);                    } else {                        if (leaf == false) {                            label.setIcon(ImageShop.SHARE_IMAGEICON);                        } else {                            label.setIcon(ImageShop.USER_IMAGEICON);                        }                    }                    return c;                }            };            fileTree.setCellRenderer(renderer2);        }    }}

⌨️ 快捷键说明

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