📄 gettingpanel.java
字号:
/* * @(#) GettingPanel.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.getting.StopGettingAction;import hws.item.smart.action.admin.getting.StartGettingAction;import hws.item.smart.action.admin.getting.RestartGettingAction;import hws.item.smart.utility.admin.GettingService;import hws.item.smart.utility.admin.XMLAccessor;//导入第三方Java类库import org.jdom.Element;/** * 文件服务面板 * * @version 0.1 2005-08-25 * @author Hwerz */public class GettingPanel extends JPanel { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 文件树根节点 */ private static final DefaultMutableTreeNode FILE_ROOT = new DefaultMutableTreeNode("下载文件"); /** * 该类自身的一个静态引用 */ private static GettingPanel panel; /** * 用户视图面板 */ private UserViewPanel1 userViewPanel; /** * 文件视图面板 */ private FileViewPanel fileViewPanel; /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * 构造函数为私有,这样在整个运行过程中该类就只能有一个实例 */ private GettingPanel() { super(new GridBagLayout()); //启动获取服务 GettingService.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 GettingPanel getInstance() { if (panel == null) { panel = new GettingPanel(); } 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(StartGettingAction.getInstance()); button.setIcon(ImageShop.START_IMAGEICON); button.addMouseListener(new SBChanger( StartGettingAction.getInstance().getHintInfo(), false)); add(button); //停止服务 button = new JButton(StopGettingAction.getInstance()); button.setIcon(ImageShop.STOP_IMAGEICON); button.addMouseListener(new SBChanger( StopGettingAction.getInstance().getHintInfo(), false)); add(button); //重启服务 button = new JButton(RestartGettingAction.getInstance()); button.setIcon(ImageShop.RESTART_IMAGEICON); button.addMouseListener(new SBChanger( RestartGettingAction.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) { List shares = XMLAccessor.getMyShares(id).getChildren(); for (int i = 0; i < shares.size(); i++) { Element share = (Element) shares.get(i); DefaultMutableTreeNode shareE = new DefaultMutableTreeNode( share.getAttributeValue("id")); fileModel.insertNodeInto(shareE, FILE_ROOT, i); List files = share.getChild("Files").getChildren(); for (int j = 0; j < files.size(); j++) { Element file = (Element) files.get(j); DefaultMutableTreeNode fileE = new DefaultMutableTreeNode( file.getAttributeValue("file")); fileModel.insertNodeInto(fileE, shareE, 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.USER_IMAGEICON); } else { label.setIcon(ImageShop.SHARE_IMAGEICON); } } return c; } }; fileTree.setCellRenderer(renderer2); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -