001: package net.sourceforge.squirrel_sql.client.gui.db.aliasproperties;
002:
003: /*
004: * Copyright (C) 2002-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: import java.sql.DriverPropertyInfo;
022:
023: import javax.swing.table.AbstractTableModel;
024:
025: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverProperty;
026: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
027: import net.sourceforge.squirrel_sql.fw.util.StringManager;
028: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
029: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
030: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
031:
032: class DriverPropertiesTableModel extends AbstractTableModel {
033: private static final long serialVersionUID = 1L;
034:
035: /** Internationalized strings for this class. */
036: private static final StringManager s_stringMgr = StringManagerFactory
037: .getStringManager(DriverPropertiesTableModel.class);
038:
039: interface IColumnIndexes {
040: int IDX_NAME = 0;
041: int IDX_SPECIFY = 1;
042: int IDX_VALUE = 2;
043: int IDX_REQUIRED = 3;
044: int IDX_DESCRIPTION = 4;
045: }
046:
047: /** Number of columns in model. */
048: private static final int COLUMN_COUNT = 5;
049:
050: /** Logger for this class. */
051: private static final ILogger s_log = LoggerController
052: .createLogger(DriverPropertiesTableModel.class);
053:
054: transient private SQLDriverPropertyCollection _props = new SQLDriverPropertyCollection();
055:
056: DriverPropertiesTableModel(SQLDriverPropertyCollection props) {
057: super ();
058: if (props == null) {
059: throw new IllegalArgumentException(
060: "SQLDriverPropertyCollection[] == null");
061: }
062:
063: load(props);
064: }
065:
066: public Object getValueAt(int row, int col) {
067: final SQLDriverProperty sdp = _props.getDriverProperty(row);
068: switch (col) {
069: case IColumnIndexes.IDX_NAME:
070: return sdp.getName();
071:
072: case IColumnIndexes.IDX_SPECIFY:
073: return Boolean.valueOf(sdp.isSpecified());
074:
075: case IColumnIndexes.IDX_VALUE:
076: return sdp.getValue();
077:
078: case IColumnIndexes.IDX_REQUIRED: {
079: // Use valueof when min supported JDK is 1.4
080: //return Boolean.valueOf(_props[row].required);
081: DriverPropertyInfo dpi = sdp.getDriverPropertyInfo();
082: if (dpi != null) {
083: return Boolean.valueOf(dpi.required);
084: }
085: return Boolean.FALSE;
086: }
087:
088: case IColumnIndexes.IDX_DESCRIPTION: {
089: DriverPropertyInfo dpi = sdp.getDriverPropertyInfo();
090: if (dpi != null) {
091: return dpi.description;
092: }
093: return s_stringMgr
094: .getString("DriverPropertiesTableModel.unknown");
095: }
096:
097: default:
098: s_log.error("Invalid column index: " + col);
099: return "???????";
100: }
101: }
102:
103: public int getRowCount() {
104: return _props.size();
105: }
106:
107: public int getColumnCount() {
108: return COLUMN_COUNT;
109: }
110:
111: public Class<?> getColumnClass(int col) {
112: switch (col) {
113: case IColumnIndexes.IDX_NAME:
114: return String.class;
115: case IColumnIndexes.IDX_SPECIFY:
116: return Boolean.class;
117: case IColumnIndexes.IDX_VALUE:
118: return String.class;
119: case IColumnIndexes.IDX_REQUIRED:
120: // return Boolean.class; // Don't show checkbox for
121: return Object.class; // output only field.
122: case IColumnIndexes.IDX_DESCRIPTION:
123: return String.class;
124: default:
125: s_log.error("Invalid column index: " + col);
126: return Object.class;
127: }
128: }
129:
130: public boolean isCellEditable(int row, int col) {
131: return col == IColumnIndexes.IDX_SPECIFY
132: || col == IColumnIndexes.IDX_VALUE;
133: }
134:
135: public void setValueAt(Object value, int row, int col) {
136: if (col == IColumnIndexes.IDX_VALUE) {
137: final SQLDriverProperty sdp = _props.getDriverProperty(row);
138: sdp.setValue(value.toString());
139: } else if (col == IColumnIndexes.IDX_SPECIFY) {
140: final SQLDriverProperty sdp = _props.getDriverProperty(row);
141: Boolean bool = Boolean.valueOf(value.toString());
142: sdp.setIsSpecified(bool.booleanValue());
143: } else {
144: throw new IllegalStateException(
145: "Can only edit value/specify column. Trying to edit "
146: + col);
147: }
148: }
149:
150: SQLDriverPropertyCollection getSQLDriverProperties() {
151: return _props;
152: }
153:
154: private final void load(SQLDriverPropertyCollection props) {
155: final int origSize = getRowCount();
156: if (origSize > 0) {
157: fireTableRowsDeleted(0, origSize - 1);
158: }
159:
160: _props = props;
161: final int newSize = getRowCount();
162: if (newSize > 0) {
163: fireTableRowsInserted(0, newSize - 1);
164: }
165: }
166: }
|