1 7 package javax.swing.text.html; 8 9 import java.util.Enumeration ; 10 import java.awt.*; 11 import javax.swing.*; 12 import javax.swing.text.*; 13 import java.beans.*; 14 import java.lang.reflect.*; 15 16 53 public class ObjectView extends ComponentView { 54 55 60 public ObjectView(Element elem) { 61 super(elem); 62 } 63 64 69 protected Component createComponent() { 70 AttributeSet attr = getElement().getAttributes(); 71 String classname = (String ) attr.getAttribute(HTML.Attribute.CLASSID); 72 try { 73 Class c = Class.forName(classname, true,Thread.currentThread(). 74 getContextClassLoader()); 75 Object o = c.newInstance(); 76 if (o instanceof Component) { 77 Component comp = (Component) o; 78 setParameters(comp, attr); 79 return comp; 80 } 81 } catch (Throwable e) { 82 } 85 86 return getUnloadableRepresentation(); 87 } 88 89 93 Component getUnloadableRepresentation() { 94 Component comp = new JLabel("??"); 97 comp.setForeground(Color.red); 98 return comp; 99 } 100 101 110 private Class getClass(String classname) throws ClassNotFoundException { 111 Class klass; 112 113 Class docClass = getDocument().getClass(); 114 ClassLoader loader = docClass.getClassLoader(); 115 if (loader != null) { 116 klass = loader.loadClass(classname); 117 } else { 118 klass = Class.forName(classname); 119 } 120 return klass; 121 } 122 123 128 private void setParameters(Component comp, AttributeSet attr) { 129 Class k = comp.getClass(); 130 BeanInfo bi; 131 try { 132 bi = Introspector.getBeanInfo(k); 133 } catch (IntrospectionException ex) { 134 System.err.println("introspector failed, ex: "+ex); 135 return; } 137 PropertyDescriptor props[] = bi.getPropertyDescriptors(); 138 for (int i=0; i < props.length; i++) { 139 Object v = attr.getAttribute(props[i].getName()); 141 if (v instanceof String ) { 142 String value = (String ) v; 144 Method writer = props[i].getWriteMethod(); 145 if (writer == null) { 146 return; } 149 Class [] params = writer.getParameterTypes(); 150 if (params.length != 1) { 151 return; } 154 String [] args = { value }; 155 try { 156 writer.invoke(comp, args); 157 } catch (Exception ex) { 158 System.err.println("Invocation failed"); 159 } 161 } 162 } 163 } 164 165 } 166 167 | Popular Tags |