001: //$Header$
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019:
020: package org.apache.jmeter.report.gui.action;
021:
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import org.apache.jmeter.gui.ReportGuiPackage;
030: import org.apache.jmeter.report.gui.action.AbstractAction;
031: import org.apache.jmeter.report.gui.action.ReportActionRouter;
032: import org.apache.jmeter.report.gui.action.ReportExitCommand;
033: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
034: import org.apache.jorphan.collections.HashTree;
035: import org.apache.jorphan.collections.HashTreeTraverser;
036: import org.apache.jorphan.collections.ListedHashTree;
037: import org.apache.jorphan.logging.LoggingManager;
038: import org.apache.log.Logger;
039:
040: /**
041: * @author Peter Lin
042: * @version $Revision: 571988 $
043: */
044: public class ReportCheckDirty extends AbstractAction implements
045: HashTreeTraverser, ActionListener {
046: private static final Logger log = LoggingManager
047: .getLoggerForClass();
048:
049: private static Map previousGuiItems;
050:
051: public static final String CHECK_DIRTY = "check_dirty";
052:
053: public static final String SUB_TREE_SAVED = "sub_tree_saved";
054:
055: public static final String SUB_TREE_LOADED = "sub_tree_loaded";
056:
057: public static final String ADD_ALL = "add_all";
058:
059: // Not implemented: public static final String SAVE = "save_as";
060: // Not implemented: public static final String SAVE_ALL = "save_all";
061: // Not implemented: public static final String SAVE_TO_PREVIOUS = "save";
062: public static final String REMOVE = "check_remove";
063:
064: boolean checkMode = false;
065:
066: boolean removeMode = false;
067:
068: boolean dirty = false;
069:
070: private static Set commands = new HashSet();
071: static {
072: commands.add(CHECK_DIRTY);
073: commands.add(SUB_TREE_SAVED);
074: commands.add(SUB_TREE_LOADED);
075: commands.add(ADD_ALL);
076: commands.add(REMOVE);
077: }
078:
079: public ReportCheckDirty() {
080: previousGuiItems = new HashMap();
081: ReportActionRouter.getInstance().addPreActionListener(
082: ReportExitCommand.class, this );
083: }
084:
085: public void actionPerformed(ActionEvent e) {
086: if (e.getActionCommand().equals(ReportExitCommand.EXIT)) {
087: doAction(e);
088: }
089: }
090:
091: /**
092: * @see org.apache.jmeter.gui.action.Command#doAction(ActionEvent)
093: */
094: public void doAction(ActionEvent e) {
095: String action = e.getActionCommand();
096: if (action.equals(SUB_TREE_SAVED)) {
097: HashTree subTree = (HashTree) e.getSource();
098: subTree.traverse(this );
099: } else if (action.equals(SUB_TREE_LOADED)) {
100: ListedHashTree addTree = (ListedHashTree) e.getSource();
101: addTree.traverse(this );
102: } else if (action.equals(ADD_ALL)) {
103: previousGuiItems.clear();
104: ReportGuiPackage.getInstance().getTreeModel()
105: .getReportPlan().traverse(this );
106: } else if (action.equals(REMOVE)) {
107: ReportGuiPackage guiPackage = ReportGuiPackage
108: .getInstance();
109: ReportTreeNode[] nodes = guiPackage.getTreeListener()
110: .getSelectedNodes();
111: removeMode = true;
112: for (int i = nodes.length - 1; i >= 0; i--) {
113: guiPackage.getTreeModel().getCurrentSubTree(nodes[i])
114: .traverse(this );
115: }
116: removeMode = false;
117: }
118: checkMode = true;
119: dirty = false;
120: HashTree wholeTree = ReportGuiPackage.getInstance()
121: .getTreeModel().getReportPlan();
122: wholeTree.traverse(this );
123: ReportGuiPackage.getInstance().setDirty(dirty);
124: checkMode = false;
125: }
126:
127: /**
128: * The tree traverses itself depth-first, calling processNode for each
129: * object it encounters as it goes.
130: */
131: public void addNode(Object node, HashTree subTree) {
132: log.debug("Node is class:" + node.getClass());
133: ReportTreeNode treeNode = (ReportTreeNode) node;
134: if (checkMode) {
135: if (previousGuiItems.containsKey(treeNode)) {
136: if (!previousGuiItems.get(treeNode).equals(
137: treeNode.getTestElement())) {
138: dirty = true;
139: }
140: } else {
141: dirty = true;
142: }
143: } else if (removeMode) {
144: previousGuiItems.remove(treeNode);
145: } else {
146: previousGuiItems.put(treeNode, treeNode.getTestElement()
147: .clone());
148: }
149: }
150:
151: /**
152: * Indicates traversal has moved up a step, and the visitor should remove
153: * the top node from it's stack structure.
154: */
155: public void subtractNode() {
156: }
157:
158: /**
159: * Process path is called when a leaf is reached. If a visitor wishes to
160: * generate Lists of path elements to each leaf, it should keep a Stack data
161: * structure of nodes passed to it with addNode, and removing top items for
162: * every subtractNode() call.
163: */
164: public void processPath() {
165: }
166:
167: /**
168: * @see org.apache.jmeter.gui.action.Command#getActionNames()
169: */
170: public Set getActionNames() {
171: return commands;
172: }
173: }
|