Wednesday, February 25, 2009

Working with Netbeans 6.5's Default Status Bar

I'm just curious about what's function of some default code in constructor body that come with netbeans 6.5 release. It show when you create new Java Desktop Application Project, then netbeans would give you default template which has default 3 java class.

this are step by step what I'm doing:
1. when dialog box about new project appear I choose 'Java' in categories box then
java desktop application in 'Projects' box.
2. when I push next button I choose Basic Application on 'choose application shell'
box. This is a message which show on the right box:

A basic application skeleton containing everything needed to start with a general desktop application, such as the following:
- main frame with a menu bar and status bar
- About box
- main Application class
- implementation of status bar messages and progress indication
- resource management mechanisms to better handle loading of icons and strings

3. click finish button and new project wolud be created. Now you will be given 3 java's class, that named with your project name in the beginning of each class name. Usually, it 's look like on below:
If your project name is 'DesktopApplication1' then netbeans would generate 3 class as,
- DesktopApplication1.java
- DesktopApplication1AboutBox.java
- DesktopApplication1View.java
Anyway, you can always change a name for each class, using refactor tool that come with netbeans.

Lets see the source for 'DesktopApplication1View.java' class. I grab part of source on the 'Default Constructor Body', it would be like this:

// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i <>
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);

// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});

Get example project's source here then load it with netbeans IDE. It doesn't work well if you use netbeans release older than 6.5. On this project, I only create one button element that named as 'Process'. You can view it through to design mode for 'DekstopApplication1View.java' class.

What is would gonna show me from this project?
if you push 'Process' button, it will do a looping process that counting forward to 1000000000. when the process is still working just take a look at the right side from status bar. It will show you what happen. Don't forget to build it first, before you 'run' this project.

How it works?
if we see the source from constructor body that mean all 'action task' would be connected and monitored through task monitor and then showed to status bar. Status bar will show you both of progress bar and animation icon when there is a process.

What was happen?
Just add one method and one inner class into 'DekstopApplication1View.java' class to make this happen. first add an action method named 'processButton' which return a task.

@Action
public org.jdesktop.application.Task processButton(){
org.jdesktop.application.Task task=null;
task = new ProcessButtonTask(this.getApplication());
return task;
}

don't forget to add "@action" label first!
switch to design mode, right click on the 'process' button and Set Action...
In the Action label column, choose "processButton", and also enable Background Task option.
second, add inner class named as "ProcessButtonTask" which extends to Task class

private class ProcessButtonTask extends org.jdesktop.application.Task{

public ProcessButtonTask(org.jdesktop.application.Application app) {
super(app);
}

@Override
protected Object doInBackground()throws Exception{
for (double i=1;i<=100000000;){
i++;
}
return null;
}
}

third, clean and build the project.
fourth, run it!
hope this helpful :-)


Sunday, February 22, 2009

The Eternal Flame/God Wrote in LISP by Julia Ecklar


Don't know why, I really interest with this song. Not only the sound from a singer, Julia Ecklar , but a lyric too. I just found it when "doing my daily activity" with firefox. Exactly in this blog. Song's lyric was written by Bob Kanefsky that a kind of parody lyric. Based-on official site, Eternal Flame is parody of “God Lives on Terra” The question of the merits of different programming languages has often been called a “religious argument”, except maybe by those who assume that there’s oviously One Right True and Only Way and it begins with C.

If you like it, you can grab it from this link then listen it. Don't worry it's free to get and copy it. If you like to sing a song, this is a lyric from the song:

I was taught assembler in my second year of school.
It’s kinda like construction work — with a toothpick for a tool.
So when I made my senior year, I threw my code away,
And learned the way to program that I still prefer today.

Now, some folks on the Internet put their faith in C++.
They swear that it’s so powerful, it’s what God used for us.
And maybe it lets mortals dredge their objects from the C.
But I think that explains why only God can make a tree.

For God wrote in Lisp code
When he filled the leaves with green.
The fractal flowers and recursive roots:
The most lovely hack I’ve seen.
And when I ponder snowflakes, never finding two the same,
I know God likes a language with its own four-letter name.

Now, I’ve used a SUN under Unix, so I’ve seen what C can hold.
I’ve surfed for Perls, found what Fortran’s for,
Got that Java stuff down cold.
Though the chance that I’d write COBOL code
is a SNOBOL’s chance in Hell.
And I basically hate hieroglyphs, so I won’t use APL.

Now, God must know all these languages, and a few I haven’t named.
But the Lord made sure, when each sparrow falls,
that its flesh will be reclaimed.
And the Lord could not count grains of sand with a 32-bit word.
Who knows where we would go to if Lisp weren’t what he preferred?

And God wrote in Lisp code
Every creature great and small.
Don’t search the disk drive for man.c,
When the listing’s on the wall.
And when I watch the lightning
Burn unbelievers to a crisp,
I know God had six days to work,
So he wrote it all in Lisp.

Yes, God had a deadline.
So he wrote it all in Lisp.


Enjoy it!

Source Link:
http://www.prometheus-music.com/roundworm.html