SeatingScreen.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 com.nokia.example.moviebooking.moviedb.Showing;

import org.eclipse.ercp.swt.mobile.Command;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Shell;

/**
 * Simple shell that contain the SeatingCanvas
 */
class SeatingScreen implements SelectionListener, ControlListener {

    private Command exitCommand, okCommand, backCommand;
    private MovieBooking main;
    private Shell seatingShell;
    private Shell mainShell;
    private SeatingCanvas seatingCanvas;

    SeatingScreen(MovieBooking main, Shell mainShell, Showing showing,
            int[][] selectedSeats, int ticketsCount) {
        this.main = main;

        this.mainShell = mainShell;

        seatingShell = new Shell(mainShell, SWT.BORDER | SWT.TITLE);
        seatingShell.setText("Seats");

        // Add a ControlListener to watch the size changes of the top-level
        // Shell and update the seatingShell's size
        mainShell.addControlListener(this);

        // Seating canvas displays the seating arrangement
        seatingCanvas = new SeatingCanvas(
                seatingShell,
                showing,
                selectedSeats,
                ticketsCount);

        // Size and location of the seatingCanvas are controlled by
        // The FillLayout of the seatingShell.
        seatingShell.setLayout(new FillLayout());
        updateSize();

        // add some commands
        exitCommand = new Command(seatingShell, Command.EXIT, 0);
        exitCommand.setText("Exit");
        exitCommand.addSelectionListener(this);

        okCommand = new Command(seatingShell, Command.OK, 1);
        okCommand.setText("Select");
        okCommand.addSelectionListener(this);

        backCommand = new Command(seatingShell, Command.BACK, 0);
        backCommand.setText("Back");
        backCommand.addSelectionListener(this);

        seatingShell.open();
    }

    private void destroy() {
        mainShell.removeControlListener(this);
        seatingCanvas.destroy();
        seatingShell.dispose();
    }

    public void widgetSelected(SelectionEvent e) {
        if (e.widget == exitCommand) {
            destroy();
            main.exit();
        } else if (e.widget == okCommand) {
            destroy();
            main.setSelectedSeats(seatingCanvas.getSelectedSeats());
        } else if (e.widget == backCommand) {
            destroy();
            main.cancelSeatSelection();
        }

    }

    public void widgetDefaultSelected(SelectionEvent e) {
        // Nothing to do
    }

    public void controlMoved(ControlEvent e) {
        // Can be left at (0, 0)
    }

    public void controlResized(ControlEvent e) {
        updateSize();
    }

    private void updateSize() {
        // Set the same size as the parent
        seatingShell.setBounds(mainShell.getBounds());

    }
}