MoviesListScreen.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.moviebooking.ui;

import java.io.InputStream;
import java.io.IOException;

import com.nokia.example.moviebooking.moviedb.Movie;

import org.eclipse.ercp.swt.mobile.Command;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

/**
 * This class takes the list of movies and displays it to the user
 * The list of movies is shown at the right and a description of each
 * at the left.
 */
class MoviesListScreen implements SelectionListener {

    private Command reserveMovieCommand, exitCommand;
    private MovieBooking main;
    private List moviesList;
    private java.util.Vector movies;
    private Composite mainComposite;
    private Image image;
    private Label imageLabel;
    private Label descriptionText;

    MoviesListScreen(MovieBooking main, Shell mainShell,
            java.util.Vector movies) {
        this.main = main;
        this.movies = movies;

        mainShell.setText("Movies");
        mainShell.setRedraw(false);
        mainComposite = new Composite(mainShell, SWT.NONE);

        // Size and location of this Composite are controlled by
        // the GridLayout of the mainShell.
        mainComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainShell.layout();

        // This contains the list of movies
        moviesList = new List(mainComposite, SWT.BORDER);

        // Widgets to describe the movie
        imageLabel = new Label(mainComposite, SWT.TOP | SWT.CENTER);
        descriptionText = new Label(mainComposite, SWT.WRAP | SWT.CENTER);

        // Populate moviesList
        for (int i = 0; i < movies.size(); ++i) {
            Movie m = (Movie) movies.elementAt(i);
            moviesList.add(m.getName());
        }
        if (movies.size() > 0) {
            moviesList.select(0);
            movieSelected(0);
        }
        moviesList.addSelectionListener(this);

        // Distribute the widgets using a FormLayout
        FormLayout layout = new FormLayout();
        mainComposite.setLayout(layout);

        // Set movies list location
        FormData moviesListData = new FormData();
        moviesListData.top = new FormAttachment(0);
        moviesListData.left = new FormAttachment(0);
        moviesListData.right = new FormAttachment(50);
        moviesListData.bottom = new FormAttachment(100);
        moviesList.setLayoutData(moviesListData);

        // Set image location
        FormData imageLabelData = new FormData();
        imageLabelData.top = new FormAttachment(moviesList, 0, SWT.TOP);
        imageLabelData.left = new FormAttachment(moviesList);
        imageLabelData.right = new FormAttachment(100);
        imageLabel.setLayoutData(imageLabelData);

        // Set image location
        FormData descriptionTextData = new FormData();
        descriptionTextData.top = new FormAttachment(imageLabel);
        descriptionTextData.left = new FormAttachment(
                imageLabel, 0, SWT.LEFT);
        descriptionTextData.right = new FormAttachment(100);
        descriptionTextData.bottom = new FormAttachment(
                moviesList,
                0,
                SWT.BOTTOM);
        descriptionText.setLayoutData(descriptionTextData);

        // Apply the FormLayout
        mainComposite.layout();
        mainShell.setRedraw(true);
        // Add commands
        exitCommand = new Command(mainShell, Command.EXIT, 0);
        exitCommand.setText("Exit");
        exitCommand.addSelectionListener(this);

        reserveMovieCommand = new Command(mainShell, Command.GENERAL, 1);
        reserveMovieCommand.setText("Reserve");
        reserveMovieCommand.addSelectionListener(this);
        reserveMovieCommand.setDefaultCommand();
    }

    // Called when a command is invoked or a movie is selected
    public void widgetSelected(SelectionEvent e) {
        if (e.widget == exitCommand) {
            destroy();
            main.exit();
        } else if (e.widget == reserveMovieCommand) {
            if (moviesList.getSelectionIndex() >= 0) {
                // Order of operations is important.
                // We need to destroy before reserve that will create a new UI.
                // But need to call moviesList.getSelectionIndex() before destroy.
                Movie selectedMovie = (Movie) movies.elementAt(moviesList.getSelectionIndex());
                destroy();
                main.startReserve(selectedMovie);
            }
        } else if (e.widget == moviesList) {
            // This is called when the user selects a movie
            movieSelected(moviesList.getSelectionIndex());
        }
    }

    public void widgetDefaultSelected(SelectionEvent e) {
        // Do nothing
    }

    // Called when a movie is selected.
    // Take the picture and name and put in the
    // right side window.
    private void movieSelected(int index) {
        if (index >= 0) {
            Movie m = (Movie) movies.elementAt(index);
            if (m != null) {
                if (m.getImageLocation() != null) {
                    try {
                        InputStream stream = getClass().getResourceAsStream(
                                m.getImageLocation());
                        if (stream != null) {
                            Image newImage = new Image(mainComposite.getDisplay(), stream);
                            imageLabel.setImage(newImage);
                            if (image != null) {
                                image.dispose();
                            }
                            image = newImage;
                            stream.close();
                        }
                    } catch (IOException e) {
                        // Do nothing, a picture is missing
                    }
                    descriptionText.setText(m.getDescription());
                }
            }
        }
    }

    private void destroy() {
        // Dispose the widgets by disposing the parent
        mainComposite.dispose();
        reserveMovieCommand.dispose();
        exitCommand.dispose();
        // Dispose the current image
        if (image != null && !image.isDisposed()) {
            image.dispose();
        }
    }
}