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;
020:
021: import java.awt.BorderLayout;
022: import java.util.Collection;
023:
024: import javax.swing.JLabel;
025: import javax.swing.JPanel;
026: import javax.swing.JPopupMenu;
027: import javax.swing.JTextField;
028: import javax.swing.event.DocumentEvent;
029: import javax.swing.event.DocumentListener;
030: import javax.swing.tree.TreeNode;
031:
032: import org.apache.jmeter.gui.tree.NamedTreeNode;
033: import org.apache.jmeter.testelement.TestElement;
034: import org.apache.jmeter.testelement.WorkBench;
035: import org.apache.jmeter.testelement.property.StringProperty;
036: import org.apache.jmeter.util.JMeterUtils;
037: import org.apache.jmeter.util.LocaleChangeEvent;
038:
039: public class NamePanel extends JPanel implements JMeterGUIComponent {
040: /** A text field containing the name. */
041: private JTextField nameField = new JTextField(15);
042:
043: /** The label for the text field. */
044: private JLabel nameLabel;
045:
046: /** The node which this component is providing the name for. */
047: private TreeNode node;
048:
049: /**
050: * Create a new NamePanel with the default name.
051: */
052: public NamePanel() {
053: setName(getStaticLabel());
054: init();
055: }
056:
057: /**
058: * Initialize the GUI components and layout.
059: */
060: private void init() {
061: setLayout(new BorderLayout(5, 0));
062:
063: nameLabel = new JLabel(JMeterUtils.getResString("name")); // $NON-NLS-1$
064: nameLabel.setName("name");
065: nameLabel.setLabelFor(nameField);
066:
067: nameField.getDocument().addDocumentListener(
068: new DocumentListener() {
069: public void insertUpdate(DocumentEvent e) {
070: updateName(nameField.getText());
071: }
072:
073: public void removeUpdate(DocumentEvent e) {
074: updateName(nameField.getText());
075: }
076:
077: public void changedUpdate(DocumentEvent e) {
078: // not for text fields
079: }
080: });
081:
082: add(nameLabel, BorderLayout.WEST);
083: add(nameField, BorderLayout.CENTER);
084: }
085:
086: public void clearGui() {
087: setName(getStaticLabel());
088: }
089:
090: /**
091: * Get the currently displayed name.
092: *
093: * @return the current name
094: */
095: public String getName() {
096: if (nameField != null)
097: return nameField.getText();
098: else
099: return ""; // $NON-NLS-1$
100: }
101:
102: /**
103: * Set the name displayed in this component.
104: *
105: * @param name
106: * the name to display
107: */
108: public void setName(String name) {
109: super .setName(name);
110: nameField.setText(name);
111: }
112:
113: /**
114: * Get the tree node which this component provides the name for.
115: *
116: * @return the tree node corresponding to this component
117: */
118: protected TreeNode getNode() {
119: return node;
120: }
121:
122: /**
123: * Set the tree node which this component provides the name for.
124: *
125: * @param node
126: * the tree node corresponding to this component
127: */
128: public void setNode(TreeNode node) {
129: this .node = node;
130: }
131:
132: /* Implements JMeterGUIComponent.configure(TestElement) */
133: public void configure(TestElement testElement) {
134: setName(testElement.getPropertyAsString(TestElement.NAME));
135: }
136:
137: /* Implements JMeterGUIComponent.createPopupMenu() */
138: public JPopupMenu createPopupMenu() {
139: return null;
140: }
141:
142: /* Implements JMeterGUIComponent.getStaticLabel() */
143: public String getStaticLabel() {
144: return JMeterUtils.getResString(getLabelResource());
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.apache.jmeter.gui.JMeterGUIComponent#getLabelResource()
151: */
152: public String getLabelResource() {
153: return "root"; // $NON-NLS-1$
154: }
155:
156: /* Implements JMeterGUIComponent.getMenuCategories() */
157: public Collection getMenuCategories() {
158: return null;
159: }
160:
161: /* Implements JMeterGUIComponent.createTestElement() */
162: public TestElement createTestElement() {
163: WorkBench wb = new WorkBench();
164: modifyTestElement(wb);
165: return wb;
166: }
167:
168: /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
169: public void modifyTestElement(TestElement wb) {
170: wb.setProperty(new StringProperty(TestElement.NAME, getName()));
171: wb.setProperty(new StringProperty(TestElement.GUI_CLASS, this
172: .getClass().getName()));
173: wb.setProperty(new StringProperty(TestElement.TEST_CLASS,
174: WorkBench.class.getName()));
175: }
176:
177: /**
178: * Called when the name changes. The tree node which this component names
179: * will be notified of the change.
180: *
181: * @param newValue
182: * the new name
183: */
184: private void updateName(String newValue) {
185: if (getNode() != null) {
186: ((NamedTreeNode) getNode()).nameChanged();
187: }
188: }
189:
190: /**
191: * Called when the locale is changed so that the label can be updated. This
192: * method is not currently used.
193: *
194: * @param event
195: * the event to be handled
196: */
197: public void localeChanged(LocaleChangeEvent event) {
198: nameLabel
199: .setText(JMeterUtils.getResString(nameLabel.getName()));
200: }
201:
202: /*
203: * (non-Javadoc)
204: *
205: * @see org.apache.jmeter.gui.JMeterGUIComponent#getDocAnchor()
206: */
207: public String getDocAnchor() {
208: return null;
209: }
210: }
|