📄 minitable.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.minigrid;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.*;
import java.sql.*;
import java.math.*;
import java.util.*;
import org.compiere.swing.*;
import org.compiere.util.*;
import org.compiere.grid.ed.*;
import java.beans.*;
/**
* Mini Table.
* Default Read Only Table for Boolean, String, Number, Timestamp values
* <p>
* After initializing the Table Model, you need to call setColumnClass,
* add columns via addColumn or in one go prepare the table.
* <code>
* Minitable mt = new MiniTable();
* String sql = mt.prepareTable(..); // table defined
* while() {
* // add where to the sql statement
* ResultSet rs = ..
* mt.loadTable(rs);
* rs.close();
* }
* </code>
* @author Jorg Janke
* @version $Id: MiniTable.java,v 1.10 2002/08/22 04:20:29 jjanke Exp $
*/
public class MiniTable extends CTable
{
/**
* Default Constructor
*/
public MiniTable()
{
super();
// Log.trace(Log.l4_Data, "MiniTable");
setCellSelectionEnabled(false);
setRowSelectionAllowed(false);
// Default Editor
this.setCellEditor(new ROCellEditor());
} // MiniTable
/** List of R/W columns */
private ArrayList m_readWriteColumn = new ArrayList();
/** List of Column Width */
private ArrayList m_minWidth = new ArrayList();
/** Color Column Index of Model */
private int m_colorColumnIndex = -1;
/** Color Column compare data */
private Object m_colorDataCompare = Env.ZERO;
/** Multi Selection mode (default false) */
private boolean m_multiSelection = false;
/** Lauout set in prepareTable and used in loadTable */
private ColumnInfo[] m_layout = null;
/**
* Size Columns.
* Uses Mimimum Column Size
*/
public void autoSize()
{
Log.trace(Log.l4_Data, "MiniTable.autoSize");
//
final int SLACK = 8; // making sure it fits in a column
final int MAXSIZE = 300; // max size of a column
//
TableModel model = this.getModel();
int size = model.getColumnCount();
// for all columns
for (int col = 0; col < size; col++)
{
// Column & minimum width
TableColumn tc = this.getColumnModel().getColumn(col);
int width = ((Integer)m_minWidth.get(col)).intValue();
// Log.trace(Log.l4_Data, "Column=" + col + " " + column.getHeaderValue());
// Header
TableCellRenderer renderer = tc.getHeaderRenderer();
if (renderer == null)
renderer = new DefaultTableCellRenderer();
Component comp = renderer.getTableCellRendererComponent
(this, tc.getHeaderValue(), false, false, 0, 0);
// Log.trace(Log.l5_DData, "Hdr - preferred=" + comp.getPreferredSize().width + ", width=" + comp.getWidth());
width = Math.max(width, comp.getPreferredSize().width + SLACK);
// Cells
int maxRow = Math.min(30, getRowCount()); // first 30 rows
for (int row = 0; row < maxRow; row++)
{
renderer = getCellRenderer(row, col);
comp = renderer.getTableCellRendererComponent
(this, getValueAt(row, col), false, false, row, col);
int rowWidth = comp.getPreferredSize().width + SLACK;
width = Math.max(width, rowWidth);
}
// Width not greater ..
width = Math.min(MAXSIZE, width);
tc.setPreferredWidth(width);
// Log.trace(Log.l5_DData, "width=" + width);
} // for all columns
} // autoSize
/**
* Is Cell Editable
* @param row row
* @param column column
* @return true if editable
*/
public boolean isCellEditable(int row, int column)
{
// if the first column is a boolean and it is false, it is not editable
if (column != 0
&& getValueAt(row, 0) instanceof Boolean
&& !((Boolean)getValueAt(row, 0)).booleanValue())
return false;
// is the column RW?
if (m_readWriteColumn.contains(new Integer(column)))
return true;
return false;
} // isCellEditable
/**
* Set Column to ReadOnly
* @param column column
* @param readOnly read only
*/
public void setColumnReadOnly (int column, boolean readOnly)
{
// Column is ReadWrite
if (m_readWriteColumn.contains(new Integer(column)))
{
// Remove from list
if (readOnly)
{
int size = m_readWriteColumn.size();
for (int i = 0; i < size; i++)
{
if (((Integer)m_readWriteColumn.get(i)).intValue() == column)
{
m_readWriteColumn.remove(i);
break;
}
}
} // ReadOnly
}
// current column is R/O - ReadWrite - add to list
else if (!readOnly)
m_readWriteColumn.add(new Integer(column));
} // setColumnReadOnly
/*************************************************************************/
/**
* Prepare Table and return SQL
*
* @param layout array of column info
* @param from SQL FROM content
* @param where SQL WHERE content
* @param multiSelection multiple selections
* @return SQL
*/
public String prepareTable(ColumnInfo[] layout, String from, String where, boolean multiSelection)
{
m_layout = layout;
m_multiSelection = multiSelection;
//
StringBuffer sql = new StringBuffer ("SELECT ");
// add columns & sql
for (int i = 0; i < layout.length; i++)
{
// create sql
if (i > 0)
sql.append(", ");
sql.append(layout[i].getColSQL());
// adding ID column
if (layout[i].isKeyPairCol())
sql.append(",").append(layout[i].getKeyPairColSQL());
// add to model
addColumn(layout[i].getColHeader());
if (layout[i].isColorColumn())
setColorColumn(i);
if (layout[i].getColClass() == IDColumn.class)
p_keyColumnIndex = i;
}
// set editors (two steps)
for (int i = 0; i < layout.length; i++)
setColumnClass(i, layout[i].getColClass(), layout[i].isReadOnly(), layout[i].getColHeader());
sql.append( " FROM ").append(from);
sql.append(" WHERE ").append(where);
// Table Selection
setRowSelectionAllowed(true);
//
return sql.toString();
} // prepareTable
/**
* Add Table Column.
* after adding a column, you need to set the column classes again
* (DefaultTableModel fires TableStructureChanged, which calls
* JTable.tableChanged .. createDefaultColumnsFromModel
* @param header header
*/
public void addColumn (String header)
{
// Log.trace(Log.l4_Data, "MiniTable.addColumn", header);
int index = getColumnCount();
if (getModel() instanceof DefaultTableModel)
{
DefaultTableModel model = (DefaultTableModel)getModel();
model.addColumn(header);
}
else
throw new IllegalArgumentException("Model must be instance of DefaultTableModel");
} // addColumn
/**
* Set Column Editor & Renderer to Class.
* (after all columns were added)
* @param index column index
* @param c class of column - determines renderere
* @param readOnly read only flag
*/
public void setColumnClass (int index, Class c, boolean readOnly)
{
setColumnClass(index, c, readOnly, null);
} // setColumnClass
/**
* Set Column Editor & Renderer to Class
* (after all columns were added)
* Lauout of IDColumn depemds on multiSelection
* @param index column index
* @param c class of column - determines renderere/editors supported:
* IDColumn, Boolean, Double (Quantity), BigDecimal (Amount), Integer, Timestamp, String (default)
* @param readOnly read only flag
* @param header optional header value
*/
public void setColumnClass (int index, Class c, boolean readOnly, String header)
{
// Log.trace(Log.l4_Data, "MiniTable.setColumnClass - " + index, c.getName() + ", r/o=" + readOnly);
TableColumn tc = getColumnModel().getColumn(index);
if (tc == null)
return;
// Set R/O
setColumnReadOnly(index, readOnly);
// Header
if (header != null && header.length() > 0)
tc.setHeaderValue(header);
// ID Column & Selection
if (c == IDColumn.class)
{
tc.setCellRenderer(new IDColumnRenderer(m_multiSelection));
if (m_multiSelection)
tc.setCellEditor(new IDColumnEditor());
else
tc.setCellEditor(new ROCellEditor());
m_minWidth.add(new Integer(10));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -