public void onClick(ClickEvent event) {
addFood();
}
});
NewFoodItemList = new ListBox();
NewFoodItemList.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER) {
addFood();
}
}
});
MealFlexTable.setWidget(0, 1, NewFoodItemList);
MealFlexTable.setWidget(0, 2, AddFoodPshBtn);
AddFoodPshBtn.setWidth("50px");
FoodsFlexTable = new FlexTable();
MealVerticalPanel.add(new HTML("<br/><br/>"));
MealVerticalPanel.add(FoodsFlexTable);
FoodsFlexTable.setWidth("500px");
FoodsFlexTable.setText(0, 0, "Food Items");
FoodsFlexTable.setText(0, 1, "Qty");
FoodsFlexTable.setText(0, 2, "Cals");
FoodsFlexTable.setText(0, 3, "Fat Cals");
FoodsFlexTable.setText(0, 4, "Fat");
FoodsFlexTable.setText(0, 5, "Carbs");
FoodsFlexTable.setText(0, 6, "Protein");
// FoodsFlexTable.setStyleName("cw-FlexTable");
FoodsFlexTable.getCellFormatter().addStyleName(0, 0,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 1,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 2,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 3,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 4,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 5,
"calculatorlistheader");
FoodsFlexTable.getCellFormatter().addStyleName(0, 6,
"calculatorlistheader");
FoodsFlexTable.setVisible(false);
Label lblTotalCalories = new Label("Total Calories:");
lblTotalCalories.setStylePrimaryName("gwt-TotalCa");
MealFlexTable.setWidget(3, 0, lblTotalCalories);
lblTotalCalories.setWidth("103px");
TotalCalsTextBox = new TextBox();
TotalCalsTextBox.setReadOnly(true);
TotalCalsTextBox.setText(Integer.toString(0));
MealFlexTable.setWidget(3, 1, TotalCalsTextBox);
Label lblNewLabel = new Label("Meal Name:");
MealFlexTable.setWidget(4, 0, lblNewLabel);
MealLabelTextBox = new TextBox();
MealFlexTable.setWidget(4, 1, MealLabelTextBox);
SaveMealPshBtn = new Button("Save");
SaveMealPshBtn.setEnabled(true);
SaveMealPshBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (MealLabelTextBox.getText().trim().equals(""))
Window.alert("Please name this meal.");
else {
newMeal.setLabel(MealLabelTextBox.getText());
newMeal.setDateOfMeal(new Date());
meals.add(newMeal);
saveUserMeal();
}
}
});
MealFlexTable.setWidget(4, 2, SaveMealPshBtn);
SaveMealPshBtn.setSize("60px", "25px");
/* Previously stored Meals */
MealVerticalPanel.add(new HTML("</br></br>"));
PreviousMealsFlexTable = new FlexTable();
PreviousMealsFlexTable.setVisible(false);
MealVerticalPanel.add(PreviousMealsFlexTable);
PreviousMealsFlexTable.setText(0, 0, "Previous Meals:");
PreviousMeals = new ListBox();
PreviousMeals.setVisible(false);
PreviousMealsFlexTable.setWidget(0, 1, PreviousMeals);
PreviousMealsPshBtn = new Button("Copy Meal");
PreviousMealsPshBtn.setVisible(false);
PreviousMealsFlexTable.setWidget(0, 2, PreviousMealsPshBtn);
PreviousMeals.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
Meal meal = meals.get(PreviousMeals.getSelectedIndex());
FlexTable flexTable = new FlexTable();
flexTable.setWidth("300px");
MealsListFlexTable.setWidget(0, 0, flexTable);
flexTable.setText(0, 0, "Items");
flexTable.setText(0, 1, "Qty");
flexTable.getCellFormatter().addStyleName(0, 0,
"calculatorlistheader");
flexTable.getCellFormatter().addStyleName(0, 1,
"calculatorlistheader");
for (int i = 0; i < meal.getFoodItems().size(); i++) {
flexTable.setText(i + 1, 0,
"- " + meal.getFoodItems().get(i).getName());
flexTable.setText(i + 1, 1, "x" + meal.getQuantity(i));
}
}
});
PreviousMealsPshBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (PreviousMeals.getSelectedIndex() == -1
|| PreviousMeals.getValue(PreviousMeals
.getSelectedIndex()) == null
|| PreviousMeals
.getValue(PreviousMeals.getSelectedIndex())
.trim().equals(""))
return;
FoodsFlexTable.setVisible(true);
Meal meal = meals.get(PreviousMeals.getSelectedIndex());
newMeal = new Meal();
for (FoodItem foodItem : meal.getFoodItems()) {
for (int i = 0; i < meal.getQuantity(foodItem); i++)
newMeal.addFoodItem(foodItem);
int row = newMeal.indexOfFoodItem(foodItem) + 1;
int qty = newMeal.getQuantity(foodItem);
final FoodItem foodItem2 = foodItem;
FoodsFlexTable.setText(row, 0, foodItem.getName());
FoodsFlexTable.setText(row, 1, "x" + qty);
FoodsFlexTable.setText(row, 2,
Double.toString((int) foodItem.getCalories() * qty));
FoodsFlexTable.setText(
row,
3,
Double.toString((int) foodItem.getFatCalories()
* qty));
FoodsFlexTable.setText(row, 4,
Double.toString((int) foodItem.getFatGrams() * qty));
FoodsFlexTable.setText(
row,
5,
Double.toString((int) foodItem.getCarbohydrates()
* qty));
FoodsFlexTable.setText(row, 6,
Double.toString((int) foodItem.getProtein() * qty));
TotalCalsTextBox.setText(Double.toString((int) newMeal
.totalCalories()));
Button removeFood = new Button("x");
removeFood.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int removedIndex = newMeal
.indexOfFoodItem(foodItem2);
newMeal.removeFoodItem(removedIndex);
FoodsFlexTable.removeRow(removedIndex + 1);
TotalCalsTextBox.setText(Double
.toString((int) newMeal.totalCalories()));
}
});
FoodsFlexTable.setWidget(row, 7, removeFood);
}
}
});
MealsListFlexTable = new FlexTable();
MealVerticalPanel.add(MealsListFlexTable);
MealsListFlexTable.setWidth("500px");
// MealsListFlexTable.setStyleName("cw-FlexTable");
/* Workout / Exercise */
WorkoutVerticalPanel = new VerticalPanel();
tabPanel.add(WorkoutVerticalPanel, " Workout ", false);
WorkoutVerticalPanel.setSize("6cm", "4cm");
tabPanel.addStyleName("table-center");
WorkoutFlexTable = new FlexTable();
WorkoutVerticalPanel.add(WorkoutFlexTable);
WorkoutFlexTable.setWidth("500px");
Label lblExerciseItem = new Label("Exercise Item:");
WorkoutFlexTable.setWidget(0, 0, lblExerciseItem);
lblExerciseItem.setWidth("121px");
AddExercisePshBtn = new Button("Add");
AddExercisePshBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
addExercise();
}
});
NewExerciseItemList = new ListBox();
NewExerciseItemList.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER) {
addExercise();
}
}