KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > NamePanel


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/NamePanel.java,v 1.16 2004/03/05 13:18:37 sebb Exp $
2
/*
3  * Copyright 2002-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * 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;
20 import java.awt.BorderLayout JavaDoc;
21 import java.util.Collection JavaDoc;
22
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JPopupMenu JavaDoc;
26 import javax.swing.JTextField JavaDoc;
27 import javax.swing.event.DocumentEvent JavaDoc;
28 import javax.swing.event.DocumentListener JavaDoc;
29
30 import org.apache.jmeter.gui.tree.JMeterTreeNode;
31 import org.apache.jmeter.testelement.TestElement;
32 import org.apache.jmeter.testelement.WorkBench;
33 import org.apache.jmeter.testelement.property.StringProperty;
34 import org.apache.jmeter.util.JMeterUtils;
35 import org.apache.jmeter.util.LocaleChangeEvent;
36 /**
37  *
38  *@version $Revision: 1.16 $ Last updated: $Date: 2004/03/05 13:18:37 $
39 */

40
41 public class NamePanel extends JPanel JavaDoc implements JMeterGUIComponent
42 {
43     /** A text field containing the name. */
44     private JTextField JavaDoc nameField = new JTextField JavaDoc(15);
45     
46     /** The label for the text field. */
47     private JLabel JavaDoc nameLabel;
48     
49     /** The node which this component is providing the name for. */
50     private JMeterTreeNode node;
51
52     /**
53      * Create a new NamePanel with the default name.
54      */

55     public NamePanel()
56     {
57         setName(getStaticLabel());
58         init();
59     }
60
61     /**
62      * Initialize the GUI components and layout.
63      */

64     private void init()
65     {
66         setLayout(new BorderLayout JavaDoc(5, 0));
67
68         nameLabel = new JLabel JavaDoc(JMeterUtils.getResString("name"));
69         nameLabel.setName("name");
70         nameLabel.setLabelFor(nameField);
71
72         nameField.getDocument().addDocumentListener(new DocumentListener JavaDoc()
73         {
74             public void insertUpdate(DocumentEvent JavaDoc e)
75             {
76                 updateName(nameField.getText());
77             }
78
79             public void removeUpdate(DocumentEvent JavaDoc e)
80             {
81                 updateName(nameField.getText());
82             }
83
84             public void changedUpdate(DocumentEvent JavaDoc e)
85             {
86                 // not for text fields
87
}
88         });
89
90         add(nameLabel, BorderLayout.WEST);
91         add(nameField, BorderLayout.CENTER);
92     }
93     
94     public void clear()
95     {
96         setName(getStaticLabel());
97     }
98
99
100     /**
101      * Get the currently displayed name.
102      *
103      * @return the current name
104      */

105     public String JavaDoc getName()
106     {
107         if (nameField != null) return nameField.getText();
108         else return "";
109     }
110
111     /**
112      * Set the name displayed in this component.
113      *
114      * @param name the name to display
115      */

116     public void setName(String JavaDoc name)
117     {
118         super.setName(name);
119         nameField.setText(name);
120     }
121
122     /**
123      * Get the tree node which this component provides the name for.
124      *
125      * @return the tree node corresponding to this component
126      */

127     protected JMeterTreeNode getNode()
128     {
129         return node;
130     }
131
132     /**
133      * Set the tree node which this component provides the name for.
134      *
135      * @param node the tree node corresponding to this component
136      */

137     public void setNode(JMeterTreeNode node)
138     {
139         this.node = node;
140     }
141
142     /* Implements JMeterGUIComponent.configure(TestElement) */
143     public void configure(TestElement testElement)
144     {
145         setName(testElement.getPropertyAsString(TestElement.NAME));
146     }
147
148     /* Implements JMeterGUIComponent.createPopupMenu() */
149     public JPopupMenu JavaDoc createPopupMenu()
150     {
151         return null;
152     }
153
154     /* Implements JMeterGUIComponent.getStaticLabel() */
155     public String JavaDoc getStaticLabel()
156     {
157         return JMeterUtils.getResString(getLabelResource());
158     }
159
160     /* (non-Javadoc)
161      * @see org.apache.jmeter.gui.JMeterGUIComponent#getLabelResource()
162      */

163     public String JavaDoc getLabelResource() {
164         return "root";
165     }
166
167     /* Implements JMeterGUIComponent.getMenuCategories() */
168     public Collection JavaDoc getMenuCategories()
169     {
170         return null;
171     }
172
173     /* Implements JMeterGUIComponent.createTestElement() */
174     public TestElement createTestElement()
175     {
176         WorkBench wb = new WorkBench();
177         modifyTestElement(wb);
178         return wb;
179     }
180
181     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
182     public void modifyTestElement(TestElement wb)
183     {
184         wb.setProperty(new StringProperty(TestElement.NAME, getName()));
185         wb.setProperty(
186             new StringProperty(
187                 TestElement.GUI_CLASS,
188                 this.getClass().getName()));
189         wb.setProperty(
190             new StringProperty(
191                 TestElement.TEST_CLASS,
192                 WorkBench.class.getName()));
193     }
194
195     /**
196      * Called when the name changes. The tree node which this component names
197      * will be notified of the change.
198      *
199      * @param newValue the new name
200      */

201     private void updateName(String JavaDoc newValue)
202     {
203         if (getNode() != null)
204         {
205             getNode().nameChanged();
206         }
207     }
208
209     /**
210      * Called when the locale is changed so that the label can be updated.
211      * This method is not currently used.
212      *
213      * @param event the event to be handled
214      */

215     public void localeChanged(LocaleChangeEvent event)
216     {
217         nameLabel.setText(JMeterUtils.getResString(nameLabel.getName()));
218     }
219
220     /* (non-Javadoc)
221      * @see org.apache.jmeter.gui.JMeterGUIComponent#getDocAnchor()
222      */

223     public String JavaDoc getDocAnchor() {
224         // TODO Auto-generated method stub
225
return null;
226     }
227 }
228
Popular Tags