001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.table;
031:
032: import java.io.Serializable;
033: import java.util.EventListener;
034:
035: import nextapp.echo2.app.event.EventListenerList;
036: import nextapp.echo2.app.event.TableModelEvent;
037: import nextapp.echo2.app.event.TableModelListener;
038:
039: /**
040: * An abstract implementation of a <code>TableModel</code>.
041: * This class provides the following conveniences for <code>TableModel</code>
042: * implementation development:
043: * <ul>
044: * <li>event listener management and notification</li>
045: * <li>a generic implementation of <code>getColumnClass()</code> which
046: * returns <code>Object.class</code></li>
047: * <li>a generic implementation of <code>getColumnName()</code> which
048: * returns "spreadsheet-style" column names, i.e.,
049: * A, B, C...Y, Z, AA, AB, AC....</li>
050: * </ul>
051: *
052: * @see DefaultTableModel
053: */
054: public abstract class AbstractTableModel implements Serializable,
055: TableModel {
056:
057: private EventListenerList listenerList = new EventListenerList();
058:
059: /**
060: * Default constructor.
061: */
062: public AbstractTableModel() {
063: super ();
064: }
065:
066: /**
067: * @see nextapp.echo2.app.table.TableModel#addTableModelListener(nextapp.echo2.app.event.TableModelListener)
068: */
069: public void addTableModelListener(TableModelListener l) {
070: listenerList.addListener(TableModelListener.class, l);
071: }
072:
073: /**
074: * Notifies <code>TableModelListener</code>s that the contents of the cell
075: * at the specified coordinate were changed.
076: *
077: * @param column the column index
078: * @param row the row index
079: */
080: public void fireTableCellUpdated(int column, int row) {
081: fireTableChanged(new TableModelEvent(this , column, row, row,
082: TableModelEvent.UPDATE));
083: }
084:
085: /**
086: * Notifies <code>TableModelListener</code>s that the content of the table
087: * (possibly including the number of rows) was changed, but that the table's
088: * structure has remained intact.
089: */
090: public void fireTableDataChanged() {
091: fireTableChanged(new TableModelEvent(this ));
092: }
093:
094: /**
095: * Notifies <code>TableModelListener</code>s that rows from
096: * <code>firstRow</code> to <code>lastRow</code> were deleted.
097: *
098: * @param firstRow the index of the first deleted row
099: * @param lastRow the index of the last deleted row
100: */
101: public void fireTableRowsDeleted(int firstRow, int lastRow) {
102: fireTableChanged(new TableModelEvent(this ,
103: TableModelEvent.ALL_COLUMNS, firstRow, lastRow,
104: TableModelEvent.DELETE));
105: }
106:
107: /**
108: * Notifies <code>TableModelListener</code>s that the rows from
109: * <code>firstRow</code> to <code>lastRow</code> were inserted.
110: *
111: * @param firstRow the index of the first inserted row
112: * @param lastRow the index of the last inserted row
113: */
114: public void fireTableRowsInserted(int firstRow, int lastRow) {
115: fireTableChanged(new TableModelEvent(this ,
116: TableModelEvent.ALL_COLUMNS, firstRow, lastRow,
117: TableModelEvent.INSERT));
118: }
119:
120: /**
121: * Notifies <code>TableModelListener</code>s that the data in the rows
122: * from <code>firstRow</code> to <code>lastRow</code> was updated.
123: * in the specified rows was updated.
124: *
125: * @param firstRow the index of the first inserted row
126: * @param lastRow the index of the last inserted row
127: */
128: public void fireTableRowsUpdated(int firstRow, int lastRow) {
129: fireTableChanged(new TableModelEvent(this ,
130: TableModelEvent.ALL_COLUMNS, firstRow, lastRow,
131: TableModelEvent.UPDATE));
132: }
133:
134: /**
135: * Notifies <code>TableModelListener</code> that all data in the table may
136: * have changed, including the size and structure of the table.
137: */
138: public void fireTableStructureChanged() {
139: fireTableChanged(new TableModelEvent(this ,
140: TableModelEvent.ALL_COLUMNS,
141: TableModelEvent.HEADER_ROW, TableModelEvent.HEADER_ROW,
142: TableModelEvent.STRUCTURE_CHANGED));
143: }
144:
145: /**
146: * Notifies <code>TableModelListener</code>s of the specified event.
147: *
148: * @param e the event
149: */
150: public void fireTableChanged(TableModelEvent e) {
151: EventListener[] listeners = listenerList
152: .getListeners(TableModelListener.class);
153:
154: for (int index = 0; index < listeners.length; ++index) {
155: ((TableModelListener) listeners[index]).tableChanged(e);
156: }
157: }
158:
159: /**
160: * Returns <code>Object.class</code>
161: *
162: * @see nextapp.echo2.app.table.TableModel#getColumnClass(int)
163: */
164: public Class getColumnClass(int column) {
165: return Object.class;
166: }
167:
168: /**
169: * Returns column names using a "spreadsheet-style" convention, i.e.,
170: * A, B, C...Y, Z, AA, AB, AC...
171: *
172: * @see nextapp.echo2.app.table.TableModel#getColumnName(int)
173: */
174: public String getColumnName(int column) {
175: StringBuffer sb = new StringBuffer();
176: int value = column;
177: do {
178: int digit = value % 26;
179: sb.insert(0, (char) ('A' + digit));
180: value = value / 26 - 1;
181: } while (value >= 0);
182:
183: return sb.toString();
184: }
185:
186: /**
187: * Returns the <code>EventListenerList</code> used to register listeners.
188: *
189: * @return the <code>EventListenerList</code>
190: */
191: public EventListenerList getEventListenerList() {
192: return listenerList;
193: }
194:
195: /**
196: * @see nextapp.echo2.app.table.TableModel#removeTableModelListener(nextapp.echo2.app.event.TableModelListener)
197: */
198: public void removeTableModelListener(TableModelListener l) {
199: listenerList.removeListener(TableModelListener.class, l);
200: }
201: }
|