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 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
0 |
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 |
0 |
private static final Object[] DEFAULT_TYPES = { |
40 |
|
new RDITargetModel(), |
41 |
|
new CRONTargetModel(), |
42 |
|
}; |
43 |
|
|
44 |
0 |
public TargetEditor(User user) { |
45 |
0 |
this.user = user; |
46 |
0 |
this.setLayout(new BorderLayout(10,10)); |
47 |
0 |
this.setBorder(BorderFactory.createEmptyBorder(15,15,15,15)); |
48 |
0 |
this.add(getSetupPanel(), BorderLayout.NORTH); |
49 |
0 |
this.add(getTabPanel(), BorderLayout.CENTER); |
50 |
0 |
this.add(getDefaultsPanel(), BorderLayout.SOUTH); |
51 |
0 |
} |
52 |
|
|
53 |
|
private JTabbedPane getTabPanel() { |
54 |
0 |
if (tabPanel == null) { |
55 |
0 |
tabPanel = new JTabbedPane(); |
56 |
0 |
tabPanel.addTab("General", getMacroNutrientsTable()); |
57 |
0 |
tabPanel.addTab("Minerals", getMineralsTable()); |
58 |
0 |
tabPanel.addTab("Vitamins", getVitaminsTable()); |
59 |
0 |
tabPanel.addTab("Amino Acids", getAminoAcidsTable()); |
60 |
0 |
tabPanel.addTab("Lipids", getLipidsTable()); |
61 |
|
} |
62 |
0 |
return tabPanel; |
63 |
|
} |
64 |
|
|
65 |
|
private TargetEditorTable getMacroNutrientsTable() { |
66 |
0 |
if (macro == null) { |
67 |
0 |
macro = new TargetEditorTable(user, NutrientInfo.getMacroNutrients()); |
68 |
|
} |
69 |
0 |
return macro; |
70 |
|
} |
71 |
|
|
72 |
|
private TargetEditorTable getMineralsTable() { |
73 |
0 |
if (minerals == null) { |
74 |
0 |
minerals = new TargetEditorTable(user, NutrientInfo.getMinerals()); |
75 |
|
} |
76 |
0 |
return minerals; |
77 |
|
} |
78 |
|
|
79 |
|
private TargetEditorTable getVitaminsTable() { |
80 |
0 |
if (vitamins == null) { |
81 |
0 |
vitamins = new TargetEditorTable(user, NutrientInfo.getVitamins()); |
82 |
|
} |
83 |
0 |
return vitamins; |
84 |
|
} |
85 |
|
|
86 |
|
private TargetEditorTable getAminoAcidsTable() { |
87 |
0 |
if (aminoacids == null) { |
88 |
0 |
aminoacids = new TargetEditorTable(user, NutrientInfo.getAminoAcids()); |
89 |
|
} |
90 |
0 |
return aminoacids; |
91 |
|
} |
92 |
|
|
93 |
|
private TargetEditorTable getLipidsTable() { |
94 |
0 |
if (lipids == null) { |
95 |
0 |
lipids = new TargetEditorTable(user, NutrientInfo.getLipids()); |
96 |
|
} |
97 |
0 |
return lipids; |
98 |
|
} |
99 |
|
|
100 |
|
private JPanel getSetupPanel() { |
101 |
0 |
if (setupPanel == null) { |
102 |
0 |
setupPanel = new JPanel(class="keyword">new BorderLayout(4,4)); |
103 |
0 |
setupPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
104 |
0 |
setupPanel.add(getInstructionLabel(), BorderLayout.CENTER); |
105 |
|
} |
106 |
0 |
return setupPanel; |
107 |
|
} |
108 |
|
|
109 |
|
private JLabel getInstructionLabel() { |
110 |
0 |
if (instructionLabel == null) { |
111 |
0 |
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 |
0 |
instructionLabel.setPreferredSize(new Dimension(200,60)); |
117 |
0 |
instructionLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); |
118 |
|
|
119 |
|
} |
120 |
0 |
return instructionLabel; |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
private JPanel getDefaultsPanel() { |
125 |
0 |
if (defaultsPanel == null) { |
126 |
0 |
defaultsPanel = new JPanel(class="keyword">new BorderLayout(8,8)); |
127 |
0 |
this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); |
128 |
0 |
defaultsPanel.add( |
129 |
|
new JLabel("Preset Targets:", JLabel.RIGHT), BorderLayout.WEST); |
130 |
0 |
defaultsPanel.add(getDefaultsComboBox(), BorderLayout.CENTER); |
131 |
0 |
defaultsPanel.add(getSetDefaultsButton(), BorderLayout.EAST); |
132 |
|
} |
133 |
0 |
return defaultsPanel; |
134 |
|
} |
135 |
|
|
136 |
0 |
private JComboBox getDefaultsComboBox() { |
137 |
0 |
if (defaultsBox == null) { |
138 |
0 |
defaultsBox = new JComboBox(DEFAULT_TYPES); |
139 |
|
} |
140 |
0 |
return defaultsBox; |
141 |
|
} |
142 |
|
|
143 |
|
private JButton getSetDefaultsButton() { |
144 |
0 |
if (setDefaultsBtn == null) { |
145 |
0 |
setDefaultsBtn = new JButton("Set Targets"); |
146 |
0 |
setDefaultsBtn.addActionListener(new ActionListener() { |
147 |
|
public void actionPerformed(ActionEvent e) { |
148 |
|
setDefaultTargets((TargetModel)getDefaultsComboBox().getSelectedItem()); |
149 |
|
} |
150 |
|
}); |
151 |
|
} |
152 |
0 |
return setDefaultsBtn; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
private JPanel getButtonPanel() { |
157 |
0 |
if (buttonPanel == null) { |
158 |
0 |
buttonPanel = new JPanel(); |
159 |
|
} |
160 |
0 |
return buttonPanel; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
public static void editTargets() { |
165 |
0 |
JDialog d = ToolBox.getDialog( |
166 |
|
CRONOMETER.getInstance(), |
167 |
|
"Target Editor", |
168 |
|
new TargetEditor(CRONUser.getUser())); |
169 |
0 |
d.setVisible(true); |
170 |
0 |
} |
171 |
|
|
172 |
|
|
173 |
0 |
private void setDefaultTargets(TargetModel model) { |
174 |
0 |
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 |
0 |
if (rc != JOptionPane.YES_OPTION) return; |
179 |
|
|
180 |
0 |
Iterator iter = NutrientInfo.getGlobalList().iterator(); |
181 |
0 |
while (iter.hasNext()) { |
182 |
0 |
NutrientInfo ni = (NutrientInfo)iter.next(); |
183 |
0 |
Target target = new Target(); |
184 |
0 |
target.setMin(model.getTargetMinimum(user, ni)); |
185 |
0 |
target.setMax(model.getTargetMaximum(user, ni)); |
186 |
0 |
user.setTarget(ni, target); |
187 |
0 |
} |
188 |
0 |
getAminoAcidsTable().fireTargetsChanged(); |
189 |
0 |
getMacroNutrientsTable().fireTargetsChanged(); |
190 |
0 |
getMineralsTable().fireTargetsChanged(); |
191 |
0 |
getVitaminsTable().fireTargetsChanged(); |
192 |
0 |
getLipidsTable().fireTargetsChanged(); |
193 |
0 |
} |
194 |
|
|
195 |
|
} |