/* * Copyright 2007 Kevin Roe, Daniel Mccarney * This file is part of Jriver. * * Jriver 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 3 of the License, or * (at your option) any later version. * * Jriver 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, see <http://www.gnu.org/licenses/>. */ package org.jriver.lib.bin; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.*; import javax.swing.border.TitledBorder; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.tree.DefaultMutableTreeNode; import org.jriver.core.*; public class ObjTree extends JPanel { private DefaultMutableTreeNode root; private JTree tree; private JScrollPane treeView; private JLabel lblID = new JLabel("ID: "); private JTextField lblIDValue = new JTextField("null"); private JLabel lblHash = new JLabel("Hash: "); private JTextField lblHashValue = new JTextField("null"); private JLabel lblName = new JLabel("Name: "); private JTextField lblNameValue = new JTextField("null"); private JLabel lblContainer = new JLabel("Container: "); private JTextField lblContainerValue = new JTextField("false"); private JLabel lblEnv = new JLabel("Env: "); private JTextField lblEnvValue = new JTextField("null"); public static void main(String[] args) { @SuppressWarnings("unused") ObjTree tree = new ObjTree(); } public ObjTree() { super(new FlowLayout()); try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.err.println("Couldn't use system look and feel."); } JFrame frame = new JFrame("TreeDemo"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setResizable(false); //Create and set up the content pane. setOpaque(true); //content panes must be opaque frame.setContentPane(this); //Display the window. /* root = new DefaultMutableTreeNode(new Master()); MudObject room = new MudObject(); room.setID("A Small Rustic Cabin"); MudObject player = new MudObject(); player.setID("Enigma"); MudObject sword = new MudObject(false); sword.setID("A Sword"); MudObject sword2 = new MudObject(false); sword2.setID("A Sword"); MudObject bag = new MudObject(); bag.setID("A Backpack"); MudObject torch = new MudObject(false); torch.setID("A Torch"); */ root = MudDriver.getMudDriver().getLoginRoom().getRootNode(); /* try { player.insertContent(sword); room.insertContent(player); addObject(room); player.insertContent(sword2); player.insertContent(bag); bag.insertContent(torch); sword2.drop(); sword2.drop(); } catch(IllegalStateException e) { System.err.println("Attemtping to put an object inside something that isn't a container."); } */ tree = new JTree(root); tree.addTreeSelectionListener(new treeListener()); JScrollPane treeView = new JScrollPane(tree); JPanel itemPanel = new JPanel(); itemPanel.setPreferredSize(new Dimension(250, 300)); itemPanel.setBorder(new TitledBorder("Obj. Details")); lblID.setPreferredSize(new Dimension(80,10)); lblHash.setPreferredSize(new Dimension(80,10)); lblName.setPreferredSize(new Dimension(80,10)); lblContainer.setPreferredSize(new Dimension(80,10)); lblEnv.setPreferredSize(new Dimension(80,10)); lblIDValue.setPreferredSize(new Dimension(120,20)); lblIDValue.setEditable(false); lblHashValue.setPreferredSize(new Dimension(100,20)); lblHashValue.setEditable(false); lblNameValue.setPreferredSize(new Dimension(100,20)); lblNameValue.setEditable(false); lblContainerValue.setPreferredSize(new Dimension(100,20)); lblContainerValue.setEditable(false); lblEnvValue.setPreferredSize(new Dimension(100,20)); lblEnvValue.setEditable(false); itemPanel.add(lblID); itemPanel.add(lblIDValue); itemPanel.add(lblHash); itemPanel.add(lblHashValue); itemPanel.add(lblName); itemPanel.add(lblNameValue); itemPanel.add(lblContainer); itemPanel.add(lblContainerValue); itemPanel.add(lblEnv); itemPanel.add(lblEnvValue); JScrollPane itemView = new JScrollPane(itemPanel); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView, itemView); splitPane.setOneTouchExpandable(true); add(splitPane); frame.pack(); frame.setVisible(true); } public void addObject(MudObject ob) { root.add(ob.getRootNode()); } private class treeListener implements TreeSelectionListener { public void valueChanged(TreeSelectionEvent e) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) return; MudObject nodeInfo = (MudObject) node.getUserObject(); MudObject nodeEnv = null; if(nodeInfo.environment() != null) { nodeEnv = (MudObject) nodeInfo.environment().getUserObject(); lblEnvValue.setText(nodeEnv.getID()); } else { lblEnvValue.setText("null"); } lblIDValue.setText(nodeInfo.getID()); lblHashValue.setText(String.valueOf(nodeInfo.hashCode())); lblNameValue.setText(nodeInfo.queryName()); lblContainerValue.setText(String.valueOf(nodeInfo.isContainer())); } } }