KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > AbstractTableModel


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: AbstractTableModel.java,v 1.7 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.util.*;
23 import org.enhydra.barracuda.core.comp.model.*;
24
25 /**
26  * This class provides the abstract implementation
27  * for a Table Model.
28  *
29  * <p>Note: this interface is designed to be used to ways. You can
30  * either implement to return a specific number of rows/cols (in
31  * which case hasMoreXXX() methods should return false) OR you
32  * can implement using the hasMoreXXX() methods (in which case the
33  * getColumnCount(), getRowCount() methods should return -1.
34  *
35  * <p>The getXXXCount() method is more like the Swing JTable interface;
36  * the hasMoreXXX() method makes it easier to implement tables where
37  * you don't know the total number of records when you start.
38  *
39  * <p>Classes implementing this interface should generally support both
40  * methods: in other words, for every row/column, invoke the getItemAt()
41  * method, then while the model has more rows/columns, it should again
42  * invoke getItemAt().
43  */

44 public abstract class AbstractTableModel implements TableModel {
45
46     protected ViewContext viewContext = null;
47     protected List listeners = new ArrayList();
48
49     //--------------- AbstractTemplateModel ----------------------
50
/**
51      * Add a listener to the template that's notified each time a change
52      * to the data model occurs.
53      *
54      * @param ml the TemplateModelListener
55      */

56     public void addModelListener(ModelListener ml) {
57         listeners.add(ml);
58     }
59
60     /**
61      * Remove a listener
62      *
63      * @param ml the TemplateModelListener
64      */

65     public void removeModelListener(ModelListener ml) {
66         listeners.remove(ml);
67     }
68     
69     /**
70      * Forwards the given notification event to all
71      * <code>TemplateModelListeners</code> that registered
72      * themselves as listeners for this template model.
73      */

74     public void fireModelChanged() {
75         Iterator it = listeners.iterator();
76         ModelListener ml = null;
77         while (it.hasNext()) {
78             ml = (ModelListener) it.next();
79             ml.modelChanged(this);
80         }
81     }
82 // protected List listeners = new ArrayList();
83

84     //--------------- AbstractTableModel -------------------------
85
/**
86      * Add a listener to the table that's notified each time a change
87      * to the data model occurs.
88      *
89      * @param l the TableModelListener
90      */

91 // public void addTableModelListener(TableModelListener l) {
92
// listeners.add(l);
93
// }
94

95     /**
96      * Remove a listener from the table.
97      *
98      * @param l the ListDataListener
99      */

100 // public void removeTableModelListener(TableModelListener l) {
101
// listeners.remove(l);
102
// }
103

104     /**
105      * Reset the model to its initial (unprocessed) state. This
106      * is a convenience method that gets invoked prior to the
107      * entire model being rendered. You only need to override this
108      * method if you want to do something (like reset internal counters)
109      * before the model is queried
110      */

111     public void resetModel() {
112         //nop
113
}
114
115     /**
116      * Notifies all listeners that all cell values in the table's
117      * rows may have changed. The number of rows may also have changed
118      * and the <code>JTable</code> should redraw the
119      * table from scratch. The structure of the table (as in the order of the
120      * columns) is assumed to be the same.
121      *
122      * @see TableModelEvent
123      */

124 // public void fireTableDataChanged() {
125
// fireTableChanged(new TableModelEvent(this));
126
// }
127

128     /**
129      * Notifies all listeners that the table's structure has changed.
130      * The number of columns in the table, and the names and types of
131      * the new columns may be different from the previous state.
132      * If the <code>JTable</code> receives this event and its
133      * <code>autoCreateColumnsFromModel</code>
134      * flag is set it discards any table columns that it had and reallocates
135      * default columns in the order they appear in the model. This is the
136      * same as calling <code>setModel(TableModel)</code> on the
137      * <code>JTable</code>.
138      *
139      * @see TableModelEvent
140      */

141 // public void fireTableStructureChanged() {
142
// fireTableChanged(new TableModelEvent(this, TableModelEvent.HEADER_ROW));
143
// }
144

145     /**
146      * Notifies all listeners that rows in the range
147      * <code>[firstRow, lastRow]</code>, inclusive, have been inserted.
148      *
149      * @param firstRow the first row
150      * @param lastRow the last row
151      * @see TableModelEvent
152      */

153 // public void fireTableRowsInserted(int firstRow, int lastRow) {
154
// fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
155
// }
156

157     /**
158      * Notifies all listeners that rows in the range
159      * <code>[firstRow, lastRow]</code>, inclusive, have been updated.
160      *
161      * @param firstRow the first row
162      * @param lastRow the last row
163      * @see TableModelEvent
164      */

165 // public void fireTableRowsUpdated(int firstRow, int lastRow) {
166
// fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.UPDATE));
167
// }
168

169     /**
170      * Notifies all listeners that rows in the range
171      * <code>[firstRow, lastRow]</code>, inclusive, have been deleted.
172      *
173      * @param firstRow the first row
174      * @param lastRow the last row
175      * @see TableModelEvent
176      */

177 // public void fireTableRowsDeleted(int firstRow, int lastRow) {
178
// fireTableChanged(new TableModelEvent(this, firstRow, lastRow, TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
179
// }
180

181     /**
182      * Notifies all listeners that the value of the cell at
183      * <code>[row, column]</code> has been updated.
184      *
185      * @param row row of cell which has been updated
186      * @param column column of cell which has been updated
187      * @see TableModelEvent
188      */

189 // public void fireTableCellUpdated(int row, int column) {
190
// fireTableChanged(new TableModelEvent(this, row, row, column));
191
// }
192

193     /**
194      * Forwards the given notification event to all
195      * <code>TableModelListeners</code> that registered
196      * themselves as listeners for this table model.
197      *
198      * @param e the event to be forwarded
199      *
200      * @see #addTableModelListener
201      * @see TableModelEvent
202      */

203 // public void fireTableChanged(TableModelEvent e) {
204
// Iterator it = listeners.iterator();
205
// TableModelListener tml = null;
206
// while (it.hasNext()) {
207
// tml = (TableModelListener) it.next();
208
// tml.tableChanged(e);
209
// }
210
// }
211

212     /**
213      * Return all the listeners for this given list model (returns
214      * a copy of the underlying List)
215      *
216      * @returns all the listeners for this given list model
217      */

218 // public List getListeners() {
219
// return new ArrayList(listeners);
220
// }
221

222     //--------------- Contextual ---------------------------------
223
/**
224      * Specify the ViewContext. This method will generally be called
225      * by the class that is using the model to actually render the data
226      * in a view. The context will be specified prior to a render pass,
227      * and the context will be reset to null after the render pass.
228      *
229      * @param ivc the current ViewContext
230      */

231     public void setViewContext(ViewContext ivc) {
232         viewContext = ivc;
233     }
234
235     /**
236      * Get the current ViewContext
237      *
238      * @return the current ViewContext
239      */

240     public ViewContext getViewContext() {
241         return viewContext;
242     }
243 }
Popular Tags