View Javadoc

1   /*
2    * Created on 21-Mar-2005
3    */
4   package ca.spaz.cron.ui;
5   
6   import java.awt.*;
7   import java.awt.event.*;
8   import java.text.DecimalFormat;
9   import java.util.*;
10  
11  import javax.swing.*;
12  import javax.swing.event.*;
13  
14  import ca.spaz.cron.CRONOMETER;
15  import ca.spaz.cron.database.*;
16  import ca.spaz.gui.*;
17  
18  import com.aimedia.ui.UIPartFactory;
19  
20  /***
21   * This is panel that displays some breif summary information about a Food item
22   * and allows choosing of a measure to be used for either adding or updating a
23   * Consumed food entry in the user data.
24   * 
25   * @author davidson
26   */
27  public class ServingEditor extends JPanel implements Observer {
28     private static final Color ACCENT_COLOR =
29        Color.LIGHT_GRAY;
30        //Color.ORANGE.brighter();//new Color(225, 245, 225);
31     
32     private CRONOMETER cwapp;
33  
34     private Serving cur;
35  
36     private JLabel titleLabel;
37  
38     private JLabel energyLabel, carbsLabel, proteinLabel, fatLabel, waterLabel,
39           fiberLabel;
40  
41     private JButton addButton;
42  
43     private MeasureWidget measure;
44     
45  
46     private JToolBar titlePanel;
47  
48     private JPanel addEatenPanel;
49  
50     private JPanel nutrientsPanel;
51     
52     private JPanel emptyPanel;
53     private JPanel mainPanel;
54     private CardLayout cards;
55     private DecimalFormat labelFormatter;
56  
57     
58     public ServingEditor(CRONOMETER cwapp) {
59        this.cwapp = cwapp;
60        initialize();
61        labelFormatter = new DecimalFormat("#####0.0");
62     }
63  
64     /***
65      *  
66      */
67     private void initialize() {
68        cards = new CardLayout();
69        this.setLayout(cards);
70        this.setBorder(BorderFactory.createCompoundBorder(
71              BorderFactory.createEtchedBorder(), 
72              BorderFactory.createEmptyBorder(4, 4, 4, 4)));
73        this.setBackground(ACCENT_COLOR);
74        //this.setPreferredSize(new java.awt.Dimension(300, 150));
75        this.add(getMainPanel(), "MAIN");
76        this.add(getEmptyPanel(), "EMPTY");
77        cards.show(this, "EMPTY");
78     }
79     
80     private JPanel getMainPanel() {
81        if (mainPanel == null) {
82           mainPanel = new JPanel(new BorderLayout(4, 4));
83           mainPanel.setBackground(ACCENT_COLOR);
84           mainPanel.add(getAddEatenPanel(), BorderLayout.SOUTH);
85           mainPanel.add(getNutrientsPanel(), BorderLayout.CENTER);
86           mainPanel.add(getTitleLabel(), BorderLayout.NORTH);
87        } 
88        return mainPanel;
89     }
90  
91     private JPanel getEmptyPanel() {
92        if (emptyPanel == null) {
93           JLabel empty = new JLabel(
94              "<html><h3 align=\"center\">" +
95              "no food selected</h3></html>",
96              JLabel.CENTER);
97           emptyPanel = new JPanel(new BorderLayout(4, 4));   
98           emptyPanel.setBackground(ACCENT_COLOR);
99           emptyPanel.add(empty);
100       } 
101       return emptyPanel;
102    }
103 
104    
105    /***
106     * @return
107     */
108    private JPanel getAddEatenPanel() {
109       if (null == addEatenPanel) {
110          addEatenPanel = new JPanel();
111          addEatenPanel.setBackground(ACCENT_COLOR);
112          addEatenPanel
113                .setLayout(new BoxLayout(addEatenPanel, BoxLayout.X_AXIS));
114          addEatenPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
115          addEatenPanel.add(Box.createHorizontalGlue());
116          addEatenPanel.add(getMeasure());
117          addEatenPanel.add(Box.createHorizontalStrut(5));
118          addEatenPanel.add(getAddButton());
119          addEatenPanel.add(Box.createHorizontalGlue());
120 
121       }
122       return addEatenPanel;
123    }
124 
125 
126    private JLabel getTitleLabel() {
127       if (null == titleLabel) {
128          titleLabel = new TranslucentLabel(0.85, " ", JLabel.CENTER);
129          titleLabel.setBackground(Color.BLACK);
130          titleLabel.setForeground(Color.WHITE);
131          titleLabel.setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
132          titleLabel.setFont(new Font("Application", Font.BOLD, 12));
133       }
134       return titleLabel;
135    }
136 
137    
138    private JPanel getNutrientsPanel() {
139       if (null == nutrientsPanel) {
140          nutrientsPanel = new TranslucentPanel(0.5f);
141          nutrientsPanel.setBackground(Color.BLACK);
142          nutrientsPanel.setForeground(Color.WHITE);
143          nutrientsPanel.setBorder(//BorderFactory.createCompoundBorder(
144                //BorderFactory.createEtchedBorder(), 
145                BorderFactory.createEmptyBorder(4, 4, 4, 4));
146         // nutrientsPanel.setBackground(ACCENT_COLOR);
147          nutrientsPanel.setLayout(new GridLayout(2, 3, 6, 3));
148 
149          nutrientsPanel.add(UIPartFactory.createFieldLabel("Energy:", true));
150          nutrientsPanel.add(getEnergyLabel());
151 
152          nutrientsPanel.add(UIPartFactory.createFieldLabel("Water:", true));
153          nutrientsPanel.add(getWaterLabel());
154 
155          nutrientsPanel.add(UIPartFactory.createFieldLabel("Fiber:", true));
156          nutrientsPanel.add(getFiberLabel());
157 
158          nutrientsPanel.add(UIPartFactory.createFieldLabel("Protein:", true));
159          nutrientsPanel.add(getProteinLabel());
160 
161          nutrientsPanel.add(UIPartFactory.createFieldLabel("Carbs:", true));
162          nutrientsPanel.add(getCarbsLabel());
163 
164          nutrientsPanel.add(UIPartFactory.createFieldLabel("Fat:", true));
165          nutrientsPanel.add(getFatLabel());
166       }
167       return nutrientsPanel;
168    }
169 
170    private MeasureWidget getMeasure() {
171       if (null == measure) {
172          measure = new MeasureWidget();
173          measure.setBackground(ACCENT_COLOR);
174          measure.addChangeListener(new ChangeListener() {
175             public void stateChanged(ChangeEvent e) {
176                update(null, null);
177             }
178          });
179       }
180       return measure;
181    }
182 
183    private JButton getAddButton() {
184       if (null == addButton) {
185          addButton = new JButton("Add");
186          addButton.addActionListener(new ActionListener() {
187             public void actionPerformed(ActionEvent e) {
188                addServingToDay();
189             }
190          });
191       }
192       return addButton;
193    }
194 
195    /***
196     * @return
197     */
198    private Component getFatLabel() {
199       if (null == fatLabel) {
200          fatLabel = new JLabel("");
201       }
202       return fatLabel;
203    }
204 
205    /***
206     * @return
207     */
208    private Component getProteinLabel() {
209       if (null == proteinLabel) {
210          proteinLabel = new JLabel("");
211       }
212       return proteinLabel;
213    }
214 
215    /***
216     * @return
217     */
218    private Component getCarbsLabel() {
219       if (null == carbsLabel) {
220          carbsLabel = new JLabel("");
221       }
222       return carbsLabel;
223    }
224 
225    /***
226     * @return
227     */
228    private Component getFiberLabel() {
229       if (null == fiberLabel) {
230          fiberLabel = new JLabel("");
231       }
232       return fiberLabel;
233    }
234 
235    /***
236     * @return
237     */
238    private Component getWaterLabel() {
239       if (null == waterLabel) {
240          waterLabel = new JLabel("");
241       }
242       return waterLabel;
243    }
244 
245    /***
246     * @return
247     */
248    private JLabel getEnergyLabel() {
249       if (null == energyLabel) {
250          energyLabel = new JLabel("");
251       }
252       return energyLabel;
253    }
254 
255    public void setServing(Serving c) {
256       setCur(c);
257       setFood(c.getFood());
258       setWeight(Measure.GRAM, c.getGrams());
259    }
260 
261    private void setFood(Food f) {
262       getMeasure().setFood(f);
263       getTitleLabel().setText(f.getDescription());
264       getAddButton().setText(getCur().exists() ? "Update" : "Add");
265       cards.show(this, "MAIN");      
266    }
267 
268    public void setWeight(Measure w, double mult) {
269       getMeasure().setMeasure(w, mult);
270    }
271 
272    public void update(Observable source, Object message) {
273       Food curFood = getCur().getFood();
274       double mult = getMeasure().getMultiplier();
275       
276       double cals = curFood.getMacroNutrients().kcals * mult;
277       energyLabel.setText(labelFormatter.format(cals) + " kcal");
278 
279       double protein = mult * curFood.getMacroNutrients().protein;
280       proteinLabel.setText(labelFormatter.format(protein) + " g");
281 
282       double carbs = mult * curFood.getMacroNutrients().carbs;
283       carbsLabel.setText(labelFormatter.format(carbs) + " g");
284 
285       double fat = mult * curFood.getMacroNutrients().lipid;
286       fatLabel.setText(labelFormatter.format(fat) + " g");
287 
288       double water = mult * curFood.getMacroNutrients().water;
289       waterLabel.setText(labelFormatter.format(water) + " g");
290 
291       double fiber = mult * curFood.getMacroNutrients().fiber;
292       fiberLabel.setText(labelFormatter.format(fiber) + " g");
293    }
294 
295    /***
296     * Add the current food to the current day
297     */
298    public void addServingToDay() {
299       getCur().setGrams(getMeasure().getGrams());
300       getCur().setMeasure(getMeasure().getSelectedMeasure());
301       cwapp.getDailySummary().addServing(getCur());
302    }
303 
304    /***
305     * @param cur
306     *           The cur to set.
307     */
308    void setCur(Serving cur) {
309       this.cur = cur;
310    }
311 
312    /***
313     * @return Returns the cur.
314     */
315    Serving getCur() {
316       return cur;
317    }
318 
319 }