/** * Copyright 2009-2013 Oy 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.addon.jpacontainer.itest.addressbook; import java.io.Serializable; import java.lang.reflect.Method; import java.util.Arrays; import com.vaadin.addon.jpacontainer.itest.domain.Person; import com.vaadin.v7.data.Item; import com.vaadin.v7.data.validator.BeanValidator; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Component; import com.vaadin.v7.ui.DefaultFieldFactory; import com.vaadin.v7.ui.Field; import com.vaadin.v7.ui.Form; import com.vaadin.v7.ui.FormFieldFactory; import com.vaadin.v7.ui.TextField; import com.vaadin.ui.Window; @SuppressWarnings("serial") public class PersonEditor extends Window implements Button.ClickListener, FormFieldFactory { private final Item personItem; private Form editorForm; private Button saveButton; private Button cancelButton; public PersonEditor(Item personItem) { this.personItem = personItem; editorForm = new Form(); editorForm.setFormFieldFactory(this); editorForm.setBuffered(true); editorForm.setImmediate(true); editorForm.setItemDataSource(personItem, Arrays.asList("firstName", "lastName", "phoneNumber", "street", "city", "zipCode", "department")); saveButton = new Button("Save", this); cancelButton = new Button("Cancel", this); editorForm.getFooter().addComponent(saveButton); editorForm.getFooter().addComponent(cancelButton); editorForm.setSizeUndefined(); setContent(editorForm); setCaption(buildCaption()); } /** * @return the caption of the editor window */ private String buildCaption() { return String.format("%s %s", personItem.getItemProperty("firstName") .getValue(), personItem.getItemProperty("lastName").getValue()); } /* * (non-Javadoc) * * @see com.vaadin.ui.Button.ClickListener#buttonClick(com.vaadin.ui.Button. * ClickEvent) */ @Override public void buttonClick(ClickEvent event) { if (event.getButton() == saveButton) { editorForm.commit(); fireEvent(new EditorSavedEvent(this, personItem)); } else if (event.getButton() == cancelButton) { editorForm.discard(); } close(); } /* * (non-Javadoc) * * @see com.vaadin.ui.FormFieldFactory#createField(com.vaadin.data.Item, * java.lang.Object, com.vaadin.ui.Component) */ @Override public Field createField(Item item, Object propertyId, Component uiContext) { Field field = DefaultFieldFactory.get().createField(item, propertyId, uiContext); if ("department".equals(propertyId)) { field = new DepartmentSelector(); } else if (field instanceof TextField) { ((TextField) field).setNullRepresentation(""); } field.addValidator(new BeanValidator(Person.class, propertyId .toString())); return field; } public void addListener(EditorSavedListener listener) { try { Method method = EditorSavedListener.class.getDeclaredMethod( "editorSaved", new Class[] { EditorSavedEvent.class }); addListener(EditorSavedEvent.class, listener, method); } catch (final java.lang.NoSuchMethodException e) { // This should never happen throw new java.lang.RuntimeException( "Internal error, editor saved method not found"); } } public void removeListener(EditorSavedListener listener) { removeListener(EditorSavedEvent.class, listener); } public static class EditorSavedEvent extends Component.Event { private Item savedItem; public EditorSavedEvent(Component source, Item savedItem) { super(source); this.savedItem = savedItem; } public Item getSavedItem() { return savedItem; } } public interface EditorSavedListener extends Serializable { public void editorSaved(EditorSavedEvent event); } }