/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Email: thlee@onemindsoft.org
*/
package org.onemind.swingweb.console;
import java.awt.*;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import org.onemind.commons.java.util.MapUtils;
import org.onemind.swingweb.console.ComponentTreeNodeCreator.ComponentTreeNode;
import org.onemind.swingweb.util.SwingWebUtils;
/**
* A component tree inspector
* @author TiongHiang Lee (thlee@onemindsoft.org)
*
*/
public class ComponentTreeInspector extends JPanel implements TreeSelectionListener
{
/** the component tree **/
private JTree _componentTree;
/** information text **/
private JTextArea _info;
/**
* Constructor
* @param com the component
*/
public ComponentTreeInspector(Component com)
{
super();
setLayout(new BorderLayout());
setBackground(Color.WHITE);
//the split pane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerLocation(.5);
add(BorderLayout.CENTER, splitPane);
//the left panel
_componentTree = new JTree();
_componentTree.addTreeSelectionListener(this);
_componentTree.setModel(new ComponentTreeModel(com));
expandAllNodes(_componentTree);
splitPane.setLeftComponent(new JScrollPane(_componentTree));
//the information panel
_info = new JTextArea("Click on a component in the component tree to view the component's information");
//_info.setEditable(false);
_info.setColumns(40);
splitPane.setRightComponent(new JScrollPane(_info));
splitPane.setDividerLocation(350);
}
public void expandAllNodes(JTree tree) {
int row = 0;
while (row < tree.getRowCount()) {
tree.expandRow(row);
row++;
}
}
/**
* {@inheritDoc}
*/
public void valueChanged(TreeSelectionEvent e)
{
ComponentTreeNode node = (ComponentTreeNode) e.getPath().getLastPathComponent();
if (node != null)
{
Component com = node.getComponent();
Map info = new LinkedHashMap();
info.put("Component toString()", com.toString());
info.put("Component class", com.getClass());
info.put("Component name", com.getName());
info.put("Component bounds", com.getBounds());
info.put("Component size", com.getSize());
if (SwingWebUtils.isInSwingWebMode())
{
info.putAll(SwingWebUtils.getSwingWebContext().getRuntimeInfo(com));
}
try
{
_info.setText(MapUtils.toString(info));
} catch (IOException e1)
{
_info.setText(e1.getMessage());
}
}
}
}