File size: 7,080 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 |
package view;
import controller.DietController;
import model.DietLog;
import model.Exercise;
import model.Food;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class DailyValuesPanel extends JPanel {
private DietController dietController;
private ViewMediator viewMediator;
private JPanel datePanel;
private JLabel dateLabel;
private JTextField dateField;
private JButton dateButton;
private JPanel namePanel;
private JLabel nameLabel;
private JLabel nameText;
private JPanel weightPanel;
private JLabel weightLabel;
private JLabel weightText;
private JPanel consCalPanel;
private JLabel consCalLabel;
private JLabel consCalText;
private JPanel expCalPanel;
private JLabel expCalLabel;
private JLabel expCalText;
private JPanel netCalPanel;
private JLabel netCalLabel;
private JLabel netCalText;
private JScrollPane foodLogPane;
private JScrollPane exerciseLogPane;
public DailyValuesPanel(DietController dietController, ViewMediator viewMediator) {
this.dietController = dietController;
this.viewMediator = viewMediator;
TitledBorder border = new TitledBorder("Daily Values");
setBorder(border);
setLayout(new GridLayout(4,1));
datePanel = new JPanel();
dateLabel = new JLabel("Date");
dateField = new JTextField(dietController.getCurrentDate());
dateButton = new JButton("Change Date");
dateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
dietController.setCurrentDate(dateField.getText());
viewMediator.updateDailyCharts();
viewMediator.updateDailyValues();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Unable to change date, please ensure your date is in YYYY/MM/DD format.");
}
}
});
datePanel.add(dateLabel);
datePanel.add(dateField);
datePanel.add(dateButton);
namePanel = new JPanel();
nameLabel = new JLabel("Name: ");
nameText = new JLabel("User");
namePanel.add(nameLabel);
namePanel.add(nameText);
weightPanel = new JPanel();
weightLabel = new JLabel("Weight: ");
weightText = new JLabel("0 lbs.");
weightPanel.add(weightLabel);
weightPanel.add(weightText);
consCalPanel = new JPanel();
consCalLabel = new JLabel("Calories Consumed: ");
consCalText = new JLabel("");
consCalPanel.add(consCalLabel);
consCalPanel.add(consCalText);
expCalPanel = new JPanel();
expCalLabel = new JLabel("Calories Expended: ");
expCalText = new JLabel("");
expCalPanel.add(expCalLabel);
expCalPanel.add(expCalText);
netCalPanel = new JPanel();
netCalLabel = new JLabel("Net Calories: ");
netCalText = new JLabel("");
netCalPanel.add(netCalLabel);
netCalPanel.add(netCalText);
JPanel infoPanel = new JPanel();
infoPanel.add(namePanel);
infoPanel.add(weightPanel);
infoPanel.add(consCalPanel);
infoPanel.add(expCalPanel);
infoPanel.add(netCalPanel);
foodLogPane = createFoodLogPane();
exerciseLogPane = createExerciseLogPane();
add(datePanel);
add(infoPanel);
add(foodLogPane);
add(exerciseLogPane);
updateValues();
}
private JScrollPane createFoodLogPane() {
DietLog log = dietController.getDesiredDietLog(dietController.getCurrentDate());
JPanel foodLogPanel = new JPanel();
foodLogPanel.setLayout(new BoxLayout(foodLogPanel, BoxLayout.Y_AXIS));
JScrollPane foodLogScrollPane = new JScrollPane(foodLogPanel);
ArrayList<Food> dailyFood = log.getDailyFood();
for (int i = 0; i<dailyFood.size(); i++) {
Food food = dailyFood.get(i);
JPanel innerPanel = new JPanel();
JLabel innerLabel = new JLabel("Food: " + food.getName() + " Servings: " + food.getServing()
+ " Calories: " + food.getCalories());
JButton innerButton = new JButton("-");
final int index = i;
innerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dietController.removeFoodFromLog(index);
viewMediator.updateDailyCharts();
updateValues();
}
});
innerPanel.add(innerLabel);
innerPanel.add(innerButton);
foodLogPanel.add(innerPanel);
}
return foodLogScrollPane;
}
private JScrollPane createExerciseLogPane() {
DietLog log = dietController.getDesiredDietLog(dietController.getCurrentDate());
JPanel exerciseLogPanel = new JPanel();
exerciseLogPanel.setLayout(new BoxLayout(exerciseLogPanel, BoxLayout.Y_AXIS));
JScrollPane exerciseLogScrollPane = new JScrollPane(exerciseLogPanel);
ArrayList<Exercise> dailyExercise = log.getDailyExercise();
for (int i = 0; i < dailyExercise.size(); i++) {
Exercise exercise = dailyExercise.get(i);
JPanel innerPanel = new JPanel();
JLabel innerLabel = new JLabel("Exercise: " + exercise.getName() + " Calories Burned: " + exercise.getCaloriesBurned());
JButton innerButton = new JButton("-");
final int index = i;
innerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dietController.removeExerciseFromLog(index);
updateValues();
}
});
innerPanel.add(innerLabel);
innerPanel.add(innerButton);
exerciseLogPanel.add(innerPanel);
}
return exerciseLogScrollPane;
}
public void updateValues() {
DietLog log = dietController.getDesiredDietLog(dietController.getCurrentDate());
nameText.setText(dietController.getUserName());
weightText.setText(Double.toString(log.getDailyWeight()) + " lbs.");
consCalText.setText(Integer.toString(log.getTotalCalories()));
expCalText.setText(Double.toString(log.getTotalCaloriesBurned()));
netCalText.setText(Double.toString(log.getNetCals()));
remove(foodLogPane);
foodLogPane = createFoodLogPane();
add(foodLogPane);
remove(exerciseLogPane);
exerciseLogPane = createExerciseLogPane();
add(exerciseLogPane);
updateUI();
}
}
|