001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001-2002 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.swing.event.EventListenerList;
025:
026: /**
027: * This is a <TT>IDataSetViewerDestination</TT> that doesn't
028: * actually display the data, it simply stores it for future use.
029: *
030: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
031: */
032: public class DataSetListModel extends BaseDataSetViewerDestination
033: implements IDataSetModel {
034: /** Event types. */
035: private interface IEventTypes {
036: int ALL_ROWS_ADDED = 1;
037: int MOVE_TO_TOP = 2;
038: }
039:
040: //??GETRIDOFTHIS
041: public java.awt.Component getComponent() {
042: return null;
043: }
044:
045: /**
046: * Data. Each element is an array of objects. Each object is
047: * the data for a column.
048: */
049: private List<Object[]> _data = new ArrayList<Object[]>();
050:
051: /** Column headings. */
052: // private ColumnDisplayDefinition[] _hdgs = new ColumnDisplayDefinition[0];
053: /** If <TT>true</TT> column headings should be shown. */
054: // private boolean _showHeadings;
055: /** Listeners for this object. */
056: private EventListenerList _listenerList = new EventListenerList();
057:
058: /**
059: * Clear the data.
060: */
061: public void clear() {
062: _data.clear();
063: }
064:
065: /**
066: * Add a row.
067: *
068: * @param row Array of objects specifying the row data.
069: */
070: protected void addRow(Object[] row) {
071: _data.add(row);
072: }
073:
074: /**
075: * Called once all rows have been added..
076: */
077: protected void allRowsAdded() {
078: fireEvent(IEventTypes.ALL_ROWS_ADDED);
079: }
080:
081: /**
082: * Indicates that the output display should scroll to the top.
083: */
084: public void moveToTop() {
085: fireEvent(IEventTypes.MOVE_TO_TOP);
086: }
087:
088: /**
089: * Return number of rows in model.
090: *
091: * @return Number of rows.
092: */
093: public int getRowCount() {
094: return _data.size();
095: }
096:
097: /**
098: * Return the data value for the specified cell.
099: *
100: * @param rowIndex The row whose value is being retrieved.
101: * @param columnIndex The column whose value is being retrieved.
102: *
103: * @return the data value for the specified cell.
104: */
105: public Object getValueAt(int rowIndex, int columnIndex) {
106: return (_data.get(rowIndex))[columnIndex];
107: }
108:
109: /**
110: * Set the data value for the specified cell.
111: *
112: * @param value The new value for the cell.
113: * @param rowIndex The row whose value is being set.
114: * @param columnIndex The column whose value is being set.
115: */
116: public void setValueAt(Object value, int rowIndex, int columnIndex) {
117: (_data.get(rowIndex))[columnIndex] = value;
118: }
119:
120: /**
121: * Adds a listener for events in this model.
122: *
123: * @param lis <TT>DataSetModelListener</TT> that will be
124: * notified when events occur in this model.
125: */
126: public synchronized void addListener(IDataSetModelListener lis) {
127: _listenerList.add(IDataSetModelListener.class, lis);
128: }
129:
130: /**
131: * Removes an event listener fromthis model.
132: *
133: * @param lis <TT>DataSetModelListener</TT> to be removed.
134: */
135: public synchronized void removeListener(IDataSetModelListener lis) {
136: _listenerList.remove(IDataSetModelListener.class, lis);
137: }
138:
139: /**
140: * Fire a <TT>DataSetModel</TT> event.
141: *
142: * @param eventType Specifies the event type. @see IEventType.
143: */
144: protected void fireEvent(int eventType) {
145: // Guaranteed to be non-null.
146: Object[] listeners = _listenerList.getListenerList();
147: // Process the listeners last to first, notifying
148: // those that are interested in this event.
149: DataSetModelEvent evt = null;
150: for (int i = listeners.length - 2; i >= 0; i -= 2) {
151: if (listeners[i] == IDataSetModelListener.class) {
152: // Lazily create the event:
153: if (evt == null) {
154: evt = new DataSetModelEvent(this );
155: }
156: IDataSetModelListener lis = (IDataSetModelListener) listeners[i + 1];
157: switch (eventType) {
158: case IEventTypes.ALL_ROWS_ADDED: {
159: lis.allRowsAdded(evt);
160: break;
161: }
162: case IEventTypes.MOVE_TO_TOP: {
163: lis.moveToTop(evt);
164: break;
165: }
166: default: {
167: throw new IllegalArgumentException(
168: "Invalid eventTypes passed: " + eventType);
169: }
170: }
171: }
172: }
173: }
174:
175: }
|