View Javadoc

1   /*
2    * Created on 4-Jun-2005
3    */
4   package ca.spaz.cron.targets;
5   
6   import java.awt.*;
7   import java.util.List;
8   
9   import javax.swing.*;
10  import javax.swing.event.*;
11  import javax.swing.table.*;
12  
13  import ca.spaz.cron.user.User;
14  import ca.spaz.gui.PrettyTable;
15  
16  public class TargetEditorTable extends JScrollPane {
17  
18     TargetEditorTableModel model;
19     PrettyTable nutrientTable;
20     
21     public TargetEditorTable(User user, List nutrients) {
22        model = new TargetEditorTableModel(user, nutrients);
23        setViewportView(getTable());
24        getViewport().setBackground(Color.WHITE);
25        setPreferredSize(new Dimension(300, 200));
26     }
27  
28     private JTable getTable() {
29        if (null == nutrientTable) {
30           nutrientTable = new PrettyTable();
31           nutrientTable.setModel(model);
32           nutrientTable.getSelectionModel().setSelectionMode(
33                 ListSelectionModel.SINGLE_SELECTION);
34           nutrientTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
35           nutrientTable.getTableHeader().setReorderingAllowed(false);
36           nutrientTable.getSelectionModel().addListSelectionListener(
37              new ListSelectionListener() {
38                 public void valueChanged(ListSelectionEvent e) {
39                    if (e.getValueIsAdjusting())
40                       return;
41                    ListSelectionModel lsm = (ListSelectionModel) e
42                          .getSource();
43                    if (!lsm.isSelectionEmpty()) {
44                       int selectedRow = lsm.getMinSelectionIndex();
45                    }
46                 }
47              });
48           // right align last column
49           TableColumnModel tcm = nutrientTable.getColumnModel();
50           DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
51           TableColumn column = tcm.getColumn(0);
52           renderer.setHorizontalAlignment(SwingConstants.RIGHT);
53           column.setCellRenderer(renderer);
54        }
55        return nutrientTable;
56     }
57     
58     public void fireTargetsChanged() {
59        model.fireTableDataChanged();
60     }
61  
62  }