dbscreen.java

来自「J2ME MIDP_Example_Applications」· Java 代码 · 共 214 行

JAVA
214
字号
// Copyright 2003 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
package example.midlet;

import javax.microedition.lcdui.*;

// Main screen showing the messages board
class DBScreen
   extends Form
   implements CommandListener
{
  private final static int INSERT = 0;
  private final static int DELETE = 1;
  private final static int READ = 2;
  private final static int NONE = 3;

  private final DBMIDlet parent;
  private final Command exitCommand =
    new Command("Exit", Command.EXIT, 1);
  private final Command readCommand =
    new Command("Read Records", Command.SCREEN, 1);
  private final Command insertCommand =
    new Command("Insert new Record", Command.SCREEN, 2);
  private final Command deleteCommand =
    new Command("Delete Current Record", Command.SCREEN, 2);
  private final Command nextCommand =
    new Command("Next", Command.SCREEN, 2);
  private final Command previousCommand =
    new Command("Previous", Command.SCREEN, 2);
  private final Command commitCommand =
    new Command("Commit", Command.SCREEN, 2);
  private final Command cancelCommand =
    new Command("Cancel", Command.SCREEN, 2);
  private final TextField firstNameField, familyNameField;
  private final TextField phoneNumberField, eMailField, ageField;
  
  private int state = NONE;


  public DBScreen(DBMIDlet parent)
  {
    super("Accounts");
    this.parent = parent;

    // set up this Displayable to listen to command events and add commands
    setCommandListener(this);
    addCommand(exitCommand);
    addCommand(readCommand);
    addCommand(insertCommand);

    firstNameField = new TextField("First name",
                                   "", 56, TextField.ANY);
    familyNameField = new TextField("Family name", "",
                                    56, TextField.ANY);
    phoneNumberField = new TextField("Phone number", "",
                                     24, TextField.PHONENUMBER);
    eMailField = new TextField("E-Mail", "", 108,
                               TextField.EMAILADDR);
    ageField = new TextField("Age", "", 3, TextField.NUMERIC);

    append(firstNameField);
    append(familyNameField);
    append(phoneNumberField);
    append(eMailField);
    append(ageField);
  }


  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exitCommand)
    {
      parent.notifyDestroyed();
    }
    else if (command == readCommand)
    {
      parent.readRecords();
    }
    else if (command == nextCommand)
    {
      parent.goNextRecord();
    }
    else if (command == previousCommand)
    {
      parent.goPreviousRecord();
    }
    else if (command == insertCommand)
    {
      firstNameField.setString("");
      familyNameField.setString("");
      phoneNumberField.setString("");
      eMailField.setString("");
      ageField.setString("");
      removeCommand(readCommand);
      removeCommand(insertCommand);
      if (state == READ)
      {
        removeCommand(nextCommand);
        removeCommand(previousCommand);
        removeCommand(deleteCommand);
      }
      addCommand(commitCommand);
      addCommand(cancelCommand);
      state = INSERT;
    }
    else if (command == commitCommand)
    {
      if (firstNameField.size() == 0 ||
        familyNameField.size() == 0 ||
        phoneNumberField.size() == 0 ||
        eMailField.size() == 0 ||
        ageField.size() == 0)
      {
        // display alarm
      }
      else
      {
        UserRecord record = new UserRecord(familyNameField.getString(),
                                           firstNameField.getString(),
                                           phoneNumberField.getString(),
                                           eMailField.getString(),
                                           Integer.parseInt(
                                              ageField.getString()));
        parent.insertNewRecord(record);
      }
    }
    else if (command == deleteCommand)
    {
      parent.deleteCurrentRecord();
    }
  }

  // sets the value of the current record.
  // it may change the commands depending on the
  // current state
  public void setCurrent(UserRecord record, int index, int total)
  {
    StringBuffer title = new StringBuffer("Record ").append(index + 1);
    title.append("/").append(total).toString();
    setTitle(title.toString());
    if (state == INSERT)
    {
      removeCommand(commitCommand);
      removeCommand(cancelCommand);
      addCommand(readCommand);
      addCommand(insertCommand);
    }
    if (state != READ)
    {
      addCommand(nextCommand);
      addCommand(previousCommand);
      addCommand(deleteCommand);
      state = READ;
    }
    firstNameField.setString(record.getFirstName());
    familyNameField.setString(record.getFamilyName());
    phoneNumberField.setString(record.getPhoneNumber());
    eMailField.setString(record.getEMail());
    ageField.setString("" + record.getAge());
  }
  
  // cleans all the fields
  public void cleanForm()
  {
    firstNameField.setString("");
    familyNameField.setString("");
    phoneNumberField.setString("");
    eMailField.setString("");
    ageField.setString("");
    setTitle("Record 0/0");
    
    if (state == READ)
    {
      removeCommand(nextCommand);
      removeCommand(previousCommand);
      removeCommand(deleteCommand);
    }
    state = NONE;
  }

}

⌨️ 快捷键说明

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