1 20 21 22 23 package org.snmp4j.agent.mo; 24 25 import java.util.*; 26 27 import org.snmp4j.smi.*; 28 29 public class DefaultMOMutableTableModel extends DefaultMOTableModel 30 implements MOMutableTableModel 31 { 32 33 protected MOTableRowFactory rowFactory; 34 private transient Vector moTableModelListeners; 35 36 public MOTableRowFactory getRowFactory() { 37 return rowFactory; 38 } 39 40 53 public synchronized List getRows(OID lowerBound, OID upperBoundEx) { 54 List view = new ArrayList(getView(lowerBound, upperBoundEx).values()); 55 return view; 56 } 57 58 73 public synchronized List getRows(OID lowerBound, OID upperBoundEx, 74 MOTableRowFilter filter) { 75 LinkedList result = new LinkedList(); 76 SortedMap view = getView(lowerBound, upperBoundEx); 77 for (Iterator it = view.values().iterator(); 78 it.hasNext(); ) { 79 MOTableRow row = (MOTableRow) it.next(); 80 if (filter.passesFilter(row)) { 81 result.add(row); 82 } 83 } 84 return result; 85 } 86 87 private SortedMap getView(OID lowerBound, OID upperBoundEx) { 88 SortedMap view; 89 if ((lowerBound == null) && (upperBoundEx == null)) { 90 view = rows; 91 } 92 else if (lowerBound == null) { 93 view = rows.headMap(upperBoundEx); 94 } 95 else if (upperBoundEx == null) { 96 view = rows.tailMap(lowerBound); 97 } 98 else { 99 view = rows.subMap(lowerBound, upperBoundEx); 100 } 101 return view; 102 } 103 104 public synchronized MOTableRow removeRow(OID index) { 105 MOTableRow row = (MOTableRow) rows.remove(index); 106 if ((row != null) && (moTableModelListeners != null)) { 107 MOTableModelEvent event = 108 new MOTableModelEvent(this, MOTableModelEvent.ROW_REMOVED, row); 109 fireTableModelChanged(event); 110 } 111 return row; 112 } 113 114 public synchronized void removeRows(OID lowerBoundIncl, 115 OID upperBoundExcl) { 116 Map m = rows.tailMap(lowerBoundIncl); 117 for (Iterator it = m.entrySet().iterator(); it.hasNext(); ) { 118 Map.Entry item = (Map.Entry) it.next(); 119 if (upperBoundExcl.compareTo(item.getKey()) > 0) { 120 if (moTableModelListeners != null) { 121 MOTableModelEvent event = 122 new MOTableModelEvent(this, MOTableModelEvent.ROW_REMOVED, 123 (MOTableRow)item.getValue()); 124 fireTableModelChanged(event); 125 } 126 it.remove(); 127 } 128 else { 129 break; 130 } 131 } 132 } 133 134 public synchronized void clear() { 135 fireTableModelChanged(new MOTableModelEvent(this, 136 MOTableModelEvent.TABLE_CLEAR)); 137 rows.clear(); 138 } 139 140 147 public synchronized void clear(MOTableRowFilter filter) { 148 for (Iterator it = rows.values().iterator(); it.hasNext();) { 149 MOTableRow row = (MOTableRow) it.next(); 150 if (!filter.passesFilter(row)) { 151 it.remove(); 152 } 153 } 154 } 155 156 166 public synchronized Iterator iterator(MOTableRowFilter filter) { 167 return new FilteredRowIterator(filter); 168 } 169 170 184 public MOTableRow createRow(OID index, Variable[] values) 185 throws UnsupportedOperationException 186 { 187 if (rowFactory == null) { 188 throw new UnsupportedOperationException ("No row factory"); 189 } 190 return rowFactory.createRow(index, values); 191 } 192 193 public void setRowFactory(MOTableRowFactory rowFactory) { 194 this.rowFactory = rowFactory; 195 } 196 197 public void setColumnCount(int columnCount) { 198 this.columnCount = columnCount; 199 } 200 201 public void freeRow(MOTableRow row) { 202 if (rowFactory != null) { 203 rowFactory.freeRow(row); 204 } 205 } 206 207 public synchronized void addMOTableModelListener(MOTableModelListener l) { 208 if (moTableModelListeners == null) { 209 moTableModelListeners = new Vector(2); 210 } 211 moTableModelListeners.add(l); 212 } 213 214 public synchronized void removeMOTableModelListener(MOTableModelListener l) { 215 if (moTableModelListeners != null) { 216 moTableModelListeners.remove(l); 217 } 218 } 219 220 protected void fireTableModelChanged(MOTableModelEvent event) { 221 if (moTableModelListeners != null) { 222 Vector listeners = moTableModelListeners; 223 int count = listeners.size(); 224 for (int i = 0; i < count; i++) { 225 ((MOTableModelListener) listeners.elementAt(i)).tableModelChanged(event); 226 } 227 } 228 } 229 230 public class FilteredRowIterator implements Iterator { 231 232 private Iterator iterator; 233 private MOTableRowFilter filter; 234 private MOTableRow next; 235 236 FilteredRowIterator(MOTableRowFilter filter) { 237 this.filter = filter; 238 this.iterator = iterator(); 239 } 240 241 public void remove() { 242 iterator.remove(); 243 } 244 245 public boolean hasNext() { 246 if (next != null) { 247 return true; 248 } 249 findNext(); 250 return (next != null); 251 } 252 253 private void findNext() { 254 while (iterator.hasNext()) { 255 next = (MOTableRow) iterator.next(); 256 if (filter.passesFilter(next)) { 257 break; 258 } 259 else { 260 next = null; 261 } 262 } 263 } 264 265 public Object next() { 266 if (next == null) { 267 findNext(); 268 } 269 if (next != null) { 270 Object retval = next; 271 next = null; 272 return retval; 273 } 274 throw new NoSuchElementException(); 275 } 276 277 } 278 279 public MOTableRow addRow(MOTableRow row) { 280 MOTableRow newRow = super.addRow(row); 281 if (moTableModelListeners != null) { 282 MOTableModelEvent event = 283 new MOTableModelEvent(this, MOTableModelEvent.ROW_ADDED, row); 284 fireTableModelChanged(event); 285 } 286 return newRow; 287 } 288 } 289 | Popular Tags |