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

📄 recordstoredemo.java

📁 手机的联网应用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        case SUB_PREV:          displayPreviousRecord();          break;        case SUB_NEXT:          displayNextRecord();          break;        case SUB_DELETE:          deleteRecord();          break;        case SUB_MODIFY:          modifyRecord();          break;      }    }    private void modifyRecord() {      clearForm(dataForm);      dataForm.setTitle("Modify Record");      recordDataTextField.setString(new String(recordData));      dataForm.append(recordDataTextField);      state = ST_MODIFYING_RECORD;      display.setCurrent(dataForm);    }    private void deleteRecord() {      int newRecordID = -1;      try {        if (enumeration.hasNextElement()) {          newRecordID = enumeration.nextRecordId();        }        else if (enumeration.hasPreviousElement()) {          newRecordID = enumeration.previousRecordId();        }      }      catch (RecordStoreException e) {        displayAlert(errorAlert, "Error while deleting record");      }      try {        currentRecordStore.deleteRecord(currentRecordID);      }      catch (RecordStoreNotOpenException e) {      }      catch (InvalidRecordIDException e) {      }      catch (RecordStoreException e) {      }      enumeration.rebuild();      int nrecs = enumeration.numRecords();      if (nrecs == 0) {        displayAlert(infoAlert,"No records in RecordStore");        return;      }      clearForm(statusForm);      statusForm.setTitle("Record Deleted");      statusForm.append(new StringItem(null,"Record # " +         Integer.toString(currentRecordID) + " deleted.\n" +        "RecordStore now contains " + Integer.toString(nrecs) +        Integer.toString(nrecs) + " records"));      while (enumeration.hasNextElement()) {          try {            if (enumeration.nextRecordId() == newRecordID) {              currentRecordID = newRecordID;              break;            }          }          catch (InvalidRecordIDException e) {          }      }      try {        recordData = currentRecordStore.getRecord(currentRecordID);      }      catch (RecordStoreNotOpenException e) {      }      catch (InvalidRecordIDException e) {      }      catch (RecordStoreException e) {      }      displayStatusForm(ST_AFTER_DELETION);    }    private void startIteration() {      try {        AlphaComparator ac = new AlphaComparator();        enumeration = currentRecordStore.enumerateRecords(filter,ac,false);        if (enumeration.numRecords() == 0) {          displayAlert(infoAlert,"Store contains no records!");          return;        }        displayNextRecord();      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }    }    private void requestRecordData() {      clearForm(dataForm);      dataForm.setTitle("Record Contents");      recordDataTextField.setString("");      dataForm.append(recordDataTextField);      displayDataForm(ST_GETTING_RECORD_DATA);    }    private void requestRecordStoreName() {      clearForm(dataForm);      dataForm.setTitle("STORE NAME");      recordStoreNameTextField.setString("");      dataForm.append(recordStoreNameTextField);      displayDataForm(ST_GETTING_RECORD_STORE_NAME);    }    private void requestFilterCharacter() {      clearForm(dataForm);      dataForm.setTitle("Filter Character");      filterCharacterTextField.setString("");      dataForm.append(filterCharacterTextField);      displayDataForm(ST_GETTING_FILTER_CHARACTER);    }    private void requestRecordStoreFromList(String title,          int choiceType) {      String[] storeNames = RecordStore.listRecordStores();      if (storeNames == null) {        displayAlert(errorAlert,msgNoRecordStores);        return;      }      else {        dataForm.setTitle(title);        clearForm(dataForm);        recordStoreChoice = new ChoiceGroup("Stores",choiceType,          storeNames,null);        dataForm.append(recordStoreChoice);        int state;        if (title.equals("SELECT")) {          state = ST_SELECTING_STORE_NAME;        }        else {          state = ST_SELECTING_STORE_NAMES_FOR_DELETION;        }        displayDataForm(state);      }    }    private void clearForm(Form f) {      int sz = f.size();      while (sz > 0) {        f.delete(--sz);      }    }    private void closeRecordStore() {      if (currentRecordStore == null) {        displayAlert(errorAlert,msgNoCurrentRecordStore);        return;      }      try {        currentRecordStore.closeRecordStore();        currentRecordStore = null;        displayAlert(infoAlert,"RecordStore closed");      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);      }      catch (RecordStoreException e) {        displayAlert(errorAlert,msgRecordStoreException);      }    }    private String getCurrentStoreName()        throws RecordStoreNotOpenException {      if (currentRecordStore == null) {        throw new RecordStoreNotOpenException();      }      else {        return currentRecordStore.getName();      }    }    private void displayRecordCount() {      clearForm(statusForm);      try {        statusForm.setTitle(getCurrentStoreName());        int nrecs = currentRecordStore.getNumRecords();        statusForm.append(new StringItem(null,"# recs: " +          Integer.toString(nrecs)));      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displayAvailable() {      clearForm(statusForm);        try {        statusForm.setTitle(getCurrentStoreName());        int bytes = currentRecordStore.getSizeAvailable();        statusForm.append(new StringItem(null,"Bytes Available: " +          Integer.toString(bytes)));      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displaySize() {      clearForm(statusForm);      try {        statusForm.setTitle(getCurrentStoreName());        int bytes = currentRecordStore.getSize();        statusForm.append(new StringItem(null,"Bytes occupied: " +          Integer.toString(bytes)));      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displayNextRecID() {      clearForm(statusForm);      try {        statusForm.setTitle(getCurrentStoreName());        int next = currentRecordStore.getNextRecordID();        statusForm.append(new StringItem(null, "Next RecID: " +           Integer.toString(next)));       }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      catch (RecordStoreException e) {        displayAlert(errorAlert,msgRecordStoreException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displayLastModified() {      clearForm(statusForm);      try {        statusForm.setTitle(getCurrentStoreName());        long lastModified = currentRecordStore.getLastModified();        Calendar cal = Calendar.getInstance();        cal.setTime(new Date(lastModified));        statusForm.append(new StringItem(null, "Last modified: " +           cal.toString()));       }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displayVersion() {      clearForm(statusForm);      try {        statusForm.setTitle(getCurrentStoreName());        int ver = currentRecordStore.getVersion();        statusForm.append(new StringItem(null,          "Version: " + Integer.toString(ver)));      }      catch (RecordStoreNotOpenException e) {        displayAlert(errorAlert,msgRecordStoreNotOpenException);        return;      }      displayStatusForm(ST_DISPLAYING_STATUS_FORM);    }    private void displayRecordStoreList() {      String[] stores = RecordStore.listRecordStores();      if (stores == null) {        displayAlert(infoAlert,msgNoRecordStores);        return;      }      clearForm(statusForm);      statusForm.setTitle("Store List");      String csn = "";      try {        if (currentRecordStore != null) {          csn = currentRecordStore.getName();        }      }      catch (RecordStoreNotOpenException e) {      }      for (int i = 0; i < stores.length; ++i) {        if (!stores[i].equals(csn)) {        statusForm.append(new StringItem(null,stores[i]));        }        else {        statusForm.append(new StringItem(null,stores[i] + " (*)"));        }        statusForm.append(new StringItem(null,"\n"));      }    displayStatusForm(ST_DISPLAYING_STATUS_FORM);  }    private void createRecordStore() {      RecordStore rs = null;      String name = recordStoreNameTextField.getString();      if ("".equals(name)) {        displayAlert(errorAlert,"Name cannot be blank");        return;      }      String[] stores = RecordStore.listRecordStores();      if (stores != null) {        for (int i = 0; i < stores.length; ++i) {          if (stores[i].equals(name)) {            displayAlert(errorAlert, name + " already exists");            return;          }        }      }      try {        rs = RecordStore.openRecordStore(name,true);        if (currentRecordStore != null) {          currentRecordStore.closeRecordStore();        }      }      catch (RecordStoreException e) {        displayAlert(errorAlert,msgRecordStoreException);        return;      }      currentRecordStore = rs;      displayAlert(infoAlert, name + " is now the active RecordStore");    }    private void processDataForm() {      RecordStore rs = null;      String sn = null;      switch (state) {        case ST_GETTING_RECORD_STORE_NAME:          createRecordStore();          break;        case ST_SELECTING_STORE_NAME:          int i = recordStoreChoice.getSelectedIndex();          sn = recordStoreChoice.getString(i);          try {            rs = RecordStore.openRecordStore(sn,false);            if (currentRecordStore != null) {              try {                currentRecordStore.closeRecordStore();              }              catch (RecordStoreException e) {              }            }            currentRecordStore = rs;          }          catch (RecordStoreException e) {            displayAlert(errorAlert, msgRecordStoreException);          }          try {            sn = currentRecordStore.getName();          }          catch (RecordStoreNotOpenException e) {            displayAlert(errorAlert,msgRecordStoreNotOpenException);          }          displayAlert(infoAlert,sn +             " is now the active RecordStore");          break;        case ST_SELECTING_STORE_NAMES_FOR_DELETION:          int ndel = 0;          for (int ix = 0; ix < recordStoreChoice.size(); ++ix) {            if (!recordStoreChoice.isSelected(ix)) {              continue;            }            sn = recordStoreChoice.getString(ix);            if (currentRecordStore != null) {              try {                if (currentRecordStore.getName().equals(sn)) {                  currentRecordStore.closeRecordStore();                }              }              catch (RecordStoreException e) {              }              finally {                currentRecordStore = null;              }            }            try {              rs.closeRecordStore();            }            catch (RecordStoreNotOpenException e) {            }            catch (RecordStoreException e) {            }            try {              RecordStore.deleteRecordStore(sn);               ++ndel;            }            catch (RecordStoreException e) {            }          }          displayAlert(infoAlert, Integer.toString(ndel) +             " RecordStores deleted");          break;        case ST_GETTING_FILTER_CHARACTER:          if (currentRecordStore == null) {            displayAlert(errorAlert,msgNoCurrentRecordStore);          }          String filterCharacter =             filterCharacterTextField.getString();          filter = new FirstCharacterFilter(filterCharacter);          startIteration();          break;        case ST_GETTING_RECORD_DATA:          if (currentRecordStore == null) {            displayAlert(errorAlert,msgNoCurrentRecordStore);          }          recordData = recordDataTextField.getString().getBytes();          try {            currentRecordStore.addRecord(recordData,0,              recordData.length);            displayAlert(infoAlert,"Record added");          }          catch (RecordStoreNotOpenException e) {            displayAlert(errorAlert,msgRecordStoreNotOpenException);          }          catch (RecordStoreException e) {            displayAlert(errorAlert,msgRecordStoreException);          }          break;        case ST_MODIFYING_RECORD:          recordData = recordDataTextField.getString().getBytes();;          try {            currentRecordStore.setRecord(currentRecordID, recordData,               0, recordData.length);          }          catch (RecordStoreNotOpenException e) {            displayAlert(errorAlert,"RecordStoreNotOpenException");          }          catch (InvalidRecordIDException e) {            displayAlert(errorAlert,"InvalidRecordIDException");          }          catch (RecordStoreFullException e) {            displayAlert(errorAlert,"RecordStoreFullException");          }          catch (RecordStoreException e) {            displayAlert(errorAlert,"RecordStoreException");          }          displayRecordData();        }      }    }  class AlphaComparator implements RecordComparator {    public AlphaComparator() {    }    public int compare(byte[] rec1, byte[] rec2) {      String s1 = new String(rec1);      String s2 = new String(rec2);      int cr = s1.compareTo(s2);      if (cr < 0) {        return RecordComparator.PRECEDES;      }      else if (cr > 0) {        return RecordComparator.FOLLOWS;      }      else {        return RecordComparator.EQUIVALENT;      }    }  }  class FirstCharacterFilter implements RecordFilter {    String startingCharacter;    public FirstCharacterFilter(String startingCharacter)  {      this.startingCharacter = startingCharacter.toLowerCase();    }    public boolean matches (byte[] candidate) {      return new String(candidate).toLowerCase()      .startsWith(startingCharacter);    }  }}

⌨️ 快捷键说明

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