Screen.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.capitals.ui;

import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Display;


import com.nokia.example.capitals.CapitalMIDlet;

/**
 * Abstract Screen class. It provides a common interface
 * for all the classes making up the user interface
 */
public abstract class Screen implements CommandListener {

    protected CapitalMIDlet midlet;
    protected Displayable displayable;

    public Screen(CapitalMIDlet midlet) {
        this.midlet = midlet;
    }

    /**
     * Makes the current screen active
     */
    public void makeActive() {
        try {
            Display d = Display.getDisplay(midlet);

            // this prevents from bringing application to foreground
            // in case is application is in background and the current 
            // displayable is the same as the one made active
            if (d.getCurrent() != displayable) {
                d.setCurrent(displayable);
            }
        } catch (NullPointerException e) {
            throw new RuntimeException("Screen.midlet == null");
        }
    }
}