package xgenerator.ui.swing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import xgenerator.ui.swing.model.DatasourceTableModel;
public class DataSourceTable extends JTable {
public static final int ROW_HEIGHT = 30;
public static final Color SELECTION_BACKGROUND_COLOR = new Color(207, 228, 249);
public static final Color SELECTION_FOREGROUND_COLOR = new Color(0, 0, 0);
public static final Color GRID_COLOR = new Color(236, 233, 216);
/**
* UI组件
*/
private Component parentComponent;
public DataSourceTable(Component parentComponent) {
this.parentComponent = parentComponent;
this.initComponents();
}
public DataSourceTable(int numRows, int numColumns) {
super(numRows, numColumns);
this.initComponents();
}
private void initComponents() {
this.setPreferredScrollableViewportSize(new Dimension(550, 30));
this.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
this.setAutoCreateRowSorter(true);
this.setRowHeight(ROW_HEIGHT);
this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// this.setColumnSelectionAllowed(true); //设置表格的的选取以单元格为单位
this.setSelectionBackground(SELECTION_BACKGROUND_COLOR);
this.setSelectionForeground(SELECTION_FOREGROUND_COLOR);
this.setGridColor(GRID_COLOR);
this.updateDataVector();
}
public void setPreferredTableColumnStyle() {
TableColumnModel tableColumnModel = this.getColumnModel(); // 列模型
TableColumn column = null;
for (int i = 0; i < tableColumnModel.getColumnCount(); i++) {
column = tableColumnModel.getColumn(i);
if(0 == i) {
column.setPreferredWidth(150);
} else if(1 == i) {
column.setPreferredWidth(300);
} else if(2 == i) {
column.setPreferredWidth(300);
} else {
column.setPreferredWidth(80);
}
}
}
public void updateDataVector() {
Object[][] data = {
{"EAP_DS", "oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL", "sys", "sys"},
{"Sybase_DS", "oracle.jdbc.driver.OracleDriver", "jdbc:sybase:Tds:192.168.70.157:4100/staticdb?charset=cp936", "sys", "sys"},
{"SqlServer2000_DS", "com.microsoft.jdbc.sqlserver.SQLServerDriver", "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=shoppingCar", "scott", "tiger"},
{"MySQL_DS", "com.mysql.jdbc.Driver", "jdbc:mysql://[HOST]:[PORT]/[database_name]", "scott", "tiger"},
{"DB2_DS", "com.ibm.db2.jdbc.app.DB2Driver", "jdbc:db2://[SERVER_NAME]:[PORT]/[DATABASE_NAME]", "scott", "tiger"}
};
DefaultTableModel tableModel = new DatasourceTableModel();
tableModel.setDataVector(data, DatasourceTableModel.COLUMN_NAMES);
this.setModel(tableModel);
this.setPreferredTableColumnStyle();
this.revalidate();
this.updateUI();
}
/**
* <p>
* Title:
* </p>
* @author <a href="mailto:shushanlee@msn.com">liss</a>
* @param row
* @param column
* @return
* @see javax.swing.JTable#getCellRenderer(int, int)
*/
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
return new MetadataTableCellRenderer();
}
public Component getParentComponent() {
return parentComponent;
}
public void setParentComponent(Component parentComponent) {
this.parentComponent = parentComponent;
}
}