Shaaf’s Blog
msgbartop
Another bit in the wall
msgbarbottom

17 Nov 08 Command, Singleton, JMenuItem, JButton, AbstractButton – One Listener for the app

Here I would like to demonstrate a simple use of JMenuItems being used with Single Listener for the entire system.
A simple sample of use would probably be SingleInstance Desktop Application.

Lets see how that is done here.

1. First lets create a OneListener class that should be able to listen to ActionEvents and also be able to add Commands to itself. Please refer to my previous post on Command,Singleton if you would like to see more about this patterns and there usage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.shaafshah.jmenus;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
 
import javax.swing.AbstractButton;
 
// Implements the ActionListener and is a Singleton also.
 
public class OneListener implements ActionListener{
 
	private static OneListener oneListener = null;
 
	// Holds the list of all commands registered to this listener
	private ArrayList<Command> commandList = null;
 
	// A private constructor
	private OneListener(){
		commandList = new ArrayList<Command>();
	}
 
	// Ensuring one instance.
	public static OneListener getInstance(){
		if(oneListener != null)	
			return oneListener;
		else return oneListener = new OneListener();
	}
 
	// Add the command and add myself as the listener
	public void addCommand(Command command){
			commandList.add(command);
		    ((AbstractButton)command).addActionListener(this);
	}
 
 
	// All Events hit here.
	@Override
	public void actionPerformed(ActionEvent e) {
		((Command)e.getSource()).execute();
	}
 
}

In the above code, the addCommand method adds the command Object and adds a listener to it.
Now how is that possible.
Basically because I am categorizing my UI objects as Commands with in the system having some UI. And I am also assuming that these commands are Currently AbstractButton i.e. JMenuItem, JButton. Lets have a look at the Command Interface and its Implementation.

1
2
3
public interface Command {
	public void execute();	
}

And the implementation, note that the Command is an interface where as the class is still extending a UI object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import javax.swing.JMenuItem;
 
public class TestCmd extends JMenuItem implements Command{
 
	public TestCmd() {
		super("Test");
		OneListener.getInstance().addCommand(this);
	}
 
	@Override
	public void execute() {
		System.out.println("HelloWorld");
	}
}

Personally don’t like calling the OneListener in the constructor of the class but just for the sake of simplicity of this post I have left it that way. There are many different ways to omit it.

So the TestCmd is a JMenuItem but is also a Command and thats what the OneListener understands.
As this commads Listener is also OneListener all ActionEvents are thrown there and from there only the Command.execute is called on.

So now you dont have to worry about what listeners are you in. The only thing you know is that when execute is called you need to do your stuff.

You can download the code from Here.

Hope this helps.

Tags: , , , , , , , , , , , , , , ,

12 Nov 08 Continous Integration – A blast from the past

Although this didn’t happen a decade ago but still has been a good case for me to learn and realize how Continuous Integration brings value addition to our work.

As I recall it was like this when they were teenagers :D
Few teams working on different modules of same application, deployed together.
No build process formalized
No builds except for the ones that need major milestone deployments.
No feedback, reports, reviews etc.

So virtually there was no build eco system.

Which meant hideous amounts of communication, tons of trouble shooting, last minute show stoppers, And extermely unhappy end users/stake holders.

So what exactly was going wrong.

There was no Configuration Control i.e. No way to figure out the changes that will be part of the release and no controls to include or exclude them.
No Status accounting i.e. No feedback or status of anything as a whole product.
No management of Environment meant no one knew what hardware/software they were using and sometimes why?
No team work whatsoever meant no guided processes, reviews or tracebility of issues and pitfalls.

So virtually a release day was the doomsday!

It took some time for developers to realize that how important the following were.
* Configuration identification – What code are we working with?
* Configuration control – Controlling the release of a product and its changes.
* Status accounting – Recording and reporting the status of components.
* Review – Ensuring completeness and consistency among components.
* Build management – Managing the process and tools used for builds.
* Process management – Ensuring adherence to the organization’s development process.
* Environment management – Managing the software and hardware that host our system.
* Teamwork – Facilitate team interactions related to the process.
* Defect tracking – Making sure every defect has traceability back to the source

And after all it takes some time but the day to day flow looked like the figure below.

Build Management

Build Management

Importantly
Developers are distributed on different locations in different teams.
There is an SCM team that takes care of the SCM activities i.e. Configuration Identification, Control, Build Management etc.
There is one single repository for all developers.
A process is defined for Product integration and its checked whenever some one checks in.
And hourly build is dont to check the sanity of the system with the unit tests.
A Nightly build is done with all full blown unit tests, coverage reports, Automated tests and installers and upgrades.
More over all of the above is shown on an FTP/HTTP site. FTP for installer/binary/archive pickup and HTTP for detailed view of builds/feedbacks etc.

All of the above meant strength and health of the build eco system. And more trust in the system that it will report failures along the way.

Tags: , , , , , ,

12 Nov 08 Doing the Locale – Danmark

The following illustrates how to get the Number format working with a danish locale.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
 
 
public class TestLocale {
 
 public static void main(String args[]){
 // Create a Locale for Danmark
 Locale DANMARK = new Locale("da","DK");
 
 // get the currency instance for this locale.
 Currency krone = Currency.getInstance(DANMARK);
 
 // Get a Number format for the locale.
 NumberFormat krFormat = NumberFormat.getCurrencyInstance(DANMARK);
 // A symbol for the currency
 String symbol = krFormat.getCurrency().getSymbol();
 // A double amount
 double amount = 10000.25;
// print it out after formatting.
 System.out.println(krFormat.format(amount));
 }
}

Tags: , , , , , , , ,