/* I always feel that the notepad on my mobile phone is not easy to use. I can't remember the privacy of my phone. So I tried to write a notepad on a j2_based browser, which mainly includes the encryption function .... Almost all the things in the textbook are covered... There is no technical content in itself. It is okay to clarify the logic of the commandAction method...
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/
Package Window;
Import javax. microedition. midlet .*;
Import javax. microedition. lcdui .*;
Import java. util .*;
Import javax. microedition. io. file .*;
Import java. io .*;
Import javax. microedition. io .*;
Import javax. microedition. rms .*;
/**
* @ Author mant
*/
Public class myNote extends MIDlet implements CommandListener {
Private Display display;
// Form fPassword;
List list;
String selectTitle = "";
Int [] ids;
// Private TextBox tb = new TextBox ("edit", "", 100000, TextField. ANY );
NoteSave ns = new noteSave ();
Form f;
TextField tfTitle;
TextBox tfRecData;
TextBox tbModify;
Alert alert;
Form frmInfo;
Form fPassword;
PasswordSave ps = new passwordSave ();
String password;
TextField tfpassword;
Int flag = 0;
Public myNote ()
{
Display = Display. getDisplay (this );
Tfpassword = new TextField ("", "", 20, TextField. ANY );
Password = ps. getPassword ();
If (password. equals ("nopassword "))
{
// FPassword. append ("You have not set a password! ");
Flag = 1;
Tfpassword. setLabel ("set password ");
}
Else
{
Tfpassword. setLabel ("Enter Password ");
}
}
Public void run ()
{
List = new List ("all records", Choice. IMPLICIT );
Ticker tk = new Ticker ("Miss U... Everyday :)");
List. setTicker (tk );
Command cmd_Exit = new Command ("Exit", Command. EXIT, 1 );
Command cmd_Add = new Command ("add", Command. OK, 1 );
Command cmd_Change = new Command ("View", Command. OK, 1 );
Command cmd_Delete = new Command ("delete", Command. OK, 1 );
Command cmd_Info = new Command ("info", Command. OK, 1 );
List. setSelectCommand (pai_add );
List. addCommand (cmd_Exit );
List. addCommand (cmd_Change );
List. addCommand (cmd_Delete );
List. addCommand (cmd_Info );
List. setCommandListener (this );
This. readAll ();
Display. setCurrent (list );
}
Public void startApp (){
FPassword = new Form ("login ");
Command c = new Command ("OK", Command. OK, 1 );
Command cexit = new Command ("Exit", Command. EXIT, 1 );
FPassword. append (tfpassword );
FPassword. addCommand (c );
FPassword. addCommand (cexit );
FPassword. setCommandListener (this );
Display. setCurrent (fPassword );
}
Private void readAll (){
List. deleteAll ();
Try {
RecordStore rs = RecordStore. openRecordStore ("DB", true );
If (rs! = Null ){
RecordEnumeration re = rs. enumerateRecords (null, null, false );
// Obtain the record ID array first
Ids = new int [re. numRecords ()];
Int I = 0;
While (re. hasNextElement ()){
Ids [I ++] = re. nextRecordId ();
}
// Obtain the record value
Re. reset ();
I = 0;
While (re. hasNextElement ()){
Byte [] data = re. nextRecord ();
Ns. readCode (data );
If (data! = Null ){
StringBuffer sb = new StringBuffer ();
// Add Record ID
// Sb. append ("[" + ids [I ++] + "]");
Sb. append (ns. noteTitle );
// Add to List
List. append (sb. toString (), null );
System. out. println (sb );
}
}
} Else {
System. out. println ("no record ");
}
Rs. closeRecordStore ();
} Catch (Exception e ){
System. out. println (e );
}
}
Public void commandAction (Command c, Displayable s ){
String cmd = c. getLabel ();
String title = "";
If (cmd. equals ("continue "))
{
TfRecData = new TextBox ("Diary", "", 100000, TextField. ANY );
Command c1 = new Command ("OK", Command. EXIT, 1 );
Command c2 = new Command ("quit", Command. BACK, 1 );
TfRecData. addCommand (c1 );
TfRecData. addCommand (c2 );
TfRecData. setCommandListener (this );
Display. setCurrent (tfRecData );
}
Else if (cmd. equals ("add ")){
F = new Form ("add record ");
// F.
TfTitle = new TextField ("Record Name", "", 20, TextField. ANY );
TfRecData = new TextBox ("Diary", "", 100000, TextField. ANY );
F. append ("Record Data ");
F. append (tfTitle );
Command cc = new Command ("continue", Command. EXIT, 1 );
Command ccc = new Command ("quit", Command. BACK, 1 );
// TfRecData. addCommand (c1 );
F. addCommand (cc );
F. addCommand (ccc );
F. setCommandListener (this );
// TfRecData. setCommandListener (this );
Display. setCurrent (f );
}
Else if (cmd. equals ("OK "))
{
If (c. getCommandType () = Command. EXIT)
{
// This. add ();
This. save (tfRecData. getString (), tfTitle. getString ());
This. readAll ();
}
Else if (c. getCommandType () = Command. BACK)
{
This. modify ();
This. readAll ();
}
Else if (c. getCommandType () = Command. HELP)
{
// System. out. print ("delete ");
This. delete ();
This. readAll ();
}
Display. setCurrent (list );
}
Else if (cmd. equals ("View "))
{
// String index = Integer. toString (ids [list. getSelectedIndex ()]);
Int indexInt = ids [list. getSelectedIndex ()];
Title = "view record" + ":" + list. getString (list. getSelectedIndex ());
TbModify = new TextBox (title, "", 100000, TextField. ANY );
TbModify. setString (this. read (indexInt ));
Command cmd_ OK = new Command ("OK", Command. BACK, 1 );
Command cmdext = new Command ("quit", Command. BACK, 1 );
TbModify. addCommand (pai_ OK );
TbModify. addCommand (cmdext );
TbModify. setCommandListener (this );
Display. setCurrent (tbModify );
}
Else if (cmd. equals ("delete "))
{
Command al_EXIT = new Command ("exit", Command. CANCEL, 1 );
Command al_ OK = new Command ("OK", Command. HELP, 1 );
// String index = Integer. toString (ids [list. getSelectedIndex ()]);
Title = "delete record" + ":" + list. getString (list. getSelectedIndex ());
Alert = new Alert (title );
Alert. setType (AlertType. WARNING );
// Alert. setTimeout (Alert .);
Alert. addCommand (Alert. DISMISS_COMMAND );
Alert. addCommand (al_ OK );
Alert. addCommand (al_EXIT );
Alert. setString ("the following will be" + title + "" n "+" "n" continue? ");
Alert. setCommandListener (this );
Display. setCurrent (alert );
}
Else if (cmd. equals ("info "))
{
This. showInfo ();
}
Else if (cmd. equals ("quit "))
{
List. setCommandListener (this );
Display. setCurrent (list );
}
Else if (cmd. equals ("Exit "))
{
This. exitMIDlet ();
}
Else if (cmd. equals ("OK "))
{
If (flag = 1)
{
Ps. setPassword (tfpassword. getString ());
This. run ();
}
Else if (flag = 0)
{
If (password. equals (tfpassword. getString ()))
{
This. run ();
}
Else
{
Tfpassword. setLabel ("Incorrect password ");
}
}
}
}
Public void save (String str, String title)
{
RecordStore rs = noteSave. openRSAnyway ("DB ");
NoteSave ns = new noteSave ();
Ns. noteTitle = title;
Ns. noteString = str;
Byte tmp [] = ns. insertCode ();
Try
{
Rs. addRecord (tmp, 0, tmp. length );
Rs. closeRecordStore ();
}
Catch (Exception e)
{
System. out. print (e. toString ());
}
}
Public String read (int index)
{
String str = "";
RecordStore rs = noteSave. openRSAnyway ("DB ");
NoteSave ns = new noteSave ();
Try
{
Byte tmp [] = rs. getRecord (index );
Ns. readCode (tmp );
Str = ns. noteString;
Rs. closeRecordStore ();
}
Catch (Exception e)
{
Return null;
}
Return str;
//
}
Private void modify (){
Try {
RecordStore rs = RecordStore. openRecordStore ("DB", true );
String title = tbModify. getTitle ();
Int pos = title. indexOf (":");
Int id = Integer. parseInt (title. substring (pos + 1 ));
Byte [] data = rs. getRecord (id );
Ns. readCode (data );
Ns. noteString = tbModify. getString ();
Data = ns. insertCode ();
// RecordStore rs = RecordStore. openRecordStore ("DB", true );
// Add record listener
// Rs. addRecordListener (new MyRecordListener ());
If (rs! = Null ){
Rs. setRecord (id, data, 0, data. length );
}
Rs. closeRecordStore ();
} Catch (Exception e ){
System. out. println (e );
}
}
Private void delete (){
// String title = alert. getTitle ();
// Int pos = title. indexOf (":");
Int id = ids [list. getSelectedIndex ()];
// System. out. print ("delete ");
Try {
RecordStore rs = RecordStore. openRecordStore ("DB", true );
// Add record listener
// Rs. addRecordListener (new MyRecordListener ());
If (rs! = Null ){
Rs. deleteRecord (id );
}
Rs. closeRecordStore ();
} Catch (Exception e ){
System. out. println (e );
}
}
Private void showInfo ()
{
FrmInfo = new Form ("display record storage information ");
Command cmdBack = new Command ("quit", Command. CANCEL, 1 );
FrmInfo. addCommand (cmdBack );
FrmInfo. setCommandListener (this );
Try {
RecordStore rs = RecordStore. openRecordStore ("DB", true );
Int recNums = rs. getNumRecords ();
FrmInfo. append ("Total number of records:" + recNums + "" n ");
FrmInfo. append ("available space:" + rs. getSizeAvailable () + "" n ");
FrmInfo. append ("next record ID:" + rs. getNextRecordID () + "" n ");
FrmInfo. append ("version:" + rs. getVersion () + "" n ");
} Catch (Exception e ){
System. out. println (e );
}
Display. setCurrent (frmInfo );
}
Public void pauseApp (){
}
Public void exitMIDlet (){
Try {
DestroyApp (false );
Yydestroyed ();
} Catch (Exception ex ){
Ex. printStackTrace ();
}
}
Public void destroyApp (boolean unconditional ){
}
}
========================================================== ========================================================== ===
/*
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/
Package Window;
Import java. io .*;
Import javax. microedition. io .*;
Import javax. microedition. rms .*;
/**
*
* @ Author mant
*/
Public class noteSave
{
String noteTitle;
String noteString;
Public byte [] insertCode ()
{
Byte [] result = null;
Try
{
ByteArrayOutputStream bos =
New ByteArrayOutputStream ();
DataOutputStream dos = new DataOutputStream (bos );
Dos. writeUTF (noteTitle );
Dos. writeUTF (noteString );
Result = bos. toByteArray ();
Dos. close ();
Bos. close ();
} Catch (Exception e)
{
}
Return result;
}
Public void readCode (byte [] data)
{
Try
{
ByteArrayInputStream bis =
New ByteArrayInputStream (data );
DataInputStream dis = new DataInputStream (bis );
NoteTitle = dis. readUTF ();
NoteString = dis. readUTF ();
Dis. close ();
Bis. close ();
} Catch (Exception e)
{
}
}
Public static RecordStore openRSAnyway (String rsname)
{
RecordStore rs = null;
// The name cannot exceed 32 characters
If (rsname. length ()> 32)
Return null;
Try
{
Rs = RecordStore. openRecordStore (rsname, true );
Return rs;
} Catch (Exception e)
{
Return null;
}
}
}
========================================================== ========================================================== =====
/*
* To change this template, choose Tools | Templates
* And open the template in the editor.
*/
Package Window;
Import javax. microedition. rms .*;
Import java. io .*;
Import javax. microedition. io .*;
Import javax. microedition. io. file .*;
/**
*
* @ Author mant
*/
Public class passwordSave {
RecordStore rs;
Public passwordSave ()
{
This. openRSAnyway ("password ");
}
Public String getPassword ()
{
String str = "";
// Byte [] data = rs. getRecord (1 );
Try
{
ByteArrayInputStream bis =
New ByteArrayInputStream (rs. getRecord (1 ));
DataInputStream dis = new DataInputStream (bis );
Str = dis. readUTF ();
// NoteString = dis. readUTF ();
Dis. close ();
Bis. close ();
} Catch (Exception e)
{
Return str = "nopassword ";
}
Return str;
}
Public void setPassword (String password)
{
Byte [] data = null;
Try
{
ByteArrayOutputStream bos =
New ByteArrayOutputStream ();
DataOutputStream dos = new DataOutputStream (bos );
Dos. writeUTF (password );
// Dos. writeUTF (noteString );
Data = bos. toByteArray ();
Dos. close ();
Bos. close ();
Rs. addRecord (data, 0, data. length );
} Catch (Exception e)
{
}
// Return result;
}
Public void openRSAnyway (String rsname)
{
// RecordStore rs = null;
// The name cannot exceed 32 characters
If (rsname. length ()> 32)
Return;
Try
{
Rs = RecordStore. openRecordStore (rsname, true );
// Return rs;
} Catch (Exception e)
{
Return;
}
}
}