1
2
3
4 package ca.spaz.cron.ui;
5
6 import java.awt.BorderLayout;
7 import java.awt.event.ActionEvent;
8 import java.util.Vector;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12
13 import se.datadosen.component.RiverLayout;
14 import ca.spaz.cron.CRONOMETER;
15 import ca.spaz.cron.database.*;
16 import ca.spaz.cron.datasource.*;
17
18 import com.aimedia.ui.*;
19
20 /***
21 * An editor panel for a food item in the database. The editor must be able to
22 * add / update / delete a food which includes all nutrient entries, and weights
23 *
24 * @todo: Import/Export
25 *
26 * @author davidson
27 */
28 public class FoodEditor extends JPanel implements ICancellable, ISaveable {
29
30 private CRONOMETER cwapp;
31
32 private JDialog dialog;
33
34 private Food food;
35
36 private JTextField name;
37
38 private JComboBox source;
39
40 private JComboBox group;
41
42 private MeasureWidget measure;
43
44 private MeasureEditor measureEditor;
45
46 private NutrientEditorTable macro, minerals, vitamins, aminoacids, lipids;
47
48 private Action editGroupAction;
49
50 private Action editSourceAction;
51
52 private JButton saveButton;
53
54 private JButton cancelButton;
55
56 private JButton editGroupsButton;
57
58 private JButton editSourceButton;
59
60 private JButton editWeightsButton;
61
62 private JPanel buttonPanel;
63
64 private JPanel generalPanel;
65
66 private JTabbedPane tabPanel;
67
68 public FoodEditor(CRONOMETER app, Food f) {
69 this.cwapp = app;
70 this.food = f;
71
72 initialize();
73
74 JDialog dialog = getDialog();
75 getMeasureSelector().focus();
76 dialog.setVisible(true);
77 }
78
79 /***
80 * @param app
81 * @return
82 */
83 private JDialog getDialog() {
84 if (null == dialog) {
85 dialog = new JDialog(cwapp);
86 dialog.setTitle("Food Editor");
87 dialog.getContentPane().add(this);
88 dialog.pack();
89 dialog.setModal(true);
90 dialog.setLocationRelativeTo(cwapp);
91 }
92 return dialog;
93 }
94
95 private void initialize() {
96 this.setLayout(new BorderLayout(4, 4));
97 this.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
98 this.add(getGeneralPanel(), BorderLayout.NORTH);
99
100 this.add(getTabPanel(), BorderLayout.CENTER);
101 this.add(getButtonPanel(), BorderLayout.SOUTH);
102 }
103
104 public void doCancel() {
105 getDialog().dispose();
106 }
107
108 private void doEditGroups() {
109
110 }
111
112 private void doEditSource() {
113
114 }
115
116 public void doSave() {
117 ILocalFoodDatasource ds = (ILocalFoodDatasource) getSourceCombo().getSelectedItem();
118 food.setDescription(getNameField().getText());
119 food.setFoodGroup((FoodGroup) getGroupCombo().getSelectedItem());
120 String sourceName = ds.toString();
121 food.setSource(sourceName);
122 food.setMeasures(getMeasureEditor().getMeasures());
123 ds.saveFood(food);
124 getDialog().dispose();
125 }
126
127 private JPanel getButtonPanel() {
128 if (null == buttonPanel) {
129 buttonPanel = new JPanel();
130 buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
131 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
132 buttonPanel.add(Box.createHorizontalGlue());
133 buttonPanel.add(getCancelButton());
134 buttonPanel.add(Box.createHorizontalStrut(10));
135 buttonPanel.add(getSaveButton());
136 buttonPanel.add(Box.createHorizontalStrut(10));
137 }
138 return buttonPanel;
139 }
140
141 /***
142 * @return
143 */
144 private JButton getCancelButton() {
145 if (null == cancelButton) {
146 cancelButton = new JButton(new CancelAction(this));
147 }
148 return cancelButton;
149 }
150
151 private JButton getSaveButton() {
152 if (null == saveButton) {
153 saveButton = new JButton(new SaveFoodAction(this));
154 }
155 return saveButton;
156 }
157
158 private JComponent getEditGroupsButton() {
159 if (null == editGroupsButton) {
160 editGroupsButton = new JButton(getEditGroupsAction());
161 }
162 return editGroupsButton;
163 }
164
165 private Action getEditGroupsAction() {
166 if (null == editGroupAction) {
167 editGroupAction = new EditGroupAction();
168 }
169 return editGroupAction;
170 }
171
172 private JComponent getEditSourceButton() {
173 if (null == editSourceButton) {
174 editSourceButton = new JButton(getEditSourceAction());
175 }
176 return editSourceButton;
177 }
178
179 /***
180 * @return
181 */
182 private Action getEditSourceAction() {
183 if (null == editSourceAction) {
184 editSourceAction = new EditGroupAction();
185 }
186 return editSourceAction;
187 }
188
189 private JPanel getGeneralPanel() {
190 if (null == generalPanel) {
191 final String TAB_HFILL = RiverLayout.TAB_STOP + " "
192 + RiverLayout.HFILL;
193 generalPanel = new JPanel(new RiverLayout());
194 generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
195 .createFieldLabel("Name:"));
196 generalPanel.add(TAB_HFILL, getNameField());
197 generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
198 .createFieldLabel("Group:"));
199 generalPanel.add(TAB_HFILL, getGroupCombo());
200 generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
201 .createFieldLabel("Source:"));
202 generalPanel.add(TAB_HFILL, getSourceCombo());
203 generalPanel.add(RiverLayout.LINE_BREAK, UIPartFactory
204 .createFieldLabel("Measure:"));
205 generalPanel.add(TAB_HFILL, getMeasureSelector());
206 }
207 return generalPanel;
208 }
209
210 private JComboBox getSourceCombo() {
211 if (null == source) {
212 Vector allSources = new Vector();
213 allSources.addAll(Datasources.getInstance().getSources());
214 source = new JComboBox(allSources);
215 source.setEditable(true);
216 source.setSelectedItem(food.getDataSource());
217 }
218 return source;
219 }
220
221 private JComboBox getGroupCombo() {
222 if (null == group) {
223 Vector allGroups = new Vector();
224 allGroups.addAll(Datasources.getInstance().getFoodGroups());
225 group = new JComboBox(allGroups);
226 for (int i=0; i<allGroups.size(); i++) {
227 FoodGroup fg = (FoodGroup)allGroups.get(i);
228 if (fg.equals(food.getFoodGroup())) {
229 group.setSelectedIndex(i);
230 }
231 }
232
233 group.setEditable(true);
234 }
235 return group;
236 }
237
238 private JTextField getNameField() {
239 if (null == name) {
240 name = new JTextField(40);
241 name.setText(food.getDescription());
242 }
243 return name;
244 }
245
246 private NutrientEditorTable getMacroNutrientsTable() {
247 if (macro == null) {
248 macro = new NutrientEditorTable(NutrientInfo.getMacroNutrients());
249 macro.setFood(food);
250 macro.setMultiplier(getMeasureSelector().getMultiplier());
251 }
252 return macro;
253 }
254
255 private NutrientEditorTable getMineralsTable() {
256 if (minerals == null) {
257 minerals = new NutrientEditorTable(NutrientInfo.getMinerals());
258 minerals.setFood(food);
259 minerals.setMultiplier(getMeasureSelector().getMultiplier());
260 }
261 return minerals;
262 }
263
264 private NutrientEditorTable getVitaminsTable() {
265 if (vitamins == null) {
266 vitamins = new NutrientEditorTable(NutrientInfo.getVitamins());
267 vitamins.setFood(food);
268 vitamins.setMultiplier(getMeasureSelector().getMultiplier());
269 }
270 return vitamins;
271 }
272
273 private NutrientEditorTable getAminoAcidsTable() {
274 if (aminoacids == null) {
275 aminoacids = new NutrientEditorTable(NutrientInfo.getAminoAcids());
276 aminoacids.setFood(food);
277 aminoacids.setMultiplier(getMeasureSelector().getMultiplier());
278 }
279 return aminoacids;
280 }
281
282 private NutrientEditorTable getLipidsTable() {
283 if (lipids == null) {
284 lipids = new NutrientEditorTable(NutrientInfo.getLipids());
285 lipids.setFood(food);
286 lipids.setMultiplier(getMeasureSelector().getMultiplier());
287 }
288 return lipids;
289 }
290
291 /***
292 * @todo implement food comments.
293 * @return the food comment panel.
294 */
295 private JPanel getCommentsPanel() {
296 return new JPanel();
297 }
298
299 private JTabbedPane getTabPanel() {
300 if (tabPanel == null) {
301 tabPanel = new JTabbedPane();
302 tabPanel.addTab("Measures", getMeasureEditor());
303 tabPanel.addTab("General", getMacroNutrientsTable());
304 tabPanel.addTab("Minerals", getMineralsTable());
305 tabPanel.addTab("Vitamins", getVitaminsTable());
306 tabPanel.addTab("Amino Acids", getAminoAcidsTable());
307 tabPanel.addTab("Lipids", getLipidsTable());
308 tabPanel.addTab("Comments", getCommentsPanel());
309 }
310 return tabPanel;
311 }
312
313 private MeasureEditor getMeasureEditor() {
314 if (measureEditor == null) {
315 measureEditor = new MeasureEditor(food);
316 measureEditor.addChangeListener(new ChangeListener() {
317 public void stateChanged(ChangeEvent e) {
318 food.setMeasures(getMeasureEditor().getMeasures());
319 getMeasureSelector().setFood(food);
320 }
321 });
322 }
323 return measureEditor;
324 }
325
326 private MeasureWidget getMeasureSelector() {
327 if (null == measure) {
328 measure = new MeasureWidget();
329 measure.setFood(food);
330 measure.addChangeListener(new ChangeListener() {
331 public void stateChanged(ChangeEvent e) {
332 getMacroNutrientsTable().setMultiplier(measure.getMultiplier());
333 getMineralsTable().setMultiplier(measure.getMultiplier());
334 getVitaminsTable().setMultiplier(measure.getMultiplier());
335 getAminoAcidsTable().setMultiplier(measure.getMultiplier());
336 getLipidsTable().setMultiplier(measure.getMultiplier());
337 }
338 });
339 }
340 return measure;
341 }
342
343
344 private class SaveFoodAction extends SaveAction {
345 public SaveFoodAction(ISaveable target) {
346 super(target);
347
348
349 putValue(AbstractAction.SHORT_DESCRIPTION,
350 "Save the changes made to this food");
351 }
352 }
353
354 private class EditGroupAction extends AbstractAction {
355 public EditGroupAction() {
356 super("Edit");
357 }
358
359 public void actionPerformed(ActionEvent e) {
360 doEditGroups();
361 }
362 }
363
364 private class EditSourceAction extends AbstractAction {
365 public EditSourceAction() {
366 super("Edit");
367 }
368
369 public void actionPerformed(ActionEvent e) {
370 doEditSource();
371 }
372 }
373
374 public static void editFood(Food f) {
375 FoodEditor editor = new FoodEditor(CRONOMETER.getInstance(), f);
376 }
377
378 }