KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > tree > JMeterTreeModel


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/tree/JMeterTreeModel.java,v 1.24 2004/02/13 02:21:36 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.tree;
20
21 import java.util.Enumeration;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import javax.swing.tree.DefaultTreeModel;
27
28 import org.apache.jmeter.config.gui.AbstractConfigGui;
29 import org.apache.jmeter.control.gui.TestPlanGui;
30 import org.apache.jmeter.control.gui.WorkBenchGui;
31 import org.apache.jmeter.exceptions.IllegalUserActionException;
32 import org.apache.jmeter.gui.GuiPackage;
33 import org.apache.jmeter.gui.JMeterGUIComponent;
34 import org.apache.jmeter.testelement.TestElement;
35 import org.apache.jmeter.testelement.TestPlan;
36 import org.apache.jmeter.testelement.WorkBench;
37 import org.apache.jmeter.testelement.property.NullProperty;
38 import org.apache.jmeter.util.NameUpdater;
39 import org.apache.jorphan.collections.HashTree;
40 import org.apache.jorphan.collections.ListedHashTree;
41
42 /**
43  *
44  * @author Michael Stover
45  * @version $Revision: 1.24 $
46  */

47 public class JMeterTreeModel extends DefaultTreeModel
48 {
49
50     public JMeterTreeModel()
51     {
52         super(new JMeterTreeNode(new WorkBenchGui().createTestElement(), null));
53         initTree();
54     }
55
56     /**
57      * Returns a list of tree nodes that hold objects of the given class type.
58      * If none are found, an empty list is returned.
59      */

60     public List getNodesOfType(Class type)
61     {
62         List nodeList = new LinkedList();
63         traverseAndFind(type, (JMeterTreeNode) this.getRoot(), nodeList);
64         return nodeList;
65     }
66
67     /**
68      * Get the node for a given TestElement object.
69      */

70     public JMeterTreeNode getNodeOf(TestElement userObject)
71     {
72         return traverseAndFind(
73             userObject,
74             (JMeterTreeNode)getRoot());
75     }
76
77     /**
78      * Adds the sub tree at the given node. Returns a boolean indicating
79      * whether the added sub tree was a full test plan.
80      */

81     public HashTree addSubTree(HashTree subTree, JMeterTreeNode current)
82         throws IllegalUserActionException
83     {
84         Iterator iter = subTree.list().iterator();
85         while (iter.hasNext())
86         {
87             TestElement item = (TestElement) iter.next();
88             if (item instanceof TestPlan)
89             {
90                 current =
91                     (JMeterTreeNode) ((JMeterTreeNode) getRoot()).getChildAt(0);
92                 ((TestElement) current.getUserObject()).addTestElement(item);
93                 ((TestPlan) current.getUserObject()).setName(
94                     item.getPropertyAsString(TestElement.NAME));
95                 ((TestPlan) current.getUserObject()).setFunctionalMode(
96                     item.getPropertyAsBoolean(TestPlan.FUNCTIONAL_MODE));
97                 ((TestPlan) current.getUserObject()).setSerialized(
98                     item.getPropertyAsBoolean(TestPlan.SERIALIZE_THREADGROUPS));
99                 addSubTree(subTree.getTree(item), current);
100             }
101             else if (item instanceof WorkBench)
102             {
103                 current =
104                     (JMeterTreeNode) ((JMeterTreeNode) getRoot()).getChildAt(1);
105                 ((TestElement) current.getUserObject()).addTestElement(item);
106                 ((WorkBench) current.getUserObject()).setName(
107                     item.getPropertyAsString(TestElement.NAME));
108                 addSubTree(subTree.getTree(item), current);
109             }
110             else
111             {
112                 addSubTree(subTree.getTree(item), addComponent(item, current));
113             }
114         }
115         return getCurrentSubTree(current);
116     }
117
118     public JMeterTreeNode addComponent(
119         TestElement component,
120         JMeterTreeNode node)
121         throws IllegalUserActionException
122     {
123         if (node.getUserObject() instanceof AbstractConfigGui)
124         {
125             throw new IllegalUserActionException(
126                     "This node cannot hold sub-elements");
127         }
128         component.setProperty(
129             TestElement.GUI_CLASS,
130             NameUpdater.getCurrentName(
131                 component.getPropertyAsString(TestElement.GUI_CLASS)));
132         GuiPackage.getInstance().updateCurrentNode();
133         JMeterGUIComponent guicomp = GuiPackage.getInstance().getGui(component);
134         guicomp.configure(component);
135         guicomp.modifyTestElement(component);
136         GuiPackage.getInstance().getCurrentGui(); //put the gui object back to the way it was.
137
JMeterTreeNode newNode =
138             new JMeterTreeNode((TestElement) component, this);
139
140         // This check the state of the TestElement and if returns false it
141
// disable the loaded node
142
try
143         {
144             if (component.getProperty(TestElement.ENABLED)
145                 instanceof NullProperty
146                 || component.getPropertyAsBoolean(TestElement.ENABLED))
147             {
148                 newNode.setEnabled(true);
149             }
150             else
151             {
152                 newNode.setEnabled(false);
153             }
154         }
155         catch (Exception e)
156         {
157             newNode.setEnabled(true);
158         }
159
160         this.insertNodeInto(newNode, node, node.getChildCount());
161         return newNode;
162     }
163
164     public void removeNodeFromParent(JMeterTreeNode node)
165     {
166         if (!(node.getUserObject() instanceof TestPlan)
167             && !(node.getUserObject() instanceof WorkBench))
168         {
169             super.removeNodeFromParent(node);
170         }
171     }
172
173     private void traverseAndFind(
174         Class type,
175         JMeterTreeNode node,
176         List nodeList)
177     {
178         if (type.isInstance(node.getUserObject()))
179         {
180             nodeList.add(node);
181         }
182         Enumeration enum = node.children();
183         while (enum.hasMoreElements())
184         {
185             JMeterTreeNode child = (JMeterTreeNode) enum.nextElement();
186             traverseAndFind(type, child, nodeList);
187         }
188     }
189
190     private JMeterTreeNode traverseAndFind(
191         TestElement userObject,
192         JMeterTreeNode node)
193     {
194         if (userObject == node.getUserObject())
195         {
196             return node;
197         }
198         Enumeration enum = node.children();
199         while (enum.hasMoreElements())
200         {
201             JMeterTreeNode child = (JMeterTreeNode) enum.nextElement();
202             JMeterTreeNode result= traverseAndFind(userObject, child);
203             if (result != null) return result;
204         }
205         return null;
206     }
207
208     public HashTree getCurrentSubTree(JMeterTreeNode node)
209     {
210         ListedHashTree hashTree = new ListedHashTree(node);
211         Enumeration enum = node.children();
212         while (enum.hasMoreElements())
213         {
214             JMeterTreeNode child = (JMeterTreeNode) enum.nextElement();
215             hashTree.add(node, getCurrentSubTree(child));
216         }
217         return hashTree;
218     }
219
220     public HashTree getTestPlan()
221     {
222         return getCurrentSubTree(
223             (JMeterTreeNode) ((JMeterTreeNode) this.getRoot()).getChildAt(0));
224     }
225
226     public void clearTestPlan()
227     {
228         super.removeNodeFromParent((JMeterTreeNode) getChild(getRoot(), 0));
229         initTree();
230     }
231
232     private void initTree()
233     {
234         TestElement tp = new TestPlanGui().createTestElement();
235         TestElement wb = new WorkBenchGui().createTestElement();
236         this.insertNodeInto(
237             new JMeterTreeNode(tp, this),
238             (JMeterTreeNode) getRoot(),
239             0);
240         try
241         {
242             super.removeNodeFromParent((JMeterTreeNode) getChild(getRoot(), 1));
243         }
244         catch (RuntimeException e)
245         {
246         }
247         this.insertNodeInto(
248             new JMeterTreeNode(wb, this),
249             (JMeterTreeNode) getRoot(),
250             1);
251     }
252 }
253
Popular Tags