List of usage examples for com.google.gwt.safehtml.shared SafeHtmlUtils fromSafeConstant
public static SafeHtml fromSafeConstant(String s)
From source file:net.scran24.user.client.survey.portionsize.experimental.PortionSizeScriptUtil.java
License:Apache License
public static final SafeHtml defaultServingSizePrompt(final String foodDescription) { return SafeHtmlUtils.fromSafeConstant( messages.asServed_servingPromptText(SafeHtmlUtils.htmlEscape(foodDescription.toLowerCase()))); }
From source file:net.scran24.user.client.survey.portionsize.experimental.PortionSizeScriptUtil.java
License:Apache License
public static final SafeHtml defaultLeftoversPrompt(final String foodDescription) { return SafeHtmlUtils.fromSafeConstant( messages.asServed_leftoversPromptText(SafeHtmlUtils.htmlEscape(foodDescription.toLowerCase()))); }
From source file:net.scran24.user.client.survey.portionsize.experimental.StandardPortionScript.java
License:Apache License
public Option<SimplePrompt<UpdateFunc>> mkQuantityPrompt(PMap<String, String> data, final int unitChoice, String foodDesc) {//from w w w . jav a 2 s . co m String message; StandardUnitDef unit = units.get(unitChoice); if (unit.omitFoodDesc) message = messages.standardUnit_quantityPromptText_omitFood( SafeHtmlUtils.htmlEscape(unitNames.getString(unit.name + "_how_many"))); else message = messages.standardUnit_quantityPromptText_includeFood( SafeHtmlUtils.htmlEscape(unitNames.getString(unit.name + "_how_many")), SafeHtmlUtils.htmlEscape(foodDesc.toLowerCase())); return Option.some(withBackLink(PromptUtil.map( quantityPrompt(SafeHtmlUtils.fromSafeConstant(message), messages.standardUnit_quantityContinueButtonLabel(), "quantity"), new Function1<UpdateFunc, UpdateFunc>() { @Override public UpdateFunc apply(final UpdateFunc f) { return new UpdateFunc() { @Override public PMap<String, String> apply(PMap<String, String> argument) { PMap<String, String> a = f.apply(argument); return a.plus("servingWeight", Double.toString(units.get(unitChoice).weight * Double.parseDouble(a.get("quantity")))) .plus("leftoversWeight", Double.toString(0)); } }; } }))); }
From source file:net.scran24.user.client.survey.portionsize.experimental.StandardPortionScript.java
License:Apache License
@Override public Option<SimplePrompt<UpdateFunc>> nextPrompt(PMap<String, String> data, FoodData foodData) { if (data.containsKey("servingWeight")) return done(); else if (!data.containsKey("unit-choice")) { if (units.size() > 1) return Option.some(withBackLink(standardUnitChoicePrompt( SafeHtmlUtils.fromSafeConstant(messages.standardUnit_unitChoicePromptText()), messages.standardUnit_unitChoiceContinueButtonLabel(), units, new Function1<StandardUnitDef, String>() { @Override public String apply(StandardUnitDef argument) { return messages.standardUnit_choiceLabel(SafeHtmlUtils .htmlEscape(unitNames.getString(argument.name + "_estimate_in"))); }//w w w . j a v a 2 s. c o m }, "unit-choice"))); else return mkQuantityPrompt(data, 0, foodData.description()); } else return mkQuantityPrompt(data, Integer.parseInt(data.get("unit-choice")), foodData.description()); }
From source file:net.scran24.user.client.survey.portionsize.experimental.WeightPortionScript.java
License:Apache License
@Override public Option<SimplePrompt<UpdateFunc>> nextPrompt(PMap<String, String> data, FoodData foodData) { if (data.containsKey("servingWeight")) return done(); else// w w w . j a v a2s .c o m return Option.some(withBackLink(foodWeightPrompt( SafeHtmlUtils.fromSafeConstant( messages.weightTypeIn_promptText(SafeHtmlUtils.htmlEscape(foodData.description()))), SafeHtmlUtils.fromSafeConstant(messages.weightTypeIn_unitLabel()), messages.weightTypeIn_continueLabel(), "servingWeight", "leftoversWeight"))); }
From source file:net.scran24.user.client.survey.prompts.AddMealPrompt.java
@Override public SurveyStageInterface getInterface(final Callback1<SurveyOperation> onComplete, final Callback1<Function1<Survey, Survey>> onIntermediateStateChange) { SafeHtml promptText = SafeHtmlUtils.fromSafeConstant(messages.addMeal_promptText()); HorizontalPanel mealNamePanel = new HorizontalPanel(); mealNamePanel.setSpacing(5);//from w w w. ja v a 2 s . co m mealNamePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); HorizontalPanel mealListPanel = new HorizontalPanel(); mealListPanel.setSpacing(5); mealListPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); final TextBox text = new TextBox(); text.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { text.selectAll(); } }); text.addKeyUpHandler(new KeyUpHandler() { @Override public void onKeyUp(KeyUpEvent event) { if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { if (!text.getText().isEmpty()) onComplete.call(SurveyOperation.update(new Function1<Survey, Survey>() { @Override public Survey apply(Survey argument) { return argument.plusMeal(Meal.empty(text.getText())) .withSelection(new Selection.SelectedMeal(argument.meals.size(), SelectionMode.AUTO_SELECTION)); } })); } } }); final ListBox list = new ListBox(); for (String s : PredefinedMeals.getSuggestedMealNamesForCurrentLocale()) list.addItem(s); list.setSelectedIndex(selectedIndex); text.setText(list.getItemText(list.getSelectedIndex())); list.addChangeHandler(new ChangeHandler() { @Override public void onChange(ChangeEvent event) { text.setText(list.getItemText(list.getSelectedIndex())); } }); mealListPanel.add(new Label(messages.addMeal_predefLabel())); mealListPanel.add(list); mealListPanel.getElement().setId("intake24-predef-meal-name"); ShepherdTour.makeShepherdTarget(mealListPanel); mealNamePanel.add(new Label(messages.addMeal_customLabel())); mealNamePanel.add(text); mealNamePanel.getElement().setId("intake24-custom-meal-name"); ShepherdTour.makeShepherdTarget(mealNamePanel); Button acceptButton = WidgetFactory.createButton(messages.addMeal_addButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { if (!text.getText().isEmpty()) onComplete.call(SurveyOperation.update(new Function1<Survey, Survey>() { @Override public Survey apply(Survey argument) { return argument.plusMeal(Meal.empty(text.getText())) .withSelection(new Selection.SelectedMeal(argument.meals.size(), SelectionMode.AUTO_SELECTION)); } })); } }); acceptButton.getElement().setId("intake24-accept-button"); Button cancelButton = WidgetFactory.createButton(messages.addMeal_cancelButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { onComplete.call(SurveyOperation.noChange); } }); cancelButton.getElement().setId("intake24-cancel-button"); ShepherdTour.makeShepherdTarget(acceptButton, cancelButton); FlowPanel contents = new FlowPanel(); contents.add(WidgetFactory.createPromptPanel(promptText, ShepherdTour.createTourButton(tour, AddMealPrompt.class.getSimpleName()))); contents.add(mealListPanel); contents.add(mealNamePanel); contents.add(WidgetFactory.createButtonsPanel(acceptButton, cancelButton)); return new SurveyStageInterface.Aligned(contents, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS); }
From source file:net.scran24.user.client.survey.prompts.AssociatedFoodPrompt.java
License:Apache License
@Override public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete, Callback1<Function1<Pair<FoodEntry, Meal>, Pair<FoodEntry, Meal>>> updateIntermediateState) { final EncodedFood food = (EncodedFood) pair.left; final FoodPrompt prompt = food.enabledPrompts.get(promptIndex); 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("<p>" + SafeHtmlUtils.htmlEscape(prompt.text) + "</p>"), WidgetFactory.createHelpButton(new ClickHandler() { @Override public void onClick(ClickEvent arg0) { String promptType = AssociatedFoodPrompt.class.getSimpleName(); GoogleAnalytics.trackHelpButtonClicked(promptType); ShepherdTour.startTour(getShepherdTourSteps(), promptType); } })); content.add(promptPanel); ShepherdTour.makeShepherdTarget(promptPanel); final Callback1<FoodData> addNewFood = new Callback1<FoodData>() { @Override public void call(final FoodData result) { onComplete.call(MealOperation.update(new Function1<Meal, Meal>() { @Override public Meal apply(final Meal meal) { // Special case for cereal: // if a "milk on cereal" food is linked to a cereal food // copy bowl type from the parent food Option<String> bowl_id = getParamValue(food, "bowl"); FoodData foodData = bowl_id.accept(new Option.Visitor<String, FoodData>() { @Override public FoodData visitSome(String bowl_id) { return result.withPortionSizeMethods( appendPotionSizeParameter(result.portionSizeMethods, "bowl", bowl_id)); } @Override public FoodData visitNone() { return result; } }); EncodedFood assocFood = new EncodedFood(foodData, FoodLink.newUnlinked(), "associated food prompt"); return linkAssociatedFood(meal.plusFood(assocFood), food, assocFood, prompt.linkAsMain); }; })); } }; final Callback addMissingFood = new Callback() { @Override public void call() { onComplete.call(MealOperation.update(new Function1<Meal, Meal>() { @Override public Meal apply(final Meal meal) { FoodEntry missingFood = new MissingFood(FoodLink.newUnlinked(), prompt.genericName.substring(0, 1).toUpperCase() + prompt.genericName.substring(1), false, Option.<MissingFoodDescription>none()) .withCustomDataField(MissingFood.KEY_ASSOC_FOOD_NAME, food.description()) .withCustomDataField(MissingFood.KEY_ASSOC_FOOD_CATEGORY, prompt.code); return linkAssociatedFood(meal.plusFood(missingFood), food, missingFood, prompt.linkAsMain); } })); } }; final Callback1<FoodEntry> addExistingFood = new Callback1<FoodEntry>() { @Override public void call(final FoodEntry existing) { onComplete.call(MealOperation.update(new Function1<Meal, Meal>() { @Override public Meal apply(final Meal meal) { return linkAssociatedFood(meal, food, existing, prompt.linkAsMain); }; })); } }; foodBrowser = new FoodBrowser(locale, new Callback1<FoodData>() { @Override public void call(FoodData result) { addNewFood.call(result); } }, new Callback1<String>() { @Override public void call(String code) { throw new RuntimeException("Special foods are not allowed as associated foods"); } }, new Callback() { @Override public void call() { addMissingFood.call(); } }, Option.<SkipFoodHandler>none(), false, Option.<Pair<String, String>>none()); Button no = WidgetFactory.createButton(messages.assocFoods_noButtonLabel(), 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.minusPrompt(promptIndex); } })); } }); Button yes = WidgetFactory.createButton(messages.assocFoods_yesButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { if (prompt.isCategoryCode) { content.clear(); PromptUtil.addBackLink(content); content.add(promptPanel); content.add(new HTMLPanel( SafeHtmlUtils.fromSafeConstant(messages.assocFoods_specificFoodPrompt()))); content.add(interf); content.add(foodBrowser); isInBrowserMode = true; foodBrowser.browse(prompt.code, messages.assocFoods_allFoodsDataSetName()); } else { content.clear(); content.add(new LoadingPanel(messages.foodBrowser_loadingMessage())); AsyncRequestAuthHandler.execute(new AsyncRequest<FoodData>() { @Override public void execute(AsyncCallback<FoodData> callback) { lookupService.getFoodData(prompt.code, locale, callback); } }, new AsyncCallback<FoodData>() { @Override public void onFailure(Throwable caught) { content.clear(); content.add(WidgetFactory.createDefaultErrorMessage()); content.add(WidgetFactory.createBackLink()); } @Override public void onSuccess(FoodData result) { addNewFood.call(result); } }); } } }); yes.getElement().setId("intake24-assoc-food-yes-button"); final int existingIndex = CollectionUtils.indexOf(pair.right.foods, new Function1<FoodEntry, Boolean>() { @Override public Boolean apply(FoodEntry argument) { return argument.accept(new FoodEntry.Visitor<Boolean>() { @Override public Boolean visitRaw(RawFood food) { return false; } @Override public Boolean visitEncoded(EncodedFood food) { // don't suggest foods that are already linked to other // foods if (food.link.isLinked()) return false; // don't suggest linking the food to itself else if (food.link.id.equals(pair.left.link.id)) return false; // don't suggest if the food has foods linked to it else if (!Meal.linkedFoods(pair.right.foods, food).isEmpty()) return false; else if (prompt.isCategoryCode) return food.isInCategory(prompt.code); else return food.data.code.equals(prompt.code); } @Override public Boolean visitTemplate(TemplateFood food) { return false; } @Override public Boolean visitMissing(MissingFood food) { return false; } @Override public Boolean visitCompound(CompoundFood food) { return false; } }); } }); no.getElement().setId("intake24-assoc-food-no-button"); tour = TreePVector.<ShepherdTour.Step>empty() .plus(new ShepherdTour.Step("noButton", "#intake24-assoc-food-no-button", helpMessages.assocFood_noButtonTitle(), helpMessages.assocFood_noButtonDescription())) .plus(new ShepherdTour.Step("yesButton", "#intake24-assoc-food-yes-button", helpMessages.assocFood_yesButtonTitle(), helpMessages.assocFood_yesButtonDescription())); if (existingIndex != -1) { Button yesExisting = WidgetFactory.createButton(messages.assocFoods_alreadyEntered(), new ClickHandler() { @Override public void onClick(ClickEvent event) { addExistingFood.call(pair.right.foods.get(existingIndex)); } }); yesExisting.getElement().setId("intake24-assoc-food-yes-existing-button"); tour = tour.plus(new ShepherdTour.Step("yesButton", "#intake24-assoc-food-yes-existing-button", helpMessages.assocFood_yesExistingButtonTitle(), helpMessages.assocFood_yesExistingButtonDescription(), "top right", "bottom right")); ShepherdTour.makeShepherdTarget(yesExisting); buttonsPanel = WidgetFactory.createButtonsPanel(no, yes, yesExisting); } else { buttonsPanel = WidgetFactory.createButtonsPanel(no, yes); } content.add(buttonsPanel); ShepherdTour.makeShepherdTarget(yes, no); interf = new FlowPanel(); return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS); }
From source file:net.scran24.user.client.survey.prompts.BrandNamePrompt.java
public SurveyStageInterface getInterface(final Callback1<FoodOperation> onComplete, final Callback1<Function1<FoodEntry, FoodEntry>> onIntermediateStateChange) { final FlowPanel content = new FlowPanel(); FlowPanel promptPanel = WidgetFactory.createPromptPanel( SafeHtmlUtils.fromSafeConstant( messages.brandName_promptText(SafeHtmlUtils.htmlEscape(description.toLowerCase()))), ShepherdTour.createTourButton(tour, BrandNamePrompt.class.getSimpleName())); ShepherdTour.makeShepherdTarget(promptPanel); content.add(promptPanel);/*from w w w . jav a2 s. co m*/ final Button contButton = WidgetFactory.createGreenButton(messages.brandName_continueButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { onComplete.call(FoodOperation.updateEncoded(new Function1<EncodedFood, EncodedFood>() { @Override public EncodedFood apply(EncodedFood argument) { return argument.withBrand(choice); } })); } }); contButton.setEnabled(false); contButton.getElement().setId("intake24-brand-continue-button"); ShepherdTour.makeShepherdTarget(contButton); VerticalPanel panel = new VerticalPanel(); for (final String name : brandNames) { RadioButton btn = new RadioButton("brand", name); btn.addValueChangeHandler(new ValueChangeHandler<Boolean>() { @Override public void onValueChange(ValueChangeEvent<Boolean> event) { if (event.getValue()) choice = name; contButton.setEnabled(true); } }); panel.add(btn); } panel.setSpacing(4); panel.addStyleName("scran24-brand-name-choice-panel"); panel.getElement().setId("intake24-brand-choice-panel"); ShepherdTour.makeShepherdTarget(panel); content.add(panel); content.add(WidgetFactory.createButtonsPanel(contButton)); return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS); }
From source file:net.scran24.user.client.survey.prompts.BreadLinkedFoodAmountPrompt.java
License:Apache License
@Override public SurveyStageInterface getInterface(final Callback1<MealOperation> onComplete, Callback1<Function1<Pair<FoodEntry, Meal>, Pair<FoodEntry, Meal>>> updateIntermediateState) { FlowPanel content = new FlowPanel(); final EncodedFood food = meal.foods.get(foodIndex).asEncoded(); final EncodedFood mainFood = meal.foods.get(mainFoodIndex).asEncoded(); final String foodDescription = SafeHtmlUtils.htmlEscape(food.description().toLowerCase()); final String mainFoodDescription = SafeHtmlUtils.htmlEscape(mainFood.description().toLowerCase()); final String quantityStr = NumberFormat.getDecimalFormat().format(quantity); final FlowPanel quantityPanel = new FlowPanel(); FlowPanel promptPanel = WidgetFactory.createPromptPanel( SafeHtmlUtils.fromSafeConstant( messages.breadLinkedFood_promptText(foodDescription, mainFoodDescription, quantityStr)), ShepherdTour.createTourButton(shepherdTour, BreadLinkedFoodAmountPrompt.class.getSimpleName())); PromptUtil.addBackLink(content);/*w w w . j a v a 2 s .c o m*/ content.add(promptPanel); ShepherdTour.makeShepherdTarget(promptPanel); Button allButton = WidgetFactory.createButton(messages.breadLinkedFood_allButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { onComplete.call(MealOperation.updateFood(foodIndex, new Function1<FoodEntry, FoodEntry>() { @Override public FoodEntry apply(FoodEntry argument) { EncodedFood f = argument.asEncoded(); return f.withPortionSize( PortionSize.complete(f.completedPortionSize().multiply(quantity))).withFlag( ShowBreadLinkedFoodAmountPrompt.FLAG_BREAD_LINKED_FOOD_AMOUNT_SHOWN); } })); } }); allButton.getElement().setId("intake24-all-button"); ShepherdTour.makeShepherdTarget(allButton); content.add(WidgetFactory.createButtonsPanel(allButton)); final QuantityCounter counter = new QuantityCounter(0.25, quantity, Math.max(1.0, quantity)); ShepherdTour.makeShepherdTarget(counter.fractionalCounter); ShepherdTour.makeShepherdTarget(counter.wholeLabel); ShepherdTour.makeShepherdTarget(counter.wholeCounter); Button confirmQuantityButton = WidgetFactory.createGreenButton(messages.quantity_continueButtonLabel(), new ClickHandler() { @Override public void onClick(ClickEvent event) { onComplete.call(MealOperation.updateFood(foodIndex, new Function1<FoodEntry, FoodEntry>() { @Override public FoodEntry apply(FoodEntry argument) { EncodedFood f = argument.asEncoded(); return f.withPortionSize( PortionSize.complete(f.completedPortionSize().multiply(counter.getValue()))) .withFlag( ShowBreadLinkedFoodAmountPrompt.FLAG_BREAD_LINKED_FOOD_AMOUNT_SHOWN); } })); } }); confirmQuantityButton.getElement().setId("intake24-quantity-prompt-continue-button"); quantityPanel.add(counter); quantityPanel.add(confirmQuantityButton); ShepherdTour.makeShepherdTarget(confirmQuantityButton); content.add(quantityPanel); return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS); }
From source file:net.scran24.user.client.survey.prompts.ChoosePortionSizeMethodPrompt.java
License:Apache License
@Override public SurveyStageInterface getInterface(final Callback1<FoodOperation> onComplete, Callback1<Function1<FoodEntry, FoodEntry>> updateIntermediateState) { final FlowPanel content = new FlowPanel(); content.addStyleName("intake24-choose-portion-method-prompt"); PromptUtil.addBackLink(content);//from w w w .j av a 2 s. c o m final HTMLPanel header = new HTMLPanel("h2", food.description()); content.add(header); FlowPanel promptPanel = WidgetFactory.createPromptPanel( SafeHtmlUtils.fromSafeConstant(messages.choosePortionMethod_promptText(food.description())), ShepherdTour.createTourButton(tour, ChoosePortionSizeMethodPrompt.class.getSimpleName())); content.add(promptPanel); ShepherdTour.makeShepherdTarget(promptPanel); final FlowPanel methodPanel = new FlowPanel(); methodPanel.getElement().setId("intake24-choose-portion-panel"); ShepherdTour.makeShepherdTarget(methodPanel); int index = 0; for (final PortionSizeMethod m : food.data.portionSizeMethods) { Image img = new Image(m.imageUrl); final int indexClosure = index; index++; ClickHandler clickHandler = new ClickHandler() { @Override public void onClick(ClickEvent event) { onComplete.call(FoodOperation.updateEncoded(new Function1<EncodedFood, EncodedFood>() { @Override public EncodedFood apply(EncodedFood argument) { return argument.withSelectedPortionSizeMethod(indexClosure); } })); } }; img.addClickHandler(clickHandler); img.addStyleName("intake24-choose-portion-image"); FlowPanel container = new FlowPanel(); container.addStyleName("intake24-choose-portion-container"); container.add(img); Label label = new Label(SafeHtmlUtils.htmlEscape(descriptions.getString(m.description))); label.addStyleName("intake24-choose-portion-label"); label.addClickHandler(clickHandler); container.add(label); methodPanel.add(container); } content.add(methodPanel); return new SurveyStageInterface.Aligned(content, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP, SurveyStageInterface.DEFAULT_OPTIONS); }