KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/GUIFactory.java,v 1.7 2004/02/13 02:21: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
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.swing.ImageIcon JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26
27 import org.apache.jmeter.testbeans.gui.TestBeanGUI;
28
29
30 /**
31  * Provides a way to register and retrieve GUI classes and icons.
32  *
33  * @author Oliver Rossmueller
34  * @version $Revision: 1.7 $
35  */

36 public final class GUIFactory
37 {
38     /** A Map from String to JComponent of registered GUI classes. */
39     private static final Map JavaDoc GUI_MAP = new HashMap JavaDoc();
40     
41     /** A Map from String to ImageIcon of registered icons. */
42     private static final Map JavaDoc ICON_MAP = new HashMap JavaDoc();
43
44     /**
45      * Prevent instantiation since this is a static utility class.
46      */

47     private GUIFactory ()
48     {
49     }
50     
51     /**
52      * Get an icon which has previously been registered for this class object.
53      *
54      * @param elementClass the class object which we want to get an icon for
55      *
56      * @return the associated icon, or null if this class or its superclass
57      * has not been registered
58      */

59     public static ImageIcon JavaDoc getIcon(Class JavaDoc elementClass)
60     {
61         String JavaDoc key = elementClass.getName();
62         ImageIcon JavaDoc icon = (ImageIcon JavaDoc) ICON_MAP.get(key);
63
64         if (icon != null)
65         {
66             return icon;
67         }
68         
69         if (elementClass.getSuperclass() != null)
70         {
71             return getIcon(elementClass.getSuperclass());
72         }
73         
74         return null;
75     }
76
77
78     /**
79      * Get a component instance which has previously been registered for this
80      * class object.
81      *
82      * @param elementClass the class object which we want to get an instance of
83      *
84      * @return an instance of the class, or null if this class or its superclass
85      * has not been registered
86      */

87     public static JComponent JavaDoc getGUI(Class JavaDoc elementClass)
88     {
89         // TODO: This method doesn't appear to be used.
90
String JavaDoc key = elementClass.getName();
91         JComponent JavaDoc gui = (JComponent JavaDoc) GUI_MAP.get(key);
92
93         if (gui != null)
94         {
95             return gui;
96         }
97
98         if (elementClass.getSuperclass() != null)
99         {
100             return getGUI(elementClass.getSuperclass());
101         }
102
103         return null;
104     }
105
106
107     /**
108      * Register an icon so that it can later be retrieved via
109      * {@link #getIcon(Class)}. The key should match the fully-qualified
110      * class name for the class used as the parameter when retrieving the
111      * icon.
112      *
113      * @param key the name which can be used to retrieve this icon later
114      * @param icon the icon to store
115      */

116     public static void registerIcon(String JavaDoc key, ImageIcon JavaDoc icon)
117     {
118         ICON_MAP.put(key, icon);
119     }
120
121
122     /**
123      * Register a GUI class so that it can later be retrieved via
124      * {@link #getGUI(Class)}. The key should match the fully-qualified
125      * class name for the class used as the parameter when retrieving the
126      * GUI.
127      *
128      * @param key the name which can be used to retrieve this GUI later
129      * @param guiClass the class object for the GUI component
130      * @param testClass the class of the objects edited by this GUI
131      *
132      * @throws InstantiationException if an instance of the GUI class can not
133      * be instantiated
134      * @throws IllegalAccessException if access rights do not permit an instance
135      * of the GUI class to be created
136      */

137     public static void registerGUI(String JavaDoc key, Class JavaDoc guiClass, Class JavaDoc testClass)
138             throws InstantiationException JavaDoc, IllegalAccessException JavaDoc
139     {
140         // TODO: This method doesn't appear to be used.
141
JMeterGUIComponent gui;
142         
143         if (guiClass == TestBeanGUI.class)
144         {
145             gui= new TestBeanGUI(testClass);
146         }
147         else
148         {
149             gui = (JMeterGUIComponent) guiClass.newInstance();
150         }
151         GUI_MAP.put(key, gui);
152     }
153 }
154
Popular Tags