001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001-2003 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:
022: import javax.swing.DefaultCellEditor;
023:
024: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
025: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
026: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
027:
028: /**
029: * This provides base behaviour for implemtations of <TT>IDataSetViewerDestination</TT>.
030: *
031: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
032: */
033: public abstract class BaseDataSetViewerDestination implements
034: IDataSetViewer {
035: /** Logger for this class. */
036: private static ILogger s_log = LoggerController
037: .createLogger(BaseDataSetViewerDestination.class);
038:
039: /** Specifies whether to show the column headings. */
040: private boolean _showHeadings = true;
041:
042: /** Column definitions. */
043: protected ColumnDisplayDefinition[] _colDefs = new ColumnDisplayDefinition[0];
044:
045: /** reference to the object generating the data being displayed */
046: private IDataSetUpdateableModel _updateableModelReference = null;
047:
048: /** reference to DefaultCellEditor currently in operation, if any */
049: protected DefaultCellEditor currentCellEditor = null;
050:
051: /**
052: * Some types of DataSetViewers require extra initialization to set up
053: * a return reference to the originating data object (e.g. to allow
054: * editing of that object later). The reference is needed to distinguish
055: * which tables/text may be edited and which may not. This cannot be included
056: * in the object creation because the Class.newInstance() method used in the
057: * getInstance() method of this class does not allow for arguments. Since this
058: * info is needed for the DataSetViewerTablePanel and DataSetViewerTextPanel classes
059: * to be able to set up the
060: * popup menu correctly, those classes cannot complete their initialization without
061: * this added info. Therefore we need to initialize that class in two stages.
062: */
063: public void init(IDataSetUpdateableModel updateableObject) {
064: // default is to do nothing. Derived classes must override this
065: // to accomplish anything.
066: }
067:
068: /**
069: * Specify the column definitions to use.
070: *
071: * @param hdgs Column definitions to use.
072: */
073: public void setColumnDefinitions(ColumnDisplayDefinition[] colDefs) {
074: _colDefs = colDefs != null ? colDefs
075: : new ColumnDisplayDefinition[0];
076: }
077:
078: /**
079: * Return the column definitions to use.
080: *
081: * @return the column definitions to use.
082: */
083: public ColumnDisplayDefinition[] getColumnDefinitions() {
084: return _colDefs;
085: }
086:
087: /**
088: * Specify whether to show the column headings.
089: *
090: * @param show <TT>true</TT> if headibgs to be shown else <TT>false</TT>.
091: */
092: public void showHeadings(boolean show) {
093: _showHeadings = show;
094: }
095:
096: /**
097: * Return whether to show the column headings.
098: *
099: * @return whether to show the column headings.
100: */
101: public boolean getShowHeadings() {
102: return _showHeadings;
103: }
104:
105: public synchronized void show(IDataSet ds) throws DataSetException {
106: show(ds, null);
107: }
108:
109: /**
110: * @throws IllegalArgumentException
111: * Thrown if <TT>null</TT> <TT>IDataSet</TT> passed.
112: */
113: public synchronized void show(IDataSet ds,
114: IMessageHandler msgHandler) throws DataSetException {
115:
116: // We are displaying a new dataset, so if there was
117: // a cell editor in operation, tell it to cancel.
118: //???? How does this impact popup display?
119: if (currentCellEditor != null) {
120: currentCellEditor.cancelCellEditing();
121: currentCellEditor = null;
122: }
123:
124: if (ds == null) {
125: throw new IllegalArgumentException("IDataSet == null");
126: }
127:
128: clear();
129: if (ds.getDataSetDefinition() != null) {
130: setColumnDefinitions(ds.getDataSetDefinition()
131: .getColumnDefinitions());
132: final int colCount = ds.getColumnCount();
133: while (ds.next(msgHandler)) {
134: addRow(ds, colCount);
135: }
136: allRowsAdded();
137: moveToTop();
138: }
139: }
140:
141: protected void addRow(IDataSet ds, int columnCount)
142: throws DataSetException {
143: Object[] row = new Object[columnCount];
144: for (int i = 0; i < columnCount; ++i) {
145: row[i] = ds.get(i);
146: }
147: addRow(row);
148: }
149:
150: /**
151: * Setter and getter for the reference to the updateable object
152: * representing the actual underlying data. This will be null if
153: * the underlying data is not updateable.
154: */
155: public void setUpdateableModelReference(
156: IDataSetUpdateableModel updateableObject) {
157: _updateableModelReference = updateableObject;
158: }
159:
160: public IDataSetUpdateableModel getUpdateableModelReference() {
161: return _updateableModelReference;
162: }
163:
164: protected abstract void allRowsAdded() throws DataSetException;
165:
166: protected abstract void addRow(Object[] row)
167: throws DataSetException;
168:
169: /**
170: * factory method for getting IDataSetViewer instances
171: * If no instance can be made then the default
172: * will be returned.
173: */
174: public static IDataSetViewer getInstance(String sName,
175: IDataSetUpdateableModel updateableModel) {
176: IDataSetViewer dsv = null;
177: try {
178: Class<?> cls = Class.forName(sName);
179: dsv = (IDataSetViewer) cls.newInstance();
180: dsv.init(updateableModel);
181: } catch (Exception e) {
182: s_log.error("Error", e);
183: }
184: if (dsv == null) {
185: dsv = new DataSetViewerTablePanel();
186: ((DataSetViewerTablePanel) dsv).init(updateableModel);
187: }
188: return dsv;
189: }
190: }
|