KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/AbstractJMeterGuiComponent.java,v 1.31.2.1 2005/03/13 17:43:44 sebb Exp $
2
/*
3  * Copyright 2001-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.Component JavaDoc;
21 import java.awt.Container JavaDoc;
22 import java.awt.Font JavaDoc;
23
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JScrollPane JavaDoc;
28 import javax.swing.border.Border JavaDoc;
29
30 import org.apache.jmeter.gui.tree.JMeterTreeNode;
31 import org.apache.jmeter.gui.util.VerticalPanel;
32 import org.apache.jmeter.testelement.TestElement;
33 import org.apache.jmeter.testelement.property.BooleanProperty;
34 import org.apache.jmeter.testelement.property.NullProperty;
35 import org.apache.jmeter.testelement.property.StringProperty;
36 import org.apache.jmeter.util.JMeterUtils;
37 import org.apache.jorphan.logging.LoggingManager;
38 import org.apache.log.Logger;
39
40 /**
41  * This abstract class takes care of the most basic functions necessary to
42  * create a viable JMeter GUI component. It extends JPanel and implements
43  * JMeterGUIComponent. This abstract class is, in turn, extended by several
44  * other abstract classes that create different classes of GUI components for
45  * JMeter (Visualizers, Timers, Samplers, Modifiers, Controllers, etc).
46  *
47  * @see org.apache.jmeter.gui.JMeterGUIComponent
48  * @see org.apache.jmeter.config.gui.AbstractConfigGui
49  * @see org.apache.jmeter.assertions.gui.AbstractAssertionGui
50  * @see org.apache.jmeter.control.gui.AbstractControllerGui
51  * @see org.apache.jmeter.timers.gui.AbstractTimerGui
52  * @see org.apache.jmeter.visualizers.gui.AbstractVisualizer
53  * @see org.apache.jmeter.samplers.gui.AbstractSamplerGui
54  *
55  * @version $Revision: 1.31.2.1 $ on $Date: 2005/03/13 17:43:44 $
56  */

57 public abstract class AbstractJMeterGuiComponent
58     extends JPanel JavaDoc
59     implements JMeterGUIComponent
60 {
61     /** Logging */
62     private static Logger log = LoggingManager.getLoggerForClass();
63     
64     /** Flag indicating whether or not this component is enabled. */
65     private boolean enabled = true;
66     
67     /** The tree node which this component is associated with. */
68     private JMeterTreeNode node;
69
70     /** A GUI panel containing the name of this component. */
71     private NamePanel namePanel;
72     
73
74     /**
75      * When constructing a new component, this takes care of basic tasks like
76      * setting up the Name Panel and assigning the class's static label as
77      * the name to start.
78      */

79     public AbstractJMeterGuiComponent()
80     {
81         namePanel = new NamePanel();
82         setName(getStaticLabel());
83     }
84
85     /**
86      * Provides a default implementation for the name property. It's unlikely
87      * developers will need to override.
88      */

89     public void setName(String JavaDoc name)
90     {
91         namePanel.setName(name);
92     }
93
94     /**
95      * Provides a default implementation for the enabled property. It's
96      * unlikely developers will need to override.
97      */

98     public boolean isEnabled()
99     {
100         return enabled;
101     }
102
103     /**
104      * Provides a default implementation for the enabled property. It's
105      * unlikely developers will need to override.
106      */

107     public void setEnabled(boolean e)
108     {
109         log.debug("Setting enabled: " + e);
110         enabled = e;
111     }
112
113     /**
114      * Provides a default implementation for the name property. It's unlikely
115      * developers will need to override.
116      */

117     public String JavaDoc getName()
118     {
119         if (getNamePanel() != null) {
120             return getNamePanel().getName();
121         }
122         else return "";
123     }
124
125     /**
126      * Provides the Name Panel for extending classes. Extending classes are
127      * free to place it as desired within the component, or not at all. Most
128      * components place the NamePanel automatically by calling
129      * {@link #makeTitlePanel()} instead of directly calling this method.
130      *
131      * @return a NamePanel containing the name of this component
132      */

133     protected NamePanel getNamePanel()
134     {
135         return namePanel;
136     }
137
138     /**
139      * Provides a label containing the title for the component. Subclasses
140      * typically place this label at the top of their GUI. The title is set
141      * to the name returned from the component's
142      * {@link JMeterGUIComponent#getStaticLabel() getStaticLabel()} method.
143      * Most components place this label automatically by calling
144      * {@link #makeTitlePanel()} instead of directly calling this method.
145      *
146      * @return a JLabel which subclasses can add to their GUI
147      */

148     protected Component JavaDoc createTitleLabel()
149     {
150         JLabel JavaDoc titleLabel = new JLabel JavaDoc(getStaticLabel());
151         Font JavaDoc curFont = titleLabel.getFont();
152         titleLabel.setFont(curFont.deriveFont((float) curFont.getSize() + 4));
153         return titleLabel;
154     }
155
156     /**
157      * A newly created gui component can be initialized with the contents of
158      * a Test Element object by calling this method. The component is
159      * responsible for querying the Test Element object for the
160      * relevant information to display in its GUI.
161      * <p>
162      * AbstractJMeterGuiComponent provides a partial implementation of this
163      * method, setting the name of the component and its enabled status.
164      * Subclasses should override this method, performing their own
165      * configuration as needed, but also calling this super-implementation.
166      *
167      * @param element the TestElement to configure
168      */

169     public void configure(TestElement element)
170     {
171         setName(element.getPropertyAsString(TestElement.NAME));
172         if (element.getProperty(TestElement.ENABLED) instanceof NullProperty)
173         {
174             enabled = true;
175         }
176         else
177         {
178             enabled = element.getPropertyAsBoolean(TestElement.ENABLED);
179         }
180     }
181     
182     /**
183      * Provides a default implementat that resets the name field to the value
184      * of getStaticLabel(), and sets enabled to true. Your GUI may need more
185      * things cleared, in which case you should override, clear the extra
186      * fields, and still call super.clear().
187      */

188     public void clear()
189     {
190         setName(getStaticLabel());
191         enabled = true;
192     }
193
194     /**
195      * This provides a convenience for extenders when they implement the
196      * {@link JMeterGUIComponent#modifyTestElement(TestElement)} method. This
197      * method will set the name, gui class, and test class for the created Test
198      * Element. It should be called by every extending class when
199      * creating/modifying Test Elements, as that will best assure consistent
200      * behavior.
201      *
202      * @param mc the TestElement being created.
203      */

204     protected void configureTestElement(TestElement mc)
205     {
206         mc.setProperty(new StringProperty(TestElement.NAME, getName()));
207
208         mc.setProperty(
209             new StringProperty(
210                 TestElement.GUI_CLASS,
211                 this.getClass().getName()));
212
213         mc.setProperty(
214             new StringProperty(
215                 TestElement.TEST_CLASS,
216                 mc.getClass().getName()));
217
218         //This stores the state of the TestElement
219
log.debug("setting element to enabled: " + enabled);
220         mc.setProperty(new BooleanProperty(TestElement.ENABLED, enabled));
221     }
222
223     /**
224      * Provides a default implementation for the node property. It is unlikely
225      * developers would need to override this method.
226      */

227     public void setNode(JMeterTreeNode node)
228     {
229         this.node = node;
230         getNamePanel().setNode(node);
231     }
232
233     /**
234      * Provides a default implementation for the node property. It is unlikely
235      * developers would need to override this method.
236      */

237     protected JMeterTreeNode getNode()
238     {
239         return node;
240     }
241     
242     /**
243      * Create a standard title section for JMeter components. This includes
244      * the title for the component and the Name Panel allowing the user to
245      * change the name for the component. This method is typically added to
246      * the top of the component at the beginning of the component's init method.
247      *
248      * @return a panel containing the component title and name panel
249      */

250     protected Container JavaDoc makeTitlePanel()
251     {
252         VerticalPanel titlePanel = new VerticalPanel();
253         titlePanel.add(createTitleLabel());
254         titlePanel.add(getNamePanel());
255         return titlePanel;
256     }
257     
258     /**
259      * Create a top-level Border which can be added to JMeter components.
260      * Components typically set this as their border in their init method. It
261      * simply provides a nice spacing between the GUI components used and the
262      * edges of the window in which they appear.
263      *
264      * @return a Border for JMeter components
265      */

266     protected Border JavaDoc makeBorder()
267     {
268         return BorderFactory.createEmptyBorder(10, 10, 5, 10);
269     }
270
271     /**
272      * Create a scroll panel that sets it's preferred size to it's minimum
273      * size. Explicitly for scroll panes that live inside other scroll panes,
274      * or within containers that stretch components to fill the area they exist
275      * in. Use this for any component you would put in a scroll pane (such as
276      * TextAreas, tables, JLists, etc). It is here for convenience and to
277      * avoid duplicate code. JMeter displays best if you follow this custom.
278      *
279      * @param comp the component which should be placed inside the scroll pane
280      * @return a JScrollPane containing the specified component
281      */

282     protected JScrollPane JavaDoc makeScrollPane(Component JavaDoc comp)
283     {
284         JScrollPane JavaDoc pane = new JScrollPane JavaDoc(comp);
285         pane.setPreferredSize(pane.getMinimumSize());
286         return pane;
287     }
288
289     /**
290      * Create a scroll panel that sets it's preferred size to it's minimum
291      * size. Explicitly for scroll panes that live inside other scroll panes,
292      * or within containers that stretch components to fill the area they exist
293      * in. Use this for any component you would put in a scroll pane (such as
294      * TextAreas, tables, JLists, etc). It is here for convenience and to
295      * avoid duplicate code. JMeter displays best if you follow this custom.
296      *
297      * @see javax.swing.ScrollPaneConstants
298      *
299      * @param comp the component which should be placed inside the scroll pane
300      * @param verticalPolicy the vertical scroll policy
301      * @param horizontalPolicy the horizontal scroll policy
302      * @return a JScrollPane containing the specified component
303      */

304     protected JScrollPane JavaDoc makeScrollPane(
305         Component JavaDoc comp,
306         int verticalPolicy,
307         int horizontalPolicy)
308     {
309         JScrollPane JavaDoc pane =
310             new JScrollPane JavaDoc(comp, verticalPolicy, horizontalPolicy);
311         pane.setPreferredSize(pane.getMinimumSize());
312         return pane;
313     }
314         
315     public String JavaDoc getStaticLabel()
316     {
317         return JMeterUtils.getResString(getLabelResource());
318     }
319
320     public String JavaDoc getDocAnchor()
321     {
322         return getStaticLabel().replace(' ', '_');
323     }
324     
325 // /*
326
// * Dummy implementation so existing code still compiles.
327
// * Throws an error because it should not be invoked - and cannot provide a useful value.
328
// *
329
// * The target class should either implement getStaticLabel(), as before, or it
330
// * should implement getLabelResource()
331
// *
332
// * DONE: remove eventually
333
// */
334
// public String getLabelResource()
335
// {
336
// throw new UnsupportedOperationException("Needs to be implemented by the class: "
337
// +this.getClass().getName());
338
// }
339
}
340
Popular Tags