package osp.utils; import java.awt.Color; import java.awt.Dimension; import java.util.Observable; import java.util.Observer; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; import osp.ui.OSPE_VisuPanelInterface; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class TableurPanel extends JPanel implements Observer { private static final long serialVersionUID = 1L; private JTable table; //private OSPE_NavigatorImageDisplay navigatorDisplay; private OSPE_VisuPanelInterface visuPanel; private TableurModel tableurModel; private int sel, num, id, ref, ra, dec, nbcat, prior, slit; private int[] colSize=null; private int idRow=0; public TableurPanel(TableurModel tableurModel, int[] colSize) { this.tableurModel = tableurModel; this.colSize = colSize; this.setPreferredSize(new Dimension(800, 100)); this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); table = new JTable(tableurModel); table.setColumnSelectionAllowed(true); table.setRowSelectionAllowed(true); table.setSelectionBackground(Color.GREEN); /* Reordering columns not allowed */ table.getTableHeader().setReorderingAllowed(false); attributeNumCol(); for (int i = 0; i < colSize.length; i++) { if (colSize[i] != Integer.MAX_VALUE) { String columnName = tableurModel.getColumnName(i); table.getColumn(columnName).setMinWidth(colSize[i]); table.getColumn(columnName).setMaxWidth(colSize[i]); table.getColumn(columnName).setPreferredWidth(colSize[i]); } } table.setAutoCreateRowSorter(true); JScrollPane scroll = new JScrollPane(table); this.add(scroll); table.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override /** * when clic on object array row, get the object selected. */ public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub int a = table.getSelectedRow(); // int colSlit=4; // slit col num in tableurModel // int slit=99; if (a>=0) { // try to higliht slit with object -- not in function // String sl = table.getValueAt(a, colSlit).toString(); // if (!sl.contentEquals("none")) // { // Integer.parseInt(sl); // } getObject(a); idRow=a; table.changeSelection(a, 0, false, false); } } }); } public void updateByHand() { table.updateUI(); } @Override public void update(Observable arg0, Object arg1) { updateByHand(); } public OSPE_VisuPanelInterface getVisuPanel() { return visuPanel; } public void setVisuPanel(OSPE_VisuPanelInterface vPanel) { visuPanel=vPanel; } /** * get the object selected on array and set it on image. * @param selectedRow : object id. */ private void getObject(int rowNb) { // System.out.println("tabPan rowNb "+rowNb); visuPanel.setImageAtObjectPosition(rowNb); } public void razTableSelect() { // TODO Auto-generated method stub table.changeSelection(idRow, 0, true, false); } public void selectCorrespondingRow(int object_id) { table.clearSelection(); for (int row = 0; row < table.getRowCount(); row++) { if ((Integer) table.getValueAt(row, tableurModel.num) == object_id) table.changeSelection(row, tableurModel.num, false, false); } } private void attributeNumCol(){ /** Set the value column number **/ num=tableurModel.num; id=tableurModel.id; ref=tableurModel.ref; ra=tableurModel.ra; dec=tableurModel.dec; nbcat=tableurModel.nbcat; prior=tableurModel.prior; slit=tableurModel.slit; } public TableurModel getModel() { return tableurModel; } /** * @return the table */ public JTable getTable() { return table; } public static void main(String[] args) { String[] colString = { "num", "isRef", "from", "RA", "DEC", "RA (recalculated)", "DEC (recalculated)", "Affected Slit" }; boolean[] colEditable = { false, true, false, false, false, false, false,false }; String[] colType = { "int", "boolean", "String", "String", "String", "String", "String", "String" }; int valMaxCol=Integer.MAX_VALUE; int[] colSize = { 30, 30,valMaxCol, valMaxCol, valMaxCol, valMaxCol, valMaxCol, 30 }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new TableurPanel(new TableurModel(colString, colEditable, colType), colSize)); frame.setVisible(true); frame.pack(); } }