Java tutorial
/* * Copyright (C) 2013 SGV * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.gvcc.jgoffice.wgsys.users; import com.vaadin.addon.jpacontainer.JPAContainer; import com.vaadin.addon.jpacontainer.JPAContainerFactory; import com.vaadin.data.fieldgroup.FieldGroup; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener; import com.vaadin.ui.Component; import com.vaadin.ui.DateField; import com.vaadin.ui.Field; import com.vaadin.ui.FormLayout; import com.vaadin.ui.Notification; import com.vaadin.ui.TextField; import java.util.Date; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; import net.gvcc.jgoffice.dbnavigatorbar.DBOperations; import net.gvcc.jgoffice.dbnavigatorbar.NavigatorButtons; import net.gvcc.jgoffice.dbnavigatorbar.interfaces.IDBNavigatorForm; import net.gvcc.jgoffice.templates.SingleForm; import net.gvcc.jgoffice.templates.interfaces.IGlobalConst; import net.gvcc.wgsys.dao.entities.WgTArUsers; /** * * @author ernst_sgv */ public class UserForm extends SingleForm implements View, IGlobalConst, IDBNavigatorForm { public final static String VIEW_NAME = "USER"; /* * JPAContainer for the master */ private final JPAContainer<WgTArUsers> masterContainer = JPAContainerFactory.make(WgTArUsers.class, PG_PERSISTENCE_NAME); private final FieldGroup masterFormFieldGroup = new FieldGroup( masterContainer.getItem(masterContainer.firstItemId())); /* * Formfields * This fields are connected to the db fields */ private Field<String> firstNameField = null; private Field<String> lastNameField = null; private Field<String> emailField = null; private Field<Date> birthdayField = null; private Field<String> ldapUserField = null; private Field<String> idField = null; public UserForm() { setMainForm(new MainForm()); mainFormHasNavigationBar(true); initializeForm(); } @Override public void attach() { super.attach(); if (isMainFormNavigationBarActive()) { getNavigationBar().addForm(this); } } @Override public void detach() { super.detach(); if (isMainFormNavigationBarActive()) { getNavigationBar().removeForm(this); } } @Override public void enter(ViewChangeListener.ViewChangeEvent event) { } @Override public void handleButtonEvents(NavigatorButtons button) { if (isMainFormNavigationBarActive()) { switch (button) { case POST: switch (getNavigationBar().getCurrentDbOperation()) { case INSERT: try { WgTArUsers user = new WgTArUsers(); user.setFirstname(firstNameField.getValue()); user.setLastname(lastNameField.getValue()); user.setEmail(emailField.getValue()); user.setLdapUser(ldapUserField.getValue()); // FIXME Check birthdayField == NULL if (((DateField) birthdayField).getValue().getTime() > 0) { user.setBirthday(new Date(((DateField) birthdayField).getValue().getTime())); } user.setId(UUID.randomUUID().toString()); JPAContainer<WgTArUsers> container = (JPAContainer<WgTArUsers>) getNavigationBar() .getContainer(); getNavigationBar().setSelectedItemId(container.addEntity(user)); Notification.show("Record inserted successfully", Notification.Type.TRAY_NOTIFICATION); } catch (UnsupportedOperationException | IllegalStateException ex) { Logger.getLogger(UserApplicationForm.class.getName()).log(Level.SEVERE, null, ex); Notification.show("Record not inserted: " + ex.getMessage(), Notification.Type.ERROR_MESSAGE); } finally { getNavigationBar().changeButtonStatus(DBOperations.NONE); } break; } break; default: break; } } } private class MainForm extends FormLayout { private final Logger LOG = Logger.getLogger("Creating " + UserForm.MainForm.class.getName()); public MainForm() { try { firstNameField = masterFormFieldGroup.buildAndBind("Firstname:", "firstname", TextField.class); lastNameField = masterFormFieldGroup.buildAndBind("Lastname:", "lastname", TextField.class); emailField = masterFormFieldGroup.buildAndBind("EMail:", "email", TextField.class); birthdayField = masterFormFieldGroup.buildAndBind("Birthday", "birthday", DateField.class); ldapUserField = masterFormFieldGroup.buildAndBind("LDAP Username:", "ldapUser", TextField.class); idField = masterFormFieldGroup.buildAndBind("Id:", "id", TextField.class); this.addComponents(new Component[] { firstNameField, lastNameField, emailField, birthdayField, ldapUserField, idField }); firstNameField.setWidth("100%"); lastNameField.setWidth("100%"); emailField.setWidth("100%"); ((TextField) firstNameField).setImmediate(true); ((TextField) lastNameField).setImmediate(true); ((TextField) emailField).setImmediate(true); ((TextField) ldapUserField).setImmediate(true); ((DateField) birthdayField).setImmediate(true); ldapUserField.setRequired(true); idField.setVisible(false); if (isMainFormNavigationBarActive()) { getNavigationBar().setDatasource(masterContainer, masterFormFieldGroup); } } catch (FieldGroup.BindException ex) { LOG.log(Level.SEVERE, ex.getMessage()); } } } }