songstock.web.extensions.browsing.AlbumsListView.java Source code

Java tutorial

Introduction

Here is the source code for songstock.web.extensions.browsing.AlbumsListView.java

Source

/**
* 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.browsing;

import java.util.List;

import catalog.dtos.IAlbum;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;

/**
 * Album's list view component
 * @author Andrs Paz
 *
 */
public class AlbumsListView extends CustomComponent {

    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    // Begin Component elements
    @AutoGenerated
    private AbsoluteLayout mainLayout;

    @AutoGenerated
    private Table tableAlbums;

    @AutoGenerated
    private Label labelTitle;
    // End Component elements

    private static final long serialVersionUID = 1L;

    /**
     *  Constant for use in the registry
     */
    public static final String ALBUMS_LIST_VIEW = "albumsListView";

    /**
     * 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 AlbumsListView() {
        buildMainLayout();
        setCompositionRoot(mainLayout);

        // User code

        // Table for the albums
        tableAlbums.addContainerProperty("Name", String.class, null);
        tableAlbums.addContainerProperty("Artist", String.class, null);
        tableAlbums.addContainerProperty("Price", Double.class, null);

        // Allow selecting items from the table.
        tableAlbums.setSelectable(true);

        // Send changes in selection immediately to server.
        tableAlbums.setImmediate(true);

        // Album selection listener
        tableAlbums.addValueChangeListener(new ValueChangeListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valueChange(ValueChangeEvent event) {
                // View album details
                viewAlbum();
            }
        });
    }

    /**
     * Gets the table for the albums.
     * @return Table for the albums
     */
    public Table getTableAlbums() {
        return tableAlbums;
    }

    /**
     * Loads and displays the albums.
     * @param albums to display
     */
    public void loadAlbums(List<IAlbum> albums) {
        tableAlbums.removeAllItems();

        if (albums != null) {
            for (IAlbum album : albums) {
                tableAlbums.addItem(new Object[] { album.getName(), album.getArtist(), album.getPrice() }, album);
            }
        }
    }

    /**
     * Gets the selected album from the albums' table and displays its details.
     */
    protected void viewAlbum() {
        IAlbum selectedAlbum = (IAlbum) tableAlbums.getValue();
        if (selectedAlbum != null) {
            BrowsingController.getInstance().viewAlbum(selectedAlbum);
        }
    }

    @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>Albums</b>");
        labelTitle.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML);
        mainLayout.addComponent(labelTitle, "top:20.0px;left:20.0px;");

        // tableAlbums
        tableAlbums = new Table();
        tableAlbums.setImmediate(false);
        tableAlbums.setWidth("100.0%");
        tableAlbums.setHeight("100.0%");
        mainLayout.addComponent(tableAlbums, "top:60.0px;right:20.0px;bottom:20.0px;left:20.0px;");

        return mainLayout;
    }

}