Java tutorial
package com.mcparland.john.vaadin_mvn_arch.samples.crud; /* * #%L * vaadin_mvn_arch-ui * %% * Copyright (C) 2014 John McParland * %% * 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. * #L% */ import com.mcparland.john.vaadin_mvn_arch.samples.backend.data.Availability; import com.mcparland.john.vaadin_mvn_arch.samples.backend.data.Product; import com.vaadin.data.Property; import com.vaadin.data.util.BeanItemContainer; import com.vaadin.data.util.filter.Or; import com.vaadin.data.util.filter.SimpleStringFilter; import com.vaadin.server.FontAwesome; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Label; import com.vaadin.ui.Table; import com.vaadin.ui.themes.ValoTheme; /** * Table of products, handling the visual presentation and filtering of a set of * items. This version uses an in-memory data source that is suitable for small * data sets. */ public class ProductTable extends Table { /** * The serialVersionUID. */ private static final long serialVersionUID = 1L; private BeanItemContainer<Product> container; private ColumnGenerator availabilityGenerator = new ColumnGenerator() { /** * The serialVersionUID. */ private static final long serialVersionUID = 1L; @Override public Object generateCell(Table source, Object itemId, Object columnId) { @SuppressWarnings("rawtypes") Property property = source.getItem(itemId).getItemProperty(columnId); if (property != null) { Availability availability = (Availability) property.getValue(); String color = ""; if (availability == Availability.AVAILABLE) { color = "#2dd085"; } else if (availability == Availability.COMING) { color = "#ffc66e"; } else if (availability == Availability.DISCONTINUED) { color = "#f54993"; } String iconCode = "<span class=\"v-icon\" style=\"font-family: " + FontAwesome.CIRCLE.getFontFamily() + ";color:" + color + "\">&#x" + Integer.toHexString(FontAwesome.CIRCLE.getCodepoint()) + ";</span>"; Label label = new Label(iconCode + " " + property.getValue(), ContentMode.HTML); label.setSizeUndefined(); return label; } return null; } }; public ProductTable() { setSizeFull(); addStyleName(ValoTheme.TABLE_NO_HORIZONTAL_LINES); container = new BeanItemContainer<Product>(Product.class); setContainerDataSource(container); setVisibleColumns("id", "productName", "price", "availability", "stockCount", "category"); setColumnHeaders("ID", "Product", "Price", "Availability", "Stock", "Categories"); setColumnCollapsingAllowed(true); setColumnCollapsed("integerProperty", true); setColumnCollapsed("bigDecimalProperty", true); setColumnWidth("id", 50); setColumnAlignment("price", Align.RIGHT); setColumnAlignment("stockCount", Align.RIGHT); setSelectable(true); setImmediate(true); // Add an traffic light icon in front of availability addGeneratedColumn("availability", availabilityGenerator); // Add " " automatically after price setConverter("price", new EuroConverter()); // Show categories as a comma separated list setConverter("category", new CollectionToStringConverter()); } @Override protected String formatPropertyValue(Object rowId, Object colId, @SuppressWarnings("rawtypes") Property property) { if (colId.equals("stockCount")) { Integer stock = (Integer) property.getValue(); if (stock.equals(0)) { return "-"; } else { return stock.toString(); } } return super.formatPropertyValue(rowId, colId, property); } /** * Filter the table based on a search string that is searched for in the * product name, availability and category columns. * * @param filterString * string to look for */ public void setFilter(String filterString) { container.removeAllContainerFilters(); if (filterString.length() > 0) { SimpleStringFilter nameFilter = new SimpleStringFilter("productName", filterString, true, false); SimpleStringFilter availabilityFilter = new SimpleStringFilter("availability", filterString, true, false); SimpleStringFilter categoryFilter = new SimpleStringFilter("category", filterString, true, false); container.addContainerFilter(new Or(nameFilter, availabilityFilter, categoryFilter)); } } @Override public Product getValue() { return (Product) super.getValue(); } @Override public BeanItemContainer<Product> getContainerDataSource() { return container; } }