Beispiel #1
1
  public void startApp() {
    // Create Record Store
    try {
      rs = RecordStore.openRecordStore("myrs", true);
    } catch (RecordStoreException e) {
    }

    dsp = Display.getDisplay(this);
    listMenu.setCommandListener(this);
    listMenu.addCommand(cmdExit);
    dsp.setCurrent(listMenu);
  }
Beispiel #2
1
  public void commandAction(Command c, Displayable d) {

    if (c == List.SELECT_COMMAND) {
      List down = (List) dsp.getCurrent();
      String title = down.getTitle();

      if (title.equals("Menu:")) {
        switch (down.getSelectedIndex()) {
          case 0:
            {
              System.out.println("Stisknul jsi " + down.getString(down.getSelectedIndex()));
              addRecordForm();
              break;
            }
          case 1:
            {
              System.out.println("Stisknul jsi " + down.getString(down.getSelectedIndex()));
              showAllRecordForm();
              break;
            }
          case 2:
            {
              System.out.println("Stisknul jsi " + down.getString(down.getSelectedIndex()));
              showFirstRecord();
              break;
            }
          case 3:
            {
              System.out.println("Stisknul jsi " + down.getString(down.getSelectedIndex()));
              getNumberRecord();
              break;
            }
          case 4:
            {
              System.out.println("Stisknul jsi " + down.getString(down.getSelectedIndex()));
              editRecord(1);
              break;
            }
        }
      }
    }

    if (c == cmdExit) {
      // rs.closeRecordStore();
      notifyDestroyed();
    }
    if (c == cmdMenu) {
      dsp.setCurrent(listMenu);
    }
    if (c == cmdEdit) {
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      // filtr; filtr dostane pri vytvoreni stream jako parametr;
      // format dat nezavisly na platforme
      // definuje metodu write() pro vsechny primitivni typy
      DataOutputStream dout = new DataOutputStream(buffer);
      // byte [] data = txf.getString().getBytes();
      try {
        dout.writeUTF(txfName.getString());
        dout.writeInt(Integer.valueOf(txfNumber.getString()).intValue());
        txfName.delete(0, txfName.size());
        txfNumber.delete(0, txfNumber.size());
        // zapise buffer do streamu
        dout.flush();
        // prevod ByteArrayOutputStream na pole bytu;
        byte[] b = buffer.toByteArray();
        rs.setRecord(1, b, 0, b.length);
      } catch (Exception e) {
      } finally {
        try {
          dout.close();
        } catch (Exception e2) {
        }
      }
    }
    if (c == cmdAdd) {
      // vystupni stream; zapisuje do buferu v pamžti
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      // filtr; filtr dostane pri vytvoreni stream jako parametr;
      // format dat nezavisly na platforme
      // definuje metodu write() pro vsechny primitivni typy
      DataOutputStream dout = new DataOutputStream(buffer);
      // byte [] data = txf.getString().getBytes();
      try {
        dout.writeUTF(txfName.getString());
        dout.writeInt(Integer.valueOf(txfNumber.getString()).intValue());
        txfName.delete(0, txfName.size());
        txfNumber.delete(0, txfNumber.size());
        // zapise buffer do streamu
        dout.flush();
        // prevod ByteArrayOutputStream na pole bytu;
        byte[] b = buffer.toByteArray();
        rs.addRecord(b, 0, b.length);
      } catch (Exception e) {
      } finally {
        try {
          dout.close();
        } catch (Exception e2) {
        }
      }
    }
  }
Beispiel #3
0
  public void readRS() {
    RecordStore rs = null;
    try {
      rs = RecordStore.openRecordStore("score", true);
      byte[] byteA = new byte[100];

      list.removeAllElements();
      // System.out.println("size of list before reading out of rs: "+ list.size());
      for (int i = 1; i < 6; i++) {
        byteA = rs.getRecord(i);
        // System.out.println((char) byteA[0]);
        // System.out.println("1");
        ByteArrayInputStream bytes = new ByteArrayInputStream(byteA);
        // System.out.println("2");
        DataInputStream dataIn = new DataInputStream(bytes);
        // System.out.println("3");
        try {
          list.addElement(
              new ScoreItem(
                  dataIn.readUTF(), dataIn.readInt(), dataIn.readInt(), dataIn.readLong()));
        } catch (IOException exp) {
        }
      }
      // System.out.println("size of list after reading out of rs: "+ list.size());
      rs.closeRecordStore();
    } catch (RecordStoreException exp) {
      System.out.println("fehler: " + exp.toString());
      exp.printStackTrace();
    }
  }
Beispiel #4
0
 private void try2OpenRS() {
   try {
     RecordStore rs = RecordStore.openRecordStore("score", false);
     rs.closeRecordStore();
   } catch (RecordStoreNotFoundException exp) {
     // System.out.println("rs was empty");
     initFirstTimeRS();
   } catch (RecordStoreException exo) {
   }
 }
Beispiel #5
0
  public void showAllRecordForm() {
    fmAllRecords = new Form("All records");
    try {
      RecordEnumeration recEnum = rs.enumerateRecords(null, null, false);
      while (recEnum.hasNextElement()) {
        try {
          // System.out.println(new String(recEnum.nextRecord()));
          String name = "Nenacetl jsem";
          int number = 0;
          try {
            byte[] record = recEnum.nextRecord();
            ByteArrayInputStream buffer = new ByteArrayInputStream(record);
            DataInputStream dis = new DataInputStream(buffer);
            name = dis.readUTF();
            number = dis.readInt();
            dis.close();
          } catch (Exception e) {
          }

          fmAllRecords.append(name + " " + String.valueOf(number) + "\n");
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }

        //
      }
    } catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
    fmAllRecords.addCommand(cmdMenu);
    fmAllRecords.setCommandListener(this);
    dsp.setCurrent(fmAllRecords);
  }
Beispiel #6
0
 private void initFirstTimeRS() {
   // System.out.println("test");
   list.removeAllElements();
   // System.out.println("size of list before first initiation: "+ list.size());
   list.addElement(new ScoreItem("Saban", 5000, 412, System.currentTimeMillis()));
   list.addElement(new ScoreItem("Osman", 2500, 380, System.currentTimeMillis()));
   list.addElement(new ScoreItem("Aslan", 500, 460, System.currentTimeMillis()));
   list.addElement(new ScoreItem("Moruk", 250, 120, System.currentTimeMillis()));
   list.addElement(new ScoreItem("Yabani", 100, 240, System.currentTimeMillis()));
   // System.out.println("size of list before after initiation: "+ list.size());
   try {
     RecordStore rs = null;
     rs = RecordStore.openRecordStore("score", true);
     rs.closeRecordStore();
     save2RS();
   } catch (RecordStoreException exp) {
   }
 }
Beispiel #7
0
 public void getNumberRecord() {
   fmNumbers = new Form("Number of Records");
   int number = 0;
   try {
     number = rs.getNumRecords();
   } catch (Exception e) {
   }
   fmNumbers.append("Numbers of Records: " + number);
   dsp.setCurrent(fmNumbers);
   fmNumbers.setCommandListener(this);
   fmNumbers.addCommand(cmdMenu);
 }
Beispiel #8
0
  private void save2RS() {
    RecordStore rs = null;
    try {
      RecordStore.deleteRecordStore("score");
      rs = RecordStore.openRecordStore("score", true);

      try {
        for (int i = 0; i < 5; i++) {
          ByteArrayOutputStream bytes;
          DataOutputStream dataOut = new DataOutputStream(bytes = new ByteArrayOutputStream());
          ScoreItem item = (ScoreItem) list.elementAt(i);
          dataOut.writeUTF(item.name);
          dataOut.writeInt(item.points);
          dataOut.writeInt(item.level);
          dataOut.writeLong(item.date);
          byte[] byteA = bytes.toByteArray();
          rs.addRecord(byteA, 0, byteA.length);
        }
      } catch (IOException exp) {
        RecordStore.deleteRecordStore("score");
        System.out.println("XXXXXXXXXXXXXXXXXXXXXX");
      }
      // System.out.println("size of rs after saving: "+ rs.getNumRecords());
      rs.closeRecordStore();
    } catch (RecordStoreException exp) {
      System.out.println("fehler: " + exp.toString());
      exp.printStackTrace();
    }
  }
Beispiel #9
0
  /** Get the settings the Manager saved for the user. */
  private void restoreSettings() {
    ByteArrayInputStream bas;
    DataInputStream dis;
    byte[] data;
    RecordStore settings = null;

    try {
      settings = RecordStore.openRecordStore(GraphicalInstaller.SETTINGS_STORE, false);

      data = settings.getRecord(1);
      if (data != null) {
        bas = new ByteArrayInputStream(data);
        dis = new DataInputStream(bas);
        defaultInstallListUrl = dis.readUTF();
      }

    } catch (RecordStoreException e) {
      if (Logging.REPORT_LEVEL <= Logging.WARNING) {
        Logging.report(
            Logging.WARNING, LogChannels.LC_AMS, "restoreSettings threw a RecordStoreException");
      }
    } catch (IOException e) {
      if (Logging.REPORT_LEVEL <= Logging.WARNING) {
        Logging.report(Logging.WARNING, LogChannels.LC_AMS, "restoreSettings threw an IOException");
      }
    } finally {
      if (settings != null) {
        try {
          settings.closeRecordStore();
        } catch (RecordStoreException e) {
          if (Logging.REPORT_LEVEL <= Logging.WARNING) {
            Logging.report(
                Logging.WARNING,
                LogChannels.LC_AMS,
                "closeRecordStore threw a RecordStoreException");
          }
        }
      }
    }
  }
Beispiel #10
0
  public void showFirstRecord() {
    fmFirstRecord = new Form("First Record");
    String name = "Nenacetl jsem";
    int number = 0;
    try {
      byte[] record = rs.getRecord(1);
      ByteArrayInputStream buffer = new ByteArrayInputStream(record);
      DataInputStream dis = new DataInputStream(buffer);
      name = dis.readUTF();
      number = dis.readInt();
      dis.close();
    } catch (Exception e) {
    }

    fmFirstRecord.append(name + " " + String.valueOf(number));
    // fmFirstRecord.append(String.valueOf(number));
    fmFirstRecord.setCommandListener(this);
    fmFirstRecord.addCommand(cmdMenu);
    dsp.setCurrent(fmFirstRecord);
  }
Beispiel #11
0
 public void editRecord(int record_num) {
   fmEditRecord = new Form("Edit Record");
   String name = "Nenacetl jsem";
   int number = 0;
   try {
     byte[] record = rs.getRecord(record_num);
     ByteArrayInputStream buffer = new ByteArrayInputStream(record);
     DataInputStream dis = new DataInputStream(buffer);
     name = dis.readUTF();
     number = dis.readInt();
     dis.close();
   } catch (Exception e) {
   }
   txfName = new TextField("Name", name, 10, TextField.ANY);
   txfNumber = new TextField("Number", String.valueOf(number), 10, TextField.NUMERIC);
   fmEditRecord.append(txfName);
   fmEditRecord.append(txfNumber);
   fmEditRecord.addCommand(cmdEdit);
   fmEditRecord.addCommand(cmdMenu);
   fmEditRecord.setCommandListener(this);
   dsp.setCurrent(fmEditRecord);
 }