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.util.HashMap;
022: import java.util.Map;
023:
024: import javax.swing.ImageIcon;
025: import javax.swing.JComponent;
026:
027: import org.apache.jmeter.testbeans.gui.TestBeanGUI;
028:
029: /**
030: * Provides a way to register and retrieve GUI classes and icons.
031: *
032: * @author Oliver Rossmueller
033: * @version $Revision: 493779 $
034: */
035: public final class GUIFactory {
036: /** A Map from String to JComponent of registered GUI classes. */
037: private static final Map GUI_MAP = new HashMap();
038:
039: /** A Map from String to ImageIcon of registered icons. */
040: private static final Map ICON_MAP = new HashMap();
041:
042: /** A Map from String to ImageIcon of registered icons. */
043: private static final Map DISABLED_ICON_MAP = new HashMap();
044:
045: /**
046: * Prevent instantiation since this is a static utility class.
047: */
048: private GUIFactory() {
049: }
050:
051: /**
052: * Get an icon which has previously been registered for this class object.
053: *
054: * @param elementClass
055: * the class object which we want to get an icon for
056: *
057: * @return the associated icon, or null if this class or its superclass has
058: * not been registered
059: */
060: public static ImageIcon getIcon(Class elementClass) {
061: return getIcon(elementClass, true);
062:
063: }
064:
065: /**
066: * Get icon/disabledicon which has previously been registered for this class
067: * object.
068: *
069: * @param elementClass
070: * the class object which we want to get an icon for
071: * @param enabled -
072: * is icon enabled
073: *
074: * @return the associated icon, or null if this class or its superclass has
075: * not been registered
076: */
077: public static ImageIcon getIcon(Class elementClass, boolean enabled) {
078: String key = elementClass.getName();
079: ImageIcon icon = (ImageIcon) (enabled ? ICON_MAP.get(key)
080: : DISABLED_ICON_MAP.get(key));
081:
082: if (icon != null) {
083: return icon;
084: }
085:
086: if (elementClass.getSuperclass() != null) {
087: return getIcon(elementClass.getSuperclass(), enabled);
088: }
089:
090: return null;
091: }
092:
093: /**
094: * Get a component instance which has previously been registered for this
095: * class object.
096: *
097: * @param elementClass
098: * the class object which we want to get an instance of
099: *
100: * @return an instance of the class, or null if this class or its superclass
101: * has not been registered
102: */
103: public static JComponent getGUI(Class elementClass) {
104: // TODO: This method doesn't appear to be used.
105: String key = elementClass.getName();
106: JComponent gui = (JComponent) GUI_MAP.get(key);
107:
108: if (gui != null) {
109: return gui;
110: }
111:
112: if (elementClass.getSuperclass() != null) {
113: return getGUI(elementClass.getSuperclass());
114: }
115:
116: return null;
117: }
118:
119: /**
120: * Register an icon so that it can later be retrieved via
121: * {@link #getIcon(Class)}. The key should match the fully-qualified class
122: * name for the class used as the parameter when retrieving the icon.
123: *
124: * @param key
125: * the name which can be used to retrieve this icon later
126: * @param icon
127: * the icon to store
128: */
129: public static void registerIcon(String key, ImageIcon icon) {
130: ICON_MAP.put(key, icon);
131: }
132:
133: /**
134: * Register an icon so that it can later be retrieved via
135: * {@link #getIcon(Class)}. The key should match the fully-qualified class
136: * name for the class used as the parameter when retrieving the icon.
137: *
138: * @param key
139: * the name which can be used to retrieve this icon later
140: * @param icon
141: * the icon to store
142: */
143: public static void registerDisabledIcon(String key, ImageIcon icon) {
144: DISABLED_ICON_MAP.put(key, icon);
145: }
146:
147: /**
148: * Register a GUI class so that it can later be retrieved via
149: * {@link #getGUI(Class)}. The key should match the fully-qualified class
150: * name for the class used as the parameter when retrieving the GUI.
151: *
152: * @param key
153: * the name which can be used to retrieve this GUI later
154: * @param guiClass
155: * the class object for the GUI component
156: * @param testClass
157: * the class of the objects edited by this GUI
158: *
159: * @throws InstantiationException
160: * if an instance of the GUI class can not be instantiated
161: * @throws IllegalAccessException
162: * if access rights do not permit an instance of the GUI class
163: * to be created
164: */
165: public static void registerGUI(String key, Class guiClass,
166: Class testClass) throws InstantiationException,
167: IllegalAccessException {
168: // TODO: This method doesn't appear to be used.
169: JMeterGUIComponent gui;
170:
171: if (guiClass == TestBeanGUI.class) {
172: gui = new TestBeanGUI(testClass);
173: } else {
174: gui = (JMeterGUIComponent) guiClass.newInstance();
175: }
176: GUI_MAP.put(key, gui);
177: }
178: }
|