KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > DefaultMOMutableTableModel


1 /*_############################################################################
2   _##
3   _## SNMP4J-Agent - DefaultMOMutableTableModel.java
4   _##
5   _## Copyright (C) 2005-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

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   /**
41    * Returns a lexicographic ordered list of the rows in the specified index
42    * range.
43    * @param lowerBound
44    * the lower bound index (inclusive) for the rows in the returned list.
45    * @param upperBoundEx
46    * the upper bound index (exclusive) for the rows in the returned list.
47    * @return
48    * the possibly empty lexicographically ordered <code>List</code>
49    * of rows of this table model in the specified index range. Modifications
50    * to the list will not affect the underlying table model, although
51    * modifications to the row elements will.
52    */

53   public synchronized List getRows(OID lowerBound, OID upperBoundEx) {
54     List view = new ArrayList(getView(lowerBound, upperBoundEx).values());
55     return view;
56   }
57
58   /**
59    * Returns a lexicographic ordered list of the rows in the specified index
60    * range that match the supplied filter.
61    * @param lowerBound
62    * the lower bound index (inclusive) for the rows in the returned list.
63    * @param upperBoundEx
64    * the upper bound index (exclusive) for the rows in the returned list.
65    * @param filter
66    * the filter to exclude rows in the range from the returned
67    * @return
68    * the possibly empty lexicographically ordered <code>List</code>
69    * of rows of this table model in the specified index range. Modifications
70    * to the list will not affect the underlying table model, although
71    * modifications to the row elements will.
72    */

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   /**
141    * Remove all rows that do not match the given filter criteria
142    * from the model.
143    * @param filter
144    * the <code>MOTableRowFilter</code> that filters out the rows to
145    * delete.
146    */

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   /**
157    * Returns an iterator over all rows in this table that pass the
158    * given filter. If the table might be modified while the iterator
159    * is used, it is recommended to synchronize on this model while
160    * iterating.
161    * @param filter
162    * a MOTableRowFilter instance that defines the rows to return.
163    * @return
164    * an Iterator.
165    */

166   public synchronized Iterator iterator(MOTableRowFilter filter) {
167     return new FilteredRowIterator(filter);
168   }
169
170   /**
171    * Create a new row and return it. The new row will not be added to the
172    * table. To add it to the model use the {@link #addRow} method.
173    * If this mutable table does not support row creation, it should
174    * throw an {@link UnsupportedOperationException}.
175    * @param index
176    * the index OID for the new row.
177    * @param values
178    * the values to be contained in the new row.
179    * @return
180    * the created <code>MOTableRow</code>.
181    * @throws java.lang.UnsupportedOperationException
182    * if the specified row cannot be created.
183    */

184   public MOTableRow createRow(OID index, Variable[] values)
185       throws UnsupportedOperationException JavaDoc
186   {
187     if (rowFactory == null) {
188       throw new UnsupportedOperationException JavaDoc("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 JavaDoc next() {
266       if (next == null) {
267         findNext();
268       }
269       if (next != null) {
270         Object JavaDoc 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