View Javadoc

1   /*
2    * $Id: AbstractFilterModel.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.components.table;
22  
23  import java.util.Vector;
24  
25  import javax.swing.table.AbstractTableModel;
26  import javax.swing.table.DefaultTableModel;
27  import javax.swing.table.TableModel;
28  
29  
30  /***
31   */
32  abstract public class AbstractFilterModel extends AbstractTableModel {
33  
34      protected TableModel model;
35  
36  
37      public AbstractFilterModel(TableModel tm) {
38          model = tm;
39      }
40  
41  
42      public boolean isCellEditable(int par1, int par2) {
43          return model.isCellEditable(par1, par2);
44      }
45  
46      public Class getColumnClass(int par1) {
47          return model.getColumnClass(par1);
48      }
49  
50      public int getColumnCount() {
51          return model.getColumnCount();
52      }
53  
54      public String getColumnName(int par1) {
55          return model.getColumnName(par1);
56      }
57  
58      public void setModel(TableModel model) {
59          this.model = model;
60          this.fireTableDataChanged();
61      }
62  
63      public TableModel getModel() {
64          return model;
65      }
66  
67      public int getRowCount() {
68          return model.getRowCount();
69      }
70  
71      public void setValueAt(Object par1, int par2, int par3) {
72          model.setValueAt(par1, par2, par3);
73      }
74  
75      public Object getValueAt(int par1, int par2) {
76          return model.getValueAt(par1, par2);
77      }
78  
79      public void addRow(Vector data) throws IllegalStateException {
80          if (model instanceof DefaultTableModel) {
81              ((DefaultTableModel) model).addRow(data);
82          } else if (model instanceof AbstractFilterModel) {
83              ((AbstractFilterModel) model).addRow(data);
84          } else {
85              throw (new IllegalStateException("Error attempting to add a row to an underlying model that is not a DefaultTableModel."));
86          }
87      }
88  
89      public void removeAllRows() throws ArrayIndexOutOfBoundsException, IllegalStateException {
90          while (this.getRowCount() > 0) {
91              this.removeRow(0);
92          }
93      }
94  
95      public void removeRow(int rowNum) throws ArrayIndexOutOfBoundsException, IllegalStateException {
96          if (model instanceof DefaultTableModel) {
97              ((DefaultTableModel) model).removeRow(rowNum);
98          } else if (model instanceof AbstractFilterModel) {
99              ((AbstractFilterModel) model).removeRow(rowNum);
100         } else {
101             throw (new IllegalStateException("Error attempting to remove a row from an underlying model that is not a DefaultTableModel."));
102         }
103     }
104 }