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.awt.Component;
022: import java.sql.DriverPropertyInfo;
023:
024: import javax.swing.DefaultCellEditor;
025: import javax.swing.JComboBox;
026: import javax.swing.JComponent;
027: import javax.swing.JTable;
028: import javax.swing.JTextField;
029: import javax.swing.ListSelectionModel;
030: import javax.swing.table.DefaultTableColumnModel;
031: import javax.swing.table.TableColumn;
032:
033: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverProperty;
034: import net.sourceforge.squirrel_sql.fw.sql.SQLDriverPropertyCollection;
035: import net.sourceforge.squirrel_sql.fw.util.StringManager;
036: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
037:
038: class DriverPropertiesTable extends JTable implements
039: DriverPropertiesTableModel.IColumnIndexes {
040: /** Internationalized strings for this class. */
041: private static final StringManager s_stringMgr = StringManagerFactory
042: .getStringManager(DriverPropertiesTable.class);
043:
044: DriverPropertiesTable(SQLDriverPropertyCollection props) {
045: super (new DriverPropertiesTableModel(props));
046: init();
047: }
048:
049: DriverPropertiesTableModel getTypedModel() {
050: return (DriverPropertiesTableModel) getModel();
051: }
052:
053: private void init() {
054: setColumnModel(new PropertiesTableColumnModel());
055: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
056: setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
057: getTableHeader().setResizingAllowed(true);
058: getTableHeader().setReorderingAllowed(false);
059: }
060:
061: private final class PropertiesTableColumnModel extends
062: DefaultTableColumnModel {
063: PropertiesTableColumnModel() {
064: super ();
065:
066: TableColumn tc = new TableColumn(IDX_NAME);
067: tc.setHeaderValue(s_stringMgr
068: .getString("DriverPropertiesTable.name"));
069: addColumn(tc);
070:
071: tc = new TableColumn(IDX_SPECIFY);
072: tc.setHeaderValue(s_stringMgr
073: .getString("DriverPropertiesTable.specify"));
074: addColumn(tc);
075:
076: tc = new TableColumn(IDX_VALUE, 75, null,
077: new ValueCellEditor());
078: tc.setHeaderValue(s_stringMgr
079: .getString("DriverPropertiesTable.value"));
080: addColumn(tc);
081:
082: tc = new TableColumn(IDX_REQUIRED);
083: tc.setHeaderValue(s_stringMgr
084: .getString("DriverPropertiesTable.required"));
085: addColumn(tc);
086:
087: tc = new TableColumn(IDX_DESCRIPTION);
088: tc.setHeaderValue(s_stringMgr
089: .getString("DriverPropertiesTable.description"));
090: addColumn(tc);
091: }
092: }
093:
094: private final class ValueCellEditor extends DefaultCellEditor {
095: private final JTextField _textEditor = new JTextField();
096: private final JComboBox _comboEditor = new JComboBox();
097: private JComponent _currentEditor;
098:
099: ValueCellEditor() {
100: super (new JTextField());
101: setClickCountToStart(1);
102: }
103:
104: public Component getTableCellEditorComponent(JTable table,
105: Object value, boolean isSelected, int row, int col) {
106: if (col != IDX_VALUE) {
107: throw new IllegalStateException(
108: "Editor used for cell other than value");
109: }
110:
111: SQLDriverPropertyCollection coll = getTypedModel()
112: .getSQLDriverProperties();
113: SQLDriverProperty sdp = coll.getDriverProperty(row);
114: DriverPropertyInfo prop = sdp.getDriverPropertyInfo();
115: if (prop.choices != null && prop.choices.length > 0) {
116: _comboEditor.removeAllItems();
117: for (int i = 0; i < prop.choices.length; ++i) {
118: _comboEditor.addItem(prop.choices[i]);
119: }
120: if (sdp.getValue() != null) {
121: _comboEditor.setSelectedItem(sdp.getValue());
122: }
123: _currentEditor = _comboEditor;
124: } else {
125: _textEditor.setText(sdp.getValue());
126: _currentEditor = _textEditor;
127: }
128: return _currentEditor;
129: }
130:
131: public Object getCellEditorValue() {
132: if (_currentEditor == _comboEditor) {
133: return _comboEditor.getSelectedItem();
134: }
135: return _textEditor.getText();
136: }
137: }
138: }
|