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.browsing; import java.util.List; 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.server.ThemeResource; import com.vaadin.ui.AbsoluteLayout; import com.vaadin.ui.CustomComponent; import com.vaadin.ui.Embedded; import com.vaadin.ui.Label; import com.vaadin.ui.Table; /** * Album's detail view component * @author Andrs Paz * */ public class AlbumDetailsView extends CustomComponent { /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */ private static final long serialVersionUID = 1L; /** * Constant for use in the registry */ public static final String ALBUM_DETAILS_VIEW = "albumDetailsView"; // Begin Component elements @AutoGenerated private AbsoluteLayout mainLayout; @AutoGenerated private Table tableAlbumSongs; @AutoGenerated private Embedded embeddedAlbumArtwork; @AutoGenerated private Label labelAlbumReleaseYear; @AutoGenerated private Label labelAlbumArtist; @AutoGenerated private Label labelAlbumName; // End Component elements /** * 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 AlbumDetailsView() { buildMainLayout(); setCompositionRoot(mainLayout); // User code // Table for the album's songs tableAlbumSongs.addContainerProperty("Name", String.class, null); tableAlbumSongs.addContainerProperty("Artist", String.class, null); tableAlbumSongs.addContainerProperty("Time", String.class, null); tableAlbumSongs.addContainerProperty("Price", Double.class, null); // Allow selecting items from the table. tableAlbumSongs.setSelectable(true); // Send changes in selection immediately to server. tableAlbumSongs.setImmediate(true); // Song selection listener tableAlbumSongs.addValueChangeListener(new ValueChangeListener() { private static final long serialVersionUID = 1L; @Override public void valueChange(ValueChangeEvent event) { // View song details viewSong(); } }); } /** * Gets the table for the album's songs. * @return Table for the album's songs */ public Table getTableAlbums() { return tableAlbumSongs; } /** * Loads and displays the album information and songs. * @param album to be displayed * @param albumSongs to be displayed */ public void loadAlbum(IAlbum album, List<ISong> albumSongs) { if (album != null) { labelAlbumName.setValue(album.getName()); labelAlbumArtist.setValue(album.getArtist()); labelAlbumReleaseYear.setValue("" + album.getReleaseYear()); // Album artwork is not yet supported. tableAlbumSongs.removeAllItems(); for (ISong song : albumSongs) { tableAlbumSongs.addItem( new Object[] { song.getName(), song.getArtist(), song.getTime(), song.getPrice() }, song); } } } /** * Gets the selected song from the album's song table and displays its details. */ protected void viewSong() { ISong selectedSong = (ISong) tableAlbumSongs.getValue(); if (selectedSong != null) { BrowsingController.getInstance().viewSong(selectedSong); } } @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%"); // labelAlbumName labelAlbumName = new Label(); labelAlbumName.setImmediate(false); labelAlbumName.setWidth("-1px"); labelAlbumName.setHeight("-1px"); labelAlbumName.setValue("<b>Album Name</b>"); labelAlbumName.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelAlbumName, "top:20.0px;left:20.0px;"); // labelAlbumArtist labelAlbumArtist = new Label(); labelAlbumArtist.setImmediate(false); labelAlbumArtist.setWidth("-1px"); labelAlbumArtist.setHeight("-1px"); labelAlbumArtist.setValue("Album Artist"); mainLayout.addComponent(labelAlbumArtist, "top:40.0px;left:20.0px;"); // labelAlbumReleaseYear labelAlbumReleaseYear = new Label(); labelAlbumReleaseYear.setImmediate(false); labelAlbumReleaseYear.setWidth("-1px"); labelAlbumReleaseYear.setHeight("-1px"); labelAlbumReleaseYear.setValue("<i>Album Release Year</i>"); labelAlbumReleaseYear.setContentMode(com.vaadin.shared.ui.label.ContentMode.HTML); mainLayout.addComponent(labelAlbumReleaseYear, "top:60.0px;left:20.0px;"); // embeddedAlbumArtwork embeddedAlbumArtwork = new Embedded(); embeddedAlbumArtwork.setImmediate(false); embeddedAlbumArtwork.setWidth("90px"); embeddedAlbumArtwork.setHeight("-1px"); embeddedAlbumArtwork.setSource(new ThemeResource("img/component/embedded_icon.png")); embeddedAlbumArtwork.setType(1); embeddedAlbumArtwork.setMimeType("image/png"); mainLayout.addComponent(embeddedAlbumArtwork, "top:20.0px;right:20.0px;"); // tableAlbumSongs tableAlbumSongs = new Table(); tableAlbumSongs.setImmediate(false); tableAlbumSongs.setWidth("100.0%"); tableAlbumSongs.setHeight("100.0%"); mainLayout.addComponent(tableAlbumSongs, "top:100.0px;right:20.0px;bottom:20.0px;left:20.0px;"); return mainLayout; } }