Skip to content

Instantly share code, notes, and snippets.

@doblesesays
Created December 16, 2016 15:04
Show Gist options
  • Save doblesesays/6897790780c1a46a404415ccf38ecd7a to your computer and use it in GitHub Desktop.
Save doblesesays/6897790780c1a46a404415ccf38ecd7a to your computer and use it in GitHub Desktop.
The type.setInputPrompt("Select the type of user") and type.setNullSelectionAllowed(false); still gime me the problem
package com.vaadinspringjpa;
import com.vaadin.ui.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.FontAwesome;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.ui.themes.ValoTheme;
import java.util.Arrays;
import java.util.List;
@SpringComponent
@UIScope
public class CustomerEditor extends VerticalLayout {
private final CustomerRepository repository;
/**
* The currently edited customer
*/
private Customer customer;
/* Fields to edit properties in Customer entity */
TextField firstName = new TextField("First name");
TextField lastName = new TextField("Last name");
List<String> types = Arrays.asList("ADMIN", "USER");
ComboBox type = new ComboBox("Type of user", types);
type.setInputPrompt("Select the type of user");
type.setNullSelectionAllowed(false);
/* Action buttons */
Button save = new Button("Save", FontAwesome.SAVE);
Button cancel = new Button("Cancel");
Button delete = new Button("Delete", FontAwesome.TRASH_O);
CssLayout actions = new CssLayout(save, cancel, delete);
@Autowired
public CustomerEditor(CustomerRepository repository) {
this.repository = repository;
addComponents(firstName, lastName, type, actions);
// Configure and style components
setSpacing(true);
actions.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
save.setStyleName(ValoTheme.BUTTON_PRIMARY);
save.setClickShortcut(ShortcutAction.KeyCode.ENTER);
// wire action buttons to save, delete and reset
save.addClickListener(e -> repository.save(customer));
delete.addClickListener(e -> repository.delete(customer));
cancel.addClickListener(e -> editCustomer(customer));
setVisible(false);
}
public interface ChangeHandler {
void onChange();
}
public final void editCustomer(Customer c) {
final boolean persisted = c.getId() != null;
if (persisted) {
// Find fresh entity for editing
customer = repository.findOne(c.getId());
} else {
customer = c;
}
cancel.setVisible(persisted);
// Bind customer properties to similarly named fields
// Could also use annotation or "manual binding" or programmatically
// moving values from fields to entities before saving
BeanFieldGroup.bindFieldsUnbuffered(customer, this);
setVisible(true);
// A hack to ensure the whole form is visible
save.focus();
// Select all text in firstName field automatically
firstName.selectAll();
}
public void setChangeHandler(ChangeHandler h) {
// ChangeHandler is notified when either save or delete
// is clicked
save.addClickListener(e -> h.onChange());
delete.addClickListener(e -> h.onChange());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment