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.event.ActionEvent;
22: import java.util.Collections;
23: import java.util.HashSet;
24: import java.util.Set;
25:
26: import javax.swing.JComponent;
27: import javax.swing.tree.TreePath;
28:
29: import org.apache.jmeter.exceptions.IllegalUserActionException;
30: import org.apache.jmeter.gui.GuiPackage;
31: import org.apache.jmeter.gui.tree.JMeterTreeNode;
32: import org.apache.jmeter.testelement.TestElement;
33: import org.apache.jmeter.util.JMeterUtils;
34: import org.apache.jorphan.logging.LoggingManager;
35: import org.apache.log.Logger;
36:
37: public class AddToTree implements Command {
38: private static final Logger log = LoggingManager
39: .getLoggerForClass();
40:
41: private static Set commandSet;
42:
43: static {
44: HashSet commands = new HashSet();
45: commands.add(ActionNames.ADD);
46: commandSet = Collections.unmodifiableSet(commands);
47: }
48:
49: public AddToTree() {
50: }
51:
52: /**
53: * Gets the Set of actions this Command class responds to.
54: *
55: * @return the ActionNames value
56: */
57: public Set getActionNames() {
58: return commandSet;
59: }
60:
61: /**
62: * Adds the specified class to the current node of the tree.
63: */
64: public void doAction(ActionEvent e) {
65: GuiPackage guiPackage = GuiPackage.getInstance();
66: try {
67: guiPackage.updateCurrentNode();
68: TestElement testElement = guiPackage
69: .createTestElement(((JComponent) e.getSource())
70: .getName());
71: JMeterTreeNode parentNode = guiPackage.getCurrentNode();
72: JMeterTreeNode node = guiPackage.getTreeModel()
73: .addComponent(testElement, parentNode);
74: guiPackage.getMainFrame().getTree().setSelectionPath(
75: new TreePath(node.getPath()));
76: } catch (IllegalUserActionException err) {
77: log.error("", err); // $NON-NLS-1$
78: String msg = err.getMessage();
79: if (msg == null) {
80: msg = err.toString();
81: }
82: JMeterUtils.reportErrorToUser(msg);
83: } catch (Exception err) {
84: log.error("", err); // $NON-NLS-1$
85: String msg = err.getMessage();
86: if (msg == null) {
87: msg = err.toString();
88: }
89: JMeterUtils.reportErrorToUser(msg);
90: }
91: }
92: }
|