1
2
3
4 package ca.spaz.cron.summary;
5
6 import java.text.DecimalFormat;
7 import java.util.*;
8
9 import javax.swing.*;
10 import javax.swing.table.*;
11
12 import ca.spaz.cron.database.*;
13 import ca.spaz.cron.targets.Target;
14 import ca.spaz.cron.user.impl.CRONUser;
15 import ca.spaz.gui.PrettyTable;
16
17 public class NutrientTable extends PrettyTable implements Observer {
18
19 private List servings;
20 private List nutrients;
21 private NutrientTableModel model;
22
23
24 public NutrientTable(List nutrients) {
25 super();
26 this.nutrients = nutrients;
27 this.setEnabled(false);
28 this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
29 this.getTableHeader().setReorderingAllowed(false);
30 this.setCellSelectionEnabled(false);
31 this.getSelectionModel().setSelectionMode(
32 ListSelectionModel.SINGLE_SELECTION);
33
34 setModel(getNutrientTableModel());
35
36
37 setColumnAlignment(1, SwingConstants.RIGHT);
38 setColumnAlignment(3, SwingConstants.RIGHT);
39 getColumnModel().getColumn(2).setMaxWidth(36);
40 getColumnModel().getColumn(2).setMinWidth(36);
41 getColumnModel().getColumn(1).setMaxWidth(64);
42 getColumnModel().getColumn(1).setMinWidth(64);
43 }
44
45
46 private void setColumnAlignment(int col, int alignment) {
47 TableColumnModel tcm = getColumnModel();
48 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
49 TableColumn column = tcm.getColumn(col);
50 renderer.setHorizontalAlignment(alignment);
51 column.setCellRenderer(renderer);
52 }
53
54 private double getAmount(NutrientInfo ni) {
55 double total = 0;
56 for (Iterator iter = servings.iterator(); iter.hasNext();) {
57 Serving serving = (Serving) iter.next();
58 double weight = serving.getGrams()/100.0;
59 total += weight * serving.getFood().getNutrientAmount(ni);
60 }
61 return total;
62 }
63
64 private NutrientTableModel getNutrientTableModel() {
65 if (model == null) {
66 model = new NutrientTableModel();
67 }
68 return model;
69 }
70
71 public class NutrientTableModel extends AbstractTableModel {
72 private DecimalFormat df = new DecimalFormat("######0.0");
73 private DecimalFormat nf = new DecimalFormat("######0%");
74 private String[] columnNames = { "Nutrient", "Amount", "Unit" , "% Target"};
75
76
77 public Class getColumnClass(int col) {
78 Object o = getValueAt(0, col);
79 if (o != null) {
80 return o.getClass();
81 }
82 return String.class;
83 }
84
85 public int getColumnCount() {
86 return columnNames.length;
87 }
88
89 public String getColumnName(int col) {
90 return columnNames[col].toString();
91 }
92
93 public NutrientInfo getNutrientInfo(int i) {
94 return (NutrientInfo) nutrients.get(i);
95 }
96
97 public int getRowCount() {
98 return nutrients.size();
99 }
100
101 public Object getValueAt(int row, int col) {
102 NutrientInfo ni = getNutrientInfo(row);
103 if (ni != null) {
104 switch (col) {
105 case 0:
106 return ni.getName();
107 case 1:
108 return df.format(getAmount(ni));
109 case 2:
110 return " " + ni.getUnits();
111 case 3:
112 Target target = CRONUser.getUser().getTarget(ni);
113 if (target.getMin() > 0) {
114 return nf.format(getAmount(ni)/target.getMin());
115 }
116 }
117 }
118 return "";
119 }
120
121 public boolean isCellEditable(int row, int col) {
122 return false;
123 }
124
125 }
126
127 public void update(Observable source, Object message) {
128 if (!(message instanceof List)) {
129 return;
130 }
131 this.servings = (List)message;
132 getNutrientTableModel().fireTableDataChanged();
133 }
134
135
136
137
138 }