1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.gui;
39
40 import com.gargoylesoftware.base.util.DetailedIllegalArgumentException;
41 import com.gargoylesoftware.base.util.DetailedNullPointerException;
42 import java.awt.Component;
43 import javax.swing.JScrollPane;
44
45 /***
46 * A default implementation of ComponentLoader that uses reflection
47 * to instantiate components.
48 *
49 * @version $Revision: 1.5 $
50 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51 */
52 public class DefaultComponentLoader implements ComponentLoader {
53
54 private final Class clazz_;
55 private final boolean wrapInJScrollPane_;
56
57
58 /***
59 * Create a new loader.
60 * @param className The name of the class that will be used to create the new object.
61 * This class must have a public no-arg constructor and must be an instance of
62 * java.awt.Component.
63 * @throws ClassNotFoundException If the class cannot be found.
64 */
65 public DefaultComponentLoader( final String className ) throws ClassNotFoundException {
66 this( className == null ? null : Class.forName(className) );
67 }
68
69 /***
70 * Create a new loader.
71 *
72 * @param clazz The class that will be used to create the new object. This
73 * class must have a public no-arg constructor and must be an instance
74 * of java.awt.Component.
75 */
76 public DefaultComponentLoader( final Class clazz ) {
77 this( clazz, false );
78 }
79
80
81 /***
82 * Create a new loader.
83 *
84 * @param clazz The class that will be used to create the new object. This
85 * class must have a public no-arg constructor and must be an instance
86 * of java.awt.Component.
87 * @param wrapInJScrollPane If true then the returned component will be a
88 * JScrollPane which contains the loaded component.
89 */
90 public DefaultComponentLoader( final Class clazz, final boolean wrapInJScrollPane ) {
91 assertNotNull("clazz", clazz);
92 if( Component.class.isAssignableFrom(clazz) == false ) {
93 throw new DetailedIllegalArgumentException("clazz", clazz, "Not instance of Component");
94 }
95 try {
96 clazz.getConstructor( new Class[0] );
97 }
98 catch( final NoSuchMethodException e ) {
99 throw new DetailedIllegalArgumentException("clazz", clazz, "Does not have a public default constructor");
100 }
101 clazz_ = clazz;
102
103 wrapInJScrollPane_ = wrapInJScrollPane;
104 }
105
106
107 /***
108 * Load the component.
109 *
110 * @return the loaded component.
111 * @throws Exception If something went wrong during the creation of the component.
112 */
113 public Component loadComponent() throws Exception {
114 final Component component = (Component)clazz_.newInstance();
115 final Component result;
116 if( wrapInJScrollPane_ ) {
117 result = new JScrollPane(component);
118 }
119 else {
120 result = component;
121 }
122 return result;
123 }
124
125
126 /***
127 * Throw an Exception if the specified object is null.
128 *
129 * @param fieldName The name of the field that we are checking.
130 * @param object The value of the field that we are checking
131 */
132 protected final void assertNotNull( final String fieldName, final Object object ) {
133 if( object == null ) {
134 throw new DetailedNullPointerException(fieldName);
135 }
136 }
137 }
138