songstock.web.extensions.searching.basic.BasicResultsView.java Source code

Java tutorial

Introduction

Here is the source code for songstock.web.extensions.searching.basic.BasicResultsView.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.searching.basic;

import java.util.List;

import songstock.web.extensions.browsing.BrowsingController;

import catalog.dtos.IAlbum;
import catalog.dtos.ISong;

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;

/**
 * 
 * @author Andrs Paz
 *
 */
public class BasicResultsView extends CustomComponent {

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

    @AutoGenerated
    private AbsoluteLayout mainLayout;

    @AutoGenerated
    private Table tableSongs;

    @AutoGenerated
    private Table tableAlbums;

    @AutoGenerated
    private Label labelSongs;

    @AutoGenerated
    private Label labelAlbums;

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 
     */
    public static final String BASIC_RESULTS_VIEW = "basicResultsView";

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

        // TODO add user code here

        // Album results:

        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);

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

            @Override
            public void valueChange(ValueChangeEvent event) {
                viewAlbum();
            }
        });

        // Song results:

        tableSongs.addContainerProperty("Name", String.class, null);
        tableSongs.addContainerProperty("Artist", String.class, null);
        tableSongs.addContainerProperty("Time", String.class, null);
        tableSongs.addContainerProperty("Price", Double.class, null);

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

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

    protected void viewAlbum() {
        IAlbum selectedAlbum = (IAlbum) tableAlbums.getValue();
        if (selectedAlbum != null) {
            BrowsingController.getInstance().viewAlbum(selectedAlbum);
        }
    }

    public Table getTableAlbums() {
        return tableAlbums;
    }

    public Table getTableSongs() {
        return tableSongs;
    }

    /**
     * 
     * @param albums
     */
    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);
            }
        }
    }

    /**
     * 
     * @param songs
     */
    public void loadSongs(List<ISong> songs) {
        tableSongs.removeAllItems();

        if (songs != null) {
            for (ISong song : songs) {
                tableSongs.addItem(
                        new Object[] { song.getName(), song.getArtist(), song.getTime(), song.getPrice() }, song);
            }
        }
    }

    @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%");

        // labelAlbums
        labelAlbums = new Label();
        labelAlbums.setImmediate(false);
        labelAlbums.setWidth("-1px");
        labelAlbums.setHeight("-1px");
        labelAlbums.setValue("<b>Albums</b>");
        labelAlbums.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML);
        mainLayout.addComponent(labelAlbums, "top:20.0px;left:20.0px;");

        // labelSongs
        labelSongs = new Label();
        labelSongs.setImmediate(false);
        labelSongs.setWidth("-1px");
        labelSongs.setHeight("-1px");
        labelSongs.setValue("<b>Songs</b>");
        labelSongs.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML);
        mainLayout.addComponent(labelSongs, "top:280.0px;left:20.0px;");

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

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

        return mainLayout;
    }

}