01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.Component;
22: import java.awt.event.ActionEvent;
23: import java.util.HashSet;
24: import java.util.Set;
25:
26: import org.apache.jmeter.gui.GuiPackage;
27: import org.apache.jmeter.gui.tree.JMeterTreeNode;
28: import org.apache.jmeter.testelement.TestElement;
29: import org.apache.jorphan.logging.LoggingManager;
30: import org.apache.log.Logger;
31:
32: /**
33: * @author Michael Stover
34: * @version $Revision: 493779 $
35: */
36: public class AddParent implements Command {
37: private static final Logger log = LoggingManager
38: .getLoggerForClass();
39:
40: private static Set commands = new HashSet();
41: static {
42: commands.add(ActionNames.ADD_PARENT);
43: }
44:
45: public AddParent() {
46: }
47:
48: public void doAction(ActionEvent e) {
49: String name = ((Component) e.getSource()).getName();
50: try {
51: TestElement controller = GuiPackage.getInstance()
52: .createTestElement(name);
53: addParentToTree(controller);
54: } catch (Exception err) {
55: log.error("", err);
56: }
57:
58: }
59:
60: public Set getActionNames() {
61: return commands;
62: }
63:
64: protected void addParentToTree(TestElement newParent) {
65: GuiPackage guiPackage = GuiPackage.getInstance();
66: JMeterTreeNode newNode = new JMeterTreeNode(newParent,
67: guiPackage.getTreeModel());
68: JMeterTreeNode currentNode = guiPackage.getTreeListener()
69: .getCurrentNode();
70: JMeterTreeNode parentNode = (JMeterTreeNode) currentNode
71: .getParent();
72: int index = parentNode.getIndex(currentNode);
73: guiPackage.getTreeModel().insertNodeInto(newNode, parentNode,
74: index);
75: JMeterTreeNode[] nodes = guiPackage.getTreeListener()
76: .getSelectedNodes();
77: for (int i = 0; i < nodes.length; i++) {
78: moveNode(guiPackage, nodes[i], newNode);
79: }
80: }
81:
82: private void moveNode(GuiPackage guiPackage, JMeterTreeNode node,
83: JMeterTreeNode newParentNode) {
84: guiPackage.getTreeModel().removeNodeFromParent(node);
85: guiPackage.getTreeModel().insertNodeInto(node, newParentNode,
86: newParentNode.getChildCount());
87: }
88: }
|