1 7 package org.ejtools.adwt; 8 9 import java.awt.Component ; 10 import java.beans.BeanInfo ; 11 import java.beans.Introspector ; 12 import java.beans.beancontext.BeanContext ; 13 import java.beans.beancontext.BeanContextChildComponentProxy ; 14 import java.beans.beancontext.BeanContextContainerProxy ; 15 import java.beans.beancontext.BeanContextMembershipEvent ; 16 import java.beans.beancontext.BeanContextMembershipListener ; 17 import java.lang.reflect.Method ; 18 import java.util.Iterator ; 19 import java.util.Vector ; 20 21 import javax.swing.DefaultListCellRenderer ; 22 import javax.swing.ImageIcon ; 23 import javax.swing.JLabel ; 24 import javax.swing.JList ; 25 import javax.swing.ListModel ; 26 import javax.swing.event.ListDataEvent ; 27 import javax.swing.event.ListDataListener ; 28 29 35 public class BeanContextListView extends JList 36 { 37 42 public BeanContextListView(BeanContext beancontext) 43 { 44 this.setModel(new BeanContextListModel(beancontext)); 45 this.setCellRenderer( 46 new DefaultListCellRenderer () 47 { 48 public Component getListCellRendererComponent(JList list, Object obj, int index, boolean isSelected, boolean cellHasFocus) 49 { 50 try 51 { 52 ContextElement contextelement = (ContextElement) obj; 53 Object obj1 = contextelement.getUserObject(); 54 super.getListCellRendererComponent(list, obj1, index, isSelected, cellHasFocus); 55 if (contextelement.icon != null) 56 { 57 setIcon(contextelement.icon); 58 } 59 return this; 60 } 61 catch (Exception _ex) 62 { 63 System.err.println(_ex.getMessage()); 64 return super.getListCellRendererComponent(list, obj, index, isSelected, cellHasFocus); 65 } 66 } 67 }); 68 69 } 70 71 72 78 protected class BeanContextListModel implements ListModel , BeanContextMembershipListener 79 { 80 81 protected Vector content = new Vector (); 82 83 protected BeanContext context; 84 85 protected Vector listeners = new Vector (); 86 87 88 93 public BeanContextListModel(BeanContext beancontext) 94 { 95 this.context = beancontext; 96 this.context.addBeanContextMembershipListener(this); 97 this.updateModel(); 98 } 99 100 101 106 public void addListDataListener(ListDataListener l) 107 { 108 this.listeners.add(l); 109 } 110 111 112 117 public void childrenAdded(BeanContextMembershipEvent beancontextmembershipevent) 118 { 119 this.updateModel(); 120 this.notifyListener(); 121 } 122 123 124 129 public void childrenRemoved(BeanContextMembershipEvent beancontextmembershipevent) 130 { 131 this.updateModel(); 132 this.notifyListener(); 133 } 134 135 136 142 public Object getElementAt(int index) 143 { 144 return this.content.elementAt(index); 145 } 146 147 148 153 public int getSize() 154 { 155 return this.context.size(); 156 } 157 158 159 164 public void removeListDataListener(ListDataListener l) 165 { 166 this.listeners.remove(l); 167 } 168 169 170 171 private void notifyListener() 172 { 173 ListDataEvent event = new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, 0, this.content.size()); 174 for (int i = 0; i < this.listeners.size(); i++) 175 { 176 ListDataListener listener = (ListDataListener ) this.listeners.elementAt(i); 177 listener.contentsChanged(event); 178 } 179 } 180 181 182 183 private void updateModel() 184 { 185 this.content.clear(); 186 Iterator iterator = this.context.iterator(); 187 while (iterator.hasNext()) 188 { 189 this.content.addElement(new ContextElement(iterator.next())); 190 } 191 } 192 } 193 194 195 201 protected class ContextElement implements BeanContextChildComponentProxy 202 { 203 204 protected ImageIcon icon = null; 205 206 protected Object obj; 207 208 protected String txt; 209 210 211 216 public ContextElement(Object obj1) 217 { 218 obj = obj1; 219 try 220 { 221 BeanInfo beaninfo = Introspector.getBeanInfo(obj1.getClass()); 222 if (beaninfo.getIcon(1) != null) 223 { 224 icon = new ImageIcon (beaninfo.getIcon(1)); 225 } 226 Method method = obj1.getClass().getMethod("toString", new Class [0]); 227 if (method.getDeclaringClass().equals(Object .class)) 228 { 229 txt = beaninfo.getBeanDescriptor().getDisplayName(); 230 } 231 } 232 catch (Exception exception) 233 { 234 exception.printStackTrace(); 235 } 236 } 237 238 239 244 public Component getComponent() 245 { 246 if (obj instanceof Component ) 247 { 248 return (Component ) obj; 249 } 250 if (obj instanceof BeanContextContainerProxy ) 251 { 252 return ((BeanContextContainerProxy ) obj).getContainer(); 253 } 254 if (obj instanceof BeanContextChildComponentProxy ) 255 { 256 return ((BeanContextChildComponentProxy ) obj).getComponent(); 257 } 258 else 259 { 260 return new JLabel ("No GUI available", 0); 261 } 262 } 263 264 265 270 public Object getUserObject() 271 { 272 return obj; 273 } 274 275 276 281 public String toString() 282 { 283 return txt != null ? txt : obj.toString(); 284 } 285 } 286 } 287 288 | Popular Tags |