package de.bwl.uis.module.vaws.test;
/*
* TableSortDemo.java requires no other files.
*/
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class TableSortDemo extends JPanel
{
public TableSortDemo()
{
super(new GridLayout(1,0));
ExtJTable table = new ExtJTable();
table.setModel(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 100));
table.setFillsViewportHeight(true);
table.setSortierung(true);
table.setHeaderLabels(new MyTableModel().columnNames);
table.setContents( new MyTableModel().data );
table.setRowSelectionInterval(0,0);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.getViewport().add(table, null);
//Add the scroll pane to this panel.
add(scrollPane);
}
class MyTableModel extends AbstractTableModel
{
public String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
public Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)},
};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col)
{
data[row][col] = value;
}
}
public class ExtJTable extends JTable
{
private String[] headerLabels;
private boolean sortierung = false;
public ExtJTable()
{
this.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
this.getTableHeader().setReorderingAllowed(false);
}
public void setHeaderLabels(String[] pHeaderLabels)
{
headerLabels = pHeaderLabels;
}
public void setContents(final Object[][] pContents)
{
TableModel dataModel = new AbstractTableModel()
{
public int getColumnCount() { return headerLabels.length; }
public int getRowCount() { return pContents.length;}
public Object getValueAt(int row, int col) {return pContents[row][col];}
public String getColumnName(int column) {return headerLabels[column];}
public Class getColumnClass(int c)
{
Object o = this.getValueAt(0, c);
if (o != null)
{
return o.getClass();
}
else
{
return Object.class;
}
}
public boolean isCellEditable(int row, int col)
{
return false;
}
public void setValueAt(Object aValue, int row, int column)
{
pContents[row][column] = aValue;
}
};
this.setModel(dataModel);
}
/**
* Bei Sortierung muss der RowIndex in den passenden Index im Model
* konvertiert werden
*/
public int getSelectedRow()
{
int selectedRowIndex = super.getSelectedRow();
if (sortierung)
{
if (selectedRowIndex < 0)
{
return selectedRowIndex;
}
selectedRowIndex = this.convertRowIndexToModel(selectedRowIndex);
}
return selectedRowIndex;
}
public int[] getSelectedRows()
{
int[] tJetztIndexe = super.getSelectedRows();
if (sortierung)
{ // analog getSelectedRow()
int[] tReturn = new int[tJetztIndexe.length];
for (int i = 0; i < tJetztIndexe.length; i++)
{
int tJetztIndex = tJetztIndexe[i];
//tReturn[i] = sorter.getRealIndex(tJetztIndex);
tReturn[i] = this.convertRowIndexToModel(tJetztIndex);
}
return tReturn;
}
return tJetztIndexe;
}
public void setSortierung(boolean pSortierung)
{
sortierung = pSortierung;
this.setAutoCreateRowSorter(sortierung);
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TableSortDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableSortDemo newContentPane = new TableSortDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}