Java tutorial
/** * Copyright 2013 Universidad Icesi * * This file is part of SongStock. * * SongStock 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. * * SongStock 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 SongStock. If not, see <http://www.gnu.org/licenses/>. **/ package songstock.web.extensions.shoppingcart; import java.text.DecimalFormat; import java.util.List; import sales.dtos.IItem; import com.vaadin.annotations.AutoGenerated; import com.vaadin.ui.AbsoluteLayout; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Button.ClickListener; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.Label; import com.vaadin.ui.Table; /** * * @author Andrs Paz * */ public class ShoppingCartView extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ @AutoGenerated private AbsoluteLayout mainLayout; @AutoGenerated private Label labelItemCountValue; @AutoGenerated private Label labelItemCount; @AutoGenerated private Label labelTotalValue; @AutoGenerated private Label labelTotal; @AutoGenerated private Button buttonCheckout; @AutoGenerated private Button buttonEmpty; @AutoGenerated private Table tableShoppingCartItems; @AutoGenerated private Label labelTitle; /** * */ private static final long serialVersionUID = 1L; public static final String SHOPPING_CART_VIEW = "shoppingCartView"; /** * The constructor should first build the main layout, set the * composition root and then do any custom initialization. * * The constructor will not be automatically regenerated by the * visual editor. */ public ShoppingCartView() { buildMainLayout(); setCompositionRoot(mainLayout); // User code labelTotalValue.setValue(""); labelItemCountValue.setValue(""); tableShoppingCartItems.addContainerProperty("Name", String.class, null); tableShoppingCartItems.addContainerProperty("Artist", String.class, null); tableShoppingCartItems.addContainerProperty("Type", String.class, null); tableShoppingCartItems.addContainerProperty("Price", Double.class, null); tableShoppingCartItems.addContainerProperty("Remove", Button.class, null); // Allow selecting items from the table. tableShoppingCartItems.setSelectable(true); // Send changes in selection immediately to server. tableShoppingCartItems.setImmediate(true); buttonEmpty.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { emptyShoppingCart(); } }); buttonCheckout.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { checkoutItems(); } }); } protected void checkoutItems() { ShoppingCartController.getInstance().getShoppingCartItems(); } protected void emptyShoppingCart() { ShoppingCartController.getInstance().emptyShoppingCart(); } public void clear() { tableShoppingCartItems.removeAllItems(); labelTotalValue.setValue("0.0"); labelItemCountValue.setValue("0"); } public void loadItems(List<IItem> items) { double totalPrice = 0; if (items != null) { for (final IItem item : items) { totalPrice += item.getPrice(); Button removeBtn = new Button("Remove"); removeBtn.addClickListener(new ClickListener() { private static final long serialVersionUID = 1L; @Override public void buttonClick(ClickEvent event) { removeItem(item); } }); tableShoppingCartItems.addItem(new Object[] { item.getName(), item.getArtist(), item.getType(), item.getPrice(), removeBtn }, item); } DecimalFormat df = new DecimalFormat("#.##"); labelTotalValue.setValue("" + df.format(totalPrice)); labelItemCountValue.setValue("" + items.size()); } } protected void removeItem(IItem item) { ShoppingCartController.getInstance().removeItem(item); tableShoppingCartItems.removeItem(item); DecimalFormat df = new DecimalFormat("#.##"); labelTotalValue .setValue("" + (df.format(Double.parseDouble(labelTotalValue.getValue()) - item.getPrice()))); labelItemCountValue.setValue("" + (Integer.parseInt(labelItemCountValue.getValue()) - 1)); } @AutoGenerated private AbsoluteLayout buildMainLayout() { // common part: create layout mainLayout = new AbsoluteLayout(); mainLayout.setImmediate(false); mainLayout.setWidth("100%"); mainLayout.setHeight("100%"); // top-level component properties setWidth("100.0%"); setHeight("100.0%"); // labelTitle labelTitle = new Label(); labelTitle.setImmediate(false); labelTitle.setWidth("-1px"); labelTitle.setHeight("-1px"); labelTitle.setValue("<b>Shopping Cart</b>"); labelTitle.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelTitle, "top:20.0px;left:20.0px;"); // tableShoppingCartItems tableShoppingCartItems = new Table(); tableShoppingCartItems.setImmediate(false); tableShoppingCartItems.setWidth("100.0%"); tableShoppingCartItems.setHeight("100.0%"); mainLayout.addComponent(tableShoppingCartItems, "top:100.0px;right:20.0px;bottom:20.0px;left:20.0px;"); // buttonEmpty buttonEmpty = new Button(); buttonEmpty.setCaption("Empty"); buttonEmpty.setImmediate(true); buttonEmpty.setWidth("-1px"); buttonEmpty.setHeight("-1px"); mainLayout.addComponent(buttonEmpty, "top:60.0px;left:20.0px;"); // buttonCheckout buttonCheckout = new Button(); buttonCheckout.setCaption("Checkout"); buttonCheckout.setImmediate(true); buttonCheckout.setWidth("-1px"); buttonCheckout.setHeight("-1px"); mainLayout.addComponent(buttonCheckout, "top:60.0px;left:100.0px;"); // labelTotal labelTotal = new Label(); labelTotal.setImmediate(false); labelTotal.setWidth("-1px"); labelTotal.setHeight("-1px"); labelTotal.setValue("Total: $"); mainLayout.addComponent(labelTotal, "top:62.0px;left:200.0px;"); // labelTotalValue labelTotalValue = new Label(); labelTotalValue.setImmediate(false); labelTotalValue.setWidth("-1px"); labelTotalValue.setHeight("-1px"); labelTotalValue.setValue("Label"); mainLayout.addComponent(labelTotalValue, "top:62.0px;left:244.0px;"); // labelItemCount labelItemCount = new Label(); labelItemCount.setImmediate(false); labelItemCount.setWidth("-1px"); labelItemCount.setHeight("-1px"); labelItemCount.setValue("Items:"); mainLayout.addComponent(labelItemCount, "top:62.0px;left:360.0px;"); // labelItemCountValue labelItemCountValue = new Label(); labelItemCountValue.setImmediate(false); labelItemCountValue.setWidth("-1px"); labelItemCountValue.setHeight("-1px"); labelItemCountValue.setValue("Label"); mainLayout.addComponent(labelItemCountValue, "top:62.0px;left:400.0px;"); return mainLayout; } }