package debugger.debuggerComponents;
import GUI.Output;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import resources.ResourceLoader;
import debugger.debuggerComponents.treemodel.NodeArray;
import debugger.debuggerComponents.treemodel.NodeBase;
import debugger.debuggerComponents.treemodel.NodeField;
import debugger.debuggerComponents.treemodel.NodeMain;
public class ClassWatcherFrame extends JFrame implements TreeWillExpandListener {
private static final long serialVersionUID = 3310087516047399747L;
private JPanel contentPane;
private JTree tree;
private NodeMain mainNode;
private DefaultTreeModel treemodel;
public ClassWatcherFrame(Class<?> mainClass) {
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(750, 100, 400, 520);
setTitle("PseuCo IDE - Values");
setIconImage(Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/resources/logo.png")));
setMinimumSize(new Dimension(300, 200));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
panel.add(new JButton(actionRefresh));
panel.add(new JButton(actionClose));
contentPane.add(panel, BorderLayout.SOUTH);
JScrollPane scrollpane = new JScrollPane();
// scrollpane.setViewportView();
scrollpane.setBackground(Color.WHITE);
scrollpane.setOpaque(true);
contentPane.add(scrollpane, BorderLayout.CENTER);
mainNode = new NodeMain(mainClass);
mainNode.refresh(null);
treemodel = new DefaultTreeModel(mainNode);
tree = new JTree(treemodel);
tree.addTreeWillExpandListener(this);
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.setCellRenderer(renderer);
scrollpane.setViewportView(tree);
ToolTipManager.sharedInstance().registerComponent(tree);
/*tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});*/
setVisible(true);
}
Action actionClose = new AbstractAction("Close window", ResourceLoader.loadIcon("close_view.gif")) {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
};
Action actionRefresh = new AbstractAction("Refresh", ResourceLoader.loadIcon("refresh_small.png")) {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0) {
nodeLoadChilds(mainNode);
mainNode.refresh(null);
nodeChanged(mainNode);
tree.repaint(50L);
}
};
private void nodeChanged(TreeNode node) {
treemodel.nodeChanged(node);
Enumeration<?> e = node.children();
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o instanceof NodeArray){
boolean ex = tree.isExpanded(new TreePath((TreeNode) o));
treemodel.nodeStructureChanged((TreeNode) o);
if (ex) tree.expandPath(new TreePath((TreeNode) o));
}
if (o instanceof TreeNode)
nodeChanged((TreeNode) o);
}
}
private void nodeLoadChilds(NodeBase node) {
if (tree.isExpanded(new TreePath(node))) {
node.loadChilds();
Enumeration<?> e = node.children();
while (e.hasMoreElements()) {
Object o = e.nextElement();
if (o instanceof NodeBase)
nodeLoadChilds((NodeBase) o);
}
}
}
@Override
public void treeWillCollapse(TreeExpansionEvent e)
throws ExpandVetoException {
}
@Override
public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
Object o = e.getPath().getLastPathComponent();
if (!(o instanceof NodeBase)) {
Output.err.println("Invalid node in tree");
throw new ExpandVetoException(e);
}
NodeBase n = (NodeBase) o;
if (n.loadChilds()) {
mainNode.refresh(null);
}
}
private DefaultTreeCellRenderer renderer = new MyRenderer();
private class MyRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component result = super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
if (result instanceof JLabel && value instanceof NodeField) {
JLabel l = (JLabel) result;
final NodeField n = (NodeField) value;
l.setIcon(n.getIcon());
}
return result;
}
};
/*void doMouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp == null) return;
NodeField n = (NodeField) tp.getLastPathComponent();
}*/
}