📄 tabledefinitionpanel.java
字号:
/* * TableDefinitionPanel.java * * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */package org.executequery.gui.table;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.FocusListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Vector;import javax.swing.DefaultCellEditor;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.SwingUtilities;import javax.swing.event.ChangeEvent;import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;import javax.swing.table.TableCellEditor;import javax.swing.table.TableColumnModel;import javax.swing.table.TableModel;import org.executequery.GUIUtilities;import org.underworldlabs.swing.print.AbstractPrintableTableModel;import org.executequery.gui.browser.ColumnData;import org.executequery.components.table.BrowsingCellEditor;import org.executequery.gui.DefaultTable;import org.executequery.util.Log;import org.underworldlabs.swing.table.NumberCellEditor;import org.underworldlabs.swing.table.StringCellEditor;/* ---------------------------------------------------------- * CVS NOTE: Changes to the CVS repository prior to the * release of version 3.0.0beta1 has meant a * resetting of CVS revision numbers. * ---------------------------------------------------------- *//** * * @author Takis Diakoumis * @version $Revision: 1.7 $ * @date $Date: 2006/07/16 15:46:54 $ */public abstract class TableDefinitionPanel extends JPanel implements TableModelListener { /** The table containing all column descriptions */ protected DatabaseTable table; /** The table's _model */ protected CreateTableModel _model; /** The cell editor for the column names */ protected static StringCellEditor colNameEditor; /** The cell editor for the column size */ protected NumberCellEditor sizeEditor; /** The cell editor for the column scale */ protected NumberCellEditor scaleEditor; /** The cell editor for the datatype column */ //protected ComboBoxCellEditor comboCell; /** The cell editor for the datatype column */ protected DataTypeSelectionTableCell dataTypeCell; /** The <code>Vector</code> of <code>ColumnData</code> objects */ protected Vector<ColumnData> tableVector; /** The literal 'PK' */ private static final String PRIMARY = "PK"; /** The literal 'FK' */ private static final String FOREIGN = "FK"; /** The literal 'FK' */ private static final String PRIMARY_AND_FOREIGN = "PKFK"; /** An empty String literal */ private static final String EMPTY = " "; protected boolean editing; /** the available data types */ private String[] dataTypes; public TableDefinitionPanel() { this(true, null); } public TableDefinitionPanel(boolean editing, String[] dataTypes) { super(new GridBagLayout()); this.editing = editing; this.dataTypes = dataTypes; try { jbInit(); } catch (Exception e) { //e.printStackTrace(); } } private void jbInit() throws Exception { // set the table model to use _model = new CreateTableModel(); table = new DatabaseTable(_model); TableColumnModel tcm = table.getColumnModel(); tcm.getColumn(0).setPreferredWidth(25); //tcm.getColumn(0).setMinWidth(25); tcm.getColumn(0).setMaxWidth(25); tcm.getColumn(1).setPreferredWidth(200); tcm.getColumn(2).setPreferredWidth(130); tcm.getColumn(3).setPreferredWidth(50); tcm.getColumn(4).setPreferredWidth(50); //tcm.getColumn(5).setPreferredWidth(60); tcm.getColumn(5).setPreferredWidth(70); tcm.getColumn(5).setMaxWidth(70); tcm.getColumn(0).setCellRenderer(new KeyCellRenderer()); // add the editors if editing if (editing) { colNameEditor = new StringCellEditor(); DefaultCellEditor colStrEditor = new DefaultCellEditor(colNameEditor) { public Object getCellEditorValue() { return colNameEditor.getValue(); } }; tcm.getColumn(1).setCellEditor(colStrEditor); //tcm.getColumn(5).setCellEditor(colStrEditor); scaleEditor = new NumberCellEditor(); DefaultCellEditor scEditor = new DefaultCellEditor(scaleEditor) { public Object getCellEditorValue() { return scaleEditor.getStringValue(); } }; sizeEditor = new NumberCellEditor(); DefaultCellEditor szEditor = new DefaultCellEditor(sizeEditor) { public Object getCellEditorValue() { return sizeEditor.getStringValue(); } }; tcm.getColumn(3).setCellEditor(szEditor); tcm.getColumn(4).setCellEditor(scEditor); dataTypeCell = new DataTypeSelectionTableCell(); tcm.getColumn(2).setCellRenderer(dataTypeCell); tcm.getColumn(2).setCellEditor(dataTypeCell); // create the key listener to notify changes KeyAdapter valueKeyListener = new KeyAdapter() { public void keyReleased(KeyEvent e) { String value = null; Object object = e.getSource(); if (object == colNameEditor) { value = colNameEditor.getValue(); } else if (object == sizeEditor) { value = sizeEditor.getEditorValue(); } else if (object == scaleEditor) { value = scaleEditor.getEditorValue(); } else if (object == dataTypeCell.getComponent()) { value = dataTypeCell.getEditorValue(); } tableChanged(table.getEditingColumn(), table.getEditingRow(), value); } }; colNameEditor.addKeyListener(valueKeyListener); dataTypeCell.addKeyListener(valueKeyListener); sizeEditor.addKeyListener(valueKeyListener); scaleEditor.addKeyListener(valueKeyListener); _model.addTableModelListener(this); } //setPreferredSize(new Dimension(460, 150)); //setPreferredSize(new Dimension(500, 150)); add(new JScrollPane(table), new GridBagConstraints( 1, 1, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0)); } public void setColumnDataArray(ColumnData[] cda) { _model.setColumnDataArray(cda); } public void setColumnDataArray(ColumnData[] cda, String[] dataTypes) { _model.setColumnDataArray(cda); this.dataTypes = dataTypes; /* if (dataTypes != null) { comboCell.setSelectionValues(dataTypes); } */ } /** * Sets the available data types to the values specified. * * @param the data type values */ public void setDataTypes(String[] dataTypes) { this.dataTypes = dataTypes; /* if (dataTypes != null) { comboCell.setSelectionValues(dataTypes); } */ } public void tableChanged(TableModelEvent e) { //Log.debug("tableChanged"); int row = table.getEditingRow(); if (row == -1) { return; } tableChanged(table.getEditingColumn(), row, null); } /** * Fires that a table cell value has changed as specified. * * @param col - the column index * @param row - the row index * @param value - the current value */ public abstract void tableChanged(int col, int row, String value); /** <p>Adds all the column definition lines to * the SQL text buffer for display. * * @param the current row being edited */ public abstract void addColumnLines(int row); /** <p>Moves the selected column up one row within * the table moving the column above the selection * below the selection. */ public void moveColumnUp() { int selection = table.getSelectedRow(); if (selection == -1 || selection == 0) { return; } table.editingStopped(null); if (table.isEditing()) { table.removeEditor(); } int newPostn = selection - 1; ColumnData move = tableVector.elementAt(selection); tableVector.removeElementAt(selection); tableVector.add(newPostn, move); table.setRowSelectionInterval(newPostn, newPostn); _model.fireTableRowsUpdated(newPostn, selection); addColumnLines(-1); } public void tableEditingStopped(ChangeEvent e) { table.editingStopped(e); } public int getEditingRow() { return table.getEditingRow(); } public void setEditingRow(int newEditingRow) { table.setEditingRow(newEditingRow); } public int getSelectedRow() { return table.getSelectedRow(); } public int getEditingColumn() { return table.getEditingColumn(); } public void addTableFocusListener(FocusListener listener) { table.addFocusListener(listener); } /** <p>Propogates the call to <code>removeEditor()</code> * on the table displaying the data. */ public void removeEditor() { table.removeEditor(); } /** <p>Propogates the call to <code>isEditing()</code> * on the table displaying the data. * * @return if a data edit is in progress on the table */ public boolean isEditing() { return table.isEditing(); } /** <p>Returns the table displaying the * column data. * * @return the table displaying the data */ public JTable getTable() { return table; } /** <p>Moves the selected column down one row within * the table moving the column below the selection * above the selection. */ public void moveColumnDown() { int selection = table.getSelectedRow(); if (selection == -1 || selection == tableVector.size() - 1) { return; } table.editingStopped(null); if (table.isEditing()) { table.removeEditor(); } int newPostn = selection + 1; ColumnData move = tableVector.elementAt(selection); tableVector.removeElementAt(selection); tableVector.add(newPostn, move); table.setRowSelectionInterval(newPostn, newPostn); _model.fireTableRowsUpdated(selection, newPostn); addColumnLines(-1); } /** <p>Inserts a new column before the selected * column moving the selected column down one row. */ public void insertBefore() { fireEditingStopped(); if (table.isEditing()) { table.removeEditor(); } int selection = table.getSelectedRow(); if (selection == -1) { return; } else { tableVector.insertElementAt(new ColumnData(), selection); } _model.fireTableRowsInserted( selection == 0 ? 0 : selection -1, selection == 0 ? 1 : selection); table.setRowSelectionInterval(selection, selection); table.setColumnSelectionInterval(1, 1); table.setEditingRow(selection); table.setEditingColumn(1); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -