Example usage for com.google.gwt.safehtml.shared SafeHtmlUtils fromSafeConstant

List of usage examples for com.google.gwt.safehtml.shared SafeHtmlUtils fromSafeConstant

Introduction

In this page you can find the example usage for com.google.gwt.safehtml.shared SafeHtmlUtils fromSafeConstant.

Prototype

public static SafeHtml fromSafeConstant(String s) 

Source Link

Document

Returns a SafeHtml constructed from a safe string, i.e., without escaping the string.

Usage

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.ReadyMealsPrompt.java

@Override
public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete,
        final Callback1<Function1<Meal, Meal>> onIntermediateStateChange) {

    FlowPanel content = new FlowPanel();

    FlowPanel promptPanel = WidgetFactory.createPromptPanel(
            SafeHtmlUtils.fromSafeConstant(
                    messages.readyMeals_promptText(SafeHtmlUtils.htmlEscape(meal.name.toLowerCase()))),
            ShepherdTour.createTourButton(tour, ReadyMealsPrompt.class.getSimpleName()));
    ShepherdTour.makeShepherdTarget(promptPanel);

    content.add(promptPanel);//from w ww  .j a v  a2  s.c  om

    PVector<WithIndex<FoodEntry>> potentialReadyMeals = filter(zipWithIndex(meal.foods),
            new Function1<WithIndex<FoodEntry>, Boolean>() {
                @Override
                public Boolean apply(WithIndex<FoodEntry> argument) {
                    return argument.value.accept(new FoodEntry.Visitor<Boolean>() {

                        @Override
                        public Boolean visitRaw(RawFood food) {
                            return false;
                        }

                        @Override
                        public Boolean visitEncoded(EncodedFood food) {
                            return !food.isDrink() && !food.link.isLinked() && food.data.readyMealOption;
                        }

                        @Override
                        public Boolean visitTemplate(TemplateFood food) {
                            return false;
                        }

                        @Override
                        public Boolean visitMissing(MissingFood food) {
                            return false;
                        }

                        @Override
                        public Boolean visitCompound(CompoundFood food) {
                            return false;
                        }
                    });
                }
            });

    final Map<CheckBox, Integer> checkBoxToIndex = new HashMap<CheckBox, Integer>();

    FlowPanel checkboxesDiv = new FlowPanel();
    checkboxesDiv.addStyleName("scran24-ready-meals-checkboxes-block");
    checkboxesDiv.getElement().setId("intake24-ready-meals-list");

    for (WithIndex<FoodEntry> f : potentialReadyMeals) {
        FlowPanel rowDiv = new FlowPanel();

        CheckBox readyMealCheck = new CheckBox(SafeHtmlUtils.htmlEscape(f.value.description()));
        readyMealCheck.addStyleName("scran24-ready-meals-checkbox");

        checkBoxToIndex.put(readyMealCheck, f.index);

        rowDiv.add(readyMealCheck);

        checkboxesDiv.add(rowDiv);
    }

    content.add(checkboxesDiv);

    Button finishedButton = WidgetFactory.createButton(messages.editMeal_finishButtonLabel(),
            new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    onComplete.call(MealOperation.update(new Function1<Meal, Meal>() {
                        @Override
                        public Meal apply(Meal argument) {
                            Meal result = argument;

                            for (CheckBox check : checkBoxToIndex.keySet()) {
                                int index = checkBoxToIndex.get(check);
                                result = (check.getValue())
                                        ? result.updateFood(index, result.foods.get(index).markReadyMeal())
                                        : result;
                            }

                            return result.markReadyMealsComplete();
                        }
                    }));
                }
            });

    finishedButton.getElement().setId("intake24-ready-meals-finished-button");

    ShepherdTour.makeShepherdTarget(checkboxesDiv, finishedButton);

    content.add(WidgetFactory.createButtonsPanel(finishedButton));

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            ReadyMealsPrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.SameAsBeforePrompt.java

@Override
public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete,
        Callback1<Function1<Pair<FoodEntry, Meal>, Pair<FoodEntry, Meal>>> updateIntermediateState) {

    final FlowPanel content = new FlowPanel();
    PromptUtil.addBackLink(content);/*  w  w w  .  j a va 2  s .  c o  m*/
    final Panel promptPanel = WidgetFactory.createPromptPanel(
            SafeHtmlUtils.fromSafeConstant(messages
                    .sameAsBefore_promptText(SafeHtmlUtils.htmlEscape(food.description().toLowerCase()))),
            ShepherdTour.createTourButton(tour, SameAsBeforePrompt.class.getSimpleName()));
    content.add(promptPanel);

    final EncodedFood mainFoodAsBefore = asBefore.mainFood;
    final PVector<FoodEntry> assocFoodsAsBefore = asBefore.linkedFoods;

    final double leftoversWeight = mainFoodAsBefore.completedPortionSize().leftoversWeight();
    final double servingWeight = mainFoodAsBefore.completedPortionSize().servingWeight();

    final int leftoversPercent = (int) (leftoversWeight * 100.0 / servingWeight);
    final int leftoversPercentRounded = (leftoversPercent + 4) / 5 * 5;

    final String portionSize = messages.sameAsBefore_servingSize(
            Integer.toString((int) servingWeight) + (mainFoodAsBefore.isDrink() ? " ml" : " g"));
    final String leftovers = (leftoversWeight < 0.01)
            ? (mainFoodAsBefore.isDrink() ? messages.sameAsBefore_noLeftoversDrink()
                    : messages.sameAsBefore_noLeftoversFood())
            : messages.sameAsBefore_leftovers(leftoversPercentRounded + "%");

    HTMLPanel portionSizePanel = new HTMLPanel(portionSize);
    portionSizePanel.getElement().setId("intake24-sab-portion-size");
    content.add(portionSizePanel);
    HTMLPanel leftoversPanel = new HTMLPanel(leftovers);
    leftoversPanel.getElement().setId("intake24-sab-leftovers");
    content.add(leftoversPanel);

    String assocFoodsHTML = messages.sameAsBefore_hadItWith();

    if (!assocFoodsAsBefore.isEmpty())
        assocFoodsHTML += "<ul>";

    for (FoodEntry f : assocFoodsAsBefore) {
        EncodedFood assocFood = f.asEncoded();

        String assocFoodDescription;

        if (assocFood.isInCategory(SpecialData.FOOD_CODE_MILK_IN_HOT_DRINK))
            assocFoodDescription = SafeHtmlUtils.htmlEscape(assocFood.description()) + " ("
                    + SafeHtmlUtils.htmlEscape(MilkInHotDrinkPortionSizeScript.amounts.get(
                            Integer.parseInt(assocFood.completedPortionSize().data.get("milkPartIndex"))).name)
                    + ")";
        else
            assocFoodDescription = SafeHtmlUtils.htmlEscape(assocFood.description()) + " ("
                    + Integer.toString((int) assocFood.completedPortionSize().servingWeight())
                    + (assocFood.isDrink() ? " ml" : " g") + ")";

        assocFoodsHTML += "<li>" + assocFoodDescription + "</li>";
    }

    HTMLPanel assocFoodsPanel;

    if (!assocFoodsAsBefore.isEmpty()) {
        assocFoodsHTML += "</ul>";
        assocFoodsPanel = new HTMLPanel(assocFoodsHTML);

    } else {
        assocFoodsPanel = new HTMLPanel(messages.sameAsBefore_noAddedFoods());
    }

    assocFoodsPanel.getElement().setId("intake24-sab-assoc-foods");
    content.add(assocFoodsPanel);

    Button yes = WidgetFactory.createButton(messages.sameAsBefore_confirmButtonLabel(), new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            onComplete.call(MealOperation.update(new Function1<Meal, Meal>() {
                @Override
                public Meal apply(final Meal meal) {

                    PVector<FoodEntry> updatedFoods = meal.foods
                            .with(foodIndex,
                                    food.withPortionSize(
                                            PortionSize.complete(mainFoodAsBefore.completedPortionSize()))
                                            .disableAllPrompts())
                            .plusAll(map(assocFoodsAsBefore, new Function1<FoodEntry, FoodEntry>() {
                                @Override
                                public FoodEntry apply(FoodEntry assocFood) {
                                    return assocFood.relink(FoodLink.newLinked(food.link.id));
                                }
                            }));

                    return meal.withFoods(updatedFoods);
                }
            }));
        }
    });

    yes.getElement().setId("intake24-sab-yes-button");

    Button no = WidgetFactory.createButton(messages.sameAsBefore_rejectButtonLabel(), new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            onComplete
                    .call(MealOperation.updateEncodedFood(foodIndex, new Function1<EncodedFood, EncodedFood>() {

                        @Override
                        public EncodedFood apply(EncodedFood argument) {
                            return argument.markNotSameAsBefore();
                        }
                    }));

        }
    });

    no.getElement().setId("intake24-sab-no-button");

    content.add(WidgetFactory.createButtonsPanel(yes, no));

    ShepherdTour.makeShepherdTarget(promptPanel, portionSizePanel, leftoversPanel, assocFoodsPanel, yes, no);

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            SameAsBeforePrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.SaveHomeRecipePrompt.java

@Override
public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete,
        final Callback1<Function1<Pair<FoodEntry, Meal>, Pair<FoodEntry, Meal>>> onIntermediateStateChange) {
    SafeHtml promptText = SafeHtmlUtils
            .fromSafeConstant(messages.homeRecipe_savePromptText(SafeHtmlUtils.htmlEscape(food.description())));

    HorizontalPanel recipeNamePanel = new HorizontalPanel();
    recipeNamePanel.setSpacing(5);// ww  w  . j a va  2  s  .  co  m
    recipeNamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

    recipeName.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            recipeName.selectAll();
        }
    });

    recipeName.addKeyUpHandler(new KeyUpHandler() {
        @Override
        public void onKeyUp(KeyUpEvent event) {
            if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
                if (!recipeName.getText().isEmpty())
                    onComplete.call(MealOperation.update(saveRecipeFunction));
            }
        }
    });

    recipeNamePanel.add(new Label(messages.homeRecipe_recipeNameLabel()));
    recipeNamePanel.add(recipeName);

    Button yesButton = WidgetFactory.createGreenButton(messages.yesNoQuestion_defaultYesLabel(),
            "saveRecipeYesButton", new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    if (!recipeName.getText().isEmpty())
                        onComplete.call(MealOperation.update(saveRecipeFunction));
                }
            });

    Button noButton = WidgetFactory.createButton(messages.yesNoQuestion_defaultNoLabel(), new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            onComplete.call(MealOperation.update(dontSaveRecipeFunction));
        }
    });

    FlowPanel contents = new FlowPanel();
    contents.add(WidgetFactory.createPromptPanel(promptText));
    contents.add(recipeNamePanel);
    contents.add(WidgetFactory.createButtonsPanel(yesButton, noButton));

    return new SurveyStageInterface.Aligned(contents, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            SaveHomeRecipePrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.SimpleHomeRecipePrompt.java

@Override
public SurveyStageInterface getInterface(final Callback1<FoodOperation> onComplete,
        final Callback1<Function1<FoodEntry, FoodEntry>> onIntermediateStateChange) {
    final FlowPanel content = new FlowPanel();

    Panel questionPanel;//  w  w w .j  a v  a 2  s .  c  o m

    if (food.customData.containsKey(MissingFood.KEY_ASSOC_FOOD_NAME)) {
        questionPanel = WidgetFactory.createPromptPanel(
                SafeHtmlUtils.fromSafeConstant(messages.missingFood_simpleRecipe_assocFoodPrompt(
                        SafeHtmlUtils.htmlEscape(food.name.toLowerCase()),
                        SafeHtmlUtils.htmlEscape(
                                food.customData.get(MissingFood.KEY_ASSOC_FOOD_NAME).toLowerCase()))),
                ShepherdTour.createTourButton(tour, SimpleHomeRecipePrompt.class.getSimpleName()));
    } else {
        questionPanel = WidgetFactory.createPromptPanel(
                SafeHtmlUtils.fromSafeConstant(messages
                        .missingFood_simpleRecipe_prompt(SafeHtmlUtils.htmlEscape(food.name.toLowerCase()))),
                ShepherdTour.createTourButton(tour, SimpleHomeRecipePrompt.class.getSimpleName()));
    }

    content.add(questionPanel);

    FlowPanel foodName = new FlowPanel();
    foodName.getElement().setId("intake24-missing-food-name");

    Label foodNameLabel = WidgetFactory.createLabel(messages.missingFood_simpleRecipe_nameLabel());
    content.add(foodNameLabel);
    final TextBox foodNameTextBox = new TextBox();
    foodNameTextBox.getElement().addClassName("intake24-missing-food-textbox");
    foodNameTextBox.setText(food.name);

    foodName.add(foodNameLabel);
    foodName.add(foodNameTextBox);

    content.add(foodName);

    if (food.name.equals("Missing food")) {
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
            @Override
            public void execute() {
                foodNameTextBox.setFocus(true);
                foodNameTextBox.selectAll();
            }
        });
    }

    FlowPanel recipe = new FlowPanel();
    recipe.getElement().setId("intake24-missing-food-recipe");

    Label recipeLabel = WidgetFactory.createLabel(messages.missingFood_recipeLabel());
    recipe.add(recipeLabel);
    final TextArea recipeTextArea = new TextArea();
    recipe.add(recipeTextArea);
    recipeTextArea.getElement().addClassName("intake24-missing-food-textarea");
    content.add(recipe);

    FlowPanel portionSize = new FlowPanel();
    portionSize.getElement().setId("intake24-missing-food-portion-size");

    Label portionSizeLabel = WidgetFactory.createLabel(messages.missingFood_simpleRecipe_servedLabel());
    portionSize.add(portionSizeLabel);
    final TextBox portionSizeTextBox = new TextBox();
    portionSizeTextBox.getElement().addClassName("intake24-missing-food-textbox");
    portionSize.add(portionSizeTextBox);
    content.add(portionSize);

    FlowPanel leftovers = new FlowPanel();
    leftovers.getElement().setId("intake24-missing-food-leftovers");

    Label leftoversLabel = WidgetFactory.createLabel(messages.missingFood_simpleRecipe_leftoversLabel());
    leftovers.add(leftoversLabel);
    final TextArea leftoversTextArea = new TextArea();
    leftoversTextArea.getElement().addClassName("intake24-missing-food-textarea");
    leftovers.add(leftoversTextArea);

    content.add(leftovers);

    Button cont = WidgetFactory.createGreenButton(messages.missingFood_continueButtonLabel(),
            "simpleRecipeContinueButton", new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {

                    String name = foodNameTextBox.getText();
                    if (name.isEmpty())
                        name = food.name;

                    onComplete.call(FoodOperation.replaceWith(

                            new MissingFood(food.link, name, food.isDrink,
                                    Option.some(new MissingFoodDescription(mkOption("Home recipe"),
                                            mkOption(recipeTextArea.getText()),
                                            mkOption(portionSizeTextBox.getText()),
                                            mkOption(leftoversTextArea.getText()))),
                                    food.flags, food.customData)));

                }
            });

    cont.getElement().setId("intake24-missing-food-continue-button");

    content.add(WidgetFactory.createButtonsPanel(cont));

    ShepherdTour.makeShepherdTarget(questionPanel, foodName, recipeTextArea, portionSize, leftovers, cont);

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            SimpleHomeRecipePrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.SplitFoodPrompt.java

License:Apache License

@Override
public SurveyStageInterface getInterface(final Callback1<FoodOperation> onComplete,
        Callback1<Function1<FoodEntry, FoodEntry>> updateIntermediateState) {

    final FlowPanel content = new FlowPanel();
    content.add(/*from  ww w.j  a v  a 2s  .c om*/
            new LoadingPanel(messages.foodLookup_loadingMessage(SafeHtmlUtils.htmlEscape(food.description))));

    final FoodOperation disableSplit = FoodOperation.updateRaw(new Function1<RawFood, RawFood>() {
        @Override
        public RawFood apply(RawFood argument) {
            return new RawFood(argument.link, argument.description,
                    argument.flags.plus(RawFood.FLAG_DISABLE_SPLIT), argument.customData);
        }
    });

    FoodLookupService.INSTANCE.getSplitSuggestion(EmbeddedData.localeId, food.description,
            new MethodCallback<SplitSuggestion>() {
                @Override
                public void onFailure(Method method, Throwable exception) {
                    BrowserConsole.error(
                            "Split description failed with code " + method.getResponse().getStatusCode());
                    onComplete.call(disableSplit);
                }

                @Override
                public void onSuccess(Method method, SplitSuggestion result) {
                    if (result.parts.size() == 1)
                        onComplete.call(disableSplit);
                    else {

                        StringBuilder sb = new StringBuilder();

                        sb.append("<p>");
                        sb.append(messages.splitFood_promptText());
                        sb.append("</p>");

                        sb.append("<p>");
                        sb.append(SafeHtmlUtils.htmlEscape(food.description()));
                        sb.append("</p>");

                        sb.append("<p>");
                        sb.append(messages.splitFood_split());

                        sb.append("<ul>");

                        for (String s : result.parts) {
                            sb.append("<li>");
                            sb.append(SafeHtmlUtils.htmlEscape(s));
                            sb.append("</li>");
                        }

                        sb.append("</ul>");
                        sb.append("</p>");

                        sb.append("<p>");
                        sb.append(messages.splitFood_keep());
                        sb.append("</p>");
                        sb.append("<p>");
                        sb.append(messages.splitFood_separateSuggestion());
                        sb.append("</p>");

                        SafeHtml promptText = SafeHtmlUtils.fromSafeConstant(sb.toString());

                        content.clear();

                        content.add(WidgetFactory.createPromptPanel(promptText));

                        Button yesButton = WidgetFactory.createButton(messages.splitFood_yesButtonLabel(),
                                new ClickHandler() {
                                    @Override
                                    public void onClick(ClickEvent event) {
                                        PVector<FoodEntry> replacement = TreePVector.<FoodEntry>empty();

                                        for (String s : result.parts)
                                            replacement = replacement.plus(new RawFood(FoodLink.newUnlinked(),
                                                    s, food.flags.plus(RawFood.FLAG_DISABLE_SPLIT),
                                                    food.customData));

                                        onComplete.call(new FoodOperation.Split(replacement));
                                    }
                                });

                        Button noButton = WidgetFactory.createButton(messages.splitFood_noButtonLabel(),
                                new ClickHandler() {
                                    @Override
                                    public void onClick(ClickEvent event) {
                                        onComplete.call(disableSplit);
                                    }
                                });

                        content.add(WidgetFactory.createButtonsPanel(yesButton, noButton));
                    }
                }
            });

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            SplitFoodPrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.prompts.UnknownPortionSizeMethodPrompt.java

public SurveyStageInterface getInterface(final Callback1<FoodOperation> onComplete,
        final Callback1<Function1<FoodEntry, FoodEntry>> onIntermediateStateChange) {

    final FlowPanel content = new FlowPanel();

    final Button contButton = WidgetFactory.createButton(messages.noPortionMethod_continueButtonLabel(),
            new ClickHandler() {
                @Override//from   ww  w. ja  v a 2s  . co m
                public void onClick(ClickEvent event) {
                    onComplete.call(FoodOperation.updateEncoded(new Function1<EncodedFood, EncodedFood>() {
                        @Override
                        public EncodedFood apply(EncodedFood argument) {
                            return argument.withPortionSize(PortionSize.complete(CompletedPortionSize
                                    .ignore("No porton size estimation method defined for " + description)));
                        }
                    }));
                }
            });

    content.add(WidgetFactory.createPromptPanel(SafeHtmlUtils
            .fromSafeConstant(messages.noPortionMethod_promptText(SafeHtmlUtils.htmlEscape(description)))));
    content.add(WidgetFactory.createButtonsPanel(contButton));

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            UnknownPortionSizeMethodPrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.rules.AskIfHomeRecipeFlexibleRecall.java

private Prompt<FoodEntry, FoodOperation> buildPrompt(final String foodName, final boolean isDrink) {
    PVector<String> options = TreePVector.<String>empty().plus(messages.homeRecipe_haveRecipeChoice())
            .plus(messages.homeRecipe_noRecipeChoice());

    return PromptUtil.asFoodPrompt(
            new RadioButtonPrompt(
                    SafeHtmlUtils.fromSafeConstant(
                            messages.homeRecipe_promptText(SafeHtmlUtils.htmlEscape(foodName.toLowerCase()))),
                    AskIfHomeRecipeFlexibleRecall.class.getSimpleName(), options,
                    messages.homeRecipe_continueButtonLabel(), "homeRecipeOption", Option.<String>none()),
            new Function1<String, FoodOperation>() {
                @Override/*from   ww  w  .  j  a va  2  s . c om*/
                public FoodOperation apply(String argument) {
                    if (argument.equals(messages.homeRecipe_haveRecipeChoice())) {
                        GoogleAnalytics.trackMissingFoodHomeRecipe();
                        return FoodOperation
                                .replaceWith(new CompoundFood(FoodLink.newUnlinked(), foodName, isDrink));
                    } else {
                        return FoodOperation.update(new Function1<FoodEntry, FoodEntry>() {
                            @Override
                            public FoodEntry apply(FoodEntry argument) {
                                GoogleAnalytics.trackMissingFoodNotHomeRecipe();
                                return argument.withFlag(MissingFood.NOT_HOME_RECIPE_FLAG);
                            }
                        });
                    }
                }
            });
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.rules.ShowHomeRecipeServingsPrompt.java

@Override
public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> apply(final Pair<FoodEntry, Meal> data,
        SelectionMode selectionType, final PSet<String> surveyFlags) {
    return data.left.accept(new FoodEntry.Visitor<Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>>>() {

        private Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> getPromptIfApplicable(
                final FoodEntry food) {
            if (forall(Meal.linkedFoods(data.right.foods, data.left), FoodEntry.isPortionSizeComplete)
                    && (!food.customData.containsKey(Recipe.SERVINGS_NUMBER_KEY))) {

                FractionalQuantityPrompt quantityPrompt = new FractionalQuantityPrompt(
                        SafeHtmlUtils.fromSafeConstant(messages
                                .homeRecipe_servingsPromptText(SafeHtmlUtils.htmlEscape(food.description()))),
                        messages.homeRecipe_servingsButtonLabel());

                return Option.some(
                        PromptUtil.asExtendedFoodPrompt(quantityPrompt, new Function1<Double, MealOperation>() {
                            @Override
                            public MealOperation apply(final Double servings) {

                                return MealOperation.update(new Function1<Meal, Meal>() {
                                    @Override
                                    public Meal apply(Meal meal) {

                                        int compoundIndex = meal.foodIndex(data.left);

                                        Meal withUpdatedCompoundFood = meal.updateFood(compoundIndex,
                                                data.left.withCustomDataField(Recipe.SERVINGS_NUMBER_KEY,
                                                        Double.toString(servings)));

                                        return foldl(Meal.linkedFoods(meal.foods, data.left),
                                                withUpdatedCompoundFood,
                                                new Function2<Meal, FoodEntry, Meal>() {
                                                    @Override
                                                    public Meal apply(Meal meal, FoodEntry food) {

                                                        if (food.isEncoded()) {
                                                            EncodedFood encodedFood = food.asEncoded();
                                                            CompletedPortionSize updatedPs = encodedFood
                                                                    .completedPortionSize()
                                                                    .multiply(1.0 / servings);
                                                            return meal.updateFood(meal.foodIndex(food),
                                                                    encodedFood.withPortionSize(
                                                                            new Either.Right(updatedPs)));
                                                        } else {
                                                            return meal;
                                                        }
                                                    }
                                                });
                                    }/*from ww  w  .ja  v  a 2 s.com*/
                                });
                            }
                        }));
            } else {
                return Option.none();
            }
        }

        @Override
        public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> visitRaw(RawFood food) {
            return Option.none();
        }

        @Override
        public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> visitEncoded(EncodedFood food) {
            return Option.none();
        }

        @Override
        public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> visitTemplate(final TemplateFood food) {
            if (food.isTemplateComplete())
                return getPromptIfApplicable(food);
            else
                return Option.none();
        }

        @Override
        public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> visitMissing(MissingFood food) {
            return Option.none();
        }

        @Override
        public Option<Prompt<Pair<FoodEntry, Meal>, MealOperation>> visitCompound(CompoundFood food) {
            return getPromptIfApplicable(food);
        }

    });
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.scheme.FoodSourcesPrompt.java

public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete,
        final Callback1<Function1<Meal, Meal>> onIntermediateStateChange) {

    FlowPanel content = new FlowPanel();

    /* PVector<WithIndex<FoodEntry>> foodsToShow = filter(zipWithIndex(meal.foods), new Function1<WithIndex<FoodEntry>, Boolean>() {
       @Override/*from  w  ww . j av a  2  s  . c  o m*/
       public Boolean apply(WithIndex<FoodEntry> argument) {
    return !argument.value.customData.containsKey("foodSource");
       }
    }); */

    content.add(WidgetFactory.createPromptPanel(
            SafeHtmlUtils.fromSafeConstant("<p>If some of the food items that you had for your <strong>"
                    + meal.safeName() + "</strong> came from a place other than "
                    + SafeHtmlUtils.htmlEscape(mainSourceOption.toLowerCase())
                    + ", please indicate where did you get those.</p>")));

    Grid foodSourceChoice = new Grid(meal.foods.size() + 1, 2);
    foodSourceChoice.setCellPadding(5);
    foodSourceChoice.setWidget(0, 0, new Label("Food"));
    foodSourceChoice.setWidget(0, 1, new Label("Source"));

    final ListBox[] sourceChoices = new ListBox[meal.foods.size()];

    for (int i = 0; i < meal.foods.size(); i++) {
        sourceChoices[i] = createSourceChoice(meal.foods.get(i).customData.get("foodSource"));

        foodSourceChoice.setWidget(i + 1, 0,
                new Label(SafeHtmlUtils.htmlEscape(meal.foods.get(i).description())));
        foodSourceChoice.setWidget(i + 1, 1, sourceChoices[i]);
    }

    content.add(foodSourceChoice);

    Button finished = WidgetFactory.createButton("Continue", new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            onComplete.call(MealOperation.update(new Function1<Meal, Meal>() {
                @Override
                public Meal apply(Meal meal) {
                    return meal.withFoods(
                            map(zipWithIndex(meal.foods), new Function1<WithIndex<FoodEntry>, FoodEntry>() {
                                @Override
                                public FoodEntry apply(WithIndex<FoodEntry> argument) {
                                    return argument.value.withCustomDataField("foodSource",
                                            sourceChoices[argument.index].getValue(
                                                    sourceChoices[argument.index].getSelectedIndex()));
                                }
                            }));
                }
            }));
        }
    });

    content.add(WidgetFactory.createButtonsPanel(finished));

    return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT,
            HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS,
            FoodSourcesPrompt.class.getSimpleName());
}

From source file:uk.ac.ncl.openlab.intake24.client.survey.scheme.LunchFrequenciesQuestion.java

@Override
public SimpleSurveyStageInterface getInterface(final Callback1<Survey> onComplete,
        Callback2<Survey, Boolean> onIntermediateStateChange) {
    final FlowPanel content = new FlowPanel();
    content.addStyleName("intake24-survey-content-container");

    final RadioButtonQuestion shopFreq = new RadioButtonQuestion(SafeHtmlUtils.fromSafeConstant(
            "<p>In a normal school week how often do you go out to the shops for <strong>lunch</strong>?</p>"),
            frequencyOptions, "shopFreq", Option.<String>none());
    final RadioButtonQuestion packFreq = new RadioButtonQuestion(SafeHtmlUtils.fromSafeConstant(
            "<p>In a normal school week how often do you bring in a packed <strong>lunch</strong> from home?</p>"),
            frequencyOptions, "packFreq", Option.<String>none());
    final RadioButtonQuestion schoolLunchFreq = new RadioButtonQuestion(
            SafeHtmlUtils.fromSafeConstant(
                    "<p>In a normal school week how often do you have a school <strong>lunch</strong>?</p>"),
            frequencyOptions, "schoolLunchFreq", Option.<String>none());
    final RadioButtonQuestion homeFreq = new RadioButtonQuestion(SafeHtmlUtils.fromSafeConstant(
            "<p>In a normal school week how often do you go home/to a friend's house for <strong>lunch</strong>?</p>"),
            frequencyOptions, "homeFreq", Option.<String>none());
    final RadioButtonQuestion skipFreq = new RadioButtonQuestion(
            SafeHtmlUtils.fromSafeConstant(
                    "<p>In a normal school week how often do you skip <strong>lunch</strong>?</p>"),
            frequencyOptions, "skipFreq", Option.<String>none());
    final RadioButtonQuestion workFreq = new RadioButtonQuestion(
            SafeHtmlUtils.fromSafeConstant(
                    "<p>In a normal school week how often do you work through <strong>lunch</strong>?</p>"),
            frequencyOptions, "workFreq", Option.<String>none());

    content.add(shopFreq);//from w  w  w  .j ava 2  s  . co  m
    content.add(packFreq);
    content.add(schoolLunchFreq);
    content.add(homeFreq);
    content.add(skipFreq);
    content.add(workFreq);

    final Button accept = WidgetFactory.createButton("Continue");

    accept.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            for (RadioButtonQuestion q : new RadioButtonQuestion[] { shopFreq, packFreq, schoolLunchFreq,
                    homeFreq, skipFreq, workFreq })
                if (q.getChoice().isEmpty()) {
                    q.showWarning();
                    return;
                } else
                    q.clearWarning();

            PMap<String, String> data = state.customData;
            data = data.plus("shopFreq", shopFreq.getChoice().getOrDie())
                    .plus("packFreq", packFreq.getChoice().getOrDie())
                    .plus("schoolLunchFreq", schoolLunchFreq.getChoice().getOrDie())
                    .plus("homeFreq", homeFreq.getChoice().getOrDie())
                    .plus("skipFreq", skipFreq.getChoice().getOrDie())
                    .plus("workFreq", workFreq.getChoice().getOrDie());

            accept.setEnabled(false);
            onComplete.call(state.withData(data));
        }
    });

    content.add(WidgetFactory.createButtonsPanel(accept));

    return new SimpleSurveyStageInterface(content, LunchFrequenciesQuestion.class.getSimpleName());
}