01: //$Header$
02: /*
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: *
18: */
19:
20: package org.apache.jmeter.report.gui.action;
21:
22: import java.awt.Component;
23: import java.awt.event.ActionEvent;
24: import java.util.HashSet;
25: import java.util.Set;
26:
27: import org.apache.jmeter.gui.ReportGuiPackage;
28: import org.apache.jmeter.gui.action.Command;
29: import org.apache.jmeter.report.gui.tree.ReportTreeNode;
30: import org.apache.jmeter.testelement.TestElement;
31: import org.apache.jorphan.logging.LoggingManager;
32: import org.apache.log.Logger;
33:
34: /**
35: * @author Peter Lin
36: * @version $Revision: 493793 $
37: */
38: public class ReportAddParent implements Command {
39: transient private static Logger log = LoggingManager
40: .getLoggerForClass();
41:
42: private static Set commands = new HashSet();
43: static {
44: commands.add("Add Parent");
45: }
46:
47: public ReportAddParent() {
48: }
49:
50: public void doAction(ActionEvent e) {
51: String name = ((Component) e.getSource()).getName();
52: try {
53: TestElement controller = ReportGuiPackage.getInstance()
54: .createTestElement(name);
55: addParentToTree(controller);
56: } catch (Exception err) {
57: log.error("", err);
58: }
59:
60: }
61:
62: public Set getActionNames() {
63: return commands;
64: }
65:
66: protected void addParentToTree(TestElement newParent) {
67: ReportGuiPackage guiPackage = ReportGuiPackage.getInstance();
68: ReportTreeNode newNode = new ReportTreeNode(newParent,
69: guiPackage.getTreeModel());
70: ReportTreeNode currentNode = guiPackage.getTreeListener()
71: .getCurrentNode();
72: ReportTreeNode parentNode = (ReportTreeNode) currentNode
73: .getParent();
74: int index = parentNode.getIndex(currentNode);
75: guiPackage.getTreeModel().insertNodeInto(newNode, parentNode,
76: index);
77: ReportTreeNode[] nodes = guiPackage.getTreeListener()
78: .getSelectedNodes();
79: for (int i = 0; i < nodes.length; i++) {
80: moveNode(guiPackage, nodes[i], newNode);
81: }
82: }
83:
84: private void moveNode(ReportGuiPackage guiPackage,
85: ReportTreeNode node, ReportTreeNode newParentNode) {
86: guiPackage.getTreeModel().removeNodeFromParent(node);
87: guiPackage.getTreeModel().insertNodeInto(node, newParentNode,
88: newParentNode.getChildCount());
89: }
90: }
|