📄 stockmidlet.java
字号:
/* * @(#)StockMIDlet.java 1.37 02/10/08 @(#) * * Copyright (c) 1999-2001 Sun Microsystems, Inc. All rights reserved. * PROPRIETARY/CONFIDENTIAL * Use is subject to license terms. */package example.stock;import javax.microedition.rms.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.io.*;import java.io.*;import java.util.Timer;import java.util.TimerTask;/** * <p>The MIDlet application class that we'll run for the Stock Demo.</p> * * @author Jeffrey Bacon */public class StockMIDlet extends MIDlet { /** * Since there is no support in MIDP/CLDC for floating point numbers, * and all of the stock data comes in the form ##.### (usually) then we * have to find a way to store the decimals as well as the integer part * of the quote data. Multiplying by the OFFSET will shift the decimal * place far enough over to make the number an integer which we can * store. Every time we retrieve quote data from our * <code>RecordStore</code> we * must make sure that we divide by the OFFSET to correctly display and * use the value we actually want. */ private static int OFFSET = 10000; // each 0 is one decimal place /** * The <code>MIDlet</code>'s display object */ Display display = null; /** * The <code>Ticker</code> that scrolls along the top of the screen */ private Ticker stockTicker = null; /** * A <code>List</code> of stocks */ private List choose = null; /** * The Stock Tracker menu */ private List view = null; /** * The Main menu */ private List menu = null; /** * The Alert menu */ private List alertList = null; /** * The Settings menu */ private List settingsList = null; /** * The 'What If?' <code>Form</code> upon which we enter our query data */ private Form whatif = null; /** * The <code>Form</code> to display the different update intervals settings */ private Form updatesForm = null; /** * Used to input a stock symbol */ private TextBox stockSymbolBox = null; /** * Used to enter the price at which the user wishes to be alerted to the * stocks's value */ private TextBox alertPriceBox = null; /** * The original price the user purchased the stock at, used on the What If? * <code>Form</code> */ private TextField origPurchPriceField = null; /** * The number of shares the users wishes to sell, used on the What If? * <code>Form</code> */ private TextField numSharesField = null; /** * Back <code>Command</code> */ private static final Command BACK_COMMAND = new Command("Back", Command.BACK, 0); /** * Main Menu <code>Command</code> */ private static final Command MAIN_MENU_COMMAND = new Command("Main", Command.SCREEN, 1); /** * Done <code>Command</code> */ private static final Command DONE_COMMAND = new Command("Done", Command.OK, 2); /** * Set <code>Command</code> */ private static final Command SET_COMMAND = new Command("Set", Command.OK, 4); /** * Exit <code>Command</code> */ private static final Command EXIT_COMMAND = new Command("Exit", Command.STOP, 5); /** * Calc <code>Command</code> */ private static final Command CALC_COMMAND = new Command("Calc", Command.OK, 6); /** * About <code>Command</code> */ private static final Command ABOUT_COMMAND = new Command("About", Command.HELP, 1); /** * The radio buttons for the update interval time */ private ChoiceGroup updatesChoices = null; /** * A textual reference to the current menu that is displayed * onscreen to * allow the <code>StockCommandListener</code> to decide what * action to perform upon * execution of a command */ private String currentMenu = null; /** * A reference to the stock that has been chosen from a list of stocks * onscreen. Using this we can extract the correct stock's data from * the <code>StockDatabase</code> */ private String stockSymbol = null; /** * The reference to the <code>StockDatabase</code> that stores * the stock data */ private StockDatabase stocks = null; /** * The reference to the <code>AlertDatabase</code> that stores the alerts */ private AlertDatabase alerts = null; /** * The server from which the quotes are downloaded * * NOTE: Currently, only the quote.yahoo.com server is supported */ private String quoteServerURL = "http://quote.yahoo.com/d/quotes.csv?s="; /** * The format parameter for the quote server to retrieve stock data * * NOTE: Currently, only this format is supported */ private String quoteFormat = "&f=slc1wop"; /** * The proxy server that must be negotiated * * NOTE: Proxy server is optional and a blank string indicates that * no proxy should be used */ // Initial default but read from settings file on subsequent runs private String proxyURL = null; /** * The <code>Timer</code> object that refreshes the stock * quotes periodically */ private Timer stockRefresh = null; /** * The <code>TimerTask</code> that the <code>Timer</code> * performs periodically. * It refreshes the stock quotes */ private StockRefreshTask stockRefreshTask = null; /** * How often are the stocks' data updated off of the server? */ // Initial default but read from settings file on subsequent runs private int refresh_interval = 900000; // 1000 = 1 second /** * <p>Default constructor that is called by the application * manager to create a new instance of this <code>MIDlet</code> after * which the <code>MIDlet</code> enters the <code>Paused</code> state.</p> */ public StockMIDlet() {} /** * <p>This method is invoked when the <code>MIDlet</code> * is ready to run and * starts/resumes execution after being in the <code>Paused</code> * state. The * <code>MIDlet</code> acquires any resources it needs, * enters the * <code>Active</code> state and begins to perform its service, * which in this * case means it displays the main menu on the screen.</p> * * <p>The method proceeds like so:</p> * <li> open the <code>StockDatabase</code> and the * <code>AlertDatabase</code></li> * <li> read the settings data from the settings * <code>RecordStore</code></li> * <li> create the string to be scrolled across the <code>Ticker</code> on * the top of the screens and instantiate the Ticker object using that * string. That string will be constructed of the names and prices of * the stocks in our database</li> * <li> get and store the <code>MIDlet</code>'s * <code>Display</code> object</li> * <li> create and show the main menu</li> * <li> instantiate the <code>TimerTask</code> and <code>Timer</code> and * associate the two setting the refresh interval to the value of * the refresh_interval variable</li> * * @throws <code>MIDletStateChangeException</code> is thrown if the * <code>MIDlet</code> cannot start now but might be able to * start at a later time. * @see javax.microedition.midlet.MIDlet */ public void startApp() throws MIDletStateChangeException { synchronized (this) { display = Display.getDisplay(this); // Open the Stocks file stocks = new StockDatabase(); try { stocks.open("Stocks"); } catch (Exception e) { try { stocks.cleanUp("Stocks"); } catch (Exception e2) {} } // Open the Alerts file alerts = new AlertDatabase(); try { alerts.open("Alerts"); } catch (Exception e) { try { alerts.cleanUp("Alerts"); } catch (Exception e2) {} } // Open the Settings file try { RecordStore settings = RecordStore.openRecordStore("Settings", true); refresh_interval = Integer.valueOf(new String(settings.getRecord(1))) .intValue(); settings.closeRecordStore(); // No settings file existed } catch (Exception e) { refresh_interval = 900000; } // Make the ticker stockTicker = new Ticker(makeTickerString()); // Create all the menus and forms origPurchPriceField = new TextField("Original Purchase Price:", "", 5, TextField.NUMERIC); numSharesField = new TextField("Number Of Shares:", "", 9, TextField.NUMERIC); menu = new List("Stock Menu", Choice.IMPLICIT); menu.append("Stock Tracker", null); menu.append("What If?", null); menu.append("Alerts", null); menu.append("Settings", null); menu.addCommand(EXIT_COMMAND); menu.addCommand(ABOUT_COMMAND); menu.setCommandListener(new StockCommandListener()); menu.setTicker(stockTicker); whatif = new Form("What If?"); whatif.setTicker(stockTicker); whatif.append(origPurchPriceField); whatif.append(numSharesField); whatif.addCommand(BACK_COMMAND); whatif.addCommand(CALC_COMMAND); whatif.setCommandListener(new StockCommandListener()); alertList = new List("Alert Menu", Choice.IMPLICIT); alertList.setTicker(stockTicker); alertList.append("Add", null); alertList.append("Remove", null); alertList.addCommand(BACK_COMMAND); alertList.setCommandListener(new StockCommandListener()); settingsList = new List("Settings", Choice.IMPLICIT); settingsList.setTicker(stockTicker); settingsList.append("Updates", null); settingsList.append("Add Stock", null); settingsList.append("Remove Stock", null); settingsList.addCommand(BACK_COMMAND); settingsList.setCommandListener(new StockCommandListener());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -