1 7 package org.ejtools.adwt; 8 9 import java.beans.beancontext.BeanContext ; 10 import java.beans.beancontext.BeanContextMembershipEvent ; 11 import java.beans.beancontext.BeanContextMembershipListener ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 import java.util.Vector ; 15 16 import javax.swing.ListModel ; 17 import javax.swing.event.ListDataEvent ; 18 import javax.swing.event.ListDataListener ; 19 20 26 public class BeanContextListModel implements ListModel , BeanContextMembershipListener 27 { 28 29 protected Collection listeners = new Vector (); 30 31 protected BeanContext root; 32 33 34 39 public BeanContextListModel(BeanContext root) 40 { 41 this.root = root; 42 this.root.addBeanContextMembershipListener(this); 43 } 44 45 46 52 public void addListDataListener(ListDataListener l) 53 { 54 this.listeners.add(l); 55 } 56 57 58 63 public void childrenAdded(BeanContextMembershipEvent bcme) 64 { 65 this.fireContentsChanged(); 66 } 67 68 69 74 public void childrenRemoved(BeanContextMembershipEvent bcme) 75 { 76 this.fireContentsChanged(); 77 } 78 79 80 86 public Object getElementAt(int index) 87 { 88 int i = 0; 89 Object result = null; 90 for (Iterator it = this.root.iterator(); it.hasNext(); i++) 91 { 92 result = it.next(); 93 if (i == index) 94 { 95 break; 96 } 97 result = null; 98 } 99 return result; 100 } 101 102 103 108 public int getSize() 109 { 110 return root.size(); 111 } 112 113 114 119 public void removeListDataListener(ListDataListener l) 120 { 121 this.listeners.remove(l); 122 } 123 124 125 126 protected void fireContentsChanged() 127 { 128 ListDataEvent event = new ListDataEvent (this, ListDataEvent.CONTENTS_CHANGED, 0, this.root.size()); 129 Object [] copy = this.listeners.toArray(); 130 for (int i = 0; i < copy.length; i++) 131 { 132 ListDataListener listener = (ListDataListener ) copy[i]; 133 listener.contentsChanged(event); 134 } 135 } 136 137 138 139 protected void fireIntervalAdded() { } 140 141 142 143 protected void fireIntervalRemoved() { } 144 } 145 | Popular Tags |