Miscellaneous Features - Part I
Categories
✦We first need to load the categories which we do in
the dish list form together with the dish loop
© Codename One 2017 all rights reserved
Menu menu = Restaurant.getInstance().menu.get();
if(menu.dishes.size() == 0) {
for(PropertyBusinessObject d : AppStorage.getInstance().fetchDishes()) {
Dish dsh = (Dish)d;
menu.dishes.add(dsh);
if(!menu.categories.asList().contains(dsh.category.get())) {
menu.categories.add(dsh.category.get());
}
}
}
Adding/Setting a Category
✦In the dish edit form we can set the category with a
completion list
© Codename One 2017 all rights reserved
String[] cats = new String[Restaurant.getInstance().menu.get().categories.size()];
Restaurant.getInstance().menu.get().categories.asList().toArray(cats);
AutoCompleteTextField category = new AutoCompleteTextField(cats);
if(!Restaurant.getInstance().menu.get().categories.asList().contains(category.getText())) {
Restaurant.getInstance().menu.get().categories.add(category.getText());
}
d.category.set(category.getText());
✦When we handle the OK event we can just add the
category automatically if it’s missing
CategoryEntity ce = null;
for(CategoryEntity current : e.getCategories()) {
if(current.getName().equals(d.getCategory())) {
ce = current;
break;
}
}
if(ce == null && d.getCategory() != null) {
ce = new CategoryEntity(d.getCategory());
categoryRepository.save(ce);
}
CategoryEntity oe = de.getCategory();
de.setCategory(ce);
dishRepository.save(de);
if(d.getId() == null) {
Set<DishEntity> dishes = new HashSet<>(e.getDishes());
dishes.add(de);
e.setDishes(dishes);
restaurantRepository.save(e);
}
if(oe != null) {
cullCategory(e, oe);
}
Server - DishService Put Dish
private void cullCategory(RestaurantEntity e, CategoryEntity ce) {
for(DishEntity de : e.getDishes()) {
if(ce.equals(de.getCategory())) {
return;
}
}
categoryRepository.delete(ce);
}
@RequestMapping(method = RequestMethod.DELETE)
public @ResponseBody String delete(@RequestBody(required = true) Dish dsh) throws IOException {
RestaurantEntity e = restaurantRepository.findBySecret(dsh.getSecret());
for(DishEntity d : e.getDishes()) {
if(d.getId().equals(dsh.getId())) {
HashSet<DishEntity> de = new HashSet<>(e.getDishes());
de.remove(d);
e.setDishes(de);
e.setDishListUpdateTimestamp(System.currentTimeMillis());
restaurantRepository.save(e);
d.setDeleteTime(System.currentTimeMillis());
if(d.getCategory() != null) {
CategoryEntity ce = d.getCategory();
d.setCategory(null);
dishRepository.save(d);
cullCategory(e, ce);
} else {
dishRepository.save(d);
}
Server - cullCategory
Validator val = new Validator();
val.addConstraint(category,
new LengthConstraint(1, "Category is required"));
val.addConstraint(price,
new NumericConstraint(true, 0, 1000000,
"Price must be a positive number"));
val.addSubmitButtons(ok);
Validation
Label price = new Label(Restaurant.getInstance().formatCurrency(d.price.get()),
"PriceBadge");
d.price.addChangeListener(pl ->
price.setText(Restaurant.getInstance().formatCurrency(d.price.get())));
Label title = new Label(d.name.get(), “DishName");
d.name.addChangeListener(pl -> title.setText(d.name.get()));
Label description = new Label(d.description.get(), "DishDescription");
d.description.addChangeListener(pl -> description.setText(d.description.get()));
Container titleAndDescription = BoxLayout.encloseY(title, description);
titleAndDescription.setUIID("BlackGradient");
Container cnt = LayeredLayout.encloseIn(sb,
BorderLayout.south(titleAndDescription),
FlowLayout.encloseRight(price)
);
DishListForm
UIID
Foreground white
background 3f51b5
Transparency 200
Alignment right
padding 1mm
margin 0
Margin top 4mm
public class DetailsForm extends BaseNavigationForm {
private Builder bld = new Builder();
public DetailsForm(AppSettings app) {
super(app, BoxLayout.y());
TextField emailField = addTextAndLabel(
"E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR);
emailField.addActionListener(e -> {
app.restaurantEmail.set(emailField.getText());
AppStorage.getInstance().update(app);
bld.updateRestaurantSettings(app);
});
TextField urlField = addTextAndLabel("Website URL", "", TextField.URL);
urlField.addActionListener(e -> {
Restaurant.getInstance().website.set(urlField.getText());
bld.updateRestaurantSettings(app);
});
MultiButton address = new MultiButton("Address & Location");
if(Restaurant.getInstance().navigationAddress.get() != null &&
Restaurant.getInstance().navigationAddress.get().length() > 0) {
address.setTextLine2(Restaurant.getInstance().navigationAddress.get());
} else {
address.setTextLine2("...");
}
add(address);
address.addActionListener(e -> new AddressForm().show());
DetailsForm
public class DetailsForm extends BaseNavigationForm {
private Builder bld = new Builder();
public DetailsForm(AppSettings app) {
super(app, BoxLayout.y());
TextField emailField = addTextAndLabel(
"E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR);
emailField.addActionListener(e -> {
app.restaurantEmail.set(emailField.getText());
AppStorage.getInstance().update(app);
bld.updateRestaurantSettings(app);
});
TextField urlField = addTextAndLabel("Website URL", "", TextField.URL);
urlField.addActionListener(e -> {
Restaurant.getInstance().website.set(urlField.getText());
bld.updateRestaurantSettings(app);
});
MultiButton address = new MultiButton("Address & Location");
if(Restaurant.getInstance().navigationAddress.get() != null &&
Restaurant.getInstance().navigationAddress.get().length() > 0) {
address.setTextLine2(Restaurant.getInstance().navigationAddress.get());
} else {
address.setTextLine2("...");
}
add(address);
address.addActionListener(e -> new AddressForm().show());
DetailsForm
public class DetailsForm extends BaseNavigationForm {
private Builder bld = new Builder();
public DetailsForm(AppSettings app) {
super(app, BoxLayout.y());
TextField emailField = addTextAndLabel(
"E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR);
emailField.addActionListener(e -> {
app.restaurantEmail.set(emailField.getText());
AppStorage.getInstance().update(app);
bld.updateRestaurantSettings(app);
});
TextField urlField = addTextAndLabel("Website URL", "", TextField.URL);
urlField.addActionListener(e -> {
Restaurant.getInstance().website.set(urlField.getText());
bld.updateRestaurantSettings(app);
});
MultiButton address = new MultiButton("Address & Location");
if(Restaurant.getInstance().navigationAddress.get() != null &&
Restaurant.getInstance().navigationAddress.get().length() > 0) {
address.setTextLine2(Restaurant.getInstance().navigationAddress.get());
} else {
address.setTextLine2("...");
}
add(address);
address.addActionListener(e -> new AddressForm().show());
DetailsForm
MultiButton styles = new MultiButton("Colors & Fonts");
styles.setTextLine2("...");
add(styles);
styles.addActionListener(e -> new StyleForm().show());
MultiButton about = new MultiButton("About Page");
styles.setTextLine2("...");
add(about);
styles.addActionListener(e -> new AboutRestaurantForm().show());
}
private TextField addTextAndLabel(String label, String value) {
TextField tf = new TextField(value);
tf.setHint(label);
add(new Label(label, "TextFieldLabel")).
add(tf);
return tf;
}
DetailsForm

Miscellaneous Features - Part 1.pdf

  • 1.
  • 2.
    Categories ✦We first needto load the categories which we do in the dish list form together with the dish loop © Codename One 2017 all rights reserved Menu menu = Restaurant.getInstance().menu.get(); if(menu.dishes.size() == 0) { for(PropertyBusinessObject d : AppStorage.getInstance().fetchDishes()) { Dish dsh = (Dish)d; menu.dishes.add(dsh); if(!menu.categories.asList().contains(dsh.category.get())) { menu.categories.add(dsh.category.get()); } } }
  • 3.
    Adding/Setting a Category ✦Inthe dish edit form we can set the category with a completion list © Codename One 2017 all rights reserved String[] cats = new String[Restaurant.getInstance().menu.get().categories.size()]; Restaurant.getInstance().menu.get().categories.asList().toArray(cats); AutoCompleteTextField category = new AutoCompleteTextField(cats); if(!Restaurant.getInstance().menu.get().categories.asList().contains(category.getText())) { Restaurant.getInstance().menu.get().categories.add(category.getText()); } d.category.set(category.getText()); ✦When we handle the OK event we can just add the category automatically if it’s missing
  • 4.
    CategoryEntity ce =null; for(CategoryEntity current : e.getCategories()) { if(current.getName().equals(d.getCategory())) { ce = current; break; } } if(ce == null && d.getCategory() != null) { ce = new CategoryEntity(d.getCategory()); categoryRepository.save(ce); } CategoryEntity oe = de.getCategory(); de.setCategory(ce); dishRepository.save(de); if(d.getId() == null) { Set<DishEntity> dishes = new HashSet<>(e.getDishes()); dishes.add(de); e.setDishes(dishes); restaurantRepository.save(e); } if(oe != null) { cullCategory(e, oe); } Server - DishService Put Dish
  • 5.
    private void cullCategory(RestaurantEntitye, CategoryEntity ce) { for(DishEntity de : e.getDishes()) { if(ce.equals(de.getCategory())) { return; } } categoryRepository.delete(ce); } @RequestMapping(method = RequestMethod.DELETE) public @ResponseBody String delete(@RequestBody(required = true) Dish dsh) throws IOException { RestaurantEntity e = restaurantRepository.findBySecret(dsh.getSecret()); for(DishEntity d : e.getDishes()) { if(d.getId().equals(dsh.getId())) { HashSet<DishEntity> de = new HashSet<>(e.getDishes()); de.remove(d); e.setDishes(de); e.setDishListUpdateTimestamp(System.currentTimeMillis()); restaurantRepository.save(e); d.setDeleteTime(System.currentTimeMillis()); if(d.getCategory() != null) { CategoryEntity ce = d.getCategory(); d.setCategory(null); dishRepository.save(d); cullCategory(e, ce); } else { dishRepository.save(d); } Server - cullCategory
  • 6.
    Validator val =new Validator(); val.addConstraint(category, new LengthConstraint(1, "Category is required")); val.addConstraint(price, new NumericConstraint(true, 0, 1000000, "Price must be a positive number")); val.addSubmitButtons(ok); Validation
  • 7.
    Label price =new Label(Restaurant.getInstance().formatCurrency(d.price.get()), "PriceBadge"); d.price.addChangeListener(pl -> price.setText(Restaurant.getInstance().formatCurrency(d.price.get()))); Label title = new Label(d.name.get(), “DishName"); d.name.addChangeListener(pl -> title.setText(d.name.get())); Label description = new Label(d.description.get(), "DishDescription"); d.description.addChangeListener(pl -> description.setText(d.description.get())); Container titleAndDescription = BoxLayout.encloseY(title, description); titleAndDescription.setUIID("BlackGradient"); Container cnt = LayeredLayout.encloseIn(sb, BorderLayout.south(titleAndDescription), FlowLayout.encloseRight(price) ); DishListForm UIID Foreground white background 3f51b5 Transparency 200 Alignment right padding 1mm margin 0 Margin top 4mm
  • 8.
    public class DetailsFormextends BaseNavigationForm { private Builder bld = new Builder(); public DetailsForm(AppSettings app) { super(app, BoxLayout.y()); TextField emailField = addTextAndLabel( "E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR); emailField.addActionListener(e -> { app.restaurantEmail.set(emailField.getText()); AppStorage.getInstance().update(app); bld.updateRestaurantSettings(app); }); TextField urlField = addTextAndLabel("Website URL", "", TextField.URL); urlField.addActionListener(e -> { Restaurant.getInstance().website.set(urlField.getText()); bld.updateRestaurantSettings(app); }); MultiButton address = new MultiButton("Address & Location"); if(Restaurant.getInstance().navigationAddress.get() != null && Restaurant.getInstance().navigationAddress.get().length() > 0) { address.setTextLine2(Restaurant.getInstance().navigationAddress.get()); } else { address.setTextLine2("..."); } add(address); address.addActionListener(e -> new AddressForm().show()); DetailsForm
  • 9.
    public class DetailsFormextends BaseNavigationForm { private Builder bld = new Builder(); public DetailsForm(AppSettings app) { super(app, BoxLayout.y()); TextField emailField = addTextAndLabel( "E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR); emailField.addActionListener(e -> { app.restaurantEmail.set(emailField.getText()); AppStorage.getInstance().update(app); bld.updateRestaurantSettings(app); }); TextField urlField = addTextAndLabel("Website URL", "", TextField.URL); urlField.addActionListener(e -> { Restaurant.getInstance().website.set(urlField.getText()); bld.updateRestaurantSettings(app); }); MultiButton address = new MultiButton("Address & Location"); if(Restaurant.getInstance().navigationAddress.get() != null && Restaurant.getInstance().navigationAddress.get().length() > 0) { address.setTextLine2(Restaurant.getInstance().navigationAddress.get()); } else { address.setTextLine2("..."); } add(address); address.addActionListener(e -> new AddressForm().show()); DetailsForm
  • 10.
    public class DetailsFormextends BaseNavigationForm { private Builder bld = new Builder(); public DetailsForm(AppSettings app) { super(app, BoxLayout.y()); TextField emailField = addTextAndLabel( "E-mail - will receive purchases from the generated app", "", TextField.EMAILADDR); emailField.addActionListener(e -> { app.restaurantEmail.set(emailField.getText()); AppStorage.getInstance().update(app); bld.updateRestaurantSettings(app); }); TextField urlField = addTextAndLabel("Website URL", "", TextField.URL); urlField.addActionListener(e -> { Restaurant.getInstance().website.set(urlField.getText()); bld.updateRestaurantSettings(app); }); MultiButton address = new MultiButton("Address & Location"); if(Restaurant.getInstance().navigationAddress.get() != null && Restaurant.getInstance().navigationAddress.get().length() > 0) { address.setTextLine2(Restaurant.getInstance().navigationAddress.get()); } else { address.setTextLine2("..."); } add(address); address.addActionListener(e -> new AddressForm().show()); DetailsForm
  • 11.
    MultiButton styles =new MultiButton("Colors & Fonts"); styles.setTextLine2("..."); add(styles); styles.addActionListener(e -> new StyleForm().show()); MultiButton about = new MultiButton("About Page"); styles.setTextLine2("..."); add(about); styles.addActionListener(e -> new AboutRestaurantForm().show()); } private TextField addTextAndLabel(String label, String value) { TextField tf = new TextField(value); tf.setHint(label); add(new Label(label, "TextFieldLabel")). add(tf); return tf; } DetailsForm