1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * ident	"%Z%%M%	%I%	%E% SMI"
24 *
25 * Copyright (c) 2000 by Sun Microsystems, Inc.
26 * All rights reserved.
27 */
28
29/*
30 *        Copyright (C) 1996  Active Software, Inc.
31 *                  All rights reserved.
32 *
33 * @(#) GenericComponentShadow.java 1.14 - last change made 07/25/97
34 */
35
36package sunsoft.jws.visual.rt.shadow;
37
38import sunsoft.jws.visual.rt.shadow.java.awt.CanvasShadow;
39import sunsoft.jws.visual.rt.awt.GBPanel;
40import sunsoft.jws.visual.rt.base.VJException;
41import sunsoft.jws.visual.rt.base.Global;
42
43import java.awt.Button;
44import java.awt.Component;
45
46/**
47         * Wraps an AWT widget.  The attributes available for this
48         * class are listed below.  In the type column, type names beginning
49         * with "sunsoft.jws.visual.rt" have been abbreviated to begin with
50	 * "rt".
51         *
52         * < pre>
53        name            type                      default value
54        -----------------------------------------------------------------------
55        class           java.lang.String          java.awt.Button
56        *  < /pre>
57        *
58        * class: the java class(that must be a sub-class of
59        * java.awt.Component and have a null constructor) of a user-written
60        * AWT class that there is no wrapper shadow class for yet.
61        * GenericComponentShadow is useful for quickly incorporating a user's
62        * existing custom AWT components in a Visual Java GUI.
63        *  < p>
64        * Check the super class for additional attributes.
65        *
66        * @see Component
67        * @see GenericWindowShadow
68        * @version 1.14, 07/25/97
69        */
70public class GenericComponentShadow extends CanvasShadow {
71    private String className;
72    private Class genericClass;
73
74    public GenericComponentShadow() {
75	attributes.add(/* NOI18N */"class",
76		       /* NOI18N */"java.lang.String",
77		       /* NOI18N */"java.awt.Button", NOEDITOR);
78    }
79
80    protected Object getOnBody(String key) {
81	if (key.equals(/* NOI18N */"class"))
82	    return getFromTable(/* NOI18N */"class");
83	else
84	    return super.getOnBody(key);
85    }
86
87    protected void setOnBody(String key, Object value) {
88	if (key.equals(/* NOI18N */"class")) {
89	    // Don't create a new instance unless the
90	    // class name has changed
91	    if (className.equals((String)value))
92		return;
93
94	    Object obj = loadClass((String)value);
95	    destroy();
96	    body = obj;
97	    create();
98	}
99	else
100	    super.setOnBody(key, value);
101    }
102
103    public void createBody() {
104	body = loadClass((String)get(/* NOI18N */"class"));
105    }
106
107    private Object loadClass(String name) {
108	Class c;
109	Object obj;
110
111	// Load the class if the name doesn't match the previous name
112	if (!name.equals(className)) {
113	    try {
114		c = Class.forName(name);
115	    }
116	    catch (ClassNotFoundException ex) {
117		throw new VJException(Global.fmtMsg(
118/* JSTYLED */
119		    "sunsoft.jws.visual.rt.shadow.GenericComponentShadow.FMT.1",
120		    Global.getMsg(
121/* JSTYLED */
122			"sunsoft.jws.visual.rt.shadow.GenericComponentShadow.Class__not__found"),
123		    name));
124	    }
125	} else {
126	    c = genericClass;
127	}
128
129	// Create a new instance from the class
130	try {
131	    obj = c.newInstance();
132	    if (!(obj instanceof Component)) {
133		throw new VJException(
134/* JSTYLED */
135		    Global.fmtMsg("sunsoft.jws.visual.rt.shadow.GenericComponentShadow.NotAComponentSubclass", name));
136	    }
137	}
138	catch (IllegalAccessException ex) {
139	    throw new VJException(
140/* JSTYLED */
141		Global.fmtMsg("sunsoft.jws.visual.rt.shadow.GenericComponentShadow.IllegalAccess", name));
142	}
143	catch (InstantiationException ex) {
144	    throw new VJException(
145/* JSTYLED */
146		Global.fmtMsg("sunsoft.jws.visual.rt.shadow.GenericComponentShadow.InstantiationException", name));
147	}
148	catch (NoSuchMethodError ex) {
149	    throw new VJException(
150/* JSTYLED */
151		Global.fmtMsg("sunsoft.jws.visual.rt.shadow.GenericComponentShadow.Noconstructor", name));
152	}
153
154	// No errors occurred, so update the name and class variables.
155	genericClass = c;
156	className = name;
157
158	// Set the runtime flag for GBPanel instances
159	if ((obj instanceof GBPanel) && inDesignerRoot())
160	    ((GBPanel)obj).setRuntime(false);
161
162	return obj;
163    }
164}
165