JavaME Library you might need

I have this javame library I have been compiling for a while. It has a lot of small small code snippets and classes I use often, there is a database part where I have tried as much as possible to encapsulate parts of the saving/retrieving parts of RMS, there is also the UI part, which is what I want to talk about today.

This part contains a TickerForm, which is a form that can show scrollable contents pulled from database or URL at intervals(I am yet to finish it) and the second thing is the Controllable form. This form has a Controller that you can use to service the form(just like posting a form through POST). You call the submit() method of the form and the service method of the controller is called. The service method stores the values of items of the form in a Properties object(which is basically a key-value pair). A code is worth more than a thousand words they say, so here we are

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.trinisoft.atpay.forms;

import com.trinisoft.atpay.MainForm;
import com.trinisoft.atpay.helpers.RecordStores;

import com.trinisoft.mlib.db.decorator.StoreDecorator;
import com.trinisoft.mlib.login.LoginForm;
import com.trinisoft.mlib.login.db.UserClass;
import com.trinisoft.mlib.ui.Form;
import com.trinisoft.mlib.util.Controller;
import com.trinisoft.mlib.util.Properties;

import java.io.IOException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;

/**
*
* @author trinisoftinc
*/
public class RegisterForm extends Form {

TextField username, password, cpassword;
Command ok, cancel;
LoginForm loginForm;
RecordStores recordStores;
MainForm mainForm;
Display display;

public RegisterForm(LoginForm loginForm, final RecordStores recordStores, MainForm mainForm, Display display) {
super("Register", new Controller() {

public boolean service(Properties properties) {
String username = properties.getParameter("Username").toString();
String password = properties.getParameter("Password").toString();
String cpassword = properties.getParameter("Confirm Password").toString();

if (password.equals(cpassword)) {
UserClass userClass = new UserClass();
userClass.setUsername(username);
userClass.setPassword(password);
try {
new StoreDecorator(userClass).save(recordStores.getUserRecordStore());
properties.setParameter("response-code", new Integer(200));
properties.setParameter("response-text", "Password Mismatch");
} catch (RecordStoreException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
properties.setParameter("response-code", new Integer(500));
properties.setParameter("response-text", "Password Mismatch");
}
return true;
}
});
this.loginForm = loginForm;
this.recordStores = recordStores;
this.mainForm = mainForm;
this.display = display;
init();
}

private void init() {
username = new TextField("Username", "", 255, TextField.ANY);
password = new TextField("Password", "", 255, TextField.PASSWORD);
cpassword = new TextField("Confirm Password", "", 255, TextField.PASSWORD);
ok = new Command("Submit", Command.OK, 0);
cancel = new Command("Quit", Command.EXIT, 0);

append(username);
append(password);
append(cpassword);
addCommand(ok);
addCommand(cancel);

setCommandListener(new CommandListener() {

public void commandAction(Command c, Displayable d) {
if (c.equals(ok)) {
submit();
if(getResponseCode() == 200) {
display.setCurrent(loginForm);
} else {
Alert l = new Alert("Error Occurred",getResponseText(),null,AlertType.ERROR);
display.setCurrent(l,d);
}
} else if (c.equals(cancel)) {
mainForm.notifyDestroyed();
}
}
});
}
}

You should draw your attention to two places, the contructor and the commandAction in the init() method. In the super constructor, you pass in a Controller, the controller have a service method, which handles the forms submit() and stores response-code and reponse-text keys and values in the Properties passed to it. These keys are used in the commandAction to get the response code and value. Any other value(or object for that matter) that you want to send back as response can be stored in the Properties object.

I think this suite of classes would be useful for anyone interested in Java ME development. Try it out and let me know your feedbacks.

Code hosted at http://www.gitorious.org/~trinisoftinc

ciao

Advertisements

2 Comments »

  1. Oscar Agyei said

    thanks alot for the help, i really appriciat. but i want code for creating database, where the data base will be uneditable, by users on phone and the database is going to store data that have chapters, verses, and sub verses, like the phone bible, where we can search for a chapter and their verses , using netbeans IDE

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: