001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.ui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.Map;
028: import javax.swing.Icon;
029: import javax.swing.JPanel;
030: import javax.swing.JScrollPane;
031: import javax.swing.JSplitPane;
032: import javax.swing.JTextArea;
033: import javax.swing.JTextPane;
034: import javax.swing.JTree;
035: import javax.swing.ToolTipManager;
036: import javax.swing.tree.DefaultMutableTreeNode;
037: import javax.swing.tree.DefaultTreeCellRenderer;
038: import javax.swing.tree.DefaultTreeModel;
039: import javax.swing.tree.TreeModel;
040: import org.mactor.framework.TestContext;
041: import org.mactor.framework.TestEvent;
042: import org.mactor.framework.spec.SpecNode;
043:
044: public class TestDetailsPanel extends JPanel {
045:
046: JTextPane detailsTextPane = new JTextPane();
047: JTree detailsTree = new JTree(new DefaultTreeModel(null));
048: JTextArea detailsContextTextArea = new JTextArea(20, 10);
049:
050: public TestDetailsPanel() {
051: super (new BorderLayout());
052: JSplitPane detailsSplitPane = new JSplitPane(
053: JSplitPane.VERTICAL_SPLIT,
054: new JScrollPane(detailsTree), new JScrollPane(
055: detailsContextTextArea));
056: detailsTree.setCellRenderer(new DetailsTreeCellRenderer());
057: ToolTipManager.sharedInstance().registerComponent(detailsTree);
058: add(detailsSplitPane, BorderLayout.CENTER);
059: }
060:
061: public void setTestDetails(LinkedList<TestEvent> log,
062: TestContext context) {
063: if (log == null) {
064: detailsTree.setVisible(false);
065: detailsContextTextArea.setText("");
066: } else {
067: detailsTree.setVisible(true);
068: /*//Memory leak
069: TreeModel current = detailsTree.getModel();
070: if(current!=null && current.getRoot()!=null) {
071: ((DefaultMutableTreeNode)current.getRoot()).removeAllChildren();
072: ((DefaultMutableTreeNode)current.getRoot()).removeFromParent();
073: }*/
074: //Memory leak end
075: detailsTree.setModel(buildTreeModel(log));
076: if (log.size() > 0) {
077: detailsContextTextArea.setText("Variables:\t"
078: + context.getValues().toString());
079: } else {
080: detailsContextTextArea.setText("");
081: }
082: }
083: }
084:
085: public TreeModel buildTreeModel(LinkedList<TestEvent> log) {
086: if (log.size() == 0)
087: return new DefaultTreeModel(new DefaultMutableTreeNode(
088: "not started"));
089: Map<SpecNode, DefaultMutableTreeNode> specMap = new HashMap<SpecNode, DefaultMutableTreeNode>();
090: Iterator<TestEvent> it = log.iterator();
091: TestEvent first = it.next();
092: DefaultMutableTreeNode root = new DefaultMutableTreeNode(first
093: .getNode().getName());
094: specMap.put(first.getNode(), root);
095: while (it.hasNext()) {
096: TestEvent te = it.next();
097: SpecNode specNode = te.getNode();
098: if (te.isStartEventType()) {
099: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
100: specNode.getName());
101: specMap.get(specNode.getParentNode()).add(node);
102: specMap.put(specNode, node);
103: node.setUserObject(te);
104: } else {
105: specMap.get(specNode).setUserObject(te);
106: }
107: }
108: return new DefaultTreeModel(root);
109: }
110:
111: private static class DetailsTreeCellRenderer extends
112: DefaultTreeCellRenderer {
113: Icon SUCCESS_ICON = GUIUtil.loadIcon("/success_16.PNG");
114: Icon WORKING_ICON = GUIUtil.loadIcon("/working_16.PNG");
115: Icon FAILED_ICON = GUIUtil.loadIcon("/failed_16.PNG");
116:
117: public DetailsTreeCellRenderer() {
118: setOpaque(true);
119: }
120:
121: public Component getTreeCellRendererComponent(JTree tree,
122: Object value, boolean sel, boolean expanded,
123: boolean leaf, int row, boolean hasFocus) {
124: super .getTreeCellRendererComponent(tree, value, sel,
125: expanded, leaf, row, hasFocus);
126: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
127: if (node.getUserObject() instanceof TestEvent) {
128: TestEvent te = (TestEvent) node.getUserObject();
129: setText(te.getNode().getName());
130: setToolTipText(te.getNode().getShortDescription());
131: if (te.isStartEventType()) {
132: setIcon(WORKING_ICON);
133: } else {
134: if (te.isSuccessful())
135: setIcon(SUCCESS_ICON);
136: else {
137: if (te.getCause() != null)
138: setText(te.getNode().getName() + " - "
139: + te.getCause().getMessage());
140: setIcon(FAILED_ICON);
141: }
142: }
143: }
144: if (sel) {
145: setBackground(Color.BLUE);
146: setForeground(Color.WHITE);
147: } else {
148: setBackground(Color.WHITE);
149: setForeground(Color.BLACK);
150: }
151: return this;
152: }
153: }
154: }
|