1 18 19 package org.objectweb.jac.aspects.gui; 20 21 import java.util.Collection; 22 import java.util.HashMap; 23 import java.util.Hashtable; 24 import java.util.Iterator; 25 import java.util.Map; 26 import java.util.Vector; 27 import javax.swing.event.ListDataEvent; 28 import javax.swing.event.ListDataListener; 29 import javax.swing.event.TableModelEvent; 30 import org.apache.log4j.Logger; 31 import org.objectweb.jac.core.Collaboration; 32 import org.objectweb.jac.core.rtti.ClassItem; 33 import org.objectweb.jac.core.rtti.CollectionItem; 34 import org.objectweb.jac.core.rtti.FieldItem; 35 import org.objectweb.jac.util.Enum; 36 37 38 public class TableFilter extends TableMap implements ListDataListener { 39 static Logger logger = Logger.getLogger("gui.filter"); 40 static Logger loggerEvents = Logger.getLogger("gui.events"); 41 42 int indexes[]; 43 44 Vector filters = new Vector(); 45 46 public TableFilter() { 47 indexes = new int[0]; } 49 50 public TableFilter(ExtendedTableModel model, Collection filteredColumns) { 51 setModel(model); 52 Iterator it = filteredColumns.iterator(); 53 while (it.hasNext()) { 54 FieldItem field = (FieldItem)it.next(); 55 int index = getColumnIndex(field); 56 if (index!=-1) 57 filters.add(new FilterCriteria(index,field)); 58 else 59 logger.warn("Ignoring filter on field "+field+" since it's not a column of the table"); 60 } 61 } 62 63 public void setModel(ExtendedTableModel model) { 64 super.setModel(model); 65 reallocateIndexes(); 66 defaultFilter(); 67 } 68 69 public int getRowCount() { 70 return indexes.length; 71 } 72 73 77 public void defaultFilter() { 78 HashMap map = (HashMap)Collaboration.get().getAttribute(GuiAC.TABLE_FILTER); 80 if (map != null) { 81 FilterCriteria criteria = (FilterCriteria)map.get(getCollection()); 82 if (criteria != null) { 83 logger.debug("Using filter criteria from context: "+criteria); 84 filter(criteria); 85 return; 86 } 87 } 88 } 89 90 91 94 public void reallocateIndexes() { 95 int rowCount = model.getRowCount(); 96 97 logger.debug(this+".reallocateIndexes "+rowCount); 98 99 indexes = new int[rowCount]; 102 103 for (int row = 0; row < rowCount; row++) { 105 indexes[row] = row; 106 } 107 } 108 109 public int getActualIndex(int row) { 110 return indexes[row]; 111 } 112 113 public void tableChanged(TableModelEvent e) { 114 if (e.getType()==TableModelEvent.INSERT || 115 e.getType()==TableModelEvent.DELETE) { 116 logger.debug(this+".tableChanged "+ 117 (e.getType()==TableModelEvent.DELETE?"DELETE":"INSERT")+ 118 " "+e.getFirstRow()+"-"+e.getLastRow()); 119 reallocateIndexes(); 120 filter(); 121 super.tableChanged(e); 122 } else { 123 logger.debug(this+".tableChanged UPDATE "+ 124 e.getFirstRow()+"-"+e.getLastRow()); 125 filter(); 126 super.tableChanged(e); 127 } 128 } 129 130 public void filter() { 131 logger.debug("Filtering with "+filters); 132 int rowCount = model.getRowCount(); 133 int filteredRowCount = 0; 134 int tmp[] = new int[rowCount]; 135 for (int i=0; i<rowCount; i++) { 136 Iterator it = filters.iterator(); 137 boolean keep = true; 138 while (it.hasNext()) { 139 FilterCriteria filter = (FilterCriteria)it.next(); 140 if (!filter.match(model,i)) { 141 keep = false; 142 break; 143 } 144 } 145 if (keep) { 146 logger.debug(" keeping row "+i); 147 tmp[filteredRowCount] = i; 148 filteredRowCount++; 149 } 150 } 151 indexes = new int[filteredRowCount]; 152 System.arraycopy(tmp, 0, indexes, 0, filteredRowCount); 153 } 154 155 public void filter(FilterCriteria filter) { 156 filters.clear(); 157 filters.add(filter); 158 filter(); 159 } 160 161 public void checkModel() { 162 } 163 164 167 public Object getValueAt(int aRow, int aColumn) { 168 checkModel(); 169 if(indexes.length>aRow) { 170 logger.debug("getValueAt("+aRow+","+aColumn+") -> "+ 171 model.getValueAt(indexes[aRow], aColumn)); 172 return model.getValueAt(indexes[aRow], aColumn); 173 } else { 174 return null; 175 } 176 } 177 178 public Object getObject(int row) { 179 checkModel(); 180 return model.getObject(indexes[row]); 181 } 182 183 public int indexOf(Object object) { 184 checkModel(); 185 return indexes[model.indexOf(object)]; 186 } 187 188 public Object getObject(int row, int column) { 189 checkModel(); 190 return model.getObject(indexes[row],column); 191 } 192 193 public void setValueAt(Object aValue, int aRow, int aColumn) { 194 checkModel(); 195 model.setValueAt(aValue, indexes[aRow], aColumn); 196 } 197 198 public boolean isFiltered(FieldItem field) { 199 Iterator it = filters.iterator(); 200 while (it.hasNext()) { 201 FilterCriteria filter = (FilterCriteria)it.next(); 202 if (getMembers()[filter.getColumn()]==field) 203 return true; 204 } 205 return false; 206 } 207 208 213 public void setFilterValue(FieldItem field, Object value) { 214 Iterator it = filters.iterator(); 215 while (it.hasNext()) { 216 FilterCriteria filter = (FilterCriteria)it.next(); 217 if (getMembers()[filter.getColumn()]==field) { 218 filter.setValue(value); 219 return; 220 } 221 } 222 logger.warn("setFilterValue: no filter for the field "+field); 223 } 224 225 229 public Map buildFilterEditors(ViewFactory factory, DisplayContext context) { 230 Hashtable editors = new Hashtable(); 231 filterEditors = new Hashtable(); 232 Iterator it = filters.iterator(); 233 while (it.hasNext()) { 234 FilterCriteria filter = (FilterCriteria)it.next(); 235 FieldItem field = filter.getField(); 236 FieldEditor editor; 237 Enum enum = GuiAC.getEnum(field); 238 if (enum==null) { 239 ClassItem type; 240 if (field instanceof CollectionItem) 241 type = ((CollectionItem)field).getComponentType(); 242 else 243 type = field.getTypeItem(); 244 editor = 245 GenericFactory.createReferenceEditor( 246 factory,context, 247 null,null, 248 "filter "+field.getName(), 249 type, 250 null, 251 true, GuiAC.getLabelAll(), 252 false); 253 } else { 254 editor = GenericFactory.createEnumEditor( 255 factory, context, 256 null, null, 257 "filter "+field.getName(), 258 enum, true, GuiAC.getLabelAll()); 259 } 260 editors.put(field,editor); 261 ComboBoxModel model = ((ReferenceEditor)editor).getModel(); 262 model.setSelectedObject(null); 263 filterEditors.put(model,filter); 264 model.addListDataListener(this); 265 context.addEditor(editor); 266 } 267 return editors; 268 } 269 270 Hashtable filterEditors; 272 273 275 public void intervalAdded(ListDataEvent event) { 276 loggerEvents.debug("intervalAdded: "+event+" from "+event.getSource()); 277 } 278 279 public void intervalRemoved(ListDataEvent event) { 280 loggerEvents.debug("intervalRemoved: "+event+" from "+event.getSource()); 281 } 282 283 public void contentsChanged(ListDataEvent event) { 284 loggerEvents.debug("contentsChanged: "+event+" from "+event.getSource()); 285 ComboBoxModel model = (ComboBoxModel)event.getSource(); 286 FilterCriteria filter = (FilterCriteria)filterEditors.get(model); 287 Object value = model.getSelectedObject(); 288 if (value==null) { 289 filter.setActive(false); 290 } else { 291 filter.setActive(true); 292 filter.setValue(value); 293 } 294 tableChanged(new TableModelEvent(this)); 295 } 296 297 298 302 313 } 314 | Popular Tags |