Datasets:

ArXiv:
License:
File size: 8,713 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package view;

import controller.DietController;

import javax.sound.midi.SysexMessage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Set;

public class FoodPanel extends JPanel {

    private DietController dietController;
    private ViewMediator viewMediator;

    private JPanel recipeIngredientsPanel;
    private JPanel addFoodPanel;
    private JPanel addRecipePanel;

    private JTextField foodNameField;
    private JTextField caloriesField;
    private JTextField fatField;
    private JTextField carbsField;
    private JTextField proteinField;
    private JTextField recipeNameField;

    private Set<String> foodList;

    public FoodPanel(DietController dietController, ViewMediator viewMediator) {

        this.dietController = dietController;
        this.viewMediator = viewMediator;

        foodList = dietController.getFoodListKeys();

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        addFoodPanel = createFoodPanel();
        add(addFoodPanel);

        addRecipePanel = createRecipePanel();
        add(addRecipePanel);
    }

    public JPanel createFoodPanel() {

        JPanel foodPanel = new JPanel();
        foodPanel.setLayout(new GridLayout(6,1));


        JPanel foodNamePanel = new JPanel();
        foodNamePanel.setLayout(new FlowLayout());
        JLabel foodNameLabel = new JLabel("Food Name");
        foodNameField = new JTextField();
        foodNameField.setText("");
        foodNameField.setPreferredSize(new Dimension(150, 20));
        foodNamePanel.add(foodNameLabel);
        foodNamePanel.add(foodNameField);

        JPanel caloriesPanel = new JPanel();
        caloriesPanel.setLayout(new FlowLayout());
        JLabel caloriesLabel = new JLabel("Calories");
        caloriesField = new JTextField();
        caloriesField.setText("");
        caloriesField.setPreferredSize(new Dimension(150, 20));
        caloriesPanel.add(caloriesLabel);
        caloriesPanel.add(caloriesField);

        JPanel carbsPanel = new JPanel();
        carbsPanel.setLayout(new FlowLayout());
        JLabel carbsLabel = new JLabel("Carbs (g)");
        carbsField = new JTextField();
        carbsField.setText("");
        carbsField.setPreferredSize(new Dimension(150, 20));
        carbsPanel.add(carbsLabel);
        carbsPanel.add(carbsField);

        JPanel proteinPanel = new JPanel();
        proteinPanel.setLayout(new FlowLayout());
        JLabel proteinLabel = new JLabel("Protein (g)");
        proteinField = new JTextField();
        proteinField.setText("");
        proteinField.setPreferredSize(new Dimension(150, 20));
        proteinPanel.add(proteinLabel);
        proteinPanel.add(proteinField);

        JPanel fatPanel = new JPanel();
        fatPanel.setLayout(new FlowLayout());
        JLabel fatLabel = new JLabel("Fat (g)");
        fatField = new JTextField();
        fatField.setText("");
        fatField.setPreferredSize(new Dimension(150, 20));
        fatPanel.add(fatLabel);
        fatPanel.add(fatField);
        

        JButton addFoodButton = new JButton("Add Basic Food");
        addFoodButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String foodName = foodNameField.getText();
                    dietController.addBasicFood(foodName,
                        Integer.parseInt(caloriesField.getText()),
                        Double.parseDouble(carbsField.getText()),
                        Double.parseDouble(proteinField.getText()),
                        Double.parseDouble(fatField.getText()));
                    resetPanel();
                    viewMediator.updateLogPanel();
                    JOptionPane.showMessageDialog(null,
                            "Food successfully added: " + foodName);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null,
                        "Food failed to add. Please ensure all values are valid.");
                }
            }
        });

        foodPanel.add(foodNamePanel);
        foodPanel.add(caloriesPanel);
        foodPanel.add(carbsPanel);
        foodPanel.add(proteinPanel);
        foodPanel.add(fatPanel);
        foodPanel.add(addFoodButton);
        return foodPanel;
    }

    public JPanel createRecipePanel() {
        JPanel recipePanel = new JPanel();
        recipePanel.setLayout(new BoxLayout(recipePanel, BoxLayout.Y_AXIS));;

        JPanel recipeNamePanel = new JPanel();
        recipeNamePanel.setLayout(new FlowLayout());
        JLabel recipeNameLabel = new JLabel("Recipe Name");
        recipeNameField = new JTextField();
        recipeNameField.setText("");
        recipeNameField.setPreferredSize(new Dimension(150, 20));
        recipeNamePanel.add(recipeNameLabel);
        recipeNamePanel.add(recipeNameField);




        recipeIngredientsPanel = new JPanel();
        recipeIngredientsPanel.setLayout(new BoxLayout(recipeIngredientsPanel, BoxLayout.Y_AXIS));
        JScrollPane recipeIngredientsScrollPane = new JScrollPane(recipeIngredientsPanel);
        addIngredientInputPanel();



        JButton recipeButton = new JButton("Add Recipe");
        recipeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String recipeName = recipeNameField.getText();
                try {
                    addRecipe();
                    JOptionPane.showMessageDialog(null,
                            "Recipe successfully added: " + recipeName);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null,
                            ex.getMessage());
                }

            }
        });

        JPanel addItemPanel = new JPanel();
        addItemPanel.setLayout(new FlowLayout());
        JButton addItemButton = new JButton("+");
        addItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addIngredientInputPanel();
            }
        });
        addItemPanel.add(addItemButton);

        recipePanel.add(recipeNamePanel);
        recipePanel.add(recipeIngredientsScrollPane);
        recipePanel.add(addItemPanel);
        recipePanel.add(recipeButton);
        return recipePanel;
    }

    private JPanel addIngredientInputPanel() {
        JPanel ingredientPanel = new JPanel();
        ingredientPanel.setLayout(new FlowLayout());

        JLabel ingredientLabel = new JLabel("Food");
        JComboBox ingredientComboBox = new JComboBox(foodList.toArray());
        JLabel servingLabel = new JLabel("Servings");
        JTextField servingField = new JTextField();
        servingField.setText("1.0");
        servingField.setPreferredSize(new Dimension(30, 20));
        JButton removeItemButton = new JButton("-");
        removeItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                recipeIngredientsPanel.remove(ingredientPanel);
                recipeIngredientsPanel.updateUI();
            }
        });

        ingredientPanel.add(ingredientLabel);
        ingredientPanel.add(ingredientComboBox);
        ingredientPanel.add(servingLabel);
        ingredientPanel.add(servingField);
        ingredientPanel.add(removeItemButton);

        recipeIngredientsPanel.add(ingredientPanel);
        recipeIngredientsPanel.updateUI();
        return ingredientPanel;
    }

    private void addRecipe() {
        ArrayList<String> recipeList = new ArrayList<String>();
        ArrayList<String> servingList = new ArrayList<String>();
        for (Component panel : recipeIngredientsPanel.getComponents()) {
            JPanel innerPanel = (JPanel)panel;
            recipeList.add(((JComboBox)innerPanel.getComponent(1)).getSelectedItem().toString());
            servingList.add(((JTextField)innerPanel.getComponent(3)).getText());
        }
        dietController.addRecipe(recipeNameField.getText(),recipeList,servingList);
        resetPanel();
        viewMediator.updateLogPanel();
    }

    public void resetPanel() {
        foodList = dietController.getFoodListKeys();

        recipeIngredientsPanel.removeAll();
        foodNameField.setText("");
        recipeNameField.setText("");
        caloriesField.setText("");
        fatField.setText("");
        carbsField.setText("");
        proteinField.setText("");

        addIngredientInputPanel();
        this.updateUI();

    }

}