Java tutorial
/** * Copyright 2013 Tommi S.E. Laukkanen * * 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 org.agocontrol.site.viewlet.element; import com.vaadin.data.util.filter.Compare; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.GridLayout; import com.vaadin.ui.HorizontalLayout; import com.vaadin.ui.Notification; import com.vaadin.ui.Table; import com.vaadin.ui.UI; import org.agocontrol.client.BusClient; import org.agocontrol.model.Element; import org.agocontrol.site.AgoControlSiteFields; import org.agocontrol.site.AgoControlSiteUI; import org.vaadin.addons.lazyquerycontainer.EntityContainer; import org.vaadin.addons.sitekit.flow.AbstractFlowlet; import org.vaadin.addons.sitekit.grid.FieldDescriptor; import org.vaadin.addons.sitekit.grid.FilterDescriptor; import org.vaadin.addons.sitekit.grid.FormattingTable; import org.vaadin.addons.sitekit.grid.Grid; import org.vaadin.addons.sitekit.model.Company; import org.vaadin.addons.sitekit.util.ContainerUtil; import javax.jms.MapMessage; import javax.jms.Message; import javax.persistence.EntityManager; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; /** * Element list Flowlet. * * @author Tommi S.E. Laukkanen */ public final class ElementsFlowlet extends AbstractFlowlet { /** Serial version UID. */ private static final long serialVersionUID = 1L; /** The container. */ private EntityContainer<Element> container; /** The grid. */ private Grid grid; @Override public String getFlowletKey() { return "elements"; } @Override public boolean isDirty() { return false; } @Override public boolean isValid() { return true; } @Override public void initialize() { final List<FieldDescriptor> fieldDescriptors = AgoControlSiteFields.getFieldDescriptors(Element.class); final List<FilterDescriptor> filterDefinitions = new ArrayList<FilterDescriptor>(); final EntityManager entityManager = getSite().getSiteContext().getObject(EntityManager.class); container = new EntityContainer<Element>(entityManager, true, true, false, Element.class, 1000, new String[] { "treeIndex" }, new boolean[] { true }, "elementId"); ContainerUtil.addContainerProperties(container, fieldDescriptors); final GridLayout gridLayout = new GridLayout(1, 2); gridLayout.setSizeFull(); gridLayout.setMargin(false); gridLayout.setSpacing(true); gridLayout.setRowExpandRatio(1, 1f); setViewContent(gridLayout); final HorizontalLayout buttonLayout = new HorizontalLayout(); buttonLayout.setSpacing(true); buttonLayout.setSizeUndefined(); gridLayout.addComponent(buttonLayout, 0, 0); final Table table = new FormattingTable(); grid = new Grid(table, container); grid.setFields(fieldDescriptors); grid.setFilters(filterDefinitions); table.setColumnCollapsed("elementId", true); table.setColumnCollapsed("bus", true); table.setColumnCollapsed("created", true); table.setColumnCollapsed("modified", true); gridLayout.addComponent(grid, 0, 1); final Button addButton = getSite().getButton("add"); buttonLayout.addComponent(addButton); addButton.addClickListener(new ClickListener() { /** Serial version UID. */ private static final long serialVersionUID = 1L; @Override public void buttonClick(final ClickEvent event) { final Element element = new Element(); element.setCreated(new Date()); element.setModified(element.getCreated()); element.setOwner((Company) getSite().getSiteContext().getObject(Company.class)); final ElementFlowlet elementView = getViewSheet().forward(ElementFlowlet.class); elementView.edit(element, true); } }); final Button editButton = getSite().getButton("edit"); buttonLayout.addComponent(editButton); editButton.addClickListener(new ClickListener() { /** Serial version UID. */ private static final long serialVersionUID = 1L; @Override public void buttonClick(final ClickEvent event) { final Element entity = container.getEntity(grid.getSelectedItemId()); final ElementFlowlet elementView = getViewSheet().forward(ElementFlowlet.class); elementView.edit(entity, false); } }); final Button removeButton = getSite().getButton("remove"); buttonLayout.addComponent(removeButton); removeButton.addClickListener(new ClickListener() { /** Serial version UID. */ private static final long serialVersionUID = 1L; @Override public void buttonClick(final ClickEvent event) { final Element element = container.getEntity(grid.getSelectedItemId()); container.removeItem(grid.getSelectedItemId()); container.commit(); final BusClient busClient = ((AgoControlSiteUI) UI.getCurrent()).getBusClient(element.getBus()); if (busClient != null) { if (busClient.removeElement(element)) { Notification.show("Element removal sent to bus.", Notification.Type.HUMANIZED_MESSAGE); } else { Notification.show("Element removal bus error.", Notification.Type.ERROR_MESSAGE); } } } }); final Company company = getSite().getSiteContext().getObject(Company.class); container.removeDefaultFilters(); container.addDefaultFilter(new Compare.Equal("owner.companyId", company.getCompanyId())); grid.refresh(); } @Override public void enter() { container.refresh(); } }