An Open Source Fascicule

Author: Jay Mahadeokar


40% faster startup in NetBeans IDE 6.1 Beta than 6.0!!!!

Thats 100% true....

But what about the overall performance??

I am writing this blog after two hours of frustration! And its 4 -30 AM . So if i curse something for wrong reasons, please don't mind it....

Yes! I am talking about 6.1 Beta slowing down my system upto the point of frustration!! It came to actual standstill!!!

All this when I was developing module project... added a window component to it.. The IDE goes to sleep and so does my system.. I restart my pc and the same thing happens again... and again!!!

Then I somehow gathered all my reserves of patience and waited for about five - ten minutes after i had clicked the close button on the IDE. It finally closed..

Then I went to the Task Manager which as expexted took ages to open up and I found this.....


What the hell are these four processes (netbeans.exe,nbexec,nbexec,Java.exe) doing even after I have closed the NetBeans IDE???
And the CPU usage is 100% !!!

I terminated these process manually...
And finally my system came to normal.

May be there might be an incompatible program running in my system which i think is highly unlikely.

If you have also experienced the same problem.. please feel free to add a comment..

Something for the NetBeans developers to look into...

Note: My desktop has a P4 2.66 GHz CPU , 704 MB Ram...

Here i present a small tutorial to built utility applications using the RMS facility
in J2ME and NetBeans.


Table Of Content:

1. Introduction to RMS.

2. Aim.

3. Tools needed.

4. Creating the basic classes.

5. Creating the basic display containers.

6. Linking the display items.

7. Adding RMS functionality in the event handlers using the classes created.

8. Compile and run the project.

9. Future scope.

1. Introduction to RMS:

Small computing devices do not have a robust file system and therefore are unable to store information in the manner that we are accustomed to when working with a PC, server, and other traditional computing devices. The Record Management System (RMS) provides a file system–like environment that is used to store and maintain persistence in a small computing device. We can insert records, read records, search for particular records, and sort records stored by the RMS.

RMS stores information in a record store. A record store is a collection of records organized as rows (records) and columns (fields). RMS automatically assigns to each row a unique integer that identifies the row in the record store, which is called the record ID.

2. Aim:

To create a simple application in J2ME that will provide facility for storing expenses. The application will provide options for storing the reason for expens and the amount spent using an easy interface.


3. Tools Needed:

  1. NetBeans IDE 6.0/ 6.1
  2. Mobility Pack.

4. Creating the basic classes:

Create New MIDP application. Name it as ExpensesApp.

I. A HelloMIDlet.java file will be created in the src\hello\ package.

II. Create a new java file: Expenses.java.

III. Paste the following code there:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
* This class is the base for handling the storage part of our
* Expenses application
*
* It handles all the writing and reading of the data from
* record store.
*
* An instance of this class will be created in the main Midlet
*
* @author jay
*/
public class Expenses {

/**
* The reason for which the expense was incurred
*/
String Reason = null;

/**
* The Amount of money that spent for the particular expense
*/
int Amount ;

/**
* Method to write the st to disk
*/
void writeToDisk() throws IOException {
try {
RecordStore ExpenseRecord = RecordStore.openRecordStore("ExpRecord", true);
//Creating output streams
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(baos);
//Writing value to be saved to output stream
os.writeUTF(this.Reason);
os.writeInt(this.Amount);

//close stream
os.close();

byte[] data = baos.toByteArray();

//Write the record to the record store
int id = ExpenseRecord.addRecord(data, 0, data.length);
//Close Record store
ExpenseRecord.closeRecordStore();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {
ex.printStackTrace();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}

/**
*Method to read record from disk and
* initialize values of member variables
* @param RecordID the id of the record to be read
*/
int readFromDisk(int RecordID) throws IOException {
boolean flag = true;
while (flag) {
try {
RecordStore ExpenseRecord = RecordStore.openRecordStore("ExpRecord",false);
//read get the record, specified by RecordID in a byte array
byte[] data = ExpenseRecord.getRecord(RecordID);


//If record is read, RecordID is valid and flag
//needs to be reset
flag = false;

DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));

//retrieve

this.Reason = is.readUTF();
this.Amount = is.readInt();


//close stream
is.close();
ExpenseRecord.closeRecordStore();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {

ex.printStackTrace();
} catch (InvalidRecordIDException ex) {
//If invalid recordID, it means that the
//the record was deleted, and we need to search
//in next id for a proper record.
RecordID++;
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
//Return the ID of next record
return (++RecordID);
}
}
You may want to add functionality to store date, place etc. Make the corresponding entries here.

IV. Create a new java file: ManageExpenses.java.

package hello;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
* This class is the base for handling the storage part of our
* Expenses application
*
* An instance of this class will be created in the main Midlet
*
* @author jay
*/
public class ManageExpenses {

/**
* The array corresponding that will store myExpense objects
*/
Expenses myExpense[] = null;



/**
* count: To indicate the total number of records
* added: To indicate number of records added since the midlet
* was last opened
*/
int count;

int added;

/**
* Constructor.
*/
ManageExpenses(){
myExpense = new Expenses[20];
count = 0;
added = 0;
};

/**
* Method to write the current state of Expense object
* to a record store.
* This is similar to Serialization of an object
*/
void writeToDisk() {
for (int i = count-added; i < count; i++) {
try {
myExpense[i].writeToDisk();
} catch (IOException ex) {

}
}
}

/**
* Method to read from disk and initialize Expense object
* It is like Deserialization
*/
void readFromDisk() {
try {

//Open Record Store
RecordStore SubjectRecord = RecordStore.openRecordStore("ExpRecord", false);

//Get the length of Record Store in a Length member variable
int Length = SubjectRecord.getNumRecords();

//Start From RecordID 1
int RecordID = 1;

for (int i = 0; i < Length; i++) {
myExpense[i] = new Expenses();
RecordID = myExpense[i].readFromDisk(RecordID);
count++;

}


//Close Record Store
SubjectRecord.closeRecordStore();

} catch (IOException ex) {
ex.printStackTrace();
} catch (RecordStoreFullException ex) {
ex.printStackTrace();
} catch (RecordStoreNotFoundException ex) {
ex.printStackTrace();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}


/**
* @param r
* @param a
* @param d
*
* To add a Expense entry in the list
*/
void addExpense(String r, String a)
{

Expenses e = new Expenses();
//e.id = 0;
e.Reason = r;
e.Amount = Integer.parseInt(a);
myExpense[count++] = e;
added++;

}

}

5. Creating the basic display containers:

I. Open the HelloMIDlet.java and click on the Flow tab you will find something like this:







II. Now go to the palette window and select the list option. Add it to the file by clicking anywhere on the Flow tab as shown:





Rename the List name as well as the instance name as menu. This can be done by right clicking on the list and selecting properties option. This list will be used to display the main menu.

III. Now create an exitCommand for the Menu List Item. This can be done by:

RightClick >> New/Add >> exitCommand as shown:

V. Similarly add two List Elements to Menu and rename them as Add Expense and View Expenses.

VI. Add another List item and rename it as viewExpense. Add backCommand to it. Rename it as backCommandView.

Go to edit view.

Right-click and add two text-fields.

Set their instance names and Labels to “Reason” and “Amount”.

VII. Now go to the Palette tab and add a new Form.

Rename it as addExpenseForm.

Give the title as Add Expense.

Add cancelCommand and okCommand to the form in similar way as described before.

Rename them as cancelCommandAddForm and okCommandAddForm.

VIII. Add a splashScreen from the palette.

Right click splashScreen >> Select Edit.

There set the message to be displayed as “Expense added successfully!”

IX. Finally delete the form that was previously present.

The layout will be as follows:

6. Linking the display items:

Now we come to the most interesting and easy part of creating the application. We will link the display items that we created. This is an easy drag and drop process.

Left click on the event and drag it to the item you want to be displayed on occurrence of that event. The display item will be automatically linked with the event.

Example: On the Started event of Mobile Device item, we want the menu to be displayed.

So, right-click on Started button and drag it to menu.

Create following links in a similar fashion:

Event

Display Item

1. Mobile Device: Started()

menu

2. menu: exitCommandMenu()

Mobile Device

3. menu: Add Expense()

addExpenseForm

4. addExpenseForm: cancelCommandaddForm()

menu

5. addExpenseForm: okCommandAddForm()

splashScreen

6. splashScreen: DISMISS_COMMAND

Menu

7. menu: View Expenses()

viewExpenses

8. viewExpenses: backCommandView()

Menu

The window will look like as follows:

(Note: To get the items arranged in a systematic fashion, click on the button highlighted in red circle in figure)

7. Adding RMS functionality in the event handlers using the classes created.

I. Open the Source Tab.

II. Add variables:

//To indicate that application has started for first time

private boolean flag = false;

//Array of expenses

private ManageExpenses myexp = new ManageExpenses();

III. In the exitMIDlet() function add the following code:

try {
myexp.writeToDisk();
} catch (Exception ex) {
ex.printStackTrace();
}

IV. Go to Generate Method: menuAction.

Place the code below as shown in next figure

if(!flag)
{
myexp.readFromDisk();
flag = true;
}
for(int i=viewExpense.size();i
{
viewExpense.append(myexp.myExpense[i].Reason+" : "+myexp.myExpense[i].Amount , null);
}

V. Similarly expand Generate Method: CommandAction for Displayables.

Add following code as shown.

myexp.addExpense(Reason.getString(),Amount.getString() );
Amount.setString("");
Reason.setString("");

8. Final Step: Compile and run the project.

You can see following outpot when you launch the Midlet.

9. Future scope:

The functionality can be extended so as to store expenses according to date, give useful information from the stored expenses data like expenses for a particular month etc.


Note: Special thanks to Vasusen Patil who literally dragged me into mobile programming and has always been there for help.

Ever since Google announced the plan to launch the Android platform and enter the mobile development world there have been waves of anticipation in the developer community. Google has released the Android SDK for development of Android applications.

There is a brilliant tutorial for starting android development using NetBeans by Amit K Saha which you can find at: http://wiki.netbeans.org/IntroAndroidDevNetBeans

The unofficial NetBeans plugin available for the android development though has not been updated and since the release of the latest Android SDK m5-rc15 the applications give error in the AndroidManifest.xml file.

I frustratingly searched a day to find out the correct solution and finally ended up here: http://abhrajit.blogspot.com/2008/03/undroid-and-android-sdk-m5-rc15.html


The android platform seems to be really cool and has all the functionality that a developer can dream of. It has a robust set of API functions and the development language is java which makes it all the more addictive and easy to grasp. Besides these Android also provides optional API that give access to the Location-Based Services (such as GPS, compass, etc.), OpenGL 3D graphics, Bluetooth, and accelerometer.

With the Android developer challenge deadline fast approaching, it needs to be seen is what the developers throughout the world have come up with. And since Google is behind the whole thing, we can just wait and be prepared for a revolution. Sky seems to be the limit!!

Documentation is the most important aspect of project development. A well documented project is not only easy to understand for others who may want to read it but also for the author himself. I never used to document my code until the day I realized first hand the problems it can create.

As it is rightly said – “Programming is 10% coding and 90%documentation”.

For me documenting has been and will always be the most boring aspect of writing code. But then I came to know about the JavaDocs and its support in Netbeans. If I like java for any reasons the most important of them would be that it has made writing world class standard and formatted documentation a child’s play. And what more...

NetBans 6.1 has made the thing even better. So now not only are you just a click away from generating neatly edited and polished html pages for all the code that you have written, but it also helps you to write formatted JavaDoc comments through the newly added feature of “intelliscence” as I like to call it. (The term was originally coined and is popularly used in regard to the Microsoft Visual Studio).

Just type ‘@’ within your javadoc comment and do ‘CTRL+SPACE’ and the feature will provide you will all the options that you can further write. If you say @see then it will provide the list for all documents that may be referred. The new addition seems to be really cool and is a real help as far as I am concerned.

Though it prompts for most of the possible options the options @author, @serialfield, @version seem to be missing.There must be a good reason for it which I haven’t yet figured out. Or else we can surely expect the additions in the next version.

Till then keep docing the intelliscent way!!

Reference:

  1. To learn how to write comments for javadoc tool : http://java.sun.com/j2se/javadoc/writingdoccomments/
  2. For a tutorial regarding creation of javadocs in NetBeans : http://javadoc.netbeans.org/




NEWS:

The Netbeans IDE has come of age and its latest release version 6.1 Beta is an outstanding evidence for that. There has been quite a bit of talk regarding the new features it has to offer. The largely awaited release has finally shown up. Download it at
http://dlc.sun.com.edgesuite.net/netbeans/6.1/beta/

The developers as well as the programmers are eager to see whether it lives up to the expectations. Some of the most promising features include:

- JavaScript Support

- Performance Enhancements

- Spring Framework Support

- New MySQL Support in Database Explorer

- Java Beans Support

- JSF CRUD Generator

- Ruby/JRuby Support

- Javadoc Code Completion

- Sharing Projects (AKA Sharable Libraries)

- New Update Center Modules

The details of the above features may be viewed at http://www.netbeans.org/community/releases/61/

The developers also seem to be eagerly anticipating the response from users and the community and have launched a blogging contest in that regard.

http://www.netbeans.org/competition/blog-contest.html?cid=923686

I still remember the good old days when I used to sit for days together writing stupid programs in C (those good old days are actually not that old – I am talking about a year and half ago when I wrote my Hello World program in C). I was proud of myself when I had written an othello game program and I thought I am a genius. I had written more than “twelve hundred” lines of code sitting in front of computer for two full days. Whew!!! The designing of the UI (using 16 bit graphics with keyboard as the only source of input) and the help file and the artificial intelligence and handling the events…. All done tediously using C!!

Now if I try to implement the same program I am sure will make it in 4 – 5 hours with a tidy user interface, click – button event handlers and 6.4 million graphic colors. This is the power that an Integrated Development Environment can offer. It offers all the conveniences that a programmer can dream about.

I first got the chance to get my hands on the Microsoft Visual Studio 6.0. And I got instantly addicted to it. And from then on I thought I will forget the Blue Turbo C++ screen.

I am completely fascinated with the .Net Framework and now by Net Beans. The quality, ease and functionality they offer is quite extraordinary. The intellisence of Visual Studio which prompts all possible options as we type our code, the refactoring facility, the wizard generated code, automatic generation of JavaDocs in NetBeans, the cool project, file and resource management tabs, versioning facilities, xml support, ant scripting, formatting there seems to be no end to the luxuries the IDE has to offer.

But then I realized there is also other side to it. Given all the comforts that the IDE’s have to offer, at the end of the day what matters is how the programmer uses it. For that the concepts underlying the software development must be strong. And to build those one has to sit in front of that Blue Screen. As Mr Yeshwant Kanetkar always says- "Code till your fingers start to swell, only then can you say you have scaled a tiny part of the monstrous hill called programming".

So, keep coding!!!

Subscribe to: Posts (Atom)