View Javadoc

1   /*
2    * Created on 11-May-2005
3    */
4   package ca.spaz.cron.ui;
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.database.Food;
14  import ca.spaz.gui.PrettyTable;
15  
16  public class NutrientEditorTable extends JScrollPane {
17  
18     NutrientTableModel model;
19     PrettyTable nutrientTable;
20     
21     public NutrientEditorTable(List nutrients) {
22        model = new NutrientTableModel(nutrients);
23        setViewportView(getNutrientTable());
24        getViewport().setBackground(Color.WHITE);
25        setPreferredSize(new Dimension(300, 200));
26     }
27     
28     public void setMultiplier(double val) {
29        model.setMultiplier(val);
30     }
31     
32     public void setFood(Food f) {
33        model.setFood(f);
34     }
35     
36     private JTable getNutrientTable() {
37        if (null == nutrientTable) {
38           nutrientTable = new PrettyTable();
39           nutrientTable.setModel(model);
40           nutrientTable.getSelectionModel().setSelectionMode(
41                 ListSelectionModel.SINGLE_SELECTION);
42           nutrientTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
43           nutrientTable.getTableHeader().setReorderingAllowed(false);
44           nutrientTable.getSelectionModel().addListSelectionListener(
45                 new ListSelectionListener() {
46                    public void valueChanged(ListSelectionEvent e) {
47                       if (e.getValueIsAdjusting())
48                          return;
49                       ListSelectionModel lsm = (ListSelectionModel) e
50                             .getSource();
51                       if (!lsm.isSelectionEmpty()) {
52                          int selectedRow = lsm.getMinSelectionIndex();
53                       }
54                    }
55                 });
56           // right align last column
57           TableColumnModel tcm = nutrientTable.getColumnModel();
58           TableColumn column = tcm.getColumn(0);
59           column.setMinWidth(150);
60        }
61        return nutrientTable;
62     }
63  
64  
65  
66  }