1 18 19 package org.objectweb.jac.aspects.gui; 20 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Vector ; 25 import javax.swing.AbstractListModel ; 26 import org.apache.log4j.Logger; 27 import org.objectweb.jac.core.rtti.CollectionItem; 28 import org.objectweb.jac.util.Stack; 29 30 33 public abstract class LessAbstractListModel extends AbstractListModel 34 implements ObjectUpdate, CollectionModel, CollectionUpdate 35 { 36 static Logger logger = Logger.getLogger("gui.model"); 37 38 CollectionItem collection=null; 39 Object substance; 40 41 List rows = new Vector (); 42 List objects = new Vector (); 43 44 Stack context = new Stack(); 45 46 49 public LessAbstractListModel() { 50 if (GuiAC.getGraphicContext()!=null) 51 context.addAll(GuiAC.getGraphicContext()); 52 } 53 54 60 public LessAbstractListModel(CollectionItem collection, Object substance) { 61 if (GuiAC.getGraphicContext()!=null) 62 context.addAll(GuiAC.getGraphicContext()); 63 64 this.collection = collection; 65 this.substance = substance; 66 67 buildData(); 68 Utils.registerCollection(substance,collection,this); 69 } 70 71 void buildData() { 72 Collection c = collection.getActualCollectionThroughAccessor(substance); 73 if (c!=null) { 74 logger.debug("buildData for "+substance+"."+collection.getName()); 75 logger.debug("objects : " + new Vector (c)); 76 Iterator i = c.iterator(); 77 while (i.hasNext()) { 78 Object obj = i.next(); 79 logger.debug("add "+obj); 80 addObject(obj); 81 } 82 } 83 } 84 85 public CollectionItem getCollection() { 86 return collection; 87 } 88 89 String nullLabel; 90 94 public void setNullLabel(String label) { 95 this.nullLabel = label; 96 } 97 98 105 public void addObject(Object object) { 106 addObject(object, 107 object==null ? 108 (nullLabel!=null ? nullLabel : GuiAC.getLabelNone()) 109 : GuiAC.toString(object,context)); 110 } 111 112 118 public void addObject(Object object, String label) { 119 objects.add(object); 120 rows.add(label); 121 fireIntervalAdded(this,objects.size()-1,objects.size()-1); 122 Utils.registerObject(object,this); 123 } 124 125 127 public int getRowCount() { 128 return rows.size(); 129 } 130 131 133 public Object getElementAt(int row) { 134 logger.debug("getElementAt("+row+") -> "+rows.get(row)); 135 return rows.get(row); 136 } 137 138 140 public int getSize() { 141 return getRowCount(); 142 } 143 144 146 public Object getObject(int index) { 147 return objects.get(index); 148 } 149 150 public int indexOf(Object object) { 151 return objects.indexOf(object); 152 } 153 154 157 158 public boolean isCellEditable(int row, int column) { 159 return false; 160 } 161 162 public void objectUpdated(Object substance,Object param) { 164 int index = objects.indexOf(substance); 165 if (index!=-1) { 166 rows.set(index,GuiAC.toString(objects.get(index),context)); 167 fireContentsChanged(this,index,index); 168 } 169 } 170 171 private int getMin(String row, Object obj) 172 { 173 int min = 0; 174 for (int i = 1; i < rows.size(); i++) 175 { 176 if ((((String ) (((Vector ) rows).elementAt(i))) 177 .compareToIgnoreCase((String ) (((Vector ) rows).elementAt(min)))) 178 < 0) 179 min = i; 180 } 181 return min; 182 } 183 184 187 public void sort() 188 { 189 Vector newRows = new Vector (); 190 Vector newObjs = new Vector (); 191 192 Object obj = null; 193 String row = null; 194 195 while (rows.size() > 0) 196 { 197 int min = getMin(row, obj); 198 row = (String ) ((Vector ) rows).elementAt(min); 199 obj = ((Vector ) objects).elementAt(min); 200 newRows.add(row); 201 newObjs.add(obj); 202 rows.remove(row); 203 objects.remove(obj); 204 } 205 rows = newRows; 206 objects = newObjs; 207 } 208 209 210 213 protected void unregisterViews() { 214 logger.debug("TableModel.unRegisterViews "+objects.size()); 215 Iterator i = objects.iterator(); 216 while (i.hasNext()) { 217 Object object = i.next(); 218 Utils.unregisterObject(object,this); 219 } 220 } 221 222 public void close() { 223 unregisterViews(); 224 if(collection!=null) { 225 Utils.unregisterCollection(substance,collection,this); 226 } 227 } 228 229 public void onChange(Object substance, CollectionItem collection, 231 Object value, Object param) { 232 unregisterViews(); 233 int size = objects.size(); 234 objects.clear(); 235 rows.clear(); 236 if (size>0) 237 fireIntervalRemoved(this,0,size-1); 238 buildData(); 239 if (!objects.isEmpty()) 240 fireIntervalAdded(this, 0, objects.size()-1); 241 } 242 243 public void onAdd(Object substance, CollectionItem collection, 244 Object value, Object added, Object param) { 245 onChange(substance,collection,value,param); 246 } 247 248 public void onRemove(Object substance, CollectionItem collection, 249 Object value, Object removed, Object param) { 250 onChange(substance,collection,value,param); 251 } 252 253 254 } 255 | Popular Tags |