⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statusscreen.java

📁 J2ME MIDP_Example_Applications
💻 JAVA
字号:
// Copyright 2002 Nokia Corporation. 
// 
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER, 
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS 
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE 
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE 
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO 
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR 
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE 
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT 
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED 
// BY THIRD PARTIES 
// 
// Furthermore, information provided in this source code is preliminary, 
// and may be changed substantially prior to final release. Nokia Corporation 
// retains the right to make changes to this source code at 
// any time, without notice. This source code is provided for informational 
// purposes only. 
// 
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
// 
// A non-exclusive, non-transferable, worldwide, limited license is hereby 
// granted to the Licensee to download, print, reproduce and modify the 
// source code. The licensee has the right to market, sell, distribute and 
// make available the source code in original or modified form only when 
// incorporated into the programs developed by the Licensee. No other 
// license, express or implied, by estoppel or otherwise, to any other 
// intellectual property rights is granted herein.


package example.delivery;

import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStoreException;
import java.io.IOException;


class StatusScreen
    extends Form
    implements CommandListener, HttpPosterListener, ItemStateListener
{
    private DeliveryMIDlet midlet;
    private HttpPoster httpPoster;
    private boolean readyForInput = true;

    private Command loginCommand;
    private Command getJobCommand;
    private Command setStatusCommand;
    private Command logoutCommand;
    private Command quitCommand;
    private boolean showLogoutCommand = true;
    private static final String LOGGEDIN_LABEL = "LOGGED IN";
    private static final String LOGGEDOUT_LABEL = "LOGGED OUT";
    private StringItem loggedInOutItem;
    private StringItem statusItem;
    private StringItem descriptionItem;
    private StringItem senderAddressItem;
    private StringItem recipientAddressItem;
    private TextField noteItem;


    StatusScreen(DeliveryMIDlet midlet, HttpPoster httpPoster)
    {
        super("Status");
        this.midlet = midlet;
        this.httpPoster = httpPoster;

        // Add 'items' to the Form. The items are first created with
        // default empty string values, then updateDeliveryOrderItems() is used
        // to update their actual values from the record store, and finally
        // the string items are appended to the Form. If a RecordStoreException
        // occurs in method updateDeliveryOrderItems(), an error screen is shown
        // to the user and the default empty string item values from their
        // respective constructors are used.

        String label = showLogoutCommand ? LOGGEDOUT_LABEL : LOGGEDIN_LABEL;
        loggedInOutItem = new StringItem(label, "");
        statusItem = new StringItem("STATUS: ", DeliveryOrder.NOJOB);
        descriptionItem = new StringItem("DESCRIPTION: ", "");
        senderAddressItem = new StringItem("SENDER: ", "");
        recipientAddressItem = new StringItem("RECIPIENT: ", "");
        noteItem = new TextField("NOTE: ", "", 75, TextField.ANY);
        updateDeliveryOrderItems();
        append(loggedInOutItem);
        append(statusItem);
        append(descriptionItem);
        append(senderAddressItem);
        append(recipientAddressItem);
        append(noteItem);


        // Add commands to the Screen.
        // The user must first login to use any remote services.
        loginCommand = new Command("Login", Command.SCREEN, 1);
        quitCommand = new Command("Quit", Command.EXIT, 3);
        addCommand(loginCommand);
        addCommand(quitCommand);

        // These commands are added later, when the user is logged in.
        logoutCommand = new Command("Logout", Command.SCREEN, 1);
        getJobCommand = new Command("Get Job", Command.SCREEN, 1);
        setStatusCommand = new Command("Set Status", Command.SCREEN, 1);

        setCommandListener(this);
        setItemStateListener(this);
    }


    public void itemStateChanged(Item item)
    {
        if (item == noteItem)
        {
            try
            {
                DeliveryOrder.getInstance().setNote(noteItem.getString());
                String note = DeliveryOrder.getInstance().getNote();
            }
            catch(RecordStoreException e)
            {
                ErrorScreen.showError("RecordStore error");
            }
        }
    }


    public void commandAction(Command c, Displayable d)
    {
        if (readyForInput)
        {
            if (c == quitCommand)
            {
                quitAction();
            }
            else if (c == loginCommand)
            {
                loginAction();
            }
            else if (c == setStatusCommand)
            {
                setStatusAction();
            }
            else if (c == logoutCommand)
            {
                logoutAction();
            }
            else if (c == getJobCommand)
            {
                try
                {
                    sendGetDeliveryJobRequest();
                    readyForInput = false;
                }
                catch (IOException e)
                {
                    ErrorScreen.showError("Communications Error");
                }
            }
        }
    }


    private void loginAction()
    {
        midlet.statusScreenLogin();
    }


    private void setStatusAction()
    {
        midlet.statusScreenSetStatus();
    }


    private void logoutAction()
    {
        midlet.statusScreenLogout();
    }


    private void quitAction()
    {
        midlet.statusScreenQuit();
    }


    void beLoggedIn()
    {
        loggedInOutItem.setLabel(LOGGEDIN_LABEL);
        showLogoutCommand = true;
        removeCommand(loginCommand);
        addCommand(logoutCommand);
        addCommand(getJobCommand);
        updateSetStatusCommand();
    }


    void beLoggedOut()
    {
        loggedInOutItem.setLabel(LOGGEDOUT_LABEL);
        showLogoutCommand = false;
        removeCommand(logoutCommand);
        addCommand(loginCommand);
        removeCommand(getJobCommand);
        removeCommand(setStatusCommand);  // no effect if has not been added
    }


    void updateDeliveryOrderItems()
    {
        try
        {
            DeliveryOrder order = DeliveryOrder.getInstance();
            statusItem.setText(order.getStatus());
            descriptionItem.setText(order.getDescription());
            senderAddressItem.setText(order.getSenderAddress());
            recipientAddressItem.setText(order.getRecipientAddress());
            noteItem.setString(order.getNote());
        }
        catch (RecordStoreException e)
        {
            ErrorScreen.showError("RecordStore error: " + e.getMessage());
        }
    }


    void updateStatus(String newStatus)
    {
        try
        {
            DeliveryOrder.getInstance().setStatus(newStatus);
            statusItem.setText(newStatus);
        }
        catch (RecordStoreException e)
        {
            ErrorScreen.showError("RecordStore error: " + e.getMessage());
        }
        catch (SetStatusException e)
        {
            ErrorScreen.showError("Error updating status: " + e.getMessage());
        }
    }


    void updateSetStatusCommand()
    {
        removeCommand(setStatusCommand);  // no effect if not already added
        try
        {
            DeliveryOrder deliveryOrder = DeliveryOrder.getInstance();
            if (deliveryOrder.isStatusCourierModifyable())
            {
                addCommand(setStatusCommand);
            }
        }
        catch(RecordStoreException e)
        {
            ErrorScreen.showError("RecordStore error: " + e.getMessage());
        }
    }


    private void sendGetDeliveryJobRequest()
        throws IOException
    {
        ValueGenerator generator = new ValueGenerator();
        generator.addValue("getJob");
        String dataStr = generator.getString();
        httpPoster.sendRequest(dataStr, this);
    }


    public void receiveHttpResponse(String responseStr)
    {
        ValueParser parser = new ValueParser(responseStr);
        String response = parser.getNextValue();
        if ("getJob-OK".equals(response))
        {
            String id = parser.getNextValue();
            String status = parser.getNextValue();
            String description = parser.getNextValue();
            String senderAddress = parser.getNextValue();
            String recipientAddress = parser.getNextValue();
            String note = parser.getNextValue();
            try
            {
                // Note that DeliveryOrder.init(...) creates a new record
                // store from scratch, with records corresponding to the
                // method parameter values.

                DeliveryOrder.getInstance().init(id,
                                                 status,
                                                 description,
                                                 senderAddress,
                                                 recipientAddress,
                                                 note);
            }
            catch(RecordStoreException e)
            {
                 ErrorScreen.showError("RecordStore error: " + e.getMessage());
            }
            statusItem.setText(status);
            descriptionItem.setText(description);
            senderAddressItem.setText(senderAddress);
            recipientAddressItem.setText(recipientAddress);
            noteItem.setString(note);

            // Note: A state transition to another screen is not needed.
            //       The UI is already in the 'StatusScreen' state.
            updateSetStatusCommand();
        }
        else if ("getJob-error".equals(response))
        {
            String errorMessage = parser.getNextValue();
            if (errorMessage != null)
            {
                if (errorMessage.equals("invalid session"))
                {
                    midlet.anyScreenInvalidSession();
                }
                else
                {
                    ErrorScreen.showError(errorMessage);
                }
            }
            else
            {
                ErrorScreen.showError("Server error");
            }
        }
        else
        {
            ErrorScreen.showError("Server error");
        }
        readyForInput = true;
    }


    public void handleHttpError(String errorStr)
    {
        ErrorScreen.showError("Error communicating with server");
        readyForInput = true;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -