MainScreen.java

/*
 * Copyright © 2012 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.bcexchanger.ui;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import com.nokia.example.bcexchanger.*;
import com.nokia.example.bcexchanger.comm.ExchangerCommImpl;
import com.nokia.example.bcexchanger.comm.ServiceDiscoveryState;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;

/**
 * 
 * This class implements the main screen of the application. Using
 * this screen user can initiate an exchange of business cards, change
 * own business card and exit the application
 * 
 * The class is extending an example.BCExchanger.ui.Screen class
 * 
 * @see example.BCExchanger.ui.Screen Design patterns: State
 */
public class MainScreen extends Screen {

    private Command changeBCCommand;
    private Command exitCommand;
    private Command sendBCCommand;
    private Form textBox;
    private int typeOfOperation = ServiceDiscoveryState.PUT;

    /**
     * Constructor
     * 
     * @param _midlet -
     *          the parent class which keeps the current UI state
     */
    public MainScreen(BCExchangerMIDlet _midlet) {
        super(_midlet);
        midlet.initComm(); // init happens only the first time MainScreen
        // is created
        String text = "Business Card Exchanger allows you to define your own business card "
                + "and exhange it with business cards of other users in vicinity "
                + "running the same application. The exchange is done over Bluetooth "
                + "and free of charge.\n\n"
                + "Use 'Change own card' to select own card from the address book.\n"
                + "Use 'Exchange card' to initate the search and exchange of the cards.\n\n"
                + "The application is always waiting for connections from remote devices.";
        textBox = new Form("");
        textBox.append(new StringItem("", text));

        changeBCCommand = new Command("Change own card", Command.ITEM, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        sendBCCommand = new Command("Exchange cards", Command.ITEM, 1);

        textBox.addCommand(changeBCCommand);
        textBox.addCommand(exitCommand);
        textBox.addCommand(sendBCCommand);
        textBox.setCommandListener(this);

        displayable = textBox;
    }

    public void commandAction(Command command, Displayable _displayable) {

        if (_displayable != textBox) {
            throw new RuntimeException("Internal error #16");
        }
        if (command == sendBCCommand) {
            try {
                setTypeOfOperation(ServiceDiscoveryState.PUT);
                ((ExchangerCommImpl) midlet.getExchanger()).startSending(ServiceDiscoveryState.PUT);
                String text = "Searching for devices...";
                midlet.changeScreen(new ProgressScreen(midlet, text));
            } catch (IOException e0) {
                midlet.changeScreen(new AlertMessage(midlet,
                        "Cannot start inquiry " + e0, null));
            } catch (Exception e1) {
            }
        } else if (command == changeBCCommand) {

            midlet.changeScreen(new AddressBookScreen(midlet));
        } else if (command == exitCommand) {
            midlet.quit();
        } else {
            // unknown command
            throw new RuntimeException("Internal error #18");
        }
    }

    public int getTypeOfOperation() {
        return typeOfOperation;
    }

    public void setTypeOfOperation(int typeOfOperation) {
        this.typeOfOperation = typeOfOperation;
    }
}