001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.tree;
020:
021: import java.awt.Image;
022: import java.beans.BeanInfo;
023: import java.beans.IntrospectionException;
024: import java.beans.Introspector;
025: import java.util.Collection;
026:
027: import javax.swing.ImageIcon;
028: import javax.swing.JPopupMenu;
029: import javax.swing.tree.DefaultMutableTreeNode;
030:
031: import org.apache.jmeter.gui.GUIFactory;
032: import org.apache.jmeter.gui.GuiPackage;
033: import org.apache.jmeter.testbeans.TestBean;
034: import org.apache.jmeter.testelement.AbstractTestElement;
035: import org.apache.jmeter.testelement.TestElement;
036: import org.apache.jmeter.testelement.property.BooleanProperty;
037: import org.apache.jmeter.testelement.property.StringProperty;
038: import org.apache.jorphan.logging.LoggingManager;
039: import org.apache.log.Logger;
040:
041: public class JMeterTreeNode extends DefaultMutableTreeNode implements
042: NamedTreeNode {
043: private static final Logger log = LoggingManager
044: .getLoggerForClass();
045:
046: private JMeterTreeModel treeModel;
047:
048: public JMeterTreeNode() {// Allow serializable test to work
049: // TODO: is the serializable test necessary now that JMeterTreeNode is
050: // no longer a GUI component?
051: this (null, null);
052: }
053:
054: public JMeterTreeNode(TestElement userObj, JMeterTreeModel treeModel) {
055: super (userObj);
056: this .treeModel = treeModel;
057: }
058:
059: public boolean isEnabled() {
060: return ((AbstractTestElement) getTestElement())
061: .getPropertyAsBoolean(TestElement.ENABLED);
062: }
063:
064: public void setEnabled(boolean enabled) {
065: getTestElement().setProperty(
066: new BooleanProperty(TestElement.ENABLED, enabled));
067: treeModel.nodeChanged(this );
068: }
069:
070: public ImageIcon getIcon() {
071: return getIcon(true);
072: }
073:
074: public ImageIcon getIcon(boolean enabled) {
075: TestElement testElement = getTestElement();
076: try {
077: if (testElement instanceof TestBean) {
078: Class testClass = testElement.getClass();
079: try {
080: Image img = Introspector.getBeanInfo(testClass)
081: .getIcon(BeanInfo.ICON_COLOR_16x16);
082: // If icon has not been defined, then use GUI_CLASS property
083: if (img == null) {
084: Object clazz = Introspector.getBeanInfo(
085: testClass).getBeanDescriptor()
086: .getValue(TestElement.GUI_CLASS);
087: if (clazz == null) {
088: log
089: .warn("getIcon(): Can't obtain GUI class from "
090: + testClass.getName());
091: return null;
092: }
093: return GUIFactory.getIcon(Class
094: .forName((String) clazz), enabled);
095: }
096: return new ImageIcon(img);
097: } catch (IntrospectionException e1) {
098: log.error("Can't obtain icon for class "
099: + testElement, e1);
100: throw new org.apache.jorphan.util.JMeterError(e1);
101: }
102: }
103: return GUIFactory.getIcon(Class.forName(testElement
104: .getPropertyAsString(TestElement.GUI_CLASS)),
105: enabled);
106: } catch (ClassNotFoundException e) {
107: log.warn("Can't get icon for class " + testElement, e);
108: return null;
109: }
110: }
111:
112: public Collection getMenuCategories() {
113: try {
114: return GuiPackage.getInstance().getGui(getTestElement())
115: .getMenuCategories();
116: } catch (Exception e) {
117: log.error("Can't get popup menu for gui", e);
118: return null;
119: }
120: }
121:
122: public JPopupMenu createPopupMenu() {
123: try {
124: return GuiPackage.getInstance().getGui(getTestElement())
125: .createPopupMenu();
126: } catch (Exception e) {
127: log.error("Can't get popup menu for gui", e);
128: return null;
129: }
130: }
131:
132: public TestElement getTestElement() {
133: return (TestElement) getUserObject();
134: }
135:
136: public String getStaticLabel() {
137: return GuiPackage.getInstance().getGui(
138: (TestElement) getUserObject()).getStaticLabel();
139: }
140:
141: public String getDocAnchor() {
142: return GuiPackage.getInstance().getGui(
143: (TestElement) getUserObject()).getDocAnchor();
144: }
145:
146: public void setName(String name) {
147: ((TestElement) getUserObject()).setProperty(new StringProperty(
148: TestElement.NAME, name));
149: }
150:
151: public String getName() {
152: return ((TestElement) getUserObject())
153: .getPropertyAsString(TestElement.NAME);
154: }
155:
156: public void nameChanged() {
157: treeModel.nodeChanged(this);
158: }
159: }
|