1 7 package org.ejtools.adwt.util; 8 9 import java.awt.Component ; 10 import java.beans.BeanInfo ; 11 import java.beans.Introspector ; 12 import java.beans.beancontext.BeanContextChildComponentProxy ; 13 import java.beans.beancontext.BeanContextContainerProxy ; 14 import java.lang.reflect.Method ; 15 16 import javax.swing.Icon ; 17 import javax.swing.ImageIcon ; 18 import javax.swing.JLabel ; 19 20 24 public class ObjectWrapper implements BeanContextChildComponentProxy 25 { 26 27 private Icon icon; 28 29 private Object object; 30 31 private String text; 32 33 34 39 public ObjectWrapper(Object object) 40 { 41 this.object = object; 42 try 43 { 44 BeanInfo beaninfo = Introspector.getBeanInfo(this.object.getClass()); 45 if (beaninfo.getIcon(BeanInfo.ICON_COLOR_16x16) != null) 46 { 47 this.icon = new ImageIcon (beaninfo.getIcon(BeanInfo.ICON_COLOR_16x16)); 48 } 49 50 Method method = this.object.getClass().getMethod("toString", new Class [0]); 51 if (method.getDeclaringClass().equals(Object .class)) 52 { 53 this.text = beaninfo.getBeanDescriptor().getDisplayName(); 54 } 55 } 56 catch (Exception exception) 57 { 58 exception.printStackTrace(); 59 } 60 } 61 62 63 68 public Component getComponent() 69 { 70 if (this.object instanceof Component ) 71 { 72 return (Component ) this.object; 73 } 74 if (this.object instanceof BeanContextContainerProxy ) 75 { 76 return ((BeanContextContainerProxy ) this.object).getContainer(); 77 } 78 if (this.object instanceof BeanContextChildComponentProxy ) 79 { 80 return ((BeanContextChildComponentProxy ) this.object).getComponent(); 81 } 82 else 83 { 84 return new JLabel ("No GUI available for this service", 0); 85 } 86 } 87 88 89 94 public Icon getIcon() 95 { 96 return this.icon; 97 } 98 99 100 105 public Object getUserObject() 106 { 107 return this.object; 108 } 109 110 111 116 public String toString() 117 { 118 return this.text != null ? this.text : this.object.toString(); 119 } 120 } 121 | Popular Tags |