1
2
3
4 package ca.spaz.cron.targets;
5
6 import java.awt.*;
7 import java.awt.event.*;
8 import java.util.Iterator;
9
10 import javax.swing.*;
11
12 import ca.spaz.cron.CRONOMETER;
13 import ca.spaz.cron.database.NutrientInfo;
14 import ca.spaz.cron.user.User;
15 import ca.spaz.cron.user.impl.CRONUser;
16 import ca.spaz.util.ToolBox;
17
18 /***
19 * A UI panel that will let the user view and edit their
20 * nutritional targets.
21 *
22 * Ideally, there will be a mechanism to suggest good default
23 * targets based on age, weight, gender, and other factors.
24 *
25 * @author Aaron Davidson
26 */
27 public class TargetEditor extends JPanel {
28 private User user;
29
30 private TargetEditorTable macro, minerals, vitamins, aminoacids, lipids;
31 private JTabbedPane tabPanel;
32 private JPanel setupPanel;
33 private JPanel buttonPanel;
34 private JPanel defaultsPanel;
35 private JLabel instructionLabel;
36 private JComboBox defaultsBox;
37 private JButton setDefaultsBtn;
38
39 private static final Object[] DEFAULT_TYPES = {
40 new RDITargetModel(),
41 new CRONTargetModel(),
42 };
43
44 public TargetEditor(User user) {
45 this.user = user;
46 this.setLayout(new BorderLayout(10,10));
47 this.setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
48 this.add(getSetupPanel(), BorderLayout.NORTH);
49 this.add(getTabPanel(), BorderLayout.CENTER);
50 this.add(getDefaultsPanel(), BorderLayout.SOUTH);
51 }
52
53 private JTabbedPane getTabPanel() {
54 if (tabPanel == null) {
55 tabPanel = new JTabbedPane();
56 tabPanel.addTab("General", getMacroNutrientsTable());
57 tabPanel.addTab("Minerals", getMineralsTable());
58 tabPanel.addTab("Vitamins", getVitaminsTable());
59 tabPanel.addTab("Amino Acids", getAminoAcidsTable());
60 tabPanel.addTab("Lipids", getLipidsTable());
61 }
62 return tabPanel;
63 }
64
65 private TargetEditorTable getMacroNutrientsTable() {
66 if (macro == null) {
67 macro = new TargetEditorTable(user, NutrientInfo.getMacroNutrients());
68 }
69 return macro;
70 }
71
72 private TargetEditorTable getMineralsTable() {
73 if (minerals == null) {
74 minerals = new TargetEditorTable(user, NutrientInfo.getMinerals());
75 }
76 return minerals;
77 }
78
79 private TargetEditorTable getVitaminsTable() {
80 if (vitamins == null) {
81 vitamins = new TargetEditorTable(user, NutrientInfo.getVitamins());
82 }
83 return vitamins;
84 }
85
86 private TargetEditorTable getAminoAcidsTable() {
87 if (aminoacids == null) {
88 aminoacids = new TargetEditorTable(user, NutrientInfo.getAminoAcids());
89 }
90 return aminoacids;
91 }
92
93 private TargetEditorTable getLipidsTable() {
94 if (lipids == null) {
95 lipids = new TargetEditorTable(user, NutrientInfo.getLipids());
96 }
97 return lipids;
98 }
99
100 private JPanel getSetupPanel() {
101 if (setupPanel == null) {
102 setupPanel = new JPanel(new BorderLayout(4,4));
103 setupPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
104 setupPanel.add(getInstructionLabel(), BorderLayout.CENTER);
105 }
106 return setupPanel;
107 }
108
109 private JLabel getInstructionLabel() {
110 if (instructionLabel == null) {
111 instructionLabel = new JLabel(
112 "<html>Targets are default to basic reference " +
113 "daily intake (RDI) values. Other suggested defaults " +
114 "are available from the menu below, or values may be " +
115 "entered manually.</html>");
116 instructionLabel.setPreferredSize(new Dimension(200,60));
117 instructionLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
118
119 }
120 return instructionLabel;
121 }
122
123
124 private JPanel getDefaultsPanel() {
125 if (defaultsPanel == null) {
126 defaultsPanel = new JPanel(new BorderLayout(8,8));
127 this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
128 defaultsPanel.add(
129 new JLabel("Preset Targets:", JLabel.RIGHT), BorderLayout.WEST);
130 defaultsPanel.add(getDefaultsComboBox(), BorderLayout.CENTER);
131 defaultsPanel.add(getSetDefaultsButton(), BorderLayout.EAST);
132 }
133 return defaultsPanel;
134 }
135
136 private JComboBox getDefaultsComboBox() {
137 if (defaultsBox == null) {
138 defaultsBox = new JComboBox(DEFAULT_TYPES);
139 }
140 return defaultsBox;
141 }
142
143 private JButton getSetDefaultsButton() {
144 if (setDefaultsBtn == null) {
145 setDefaultsBtn = new JButton("Set Targets");
146 setDefaultsBtn.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 setDefaultTargets((TargetModel)getDefaultsComboBox().getSelectedItem());
149 }
150 });
151 }
152 return setDefaultsBtn;
153 }
154
155
156 private JPanel getButtonPanel() {
157 if (buttonPanel == null) {
158 buttonPanel = new JPanel();
159 }
160 return buttonPanel;
161 }
162
163
164 public static void editTargets() {
165 JDialog d = ToolBox.getDialog(
166 CRONOMETER.getInstance(),
167 "Target Editor",
168 new TargetEditor(CRONUser.getUser()));
169 d.setVisible(true);
170 }
171
172
173 private void setDefaultTargets(TargetModel model) {
174 int rc = JOptionPane.showConfirmDialog(this,
175 "Are you sure you want to replace the current targets with" +
176 " '"+ model.toString()+"'?",
177 "Replace Targets?", JOptionPane.YES_NO_OPTION);
178 if (rc != JOptionPane.YES_OPTION) return;
179
180 Iterator iter = NutrientInfo.getGlobalList().iterator();
181 while (iter.hasNext()) {
182 NutrientInfo ni = (NutrientInfo)iter.next();
183 Target target = new Target();
184 target.setMin(model.getTargetMinimum(user, ni));
185 target.setMax(model.getTargetMaximum(user, ni));
186 user.setTarget(ni, target);
187 }
188 getAminoAcidsTable().fireTargetsChanged();
189 getMacroNutrientsTable().fireTargetsChanged();
190 getMineralsTable().fireTargetsChanged();
191 getVitaminsTable().fireTargetsChanged();
192 getLipidsTable().fireTargetsChanged();
193 }
194
195 }