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

📄 recordstoredemo.java

📁 手机的联网应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * RecordStoreDemo.java * * Created on September 18, 2001, 9:05 PM */package cn.com.javachen;import java.util.*;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;/** * * @author  paul * @version  */public class RecordStoreDemo extends javax.microedition.midlet.MIDlet     implements CommandListener {  private Display display;  private Form mainMenuForm;  private Form dataForm;  private Form recordForm;  private Form statusForm;    private TextField recordStoreNameTextField;  private TextField filterCharacterTextField;  private TextField recordDataTextField;  private ChoiceGroup recordStoreChoice;  private Alert errorAlert;  private Alert infoAlert;  private Command cmdOK;  private Command cmdBack;  private Command cmdNext;  private Command cmdPrev;  private Command cmdDelete;  private Command cmdModify;  private Command cmdExit;  private Command cmdHelp;    private ChoiceGroup mainMenu;    private RecordStore currentRecordStore = null;  private RecordEnumeration enumeration;  private RecordFilter filter;  private int currentRecordID;  private byte[] recordData;  private boolean commandAvailable;  private Command currentCommand;  private Displayable currentDisplayable;  private int state;  private static final int ST_MAIN_MENU =                          100;  private static final int ST_GETTING_RECORD_STORE_NAME =          101;  private static final int ST_SELECTING_STORE_NAME =               102;  private static final int ST_SELECTING_STORE_NAMES_FOR_DELETION = 103;  private static final int ST_GETTING_RECORD_DATA =                104;  private static final int ST_BROWSING_STORE =                     105;  private static final int ST_MODIFYING_RECORD =                   106;  private static final int ST_AFTER_DELETION =                     107;  private static final int ST_GETTING_FILTER_CHARACTER =           108;  private static final int ST_DISPLAYING_STATUS_FORM =             109;  private String[] actions = {    "List Stores",       //  0    "Create Store",      //  1    "Select Store",      //  2    "Delete Store(s)",   //  3    "Add Record",        //  4    "Browse Records",    //  5    "Browse (filtered)", //  6    "Version",           //  7    "Last Modified",     //  8    "Get Next recID",    //  9    "Get # Recs",        // 10    "Get Size",          // 11    "Get Available",     // 12    "Close Store"        // 13  };  private static final int LIST_STORES =      0;  private static final int CREATE_STORE =     1;  private static final int SELECT_STORE =     2;  private static final int DELETE_STORE =     3;  private static final int ADD_RECORD =       4;  private static final int BROWSE_RECORDS =   5;  private static final int BROWSE_FILTERED =  6;  private static final int GET_VERSION =      7;  private static final int LAST_MODIFIED =    8;  private static final int GET_NEXT_REC_ID =  9;  private static final int GET_NUM_RECS =    10;  private static final int GET_SIZE =        11;  private static final int GET_AVAIL =       12;  private static final int CLOSE_STORE =     13;  private String[] recordSubCommands = {    "Back",    "Prev",    "Next",    "Delete",    "Modify"  };  private static final int SUB_BACK =   0;  private static final int SUB_PREV =   1;  private static final int SUB_NEXT =   2;  private static final int SUB_DELETE = 3;  private static final int SUB_MODIFY = 4;    private String msgNoRecordStores =    "MIDlet Suite contains no RecordStores";  private String msgNoCurrentRecordStore =    "You must first select a RecordStore";  private String msgRecordStoreException =    "RecordStoreException";  private String msgRecordStoreNotOpenException =    "RecordStoreNotOpenException";  private String[] helpText = {      "This command lists all of the record stores in this " +      "MIDlet suite. The RecordStore that has been designated as " +      "'active' will be indicated by '(*)'",      "This command is used to create a record store. " +      "You will be prompted for the name of the record store. " +      "After the store has been added, it will automatically " +      "be made the 'active' RecordStore and the RecordStore " +      "(if any) that had been the 'active' RecordStore is closed.",      "This command is used to designate the 'active' RecordStore " +      "(i.e. the one that is the target of subsequent commands " +      "that operate on records or request information about the " +      "RecordStore.",      "This command is used to delete the designated RecordStore. " +      "When a RecordStore is deleted, all of the records it " +      "contains are also deleted.",      "This command is used to add a record to the 'active' " +      "RecordStore. You will be presented with a screen " +      "containing a TextField into which you type the contents of " +      "the record. You can create records whose contents are " +      "identical.",      "This command is used to initiate the process of iterating " +      "over all of the records in the record store. For each " +      "record that is presented, you have the option of deleting " +      "or modifying the record or of navigating to the next or " +      "previous record if such a record exists.",      "This command is the same as the 'browse' command but it " +      "uses a filter. You will be presented with a screen " +      "containing a TextField into which you type a letter. Only " +      "those names beginning with that letter will be displayed " +      "during the browse operation.",      "This command displays the version of the 'active' " +      "RecordStore. The version number is incremented by 1 " +      "each time the RecordStore is modified (i.e. a record is " +      "added, modified or deleted).",       "This command displays the date/time the 'active' " +      "RecordStore was modified.",      "This command displays the recordID that will be assigned " +      "to the next record added to the 'active' RecordStore.",      "This command displays the total number of records in the " +      "'active' RecordStore.",      "This command displays the amount of space, in bytes, that " +      "the 'active' RecordStore occupies.",      "This command displays the additional room (in bytes) " +      "available for the 'active' RecordStore to grow. You should " +      "NOT make any decision related to storage based on this number.",      "This command is used to close the 'active' RecordStore."    };  public void startApp() {    display = Display.getDisplay(this);    cmdOK =     new Command("OK",Command.SCREEN,1);    cmdBack =   new Command("Back",Command.SCREEN,1);    cmdNext =   new Command("Next",Command.SCREEN,1);    cmdPrev =   new Command("Prev",Command.SCREEN,1);    cmdDelete = new Command("Delete",Command.SCREEN,1);    cmdModify = new Command("Modify",Command.SCREEN,1);    cmdHelp =   new Command("Help",Command.SCREEN,1);    cmdExit =   new Command("Exit",Command.SCREEN,1);    mainMenuForm = new Form("Menu");    mainMenu = new ChoiceGroup("Action",Choice.EXCLUSIVE,       actions, null);    mainMenuForm.append(mainMenu);    recordForm = new Form("RECORD DATA");    recordForm.addCommand(cmdBack);    recordForm.addCommand(cmdDelete);    recordForm.addCommand(cmdModify);    recordForm.setCommandListener(this);    dataForm = new Form(null);    dataForm.setCommandListener(this);    dataForm.addCommand(cmdOK);    recordStoreNameTextField =       new TextField("Store Name",null,32,TextField.ANY);    recordDataTextField =       new TextField("Record Data",null,80,TextField.ANY);    filterCharacterTextField =       new TextField("Filter Character",null,1,TextField.ANY);    statusForm = new Form(null);    statusForm.setCommandListener(this);    statusForm.addCommand(cmdOK);    mainMenuForm.addCommand(cmdOK);    mainMenuForm.addCommand(cmdHelp);     mainMenuForm.addCommand(cmdExit);     commandAvailable = false;    new CommandThread(this).start();        infoAlert = new Alert("Help");    infoAlert.setType(AlertType.INFO);    infoAlert.setTimeout(Alert.FOREVER);    errorAlert = new Alert("Error");    errorAlert.setType(AlertType.ERROR);    errorAlert.setTimeout(Alert.FOREVER);            mainMenuForm.setCommandListener(this);    display.setCurrent(mainMenuForm);  }  public void pauseApp() {  }  public void destroyApp(boolean unconditional) {  }  public void commandAction(Command cmd, Displayable d) {    if (cmd == cmdExit) {      destroyApp(true);      notifyDestroyed();    }    else if (cmd == cmdHelp) {      int index = mainMenu.getSelectedIndex();      infoAlert.setTitle(mainMenu.getString(index));      infoAlert.setString(helpText[mainMenu.getSelectedIndex()]);      display.setCurrent(infoAlert, mainMenuForm);    }    else {      synchronized (this) {        currentCommand = cmd;        currentDisplayable = d;        commandAvailable = true;        notify();      }    }  }  class CommandThread extends Thread {    MIDlet parent;    public CommandThread(MIDlet parent) {      this.parent = parent;    }    public void run() {      while (true) {        synchronized(parent) {          while(!commandAvailable) {            try {              parent.wait();            }            catch (InterruptedException e) {            }          }        }        commandAvailable = false;              if (currentDisplayable == mainMenuForm) {          processMainMenuItem();        }        else if (currentDisplayable == dataForm) {          processDataForm();        }        else if (currentDisplayable == recordForm) {          processRecordForm();        }        else if (currentDisplayable == statusForm) {          if (state == ST_AFTER_DELETION) {            state = ST_BROWSING_STORE;            displayRecordData();          }          else {            displayMainMenu();          }        }      }    }    private void displayDataForm(int st) {      state = st;      display.setCurrent(dataForm);    }    private void displayStatusForm(int st) {      state = st;      display.setCurrent(statusForm);    }    private void displayMainMenu() {      mainMenu.setSelectedIndex(0,true);      state = ST_MAIN_MENU;      display.setCurrent(mainMenuForm);    }    private void processMainMenuItem() {      int commandIndex = mainMenu.getSelectedIndex();      if (commandRequiresOpenStore(commandIndex))        if (!storeOpen()) {          displayAlert(errorAlert, msgNoCurrentRecordStore);          return;        }      switch (commandIndex) {        case LIST_STORES:          displayRecordStoreList();          break;        case CREATE_STORE:          requestRecordStoreName();          break;        case SELECT_STORE:          requestRecordStoreFromList("SELECT", ChoiceGroup.EXCLUSIVE);          break;        case DELETE_STORE:          requestRecordStoreFromList("DELETE", ChoiceGroup.MULTIPLE);          break;        case ADD_RECORD:          requestRecordData();          break;        case BROWSE_RECORDS:          filter = null;          startIteration();          break;        case BROWSE_FILTERED:          requestFilterCharacter();          break;        case GET_VERSION:          displayVersion();          break;        case LAST_MODIFIED:          displayLastModified();          break;        case GET_NEXT_REC_ID:          displayNextRecID();          break;        case GET_NUM_RECS:          displayRecordCount();          break;        case GET_SIZE:          displaySize();          break;       case GET_AVAIL:          displayAvailable();          break;         case CLOSE_STORE:          closeRecordStore();          break;        default:          break;       }    }    private boolean commandRequiresOpenStore(int index) {      if ((index == ADD_RECORD) ||          (index == BROWSE_RECORDS) ||          (index == BROWSE_FILTERED) ||          (index == GET_VERSION) ||          (index == LAST_MODIFIED) ||          (index == GET_NEXT_REC_ID) ||          (index == GET_NUM_RECS) ||          (index == GET_SIZE) ||          (index == GET_AVAIL) ||          (index == CLOSE_STORE)) {        return true;      }      else {       return false;      }    }    private boolean storeOpen() {      boolean open = true;      try {        getCurrentStoreName();      }      catch (RecordStoreNotOpenException e) {        open = false;      }    return open;    }    private void displayAlert(Alert alert, String message) {      alert.setString(message);      mainMenu.setSelectedIndex(0,true);      state = ST_MAIN_MENU;      display.setCurrent(alert,mainMenuForm);    }    private void displayNextRecord() {      try {        currentRecordID = enumeration.nextRecordId();        recordData = currentRecordStore.getRecord(currentRecordID);        displayRecordData();      }      catch (InvalidRecordIDException e) {        displayAlert(errorAlert,"Invalid RecordID");      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,"RecordStore not open");      }      catch (RecordStoreException e) {        displayAlert(errorAlert,"RecordStoreException");      }    }    private void displayPreviousRecord() {      try {        currentRecordID = enumeration.previousRecordId();        recordData = currentRecordStore.getRecord(currentRecordID);        displayRecordData();      }      catch (InvalidRecordIDException e) {        displayAlert(errorAlert,"Invalid RecordID");      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,"RecordStore not open");      }      catch (RecordStoreException e) {        displayAlert(errorAlert,"RecordStoreException");      }    }    private void displayRecordData() {      clearForm(recordForm);      recordForm.removeCommand(cmdPrev);      recordForm.removeCommand(cmdNext);      if (enumeration.hasNextElement()) {        recordForm.addCommand(cmdNext);      }      if (enumeration.hasPreviousElement()) {        recordForm.addCommand(cmdPrev);      }      recordForm.setTitle("Record #: " +         Integer.toString(currentRecordID));      recordForm.append(new StringItem(null,new String(recordData)));      state = ST_BROWSING_STORE;      display.setCurrent(recordForm);   }    private void processRecordForm() {      int subCommandIndex = 0;      String label = currentCommand.getLabel();      for (int i = 0; i < recordSubCommands.length; ++i) {        if (recordSubCommands[i].equals(label)) {          subCommandIndex = i;          break;        }      }      switch (subCommandIndex) {        case SUB_BACK:          displayMainMenu();          break;

⌨️ 快捷键说明

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