View Javadoc

1   /*
2    * Created on 4-Jun-2005
3    */
4   package ca.spaz.cron.targets;
5   
6   import java.util.List;
7   
8   import javax.swing.table.AbstractTableModel;
9   
10  import ca.spaz.cron.database.NutrientInfo;
11  import ca.spaz.cron.user.User;
12  
13  public class TargetEditorTableModel extends AbstractTableModel {
14  
15     private static final int NAME_COLUMN = 0;
16     private static final int TARGET_MIN = 1;
17     private static final int TARGET_MAX = 2;   
18     private static final int UNIT_COLUMN = 3;
19  
20     private String[] columnNames = { "Nutrient", "Minimum", "Maximum", "Units"};
21     List nutrients;
22     User user;
23  
24     /***
25      * @param user the user whos targets we're displaying/editing
26      * @param nutrientInfo list of nutrients to display targets for
27      */
28     public TargetEditorTableModel(User user, List nutrientInfo) {
29        this.user = user;
30        this.nutrients = nutrientInfo;
31     }
32  
33     public Class getColumnClass(int col) {
34        Object o = getValueAt(0, col);
35        if (o != null) {
36           return o.getClass();
37        }
38        return String.class;
39     }
40  
41     public int getColumnCount() {
42        return columnNames.length;
43     }
44  
45     public String getColumnName(int col) {
46        return columnNames[col].toString();
47     }
48  
49     public NutrientInfo getNutrientInfo(int i) {
50        return (NutrientInfo) nutrients.get(i);
51     }
52  
53     public int getRowCount() {
54        return nutrients.size();
55     }
56  
57     public Object getValueAt(int row, int col) {
58        NutrientInfo ni = getNutrientInfo(row);
59        if (ni != null) {
60           Target target = user.getTarget(ni);
61           switch (col) {
62           case NAME_COLUMN:
63              return ni.getName();
64           case TARGET_MIN:
65              return new Double(target.getMin());
66              //return new Double(ni.getReferenceDailyIntake());
67           case TARGET_MAX:
68              return new Double(target.getMax());
69              //return new Double(ni.getReferenceDailyIntake());
70           case UNIT_COLUMN:
71              return ni.getUnits();
72           }
73        }
74        return "";
75     }
76  
77     public boolean isCellEditable(int row, int col) {
78        NutrientInfo ni = getNutrientInfo(row);
79        if (ni != null) {
80           if (col == TARGET_MIN || col == TARGET_MAX) {
81              return true;
82           }
83        }
84        return false;
85     }
86  
87     public void setValueAt(Object value, int row, int col) {
88        if (col == TARGET_MIN && value != null) {
89           NutrientInfo ni = getNutrientInfo(row);
90           if (ni != null) {
91              double val = ((Double) value).doubleValue();
92              Target target = user.getTarget(ni);
93              target.setMin(val);
94              user.setTarget(ni, target);
95              fireTableCellUpdated(row, col);
96           }
97        }
98  
99        if (col == TARGET_MAX && value != null) {
100          NutrientInfo ni = getNutrientInfo(row);
101          if (ni != null) {
102             double val = ((Double) value).doubleValue();
103             Target target = user.getTarget(ni);
104             target.setMax(val);
105             user.setTarget(ni, target);
106             fireTableCellUpdated(row, col);
107          }
108       }
109    }
110 
111 }