KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > action > CheckDirty


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/action/CheckDirty.java,v 1.11.2.2 2005/03/13 17:41:58 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.action;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.jmeter.gui.GuiPackage;
29 import org.apache.jmeter.gui.tree.JMeterTreeNode;
30 import org.apache.jorphan.collections.HashTree;
31 import org.apache.jorphan.collections.HashTreeTraverser;
32 import org.apache.jorphan.collections.ListedHashTree;
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * @author mstover
38  * @version $Revision: 1.11.2.2 $
39  */

40 public class CheckDirty
41     extends AbstractAction
42     implements HashTreeTraverser, ActionListener JavaDoc
43 {
44     private static final Logger log = LoggingManager.getLoggerForClass();
45
46     private static Map JavaDoc previousGuiItems;
47     public static final String JavaDoc CHECK_DIRTY = "check_dirty";
48     public static final String JavaDoc SUB_TREE_SAVED = "sub_tree_saved";
49     public static final String JavaDoc SUB_TREE_LOADED = "sub_tree_loaded";
50     public static final String JavaDoc ADD_ALL = "add_all";
51     //Not implemented: public static final String SAVE = "save_as";
52
//Not implemented: public static final String SAVE_ALL = "save_all";
53
//Not implemented: public static final String SAVE_TO_PREVIOUS = "save";
54
public static final String JavaDoc REMOVE = "check_remove";
55
56     boolean checkMode = false;
57     boolean removeMode = false;
58     boolean dirty = false;
59
60     private static Set JavaDoc commands = new HashSet JavaDoc();
61     static
62     {
63         commands.add(CHECK_DIRTY);
64         commands.add(SUB_TREE_SAVED);
65         commands.add(SUB_TREE_LOADED);
66         commands.add(ADD_ALL);
67         commands.add(REMOVE);
68     }
69
70     public CheckDirty()
71     {
72         previousGuiItems = new HashMap JavaDoc();
73         ActionRouter.getInstance().addPreActionListener(
74             ExitCommand.class,
75             this);
76     }
77
78     public void actionPerformed(ActionEvent JavaDoc e)
79     {
80         if (e.getActionCommand().equals(ExitCommand.EXIT))
81         {
82             doAction(e);
83         }
84     }
85
86     /**
87      * @see Command#doAction(ActionEvent)
88      */

89     public void doAction(ActionEvent JavaDoc e)
90     {
91         String JavaDoc action = e.getActionCommand();
92         if (action.equals(SUB_TREE_SAVED))
93         {
94             HashTree subTree = (HashTree) e.getSource();
95             subTree.traverse(this);
96         }
97         else if (action.equals(SUB_TREE_LOADED))
98         {
99             ListedHashTree addTree = (ListedHashTree) e.getSource();
100             addTree.traverse(this);
101         }
102         else if (action.equals(ADD_ALL))
103         {
104             previousGuiItems.clear();
105             GuiPackage.getInstance().getTreeModel().getTestPlan().traverse(
106                 this);
107         }
108         else if (action.equals(REMOVE))
109         {
110             GuiPackage guiPackage = GuiPackage.getInstance();
111             JMeterTreeNode[] nodes =
112                 guiPackage.getTreeListener().getSelectedNodes();
113             removeMode = true;
114             for (int i = nodes.length - 1; i >= 0; i--)
115             {
116                 guiPackage.getTreeModel().getCurrentSubTree(nodes[i]).traverse(
117                     this);
118             }
119             removeMode = false;
120         }
121         checkMode = true;
122         dirty = false;
123         HashTree wholeTree =
124             GuiPackage.getInstance().getTreeModel().getTestPlan();
125         wholeTree.traverse(this);
126         GuiPackage.getInstance().setDirty(dirty);
127         checkMode = false;
128     }
129
130     /**
131      * The tree traverses itself depth-first, calling processNode for each
132      * object it encounters as it goes.
133      */

134     public void addNode(Object JavaDoc node, HashTree subTree)
135     {
136         log.debug("Node is class:" + node.getClass());
137         JMeterTreeNode treeNode = (JMeterTreeNode) node;
138         if (checkMode)
139         {
140             if (previousGuiItems.containsKey(treeNode))
141             {
142                 if (!previousGuiItems
143                     .get(treeNode)
144                     .equals(treeNode.getTestElement()))
145                 {
146                     dirty = true;
147                 }
148             }
149             else
150             {
151                 dirty = true;
152             }
153         }
154         else if (removeMode)
155         {
156             previousGuiItems.remove(treeNode);
157         }
158         else
159         {
160             previousGuiItems.put(
161                 treeNode,
162                 treeNode.getTestElement().clone());
163         }
164     }
165
166     /**
167      * Indicates traversal has moved up a step, and the visitor should remove
168      * the top node from it's stack structure.
169      */

170     public void subtractNode()
171     {
172     }
173
174     /**
175      * Process path is called when a leaf is reached. If a visitor wishes to
176      * generate Lists of path elements to each leaf, it should keep a Stack
177      * data structure of nodes passed to it with addNode, and removing top
178      * items for every subtractNode() call.
179      */

180     public void processPath()
181     {
182     }
183
184     /**
185      * @see Command#getActionNames()
186      */

187     public Set JavaDoc getActionNames()
188     {
189         return commands;
190     }
191 }
192
Popular Tags