1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
package ca.spaz.cron.ui; |
5 |
|
|
6 |
|
import java.util.List; |
7 |
|
|
8 |
|
import javax.swing.table.AbstractTableModel; |
9 |
|
|
10 |
|
import ca.spaz.cron.database.*; |
11 |
|
|
12 |
|
public class NutrientTableModel extends AbstractTableModel { |
13 |
|
|
14 |
|
private static final int NAME_COLUMN = 0; |
15 |
|
private static final int AMOUNT_COLUMN = 1; |
16 |
|
private static final int UNIT_COLUMN = 2; |
17 |
|
private static final int RDI_COLUMN = 3; |
18 |
|
|
19 |
0 |
private String[] columnNames = { "Nutrient", "Amount", "Units", "%DV" }; |
20 |
|
|
21 |
|
List nutrients; |
22 |
|
|
23 |
|
private Food food; |
24 |
0 |
private double multiplier = 1; |
25 |
|
|
26 |
0 |
public NutrientTableModel(List nutrientInfo) { |
27 |
0 |
this.nutrients = nutrientInfo; |
28 |
0 |
} |
29 |
|
|
30 |
|
public void setMultiplier(double val) { |
31 |
0 |
this.multiplier = val; |
32 |
0 |
fireTableDataChanged(); |
33 |
0 |
} |
34 |
|
|
35 |
|
public void setFood(Food f) { |
36 |
0 |
this.food = f; |
37 |
0 |
} |
38 |
|
|
39 |
|
public Class getColumnClass(int col) { |
40 |
0 |
Object o = getValueAt(0, col); |
41 |
0 |
if (o != null) { |
42 |
0 |
return o.getClass(); |
43 |
|
} |
44 |
0 |
return String.class; |
45 |
|
} |
46 |
|
|
47 |
|
public int getColumnCount() { |
48 |
0 |
return columnNames.length; |
49 |
|
} |
50 |
|
|
51 |
|
public String getColumnName(int col) { |
52 |
0 |
return columnNames[col].toString(); |
53 |
|
} |
54 |
|
|
55 |
|
public NutrientInfo getNutrientInfo(int i) { |
56 |
0 |
return (NutrientInfo) nutrients.get(i); |
57 |
|
} |
58 |
|
|
59 |
|
public int getRowCount() { |
60 |
0 |
return nutrients.size(); |
61 |
|
} |
62 |
|
public Object getValueAt(int row, class="keyword">int col) { |
63 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
64 |
0 |
if (ni != null) { |
65 |
0 |
double val = multiplier * food.getNutrientAmount(ni); |
66 |
0 |
switch (col) { |
67 |
|
case NAME_COLUMN: |
68 |
0 |
return ni.getName(); |
69 |
|
case AMOUNT_COLUMN: |
70 |
0 |
val = Math.round(100000 * val) / 100000.0; |
71 |
0 |
return new Double(val); |
72 |
|
case UNIT_COLUMN: |
73 |
0 |
return ni.getUnits(); |
74 |
|
case RDI_COLUMN: |
75 |
0 |
double RDI = ni.getReferenceDailyIntake(); |
76 |
0 |
if (RDI > 0) { |
77 |
0 |
return new Integer((int)(100*val/RDI)); |
78 |
|
} |
79 |
0 |
return null; |
80 |
|
} |
81 |
|
} |
82 |
0 |
return ""; |
83 |
|
} |
84 |
|
|
85 |
|
public boolean isCellEditable(int row, class="keyword">int col) { |
86 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
87 |
0 |
if (food != null && ni != class="keyword">null) { |
88 |
0 |
if (col == AMOUNT_COLUMN) { |
89 |
0 |
return true; |
90 |
|
} |
91 |
0 |
if (col == RDI_COLUMN) { |
92 |
0 |
if (ni.getReferenceDailyIntake() > 0) { |
93 |
0 |
return true; |
94 |
|
} |
95 |
|
} |
96 |
|
} |
97 |
0 |
return false; |
98 |
|
} |
99 |
|
|
100 |
|
public void setValueAt(Object value, int row, class="keyword">int col) { |
101 |
0 |
if (col == AMOUNT_COLUMN && value != null) { |
102 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
103 |
0 |
if (ni != null) { |
104 |
0 |
double val = ((Double) value).class="keyword">doubleValue(); |
105 |
0 |
food.setNutrientAmount(ni, val / multiplier); |
106 |
0 |
fireTableRowsUpdated(row, row); |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
0 |
if (col == RDI_COLUMN && value != null) { |
111 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
112 |
0 |
if (ni != null) { |
113 |
0 |
double val = ((Integer) value).intValue(); |
114 |
0 |
double RDI = ni.getReferenceDailyIntake(); |
115 |
0 |
val = (val/100.0) * RDI; |
116 |
0 |
food.setNutrientAmount(ni, val / multiplier); |
117 |
0 |
fireTableRowsUpdated(row, row); |
118 |
|
} |
119 |
|
} |
120 |
0 |
} |
121 |
|
|
122 |
|
} |