/** */ /** * */ package com.jinfonet.loganalyzer.ui.viewers; import java.awt.Color; import java.awt.Component; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.sql.ResultSet; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.DefaultCellEditor; import javax.swing.JCheckBox; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.border.BevelBorder; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; /** */ /** * @author gengwei.zheng * */ public class DataTable extends JTable ... { /** *//** * serialVersionUID = -6699518565625290401L */ private static final long serialVersionUID = -6699518565625290401L; private static final int CHECK = 0; private static final String COL_ITEM_CHECK = ""; private OperateDataTableModel tableModel; private TableColumnModel tableColumnModel; private String tableName = null; public DataTable(ResultSet rs) ...{ tableModel = new OperateDataTableModel(rs); setModel(tableModel); setAutoResizeMode(JTable.AUTO_RESIZE_OFF); getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); getTableHeader().setReorderingAllowed(false); paintColumn(); JTableHeader tableHeader = getTableHeader(); tableColumnModel = tableHeader.getColumnModel(); tableHeader.addMouseListener( new MouseAdapter() ...{ public void mouseClicked(MouseEvent e) ...{ TableColumn column; DataTableHeaderRenderer render; int idx = tableColumnModel.getColumnIndexAtX(e.getX()); if (idx == CHECK ) ...{ column = getColumnModel().getColumn(idx); render = (DataTableHeaderRenderer) column.getHeaderRenderer(); Boolean selectAll = new Boolean(render.changeStatus()); for (int i = 0; i < tableModel.getRowCount(); i++) ...{ tableModel.setValueAt(selectAll, i, idx); } tableModel.fireTableDataChanged(); } getTableHeader().resizeAndRepaint(); } }); addMouseListener( new MouseAdapter() ...{ public void mouseClicked(MouseEvent e) ...{ int index = getSelectedRow(); ((DetailViewListModel)LogAnalyzer.getTheApp().getLAFrame().getDetailViewList().getModel()).update(getSelectedContent(index)); LogAnalyzer.getTheApp().getLAFrame().getDetailViewList().updateUI(); } }); } private void paintColumn() ...{ TableColumn column = getColumnModel().getColumn(CHECK); if (column.getCellEditor() == null) column.setCellEditor(new DataTableCellEditor(new JCheckBox())); if (column.getCellRenderer() == null) column.setCellRenderer(new DataTableCellRenderer()); column.setHeaderRenderer(new DataTableHeaderRenderer()); column.setHeaderValue(COL_ITEM_CHECK); column.setPreferredWidth(40); } private boolean getSelectedValue(int index, JTable table) ...{ boolean isSelected = true; for (int i = 0; i < table.getModel().getRowCount(); i++) ...{ Object value = table.getValueAt(i, index); if ((value instanceof Boolean) && isSelected == ((Boolean) value).booleanValue()) ...{ continue; } else ...{ isSelected = false; break; } } return isSelected; } public OperateDataTableModel getTableModel() ...{ return tableModel; } public void clearLogs() ...{ tableModel.deleteRows(); } public void clearAllLogs() ...{ tableModel.deleteAllRows(); } public void refreshLogs() ...{ tableModel.refresh(); } public void refetchData(ResultSet newResultSet) ...{ tableModel.refetchData(newResultSet); } public void setTableName(String name) ...{ this.tableName = name; } public String getTableName() ...{ return this.tableName; } public void filter() ...{ int n = getRowCount(); tableModel.filter(); for (int i = 0; i < n; i++) ...{ if (getValueAt(i, 0).toString().equalsIgnoreCase("false")) setRowHeight(i, 1); } LogAnalyzer.getTheApp().getLAFrame().setFilteredLogsStatus(tableModel.getFilteredLogs()); } public void showAllRecords() ...{ int n = getRowCount(); for (int i = 0; i < n; i++) ...{ setRowHeight(i, 16); } LogAnalyzer.getTheApp().getLAFrame().setFilteredLogsStatus(0); } public Vector getSelectedContent(int index) ...{ String title = null; Object item = null; Vector content = new Vector(); for (int i = 0; i < tableModel.getColumnNames().size(); i++) ...{ title = tableModel.getColumnNames().get(i).toString(); item = tableModel.getValueAt(index, i + 1); if (item == null) content.add(new DetailViewListItem(title, "")); else content.add(new DetailViewListItem(title, item.toString())); } return content; } private class DataTableCellEditor extends DefaultCellEditor ...{ /** *//** * serialVersionUID = -2471789138026487761L */ private static final long serialVersionUID = -2471789138026487761L; public DataTableCellEditor(JCheckBox checkBox) ...{ super(checkBox); checkBox.setBackground(Color.white); setClickCountToStart(1); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) ...{ ((JCheckBox) editorComponent).setSelected(((Boolean) value).booleanValue()); ((JCheckBox) editorComponent).setHorizontalAlignment(SwingConstants.CENTER); return editorComponent; } } private class DataTableCellRenderer implements TableCellRenderer ...{ private JCheckBox checkBox = new JCheckBox(); public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) ...{ checkBox.setBackground(Color.white); checkBox.setSelected(((Boolean) value).booleanValue()); checkBox.setHorizontalAlignment(SwingConstants.CENTER); table.getTableHeader().resizeAndRepaint(); return checkBox; } } private class DataTableHeaderRenderer extends DefaultTableCellRenderer ...{ /** *//** * serialVersionUID = -7760619638465285386L */ private static final long serialVersionUID = -7760619638465285386L; private JCheckBox checkBox = new JCheckBox(); private boolean status = false; public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) ...{ status = getSelectedValue(column, table); checkBox.setText((String) value); checkBox.setSelected(status); checkBox.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED)); checkBox.setBorderPainted(true); checkBox.setHorizontalTextPosition(SwingConstants.LEFT); checkBox.setHorizontalAlignment(SwingConstants.CENTER); checkBox.setEnabled(table.getRowCount() > 0 ? true : false); return checkBox; } public boolean changeStatus() ...{ status = !checkBox.isSelected(); checkBox.setSelected(status); return status; } }}