package routine.view.gui;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import javax.swing.JTable;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.event.TableModelListener;
import javax.swing.event.TableModelEvent;
import routine.model.RoutineCollection;
import routine.model.RunParams;
import org.apache.log4j.Logger;
public class RoutinesTable extends JTable {
private static org.apache.log4j.Logger log = Logger.getLogger(RoutinesTable.class);
private RoutineCollection routines;
private String columnNames[] = {"Name", "#items", "CalTag"};
private JPopupMenu popup;
public RoutinesTable() {
initialize();
}
public RoutinesTable(RoutineCollection routines) {
initialize();
this.routines = routines;
}
private void initialize() {
routines = new RoutineCollection();
this.setModel(new MyTableModel());
// Create popup menu
popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("New");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// New Listener
log.info("New");
}
});
popup.add(menuItem);
//Add listener to components that can bring up popup menus.
MouseListener popupListener = new PopupListener();
addMouseListener(popupListener);
}
public void setRoutines(RoutineCollection routines) {
this.routines = routines;
}
private class MyTableModel extends AbstractTableModel {
// den h�r table s� skall hand och eventuellt senare en str�ng vad han gjorde med handen.
// tid n�r b�rjade, bordsnamn, hur m�nga h�nder, spel, limit och vinst/f�rlust.
public MyTableModel() {
itemRevaluate();
}
public void itemRevaluate() {
if (routines.getSize() == 0)
routines.addRunParams(new RunParams());
fireTableDataChanged();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return routines.getRunParams().size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
switch (col) {
case 0 :
if (routines.getRunParams().get(row).getRoutine().getName() != null)
return routines.getRunParams().get(row).getRoutine().getName();
else return "";
case 1 :
return routines.getRunParams().get(row).getRoutine().getSize();
case 2 :
return routines.getRunParams().get(row).getCalendarTag();
default : throw new IllegalArgumentException("Wrong column");
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0)
return true;
else return false;
}
public void setValueAt(Object value, int row, int col) {
if (col == 0) {
routines.get(row).getRoutine().setName((String)value);
}
fireTableCellUpdated(row, col);
}
}
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
}