001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.report.gui.tree;
020:
021: import java.util.Enumeration;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import javax.swing.tree.DefaultTreeModel;
027:
028: import org.apache.jmeter.config.gui.AbstractConfigGui;
029: import org.apache.jmeter.control.gui.ReportGui;
030: import org.apache.jmeter.exceptions.IllegalUserActionException;
031: import org.apache.jmeter.gui.ReportGuiPackage;
032: import org.apache.jmeter.gui.JMeterGUIComponent;
033: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
034: import org.apache.jmeter.testelement.TestElement;
035: import org.apache.jmeter.testelement.ReportPlan;
036: import org.apache.jmeter.testelement.property.NullProperty;
037: import org.apache.jmeter.util.NameUpdater;
038: import org.apache.jorphan.collections.HashTree;
039: import org.apache.jorphan.collections.ListedHashTree;
040:
041: /**
042: *
043: * author Peter Lin
044: *
045: * @version $Revision: 493793 $
046: */
047: public class ReportTreeModel extends DefaultTreeModel {
048:
049: public ReportTreeModel() {
050: super (new ReportTreeNode(new ReportGui().createTestElement(),
051: null));
052: initTree();
053: }
054:
055: /**
056: * Returns a list of tree nodes that hold objects of the given class type.
057: * If none are found, an empty list is returned.
058: */
059: public List getNodesOfType(Class type) {
060: List nodeList = new LinkedList();
061: traverseAndFind(type, (ReportTreeNode) this .getRoot(), nodeList);
062: return nodeList;
063: }
064:
065: /**
066: * Get the node for a given TestElement object.
067: */
068: public ReportTreeNode getNodeOf(TestElement userObject) {
069: return traverseAndFind(userObject, (ReportTreeNode) getRoot());
070: }
071:
072: /**
073: * Adds the sub tree at the given node. Returns a boolean indicating whether
074: * the added sub tree was a full test plan.
075: */
076: public HashTree addSubTree(HashTree subTree, ReportTreeNode current)
077: throws IllegalUserActionException {
078: Iterator iter = subTree.list().iterator();
079: while (iter.hasNext()) {
080: TestElement item = (TestElement) iter.next();
081: if (item instanceof ReportPlan) {
082: current = (ReportTreeNode) ((ReportTreeNode) getRoot())
083: .getChildAt(0);
084: ((TestElement) current.getUserObject())
085: .addTestElement(item);
086: ((ReportPlan) current.getUserObject()).setName(item
087: .getPropertyAsString(TestElement.NAME));
088: addSubTree(subTree.getTree(item), current);
089: } else {
090: if (subTree.getTree(item) != null) {
091: addSubTree(subTree.getTree(item), addComponent(
092: item, current));
093: }
094: }
095: }
096: return getCurrentSubTree(current);
097: }
098:
099: public ReportTreeNode addComponent(TestElement component,
100: ReportTreeNode node) throws IllegalUserActionException {
101: if (node.getUserObject() instanceof AbstractConfigGui) {
102: throw new IllegalUserActionException(
103: "This node cannot hold sub-elements");
104: }
105: component.setProperty(TestElement.GUI_CLASS, NameUpdater
106: .getCurrentName(component
107: .getPropertyAsString(TestElement.GUI_CLASS)));
108: ReportGuiPackage.getInstance().updateCurrentNode();
109: JMeterGUIComponent guicomp = ReportGuiPackage.getInstance()
110: .getGui(component);
111: guicomp.configure(component);
112: guicomp.modifyTestElement(component);
113: ReportGuiPackage.getInstance().getCurrentGui(); // put the gui object back
114: // to the way it was.
115: ReportTreeNode newNode = new ReportTreeNode(component, this );
116:
117: // This check the state of the TestElement and if returns false it
118: // disable the loaded node
119: try {
120: if (component.getProperty(TestElement.ENABLED) instanceof NullProperty
121: || component
122: .getPropertyAsBoolean(TestElement.ENABLED)) {
123: newNode.setEnabled(true);
124: } else {
125: newNode.setEnabled(false);
126: }
127: } catch (Exception e) {
128: newNode.setEnabled(true);
129: }
130:
131: this .insertNodeInto(newNode, node, node.getChildCount());
132: return newNode;
133: }
134:
135: public void removeNodeFromParent(ReportTreeNode node) {
136: if (!(node.getUserObject() instanceof ReportPlan)) {
137: super .removeNodeFromParent(node);
138: }
139: }
140:
141: private void traverseAndFind(Class type, ReportTreeNode node,
142: List nodeList) {
143: if (type.isInstance(node.getUserObject())) {
144: nodeList.add(node);
145: }
146: Enumeration enumNode = node.children();
147: while (enumNode.hasMoreElements()) {
148: ReportTreeNode child = (ReportTreeNode) enumNode
149: .nextElement();
150: traverseAndFind(type, child, nodeList);
151: }
152: }
153:
154: private ReportTreeNode traverseAndFind(TestElement userObject,
155: ReportTreeNode node) {
156: if (userObject == node.getUserObject()) {
157: return node;
158: }
159: Enumeration enumNode = node.children();
160: while (enumNode.hasMoreElements()) {
161: ReportTreeNode child = (ReportTreeNode) enumNode
162: .nextElement();
163: ReportTreeNode result = traverseAndFind(userObject, child);
164: if (result != null)
165: return result;
166: }
167: return null;
168: }
169:
170: public HashTree getCurrentSubTree(ReportTreeNode node) {
171: ListedHashTree hashTree = new ListedHashTree(node);
172: Enumeration enumNode = node.children();
173: while (enumNode.hasMoreElements()) {
174: ReportTreeNode child = (ReportTreeNode) enumNode
175: .nextElement();
176: hashTree.add(node, getCurrentSubTree(child));
177: }
178: return hashTree;
179: }
180:
181: public HashTree getReportPlan() {
182: return getCurrentSubTree((ReportTreeNode) ((ReportTreeNode) this
183: .getRoot()).getChildAt(0));
184: }
185:
186: public void clearTestPlan() {
187: super .removeNodeFromParent((ReportTreeNode) getChild(getRoot(),
188: 0));
189: initTree();
190: }
191:
192: private void initTree() {
193: TestElement rp = new ReportGui().createTestElement();
194: this .insertNodeInto(new ReportTreeNode(rp, this ),
195: (ReportTreeNode) getRoot(), 0);
196: }
197: }
|