001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.editor;
022:
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.AbstractCellEditor;
029: import javax.swing.JButton;
030: import javax.swing.JTable;
031: import javax.swing.ListSelectionModel;
032: import javax.swing.UIManager;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.TableModelEvent;
035: import javax.swing.table.DefaultTableCellRenderer;
036: import javax.swing.table.DefaultTableModel;
037: import javax.swing.table.TableCellEditor;
038: import javax.swing.table.TableCellRenderer;
039: import javax.swing.table.TableColumn;
040:
041: public class BaseDynamicTable extends JTable {
042: _ClauseOwner owner;
043:
044: BaseDynamicTable(_ClauseOwner owner, int col) {
045: this .owner = owner;
046: initTableModel(col);
047: }
048:
049: void initTableModel(int col) {
050: DefaultTableModel model = new DefaultTableModel(1, col + 1) {
051: public boolean isCellEditable(int row, int column) {
052: return row < (getRowCount() - 1) || column == 0;
053: }
054: };
055: setModel(model);
056:
057: setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
058: setColumnSelectionAllowed(false);
059: setRowSelectionAllowed(false);
060: setDefaultRenderer(Object.class, new InternalCellRenderer());
061: getTableHeader().setPreferredSize(new Dimension(0, 0));
062: getTableHeader().setVisible(false);
063:
064: TableColumn tableColumn = this .getColumn(0);
065: tableColumn.setCellEditor(new ButtonCell());
066: tableColumn.setCellRenderer(new ButtonCell());
067: tableColumn.setPreferredWidth(15);
068: tableColumn.setMaxWidth(15);
069: tableColumn.setResizable(false);
070: }
071:
072: protected TableColumn getColumn(int index) {
073: return this .getColumn(this .getColumnName(index));
074: }
075:
076: public int addRow() {
077: int row = this .getRowCount() - 1;
078:
079: this .editingStopped(new ChangeEvent(this ));
080: ((DefaultTableModel) this .getModel()).insertRow(row,
081: new Object[this .getColumnCount()]);
082:
083: return row;
084: }
085:
086: public void removeRow() {
087: this .editingCanceled(new ChangeEvent(this ));
088: ((DefaultTableModel) this .getModel()).removeRow(this
089: .getSelectedRow());
090: }
091:
092: public void removeRows() {
093: this .editingStopped(new ChangeEvent(this ));
094: for (int i = 0; this .getRowCount() > 1; i++)
095: ((DefaultTableModel) this .getModel()).removeRow(0);
096: }
097:
098: public final void tableChanged(TableModelEvent e) {
099: super .tableChanged(e);
100:
101: if (owner != null)
102: owner.fireQueryChanged();
103: }
104:
105: private class InternalCellRenderer extends DefaultTableCellRenderer {
106: public Component getTableCellRendererComponent(JTable table,
107: Object value, boolean isSelected, boolean hasFocus,
108: int row, int column) {
109: if (table.isCellEditable(row, column))
110: super .setBackground(UIManager
111: .getColor("Table.background"));
112: else
113: super .setBackground(UIManager
114: .getColor("TableHeader.background"));
115:
116: setFont(table.getFont());
117: setValue(value);
118:
119: return this ;
120: }
121: }
122:
123: private class ButtonCell extends AbstractCellEditor implements
124: TableCellEditor, TableCellRenderer, ActionListener {
125: private JButton button = new JButton();
126:
127: private ButtonCell() {
128: button.addActionListener(this );
129: button.setBorder(null);
130: }
131:
132: public void actionPerformed(ActionEvent e) {
133: if (button.getText().equals("+")) {
134: BaseDynamicTable.this .addRow();
135: this .setMinus();
136: } else {
137: BaseDynamicTable.this .removeRow();
138: }
139: }
140:
141: private void setMinus() {
142: button.setToolTipText("remove");
143: button.setText("-");
144: }
145:
146: private void setPlus() {
147: button.setToolTipText("add");
148: button.setText("+");
149: }
150:
151: private Component getCell(JTable table, int row) {
152: if (row == table.getRowCount() - 1)
153: this .setPlus();
154: else
155: this .setMinus();
156:
157: return button;
158: }
159:
160: public Component getTableCellEditorComponent(JTable table,
161: Object value, boolean isSelected, int row, int column) {
162: return getCell(table, row);
163: }
164:
165: public Component getTableCellRendererComponent(JTable table,
166: Object value, boolean isSelected, boolean hasFocus,
167: int row, int column) {
168: return getCell(table, row);
169: }
170:
171: public Object getCellEditorValue() {
172: return button.getText();
173: }
174: }
175: }
|