123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- /*
- * Copyright 2000-2014 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.tests.fieldgroup;
-
- import java.util.Iterator;
- import java.util.Map;
-
- import com.vaadin.annotations.Theme;
- import com.vaadin.data.Property.ValueChangeEvent;
- import com.vaadin.data.Property.ValueChangeListener;
- import com.vaadin.data.fieldgroup.BeanFieldGroup;
- import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
- import com.vaadin.data.fieldgroup.PropertyId;
- import com.vaadin.data.util.BeanItem;
- import com.vaadin.data.util.BeanItemContainer;
- import com.vaadin.legacy.data.Validator.InvalidValueException;
- import com.vaadin.legacy.data.validator.LegacyIntegerRangeValidator;
- import com.vaadin.legacy.ui.LegacyField;
- import com.vaadin.server.VaadinRequest;
- import com.vaadin.shared.ui.label.ContentMode;
- import com.vaadin.shared.util.SharedUtil;
- import com.vaadin.tests.components.AbstractTestUIWithLog;
- import com.vaadin.ui.Alignment;
- import com.vaadin.ui.Button;
- import com.vaadin.ui.Button.ClickEvent;
- import com.vaadin.ui.Button.ClickListener;
- import com.vaadin.ui.CheckBox;
- import com.vaadin.ui.ComboBox;
- import com.vaadin.ui.Component;
- import com.vaadin.ui.GridLayout;
- import com.vaadin.ui.HorizontalLayout;
- import com.vaadin.ui.Label;
- import com.vaadin.ui.Notification;
- import com.vaadin.ui.Notification.Type;
- import com.vaadin.ui.TextField;
- import com.vaadin.ui.themes.ValoTheme;
-
- @Theme("valo")
- public abstract class AbstractBasicCrud extends AbstractTestUIWithLog {
-
- protected AbstractForm form;
- protected static String[] columns = new String[] { "firstName", "lastName",
- "gender", "birthDate", "age", "alive", "address.streetAddress",
- "address.postalCode", "address.city", "address.country" };
- protected BeanItemContainer<ComplexPerson> container = ComplexPerson
- .createContainer(100);;
- {
- container.addNestedContainerBean("address");
- }
- protected ComboBox formType;
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.tests.components.AbstractTestUI#setup(com.vaadin.server.
- * VaadinRequest)
- */
- @Override
- protected void setup(VaadinRequest request) {
- getLayout().setSizeFull();
- getLayout().setSpacing(true);
- getContent().setSizeFull();
- form = new CustomForm();
-
- formType = new ComboBox();
- formType.setNullSelectionAllowed(false);
- formType.setWidth("300px");
- formType.addItem(form);
- formType.setValue(form);
- formType.addItem(new AutoGeneratedForm(TextField.class));
- formType.addItem(new AutoGeneratedForm(LegacyField.class));
- Iterator<?> iterator = formType.getItemIds().iterator();
- formType.setItemCaption(iterator.next(), "TextField based form");
- formType.setItemCaption(iterator.next(),
- "Auto generated form (TextFields)");
- formType.setItemCaption(iterator.next(),
- "Auto generated form (Any fields)");
- formType.addValueChangeListener(new ValueChangeListener() {
- @Override
- public void valueChange(ValueChangeEvent event) {
- AbstractForm oldForm = form;
- form = (AbstractForm) formType.getValue();
- replaceComponent(oldForm, form);
- }
- });
-
- addComponent(formType);
-
- }
-
- public class CustomForm extends AbstractForm {
-
- private TextField firstName = new TextField("First name");
- private TextField lastName = new TextField("Last name");
- private TextField gender = new TextField("Gender");
- private TextField birthDate = new TextField("Birth date");
- private TextField age = new TextField("Age");
- private CheckBox alive = new CheckBox("Alive");
- private Label errorLabel = new Label((String) null, ContentMode.HTML);
-
- @PropertyId("address.streetAddress")
- private TextField address_streetAddress = new TextField(
- "Street address");
- @PropertyId("address.postalCode")
- private TextField address_postalCode = new TextField("Postal code");
- @PropertyId("address.city")
- private TextField address_city = new TextField("City");
- @PropertyId("address.country")
- private TextField address_country = new TextField("Country");
-
- public CustomForm() {
- fieldGroup.bindMemberFields(this);
-
- address_postalCode.setNullRepresentation("");
- gender.setNullRepresentation("");
- age.setNullRepresentation("");
- address_country.setNullRepresentation("");
-
- // Last name editing is disabled through property readonly.
- // Postal code editing is disabled through disabling field.
- /*
- * Currently only sets the initial state because of
- * https://dev.vaadin.com/ticket/17847
- *
- * Must set lastName state initially as BeanFieldGroup can't tell it
- * should be read-only before setting an item data source
- */
- lastName.setReadOnly(true);
- address_postalCode.setEnabled(false);
-
- birthDate.setNullRepresentation("");
-
- age.addValidator(new LegacyIntegerRangeValidator(
- "Must be between 0 and 100", 0, 100));
-
- setDefaultComponentAlignment(Alignment.MIDDLE_LEFT);
- addComponents(firstName, lastName, gender, birthDate, age, alive,
- address_streetAddress, address_postalCode, address_city,
- address_country);
-
- errorLabel.addStyleName(ValoTheme.LABEL_COLORED);
- setRows(3);
- addComponent(errorLabel, 0, 2, getColumns() - 1, 2);
-
- HorizontalLayout hl = new HorizontalLayout(save, cancel);
- hl.setSpacing(true);
- addComponent(hl);
-
- }
-
- @Override
- protected void handleCommitException(CommitException e) {
- String message = "";
- // Produce error message in the order in which the fields are in the
- // layout
- for (Component c : this) {
- if (!(c instanceof LegacyField)) {
- continue;
- }
- LegacyField<?> f = (LegacyField<?>) c;
- Map<LegacyField<?>, InvalidValueException> exceptions = e
- .getInvalidFields();
- if (exceptions.containsKey(f)) {
- message += f.getCaption() + ": "
- + exceptions.get(f).getLocalizedMessage()
- + "<br/>\n";
- }
- }
-
- errorLabel.setValue(message);
- }
-
- @Override
- protected void discard() {
- super.discard();
- errorLabel.setValue(null);
- }
- }
-
- protected abstract void deselectAll();
-
- public class AbstractForm extends GridLayout {
- protected Button save = new Button("Save");
- protected Button cancel = new Button("Cancel");
-
- protected BeanFieldGroup<ComplexPerson> fieldGroup = new BeanFieldGroup<ComplexPerson>(
- ComplexPerson.class) {
- @Override
- protected void configureField(
- com.vaadin.legacy.ui.LegacyField<?> field) {
- super.configureField(field);
- if (field.getCaption().equals("Postal code")) {
- // Last name editing is disabled through property.
- // Postal code editing is disabled through field.
- /*
- * This is needed because of
- * https://dev.vaadin.com/ticket/17847
- */
- field.setEnabled(false);
- }
- };
- };
-
- public AbstractForm() {
- super(5, 1);
- setSpacing(true);
- setId("form");
- save.addClickListener(new ClickListener() {
- @Override
- public void buttonClick(ClickEvent event) {
- try {
- fieldGroup.commit();
- log("Saved " + fieldGroup.getItemDataSource());
- } catch (CommitException e) {
- handleCommitException(e);
- log("Commit failed: " + e.getMessage());
- }
- }
- });
- cancel.addClickListener(new ClickListener() {
- @Override
- public void buttonClick(ClickEvent event) {
- log("Discarded " + fieldGroup.getItemDataSource());
- discard();
- }
- });
- }
-
- protected void discard() {
- deselectAll();
- }
-
- protected void handleCommitException(CommitException e) {
- String message = "";
- for (Object propertyId : e.getInvalidFields().keySet()) {
- LegacyField<?> f = e.getFieldGroup().getField(propertyId);
- message += f.getCaption() + ": "
- + e.getInvalidFields().get(propertyId);
- }
-
- if (!message.isEmpty()) {
- Notification.show(message, Type.ERROR_MESSAGE);
- }
- }
-
- public void edit(BeanItem<ComplexPerson> item) {
- fieldGroup.setItemDataSource(item);
- }
- }
-
- public class AutoGeneratedForm extends AbstractForm {
-
- public AutoGeneratedForm(Class<? extends LegacyField> class1) {
- for (String p : columns) {
- LegacyField f = fieldGroup.getFieldFactory()
- .createField(container.getType(p), class1);
- f.setCaption(SharedUtil.propertyIdToHumanFriendly(p));
- fieldGroup.bind(f, p);
- addComponent(f);
- }
-
- HorizontalLayout hl = new HorizontalLayout(save, cancel);
- hl.setSpacing(true);
- addComponent(hl);
- }
-
- }
- }
|