|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultComponentLoader.java | 75% | 66.7% | 80% | 71% |
|
1 | /* | |
2 | * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions are met: | |
6 | * | |
7 | * 1. Redistributions of source code must retain the above copyright notice, | |
8 | * this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
10 | * this list of conditions and the following disclaimer in the documentation | |
11 | * and/or other materials provided with the distribution. | |
12 | * 3. The end-user documentation included with the redistribution, if any, must | |
13 | * include the following acknowledgment: | |
14 | * | |
15 | * "This product includes software developed by Gargoyle Software Inc. | |
16 | * (http://www.GargoyleSoftware.com/)." | |
17 | * | |
18 | * Alternately, this acknowledgment may appear in the software itself, if | |
19 | * and wherever such third-party acknowledgments normally appear. | |
20 | * 4. The name "Gargoyle Software" must not be used to endorse or promote | |
21 | * products derived from this software without prior written permission. | |
22 | * For written permission, please contact info@GargoyleSoftware.com. | |
23 | * 5. Products derived from this software may not be called "GSBase", nor may | |
24 | * "GSBase" appear in their name, without prior written permission of | |
25 | * Gargoyle Software Inc. | |
26 | * | |
27 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
28 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
29 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE | |
30 | * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
33 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
36 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
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 | 1 | public DefaultComponentLoader( final String className ) throws ClassNotFoundException { |
66 | 1 | 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 | 1 | public DefaultComponentLoader( final Class clazz ) { |
77 | 1 | 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 | 6 | public DefaultComponentLoader( final Class clazz, final boolean wrapInJScrollPane ) { |
91 | 6 | assertNotNull("clazz", clazz); |
92 | 4 | if( Component.class.isAssignableFrom(clazz) == false ) { |
93 | 1 | throw new DetailedIllegalArgumentException("clazz", clazz, "Not instance of Component"); |
94 | } | |
95 | 3 | try { |
96 | 3 | clazz.getConstructor( new Class[0] ); |
97 | } | |
98 | catch( final NoSuchMethodException e ) { | |
99 | 2 | throw new DetailedIllegalArgumentException("clazz", clazz, "Does not have a public default constructor"); |
100 | } | |
101 | 1 | clazz_ = clazz; |
102 | ||
103 | 1 | 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 | 0 | public Component loadComponent() throws Exception { |
114 | 0 | final Component component = (Component)clazz_.newInstance(); |
115 | 0 | final Component result; |
116 | 0 | if( wrapInJScrollPane_ ) { |
117 | 0 | result = new JScrollPane(component); |
118 | } | |
119 | else { | |
120 | 0 | result = component; |
121 | } | |
122 | 0 | 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 | 6 | protected final void assertNotNull( final String fieldName, final Object object ) { |
133 | 6 | if( object == null ) { |
134 | 2 | throw new DetailedNullPointerException(fieldName); |
135 | } | |
136 | } | |
137 | } | |
138 |
|