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

📄 stockmidlet.java

📁 j2me Demos for example
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * <p>This class is the Listener for ALL of the events that     * take place during     * life span of the <code>MIDlet</code>.  It handles <code>Command</code>     * events and list selections which are the only events that this     * <code>MIDlet</code> will generate.  In order to determine what to do,     * the Listener checks the name of the command and the currently displayed     * menu/list and matches them up to execute the appropriate action.</p>     * <BR>     * NOTE: The parseCommandString(String) is only there because the getXXX     *       methods are missing from the Command class.  When they are added,     *       this method can be removed and the Command.getLabel() can be used     */    private class StockCommandListener implements CommandListener, Runnable {        /** Current command to process. */        private Command currentCommand;        /** Displayable of the current command to process. */        private Displayable currentDisplayable;        /** The current command processing thread. */        private Thread commandThread;        /**         * <p>The method to determine what action to take</p>         *         * @param c The <code>Command</code> object that has been activated         * @param d The <code>Displayable</code> object that the command was         *          associated with         */        public void commandAction(Command c, Displayable d) {            synchronized (this) {                if (commandThread != null) {                    // process only one command at a time                    return;                }                currentCommand = c;                currentDisplayable = d;                commandThread = new Thread(this);                commandThread.start();            }        }        /**         * Perform the current command set by the method commandAction.         */        public void run() {            String type = currentCommand.getLabel();            // Main command executed, always show Main Menu regardless of            // which screen is showing            if (type.equals("Main")) {                mainMenu();                // Back command executed, check which screen is and move to                // the previous one            } else if (type.equals("Back")) {                // Screens off the Main Menu                if (currentMenu.equals("View") || currentMenu.equals("WhatChoose") ||                        currentMenu.equals("AlertMenu") || currentMenu.equals("Settings")) {                    mainMenu();                    // Screens off the Settings menu                } else if (currentMenu.equals("Add") || currentMenu.equals("Updates") ||                        currentMenu.equals("RemoveStock")) {                    settings(false);                } else if (currentMenu.equals("stockInfo")) {                    chooseStock(false, "View", Choice.IMPLICIT, true);                } else if (currentMenu.equals("WhatIfForm")) {                    chooseStock(false, "WhatChoose", Choice.IMPLICIT, false);                } else if (currentMenu.equals("AlertForm")) {                    chooseStock(false, "AddAlert", Choice.IMPLICIT, false);                } else if (currentMenu.equals("AnswerForm")) {                    whatIfForm(stockSymbol);                    // Screens off the Alerts menu                } else if (currentMenu.equals("RemoveAlert") || currentMenu.equals("AddAlert")) {                    alertMenu(false);                } else if (currentMenu.equals("AddStock")) {                    settings(false);                }                /*                 * OK command executed, perform different actions                 * depending on which screen is showing                 */            } else if (type.equals("Done")) {                if (currentMenu.equals("AddStock")) {                    if ((!stockSymbolBox.getString().trim().equals("")) &&                            (addNewStock(stockSymbolBox.getString().trim()))) {                        settings(true);                    }                } else if (currentMenu.equals("AlertForm")) {                    setAlert(((TextBox)currentDisplayable).getString());                    alertMenu(true);                    // Remove an alert                } else if (currentMenu.equals("RemoveAlert")) {                    boolean[] chosen = new boolean[choose.size()];                    choose.getSelectedFlags(chosen);                    for (int i = 0; i < chosen.length; i++) {                        if (chosen[i]) {                            removeAlert(choose.getString(i));                        }                    }                    alertMenu(true);                    // Remove a Stock                } else if (currentMenu.equals("RemoveStock")) {                    boolean[] chosen = new boolean[choose.size()];                    choose.getSelectedFlags(chosen);                    for (int i = 0; i < chosen.length; i++) {                        if (chosen[i]) {                            deleteStock(choose.getString(i));                        }                    }                    chosen = null;                    settings(true);                    // Set the quote update interval                } else if (currentMenu.equals("Updates")) {                    switch (updatesChoices.getSelectedIndex()) {                    case 0:                        refresh_interval = 30000;                        break;                    case 1:                        refresh_interval = 900000;                        break;                    case 2:                        refresh_interval = 1800000;                        break;                    case 3:                        refresh_interval = 3600000;                        break;                    case 4:                        refresh_interval = 10800000;                        break;                    default:                        break;                    }                    stockRefreshTask.cancel();                    stockRefreshTask = new StockRefreshTask();                    stockRefresh.schedule(stockRefreshTask, 0, refresh_interval);                    settings(true);                }                // Exit command executed            } else if (type.equals("Exit")) {                try {                    destroyApp(true);                } catch (MIDletStateChangeException msce) {                    mainMenu();                }                // Calc command executed            } else if (type.equals("Calc")) {                if (origPurchPriceField.size() == 0) {                    error("You must enter the price you originally " + "purchased the stock at.",                        2000);                } else {                    if (numSharesField.size() == 0) {                        error("You must specify the number of shares" + " to calculate with.", 2000);                    } else {                        calc();                    }                }                /*                 * No command button was pressed but a list selection                 * was made                 */            } else {                List shown = (List)display.getCurrent();                /*                 * if it's a menu not a list of stocks then we'll                 * use a switch to select which action to perform                 */                if (currentMenu.equals("Main") || currentMenu.equals("Settings") ||                        currentMenu.equals("AlertMenu")) {                    switch (shown.getSelectedIndex()) {                    case 0:                        // View Stocks                        if (currentMenu.equals("Main")) {                            chooseStock(true, "View", Choice.IMPLICIT, true);                            // Updates                        } else if (currentMenu.equals("Settings")) {                            updates();                            // Add Alert                        } else {                            chooseStock(true, "AddAlert", Choice.IMPLICIT, false);                        }                        break;                    case 1:                        // What If?                        if (currentMenu.equals("Main")) {                            chooseStock(true, "WhatChoose", Choice.IMPLICIT, false);                            // Add Stock                        } else if (currentMenu.equals("Settings")) {                            addStock();                            // Remove Alert                        } else {                            viewAlerts();                        }                        break;                    case 2:                        // Alerts                        if (currentMenu.equals("Main")) {                            alertMenu(false);                            // Remove Stock                        } else if (currentMenu.equals("Settings")) {                            chooseStock(true, "RemoveStock", Choice.MULTIPLE, false);                        }                        break;                    case 3:                        // Settings                        if (currentMenu.equals("Main")) {                            settings(false);                        }                        break;                    default:                        break;                    }                    /*                     * we've now determined that it is a menu of stocks                     * so we have to either show the stock info (from the                     * View menu), add an alert (if Alert Choose screen is                     * showing) or perform a What If? (if the What If?                     * Choose screen is showing)                     */                } else {                    if (currentMenu.equals("View")) {                        displayStock(choose.getString(choose.getSelectedIndex())                                           .substring(0,                                choose.getString(choose.getSelectedIndex()).indexOf('@') - 1));                    } else if (currentMenu.equals("WhatChoose")) {                        if (choose.getSelectedIndex() >= 0) {                            whatIfForm(choose.getString(choose.getSelectedIndex()));                        }                    } else if (currentMenu.equals("AddAlert")) {                        if (choose.getSelectedIndex() >= 0) {                            alertForm(choose.getString(choose.getSelectedIndex()));                        }                    }                }            }            synchronized (this) {                // signal that another command can be processed                commandThread = null;            }        }    }    /**     * <p>This is an extension of the <code>TimerTask</code> class which runs     * when called by <code>Timer</code>.  It refreshes the stock info for     * each stock from the quote server and checks to see if any of the alerts     * should be fired.</p>     *     * @see java.util.TimerTask     */    private class StockRefreshTask extends TimerTask {        /**         * <p>Execute the Timer's Task</p>         */        public void run() {            try {                // Just return if the database is empty                if (stocks.getNumRecords() == 0) {                    return;                }                // Get all the records                RecordEnumeration re = stocks.enumerateRecords();                while (re.hasNextElement()) {                    String tkrSymbol = Stock.getName(new String(re.nextRecord()));                    try {                        byte[] rec = getStockQuote(tkrSymbol).getBytes();                        // Update the record and check for any alerts that                        // may need to be executed                        stocks.update(tkrSymbol, rec);                        checkAlerts(tkrSymbol);                    } catch (NumberFormatException nfe) {                        error("\"" + tkrSymbol + "\" not found on server, or invalid data " +                            "received from server", 2000);                    }                }            } catch (Exception e) {                error("Update Failed\n\nStocks were not updated", 2000);            }        }    }}

⌨️ 快捷键说明

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