KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > welcome > content > DefaultComponentDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.welcome.content;
21
22 import java.awt.Component JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import org.openide.ErrorManager;
27 import org.openide.util.Lookup;
28
29 /**
30  *
31  * @author S. Aubrecht
32  */

33 class DefaultComponentDescriptor extends ComponentDescriptor {
34
35     /** Creates a new instance of DefaultComponentInfo */
36     public DefaultComponentDescriptor( String JavaDoc id ) {
37         super( id );
38     }
39
40     public Component JavaDoc construct() {
41         try {
42             String JavaDoc clz = getClassName();
43             if (clz != null) {
44                 return (Component JavaDoc)loadClass(clz).newInstance();
45             }
46             else {
47                 return newFromFactory(getMethod());
48             }
49             
50         } catch( Exception JavaDoc e ) {
51             ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e );
52         }
53         return new JLabel JavaDoc( "<html>Cannot create component from <br>" + getClassName() ); // NOI18N
54
}
55     
56     private Class JavaDoc loadClass(final String JavaDoc clz) throws ClassNotFoundException JavaDoc {
57         ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup( ClassLoader JavaDoc.class );
58         if( null == loader )
59             loader = ClassLoader.getSystemClassLoader();
60         
61         Class JavaDoc clazz = Class.forName( clz, true, loader );
62         return clazz;
63     }
64     
65     private Component JavaDoc newFromFactory (String JavaDoc methodName)
66             throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
67             IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
68         assert methodName != null: "neither class name nor factory method is specified in "+this;
69         int idx = methodName.lastIndexOf('.');
70         Class JavaDoc clz = loadClass( methodName.substring(0, idx));
71         Method JavaDoc m = clz.getMethod(methodName.substring(idx+1), new Class JavaDoc[] {});
72         Object JavaDoc obj = m.invoke(null, new Object JavaDoc [] {});
73         return (Component JavaDoc)obj;
74     }
75 }
76
Popular Tags