- MessageTreePlugin.java
- final MessageTreeTab messageTreeTab = new MessageTreeTab();
- /**
- * Adds a tab to Spark
- */
- private void addTabToSpark(){
- // Get Workspace UI from SparkManager
- Workspace workspace = SparkManager.getWorkspace();
- // Retrieve the Tabbed Pane from the WorkspaceUI.
- SparkTabbedPane tabbedPane = workspace.getWorkspacePane();
- // Add own Tab.
- tabbedPane.addTab("消息", null,<SPAN style="COLOR: #ff0000">messageTreeTab</SPAN>);
- //messageTreeTab.invokeDialog();
- }
- MessageTreeTab.java
- public class MessageTreeTab extends JPanel {
- private static final long serialVersionUID = -381458717030745122L;
- private JiveTreeNode msgType;
- private List<JiveTreeNode> groupNodes = new ArrayList<JiveTreeNode>();
- private MessageTree msgTree;
- private JiveTable messageType;
- public MessageTreeTab(){
- setLayout(new GridBagLayout());
- msgType = new JiveTreeNode("分类");
- msgTree = <SPAN style="COLOR: #ff0000">new MessageTree</SPAN>(msgType);
- msgTree.setPreferredSize(new Dimension(150, 400));
- String groupName = Res.getString("status.online");
- String[] headers = new String[]{ "类型","时间","内容"};
- messageType = new JiveTable(headers, null);
- messageType.setPreferredSize(new Dimension(350, 400));
- final JScrollPane pane = new JScrollPane(messageType);
- pane.setBorder(BorderFactory.createTitledBorder(Res.getString("label.message").replace("&", "")));
- final JScrollPane treePane = new JScrollPane(msgTree);
- treePane.setBorder(BorderFactory.createTitledBorder(Res.getString("message.send.to.these.people")));
- // treePane.getVerticalScrollBar().setBlockIncrement(200);
- // treePane.getVerticalScrollBar().setUnitIncrement(20);
- // Add to UI
- add(treePane, new GridBagConstraints(0, 0, 1, 1, 0.5, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 0), 0, 0));
- add(pane, new GridBagConstraints(1, 0, 1, 3, 0.5, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 0, 2, 5), 0, 0));
- msgTree.expandTree();
- }
- /**
- * Displays the message tree dialog.
- */
- public void invokeDialog() {
- JiveTreeNode groupNode = new JiveTreeNode("通知类型1");
- groupNode.add( new JiveTreeNode("通知类型11"));
- groupNode.add(new JiveTreeNode("通知类型12"));
- groupNode.add(new JiveTreeNode("通知类型13"));
- groupNode.add(new JiveTreeNode("通知类型14"));
- groupNode.add(new JiveTreeNode("通知类型15"));
- JiveTreeNode groupNode2 = new JiveTreeNode("通知类型2");
- groupNode2.add( new JiveTreeNode("通知类型21"));
- groupNode2.add(new JiveTreeNode("通知类型22"));
- groupNode2.add(new JiveTreeNode("通知类型23"));
- groupNode2.add(new JiveTreeNode("通知类型24"));
- groupNode2.add(new JiveTreeNode("通知类型25"));
- msgType.add(groupNode);
- msgType.add(groupNode2);
- }
- }
- MessageTree.java
- public class MessageTree extends JPanel {
-
- private static final long serialVersionUID = 5992552368286729600L;
-
- private Tree tree;
-
- public MessageTree(JiveTreeNode rootNode) {
- tree = new Tree(rootNode);
- tree.setCellRenderer(new DefaultTreeCellRenderer());
- tree.setRowHeight(18);
- tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
- tree.setToggleClickCount(1000);
- tree.putClientProperty("JTree.lineStyle", "Angled");
- tree.addMouseListener(new NodeSelectionListener(tree));
-
- setLayout(new BorderLayout());
- add(tree);
- }
-
- class NodeSelectionListener extends MouseAdapter {
- JTree tree;
-
- NodeSelectionListener(JTree tree) {
- this.tree = tree;
- }
-
- public void mouseClicked(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
- int row = tree.getRowForLocation(x, y);
- TreePath path = tree.getPathForRow(row);
- if (path != null) {
- JiveTreeNode node = (JiveTreeNode)path.getLastPathComponent();
- if (!node.isLeaf()) {
- tree.expandPath(path);
- }
- else {
- tree.collapsePath(path);
- }
- ((DefaultTreeModel)tree.getModel()).nodeChanged(node);
- // I need revalidate if node is root. but why?
-
- tree.revalidate();
- tree.repaint();
-
- }
- }
- }
-
- public JTree getTree() {
- return tree;
- }
-
- /**
- * Call to expand the entire tree.
- */
- public void expandTree() {
- for (int i = 0; i <= tree.getRowCount(); i++) {
- tree.expandPath(tree.getPathForRow(i));
- }
- }
- }
效果图: