1 19 20 package org.netbeans.modules.welcome.content; 21 22 import java.awt.Component ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import javax.swing.JLabel ; 26 import org.openide.ErrorManager; 27 import org.openide.util.Lookup; 28 29 33 class DefaultComponentDescriptor extends ComponentDescriptor { 34 35 36 public DefaultComponentDescriptor( String id ) { 37 super( id ); 38 } 39 40 public Component construct() { 41 try { 42 String clz = getClassName(); 43 if (clz != null) { 44 return (Component )loadClass(clz).newInstance(); 45 } 46 else { 47 return newFromFactory(getMethod()); 48 } 49 50 } catch( Exception e ) { 51 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e ); 52 } 53 return new JLabel ( "<html>Cannot create component from <br>" + getClassName() ); } 55 56 private Class loadClass(final String clz) throws ClassNotFoundException { 57 ClassLoader loader = (ClassLoader )Lookup.getDefault().lookup( ClassLoader .class ); 58 if( null == loader ) 59 loader = ClassLoader.getSystemClassLoader(); 60 61 Class clazz = Class.forName( clz, true, loader ); 62 return clazz; 63 } 64 65 private Component newFromFactory (String methodName) 66 throws ClassNotFoundException , NoSuchMethodException , 67 IllegalAccessException , InvocationTargetException { 68 assert methodName != null: "neither class name nor factory method is specified in "+this; 69 int idx = methodName.lastIndexOf('.'); 70 Class clz = loadClass( methodName.substring(0, idx)); 71 Method m = clz.getMethod(methodName.substring(idx+1), new Class [] {}); 72 Object obj = m.invoke(null, new Object [] {}); 73 return (Component )obj; 74 } 75 } 76 | Popular Tags |