J2ME Polish - Development Framework
Hi Readers,
I hope that my previous post help you some how. Please let me know if you want some more updates or article on any specific area. Today I am going to discuss about J2ME Polish Framework. What is the requirement for development frameworks? Features of J2ME Polish etc. So let's start:-
Why we need development frameworks?
To answer this question I am going to share one small example that will help you to have better understanding. We'll create a small app which has login functionality. First I am going to use only Java ME classes without any framework. Let's see:-
We have two options two create this app, one is by using High Level APIs and other is Low Level APIs. High Level APIs have pre-defined classes defined. You need to implement the functionality and you need not to worry about coding any graphics yourself and the application looks like (at least to some degree) native application. However there is limited control of the UI and the UI will look different on different devices.
Low level API requires you to implement everything (how the keys work, how the different UI are drawn on the screen, etc). You have full control of the UI and the app looks the same on all devices (but usually different from the natve application on that phone).
We are going to use High Level APIs in this example. The app code look like this.
/**
* Called at the start of MIDlet application
*/
protected void startApp() throws MIDletStateChangeException {
midletDisplay = Display.getDisplay(this);
ashnaForm = new Form("Ashna MIDlet");
ashnaForm.append("Please enter your login credentials!");
ashnaForm.append(userIDTxt);
ashnaForm.append(pwdTxt);
ashnaForm.addCommand(exitCommand);
ashnaForm.addCommand(loginCommand);
ashnaForm.setCommandListener(this);
midletDisplay.setCurrent(ashnaForm);
}
/**
* Called at the exit of MIDlet application
*/
protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
if (! unconditional) {
throw new MIDletStateChangeException();
}
}
/**
* Used to handle the commands.
*/
if (command == exitCommand) {
try {
destroyApp(false);
} catch (MIDletStateChangeException ex) {
Alert exitAlert = new Alert("Alert", "Do you really want to exit?", null, AlertType.INFO);
exitAlert.addCommand(yesCommand);
exitAlert.addCommand(noCommand);
exitAlert.setCommandListener(this);
midletDisplay.setCurrent(exitAlert);
}
}
Now, let's see the output for this:-
You can notice that, UI for this app is not that much attractive. To make attractive UI we have so many options such as:-
- Use J2ME Low Level APIs
- Use different UI frameworks:-
- J2ME Polish
- LWUIT
- Mobile Distillery’s Celsius
I am using J2ME Polish framework in this article. So before start coding we need to understand J2ME Polish first.
J2ME Polish:-
- J2ME Polish combines the power of Java Mobile (J2ME) with web-designing tool CSS.
- Separate the application UI designing from the application’s source code.
- Develop GUI using High Level APIs like Form, List, TextField etc.
- Provide additional J2ME Polish Custom Components like TabbedForm, TreeItem and soon.
- Add #style directives to applications source code for connecting code with the design.
- We can design and customize application using simple CSS text files.
- Configure additional GUI options in build.xml script.
J2ME Polish Architecture:-
J2ME Polish architecture based upon three main components, which are:-
- Device Database
- Build Framework
- Client APIs
Device Database is the foundation of J2ME Polish and defined as device.xml. It includes the detailed information for J2ME capable devices – APIs support, Screen resolution, known issues, etc. It is used by build framework and client API to generate the executable for particular device.
Build Framework automate the necessary steps and based on the Ant tool. We can easily integrate it with IDE. The main advantage is that, it's extensible.
Client API has support for great GUI using CSS. It provide support for advance GUI classes such as, Tabbed Form, Tree Item, FilterList etc. In addition to this, it also support animation effects such as, text and screen transition effects.
For every device, the approach followed by J2ME Polish:-
Now I am going to use same example code that I used earlier, but with few additional changes. We have to create a file with name polish.css in the resource folder and need to define all the styles. After that we'll use same styles with pre-processing statements in our code. I'll show you with example:-
/***Style entries in polish.css file***/
titleBackground {
color: #5F0000;
}
basicBackground {
color: #9C0000;
}
menuBackground {
type: round-rect;
color: #5f0000;
border-width: 2;
border-color: #5f0000;
}
}
basicBackground {
color: #9C0000;
}
menuBackground {
type: round-rect;
color: #5f0000;
border-width: 2;
border-color: #5f0000;
}
title {
padding: 2;
background: titleBackground;
font-style: bold;
font-size: medium;
font-face: system;
font-color: #ffffff;
layout: center | expand;
}
menu {
menubar-color: #5f0000;
background: menuBackground;
min-width: 100;
padding: 1;
//#if polish.MenuBar.Position != right && polish.MenuBar.Position != invisible
view-type: slide-up;
//#endif
font-style: bold;
font-size: medium;
font-face: system;
font-color: rgb(255, 255, 255);
layout: left;
focused-style: menu:hover;
}
padding: 2;
background: titleBackground;
font-style: bold;
font-size: medium;
font-face: system;
font-color: #ffffff;
layout: center | expand;
}
menu {
menubar-color: #5f0000;
background: menuBackground;
min-width: 100;
padding: 1;
//#if polish.MenuBar.Position != right && polish.MenuBar.Position != invisible
view-type: slide-up;
//#endif
font-style: bold;
font-size: medium;
font-face: system;
font-color: rgb(255, 255, 255);
layout: left;
focused-style: menu:hover;
}
Here I defined few styles, but in same way you can define as per your requirement. Now we'll see how we can use these defined styles in our app and now code look like this:-
/**
* Called at the start of MIDlet application
*/
protected void startApp() throws MIDletStateChangeException {
midletDisplay = Display.getDisplay(this);
//#style form
ashnaForm = new Form("Ashna");
//#style headingLabel
ashnaForm.append("Please enter your login credentials!");
//#style txtField
ashnaForm.append(userIDTxt);
//#style txtField
ashnaForm.append(pwdTxt);
//#style loginButton
ashnaForm.append(loginBtn);
loginBtn.setDefaultCommand(pressCommand);
loginBtn.setItemCommandListener(this);
ashnaForm.addCommand(exitCommand);
ashnaForm.addCommand(loginCommand);
ashnaForm.setCommandListener(this);
midletDisplay.setCurrent(ashnaForm);
}
midletDisplay = Display.getDisplay(this);
//#style form
ashnaForm = new Form("Ashna");
//#style headingLabel
ashnaForm.append("Please enter your login credentials!");
//#style txtField
ashnaForm.append(userIDTxt);
//#style txtField
ashnaForm.append(pwdTxt);
//#style loginButton
ashnaForm.append(loginBtn);
loginBtn.setDefaultCommand(pressCommand);
loginBtn.setItemCommandListener(this);
ashnaForm.addCommand(exitCommand);
ashnaForm.addCommand(loginCommand);
ashnaForm.setCommandListener(this);
midletDisplay.setCurrent(ashnaForm);
}
/**
* Used to handle the commands.
*/
if (command == exitCommand) {
try {
destroyApp(false);
} catch (MIDletStateChangeException ex) {
//#style infoAlert
Alert exitAlert = new Alert("Alert", "Do you really want to exit?", null, AlertType.INFO);
exitAlert.addCommand(yesCommand);
exitAlert.addCommand(noCommand);
exitAlert.setCommandListener(this);
midletDisplay.setCurrent(exitAlert);
}
}
Check the text in blue color in above sample code, in this way you can use pre-defined styles in your app. Now check the output for this app.
So now you can see the difference between the GUI for same app. Same GUI effect we can provide using Low Level APIs also, but over there it's time consuming and for each device we need to port our application. Using J2ME polish you need to just code once and define all the styles after that build framework help you with the device fragmentation.
I hope this article gives you some understanding about J2ME Polish framework. If you have any queries, Please let me know. I'll try my best to answer your queries.
can u write about j2meunit for beginers
ReplyDeleteHi,
ReplyDeleteThanks for your feedback.. I already written a post on J2ME Unit... Please check
http://ashnatechnologies.blogspot.com/2009/03/j2me-unit-testing.html
Please send me your feedback... after reading..
Regards,
Ashish.
Hi,
ReplyDeleteCan you write about Ant scripting and changing Netbean's build file. I am currently facing problem with two things.
1. How to add build date in manifest file.
2. I want to add a new phone api abc.jar to a project. When I add into platform.bootclasspath and change any project settings, netbeans will removes the api which i added.
Hi Anup,
ReplyDeleteSur I'll write post on this and will update you, when I finish it.
Regards,
Ashish
Hi,
ReplyDeletedid you by any chance tried to load resources like Images to show in a List ? And if so could you plese tell me where to place these resources and with that path those should be adressed ?
Yep you can do it... you just need to define "IMAGEURL" in the POLISH.CSS file under the style that you want to apply. Also a style:hover need to define because it give the rollover effect.
ReplyDeletePlease let me know if you still have any query.
Regards,
Ashish
Hi, thanx for your post. I'm new to j2me polish, I can run a sample program using nokia series40 emulator. but when I try to install it in Nokia-3110c it shows invalid application..
ReplyDeletecan u pls help me for this?
Hi,
ReplyDeletePlease check the APIs support on the real device. I think you are using APIs which are not supported on the real device.
Regards,
Ashish
Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
ReplyDeleteJ2ME Application Development
Thanks & Surely I'll post few more articles; these days quite busy, so not able to get time for blogging.
ReplyDeletehow to run open POLISH SAMPLES into netbeans ide..
ReplyDeletecan any one tell me steps to run J2ME POLISH ..
Any sample Program with CSS
Check this - http://www.enough.de/products/j2me-polish/documentation/installation/ide-integration/netbeans/installation/
ReplyDelete