Friday, June 27, 2008

Working with time

It can seem trivial but sometimes working with time can become a real headache.
Here I present a short code snippet showing one common but confusing situation:
if you create two different Calendar objects and print it getting its Date and using 'println()' method they can seem the same.

Please read code comments which clarifies every situation.


import java.util.Calendar;
import java.util.TimeZone;

public class utc {

/**
* @param args
*/
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance(TimeZone
.getTimeZone("Europe/Madrid"));

Calendar c2 = (Calendar) c1.clone();
c2.setTimeZone(TimeZone.getTimeZone("UTC"));

// Print Calendars. By default the toString() Calendar method is called
// and gets a String version of the objects.
// As you can see the String version shows the time zone of the Calendar.
System.out.println("Cal1: " + c1);
System.out.println("Cal2: " + c2);

// Print the Date objects of each Calendar.
// Here the toString method of the Date objects are invoked and this method
// by default translates the Date to the default system timezone. Thus both Dates
// seems equal.
System.out.println("Cal1: " + c1.getTime());
System.out.println("Cal2: " + c2.getTime());

// Finally we get the value of HOUR, MINUTE and SECOND of each calendar and
// prints it on the screen.
int h1 = c1.get(Calendar.HOUR_OF_DAY);
int m1 = c1.get(Calendar.MINUTE);
int s1 = c1.get(Calendar.SECOND);

int h2 = c2.get(Calendar.HOUR_OF_DAY);
int m2 = c2.get(Calendar.MINUTE);
int s2 = c2.get(Calendar.SECOND);

System.out.println("Cal1: " + h1 + ":" + m1 + ":" + s1);
System.out.println("Cal2: " + h2 + ":" + m2 + ":" + s2);
}
}

Sunday, June 15, 2008

Why I like EJB3...

Today I would like to put a link to an Adam Bien post: Why I Like EJB 3.0 And Really Like EJB 3.1, that is a summary of EJB3 benefits.
You can achieve the same with other technologies, but EJB3 offers you the whole pack.

Wednesday, May 14, 2008

Using JPA in a web application

I usually work creating my entity data model classes using JPA and creating session beans (or stateless/web services beans) in an EJB module to acces this data. Later, I create a web/desktop application to access entities through the previous module.

But sometimes you need/want to do a more quickly/ugly job working with entities directly from your web application. For those (like me today ;)) I put here this link to a JEE5 blueprint named: Design Choices in a Web-only Application Using Java Persistence.
Among other things, It talks about using container or application managed entity managers.

Saturday, April 26, 2008

Obfuscating a NetBeans Java application project

Some time ago I found a couple of posts talking about how obfuscating a NetBeans RCP module (here and here).

Getting some parts of the ant targets presented in the previous post, this one present a simple target that allows to obfuscate a normal java library.

For this, you need to have installed the obfuscator ProGuard.

Take into account I am talking about obfuscating a Java library. This implies the obfuscation is lighter than if you obfuscate a closed application, that is, all public methods and interfaces must maintain its name (if not you can call your library methods anymore).

Open your build.xml Java application file and paste this target:






classpath="${proguard.jar.path}" />



renamesourcefileattribute="SourceFile" ignorewarnings="true">




























name ="class$"
parameters="java.lang.String" />
name ="class$"
parameters="java.lang.String,boolean" />











type="**[]"
name="values"
parameters="" />
type="**"
name="valueOf"
parameters="java.lang.String" />






type ="long"
name ="serialVersionUID" />
name ="**"/>
name ="**"/>
name ="**"/>
type ="void"
name ="writeObject"
parameters="java.io.ObjectOutputStream" />
type ="void"
name ="readObject"
parameters="java.io.ObjectOutputStream" />
name ="writeReplace"
parameters="" />
name ="readResolve"
parameters="" />








Special attention to these couple of lines:



Tuesday, April 08, 2008

Curious ?

Tuesday, March 18, 2008

Differences between source level 1.5 and 1.6 in NetBeans

When you create a new project you can specify the source level and the JDK to use with it.
This is an important fact because depending on the source level the GUI builder matisse will use new classes available in JDK6 or not.

Before JDK6 or using 1.5 source level, swing-layout project is used to provide some useful swing layouts.
As you can read in the project home page: As with other swing-labs projects this is exploratory work, parts of this that are successful will be pulled into future JDKs. The baseline work has already been pulled into 1.6.

This means that for source level 1.6, NetBeans builds the GUIs using the same (or similar) classes located in the JDK6 instead of swing-layout library.

Remember it, if you want to create GUIs that must run in older version of JDK and not only in JDK6.

Sunday, March 09, 2008

My new guitar

I am an aficionado guitar player from 16 and yesterday my wife made me a gift... a new guitar :)

This is the old celebrity guitar:


And this is the new Cort one:

Monday, March 03, 2008

A classic of rock...

Wow !!! I was taking a look at YouTube's videos and found this jewel, the classic sound Paranoid from Black Sabbath:

Using SyntaxHighlighter on Blogger

If you want to use syntaxhighlighter on blogger to beautifully your code remember to take a look here to set the blogger mode enable:


dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');

Monday, February 25, 2008

Using boolean state actions in NetBeans RCP

This post can be categorized as a code snippet and shows how to work with CallableSystemAction and BooleanStateAction.

If you create a new action through the new action wizard your code looks something like:


public final class SomeAction extends CallableSystemAction {

public void performAction() {
// TODO implement action body
}

public String getName() {
return NbBundle.getMessage(SomeAction.class, "CTL_SomeAction");
}

@Override
protected String iconResource() {
return "path/to/your/icon.png";
}

public HelpCtx getHelpCtx() {
return HelpCtx.DEFAULT_HELP;
}

@Override
protected boolean asynchronous() {
return false;
}
}


The action allows you to execute some code (in the performAction() method) when the used clicks the button.
On the other side, a boolean action is like a toggle button. Imagine a play button, it can be pushed to indicate the music is playing and in normal state (not pushed) indicating the player is stopped. This can be achieved with a BooleanStateAction instead of a CallableSystemAction. Modify the previous code to:


public final class SomeAction extends BooleanStateAction implements PropertyChangeListener
{

public SomeAction()
{
// Set the initial state
setBooleanState(true);

// Register as a property listener
addPropertyChangeListener(this);
}

public String getName()
{
return NbBundle.getMessage(SomeAction.class, "CTL_SomeAction");
}

@Override
protected String iconResource()
{
return "path/to/your/icon.png";
}

public HelpCtx getHelpCtx()
{
return HelpCtx.DEFAULT_HELP;
}

public void propertyChange(PropertyChangeEvent evt)
{
// Check if the boolean state has changed.
if(BooleanStateAction.PROP_BOOLEAN_STATE.equals(evt.getPropertyName())) {

Boolean state = (Boolean) evt.getNewValue();
if (state.equals(Boolean.TRUE)) {
// Button has been pressed
}
else {
// Button has been releases
}
}
}
}


How it works?
Both CallableSystemAction and BooleanStateAction are direct subclasses of org.openide.util.actions.SystemAction. When a system action is executed its abstract actionPerformed method is invoked.

// In SystemAction...
public abstract void actionPerformed(ActionEvent ev);


In the case of CallableSystemAction, the override method makes some work and finally invokes the performAction which is the method you need to code.

public void actionPerformed(ActionEvent ev) {
if (isEnabled()) {
org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(
this,
new org.netbeans.modules.openide.util.ActionsBridge.ActionRunnable(ev, this, asynchronous()) {
public void run() {
performAction();
}
}
);
} else {
// Should not normally happen.
Toolkit.getDefaultToolkit().beep();
}
}


In the case of BooleanStateAction, this class maintains a property which represents the state of the button. When the override actionPerformed method is execute it changes the property state and fires a property change event to be cached by the BooleanStateAction listeners:

public void actionPerformed(java.awt.event.ActionEvent ev) {
setBooleanState(!getBooleanState());
}

public void setBooleanState(boolean value) {
Boolean newValue = value ? Boolean.TRUE : Boolean.FALSE;
Boolean oldValue = (Boolean) putProperty(PROP_BOOLEAN_STATE, newValue);

firePropertyChange(PROP_BOOLEAN_STATE, oldValue, newValue);
}


As you can see in the SomeAction code the tip resides in registering the class as its own listener and handle the state change in the propertyChange method.