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

📄 setstatusscreen.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 SetStatusScreen
    extends List
    implements CommandListener, HttpPosterListener
{
    private DeliveryMIDlet midlet;
    private HttpPoster httpPoster;
    private Command cancelCommand;
    private Command setStatusCommand;
    private boolean readyForInput = true;


    SetStatusScreen(DeliveryMIDlet midlet, HttpPoster httpPoster)
    {
        super("Set Status", List.IMPLICIT);

        this.midlet = midlet;
        this.httpPoster = httpPoster;

        update(); // add List elements

        setStatusCommand = new Command("Submit", Command.OK, 1);
        addCommand(setStatusCommand);
        cancelCommand = new Command("Cancel", Command.CANCEL, 2);
        addCommand(cancelCommand);

        setCommandListener(this);
    }


    // Update list elements (possible statuses that could be set)
    private void update()
    {
        try
        {
            DeliveryOrder deliveryOrder = DeliveryOrder.getInstance();
            String id = deliveryOrder.getId();
            String status = deliveryOrder.getStatus();

            // The List is a variable size one, depending on the status.

            // remove the old List elements
            while (size() > 0)
            {
                // deleting elements changes the List's size()
                delete(0);
            }

            // add the new List elements
            if (status.equals(DeliveryOrder.ASSIGNED))
            {
                this.append(DeliveryOrder.PICKEDUP, null);
                this.append(DeliveryOrder.SENDERDOESNOTEXIST, null);
                this.append(DeliveryOrder.NOJOB, null);
            }
            else if (status.equals(DeliveryOrder.PICKEDUP))
            {
                this.append(DeliveryOrder.COMPLETED, null);
                this.append(DeliveryOrder.RETURNEDTOSENDER, null);
                this.append(DeliveryOrder.COURIERCANCELLED, null);
            }
            else if (status.equals(DeliveryOrder.COURIERCANCELLED))
            {
                this.append(DeliveryOrder.RETURNEDTOSENDER, null);
            }
        }
        catch (RecordStoreException e)
        {
            ErrorScreen.showError("RecordStore error: " + e.getMessage());
        }
    }


    public void commandAction(Command c, Displayable d)
    {
        if (c == cancelCommand)
        {
            midlet.setStatusScreenCancel();
        }
        else if (readyForInput)
        {
            if (c == setStatusCommand)
            {
                try
                {
                    String newStatus = getString(getSelectedIndex());
                    if (newStatus.equals(DeliveryOrder.NOJOB)) // client-side
                    {
                        newStatus = DeliveryOrder.OPENED; // server-side status
                    }

                    DeliveryOrder deliveryOrder = DeliveryOrder.getInstance();
                    if (deliveryOrder.isStatusCourierModifyable())
                    {
                        sendSetStatusRequest(newStatus);
                        readyForInput = false; // wait for HTTP response
                    }
                    else
                    {
                        ErrorScreen.showError(
                                        "Can't modify status. " +
                                        "Use 'Get Job' to request a new job."
                                    );
                    }
                }
                catch (IOException e)
                {
                    ErrorScreen.showError("Communications error: " +
                                          e.getMessage());
                }
                catch (RecordStoreException e)
                {
                    ErrorScreen.showError("RecordStore error: " +
                                          e.getMessage());
                }
            }
        }
    }


    private void sendSetStatusRequest(String status)
        throws IOException, RecordStoreException
    {
        ValueGenerator generator = new ValueGenerator();
        generator.addValue("setStatus");
        generator.addValue(DeliveryOrder.getInstance().getId());
        generator.addValue(status);
        String note = DeliveryOrder.getInstance().getNote();
        if (note == null)
        {
            note = "";
        }
        generator.addValue(note);
        String dataStr = generator.getString();
        httpPoster.sendRequest(dataStr, this);
    }


    public void receiveHttpResponse(String responseStr)
    {
        ValueParser parser = new ValueParser(responseStr);
        String response = parser.getNextValue();
        if ("setStatus-OK".equals(response))
        {
            try
            {
                String remoteId = parser.getNextValue();
                String remoteStatus = parser.getNextValue();
                String localId =
                           DeliveryOrder.getInstance().getId();

                if (remoteId != null && remoteStatus != null &&
                    remoteId.equals(localId))
                {
                    midlet.setStatusScreenDone(remoteStatus);
                }
                else
                {
                    // This is 'else' clause is bulletproofing. The id
                    // mismatch error should be caught by the remote
                    // service, which would generate an error message
                    // 'setStatus-error invalid id' for
                    // the MIDlet to handle (see below).

                    midlet.setStatusScreenRemoteError();
                }
            }
            catch (RecordStoreException e)
            {
                midlet.setStatusScreenError("RecordStore error: " +
                                            e.getMessage());
            }
        }
        else if ("setStatus-error".equals(response))
        {

            String errorMessage = parser.getNextValue();
            if (errorMessage != null)
            {
                if (errorMessage.equals("invalid session"))
                {
                    midlet.anyScreenInvalidSession();
                }
                else
                {
                    // "invalid id" and other errors are handled here
                    midlet.setStatusScreenRemoteError();
                }
            }
            else
            {
                midlet.setStatusScreenError("Server error");
            }
        }
        else
        {
            midlet.setStatusScreenError("Server error");
        }

        readyForInput = true;
    }


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

⌨️ 快捷键说明

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