Showing posts with label NetBeans. Show all posts
Showing posts with label NetBeans. Show all posts

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:



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.

Monday, February 18, 2008

Changing default action's icon in NetBeans RCP

If you are developing an application using NetBeans RCP probably you are using default actions like Delete, Cut or Save that uses its own icons. This post talks about two techniques so change the default icons associated to an existent action.

Branding
The first method to change the default icon is through the branding directory in your module suite. To allow this, you need to know in which Java package is stored the icon resource used by the action you want to change its icon. A good way to know this is downloading the NetBeans platform (or other module) source code, looking for the action code and get the icon's resource path.

For example, the Cut and Delete actions are in the package org.openide.action. If you want to override it with your own icons all you need to is is to create, in your module suite branding directory, a folder called org-openide-action.jar, create a subfolder hierarchy representing the package structure and put your own icons with the same name the action code uses.



Wrapping
The second method implies to create a new action that wraps the target action you want to change its icons.
The below present a little class WrapperCutAction that wraps the NetBeans CutAction. The idea is pretty simple, the wrapper action can have any desired icon and when it is executed only you need to do is redirect the event to the target action.


package yourpackage;

import java.awt.event.ActionEvent;
import org.openide.actions.CutAction;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallbackSystemAction;

public final class WrapperCutAction extends CallbackSystemAction {

public static final String ICON_PATH = "org/balloon/ui/icons/edit-cut.png";
// Wrap the target action
private CutAction ca = new CutAction();

public String getName() {
return NbBundle.getMessage(WrapperCutAction.class, "CTL_WrapperCutAction");
}

protected String iconResource() {
return ICON_PATH;
}

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

protected boolean asynchronous() {
return false;
}

// Wrap the target methods
public void actionPerformed(ActionEvent e) {
ca.actionPerformed(e);
}

public Object getActionMapKey() {
return ca.getActionMapKey();
}
}

Wednesday, January 30, 2008

A quieter theme for the eyes

I found this post (via DZone) pointing to a new NetBeans editor color theme, called Aloha, that is more quiet, not as highlighted as the default but similar to norway today theme.

Saturday, December 15, 2007

Working with wizards

Looking for information about how pass data among the panels of a wizards, I found these links posts in the Geertjan's Weblog about how wizards works in NB:

How wizards work: part 1, 2, 3, 4 and 5.

Wednesday, September 12, 2007

I have it !!!

Finally I have the book.
Rich Client Programming: Plugging into the NetBeans Platform
Yeah.

Friday, August 31, 2007

Visual Web Application and JPA

Today I want to point to a couple of articles Contributed by Winston Prakash, maintained by Beth Stearns about Visual Web Pack in NetBeans 6 and the more useful option to use JPA to bind component.
Now instead bind a table ind a DB with a VWP table component and edit directly to the DB you can obtain data as entities and put it in the visual table component.

http://www.netbeans.org/kb/60/web/web-jpa.html
http://www.netbeans.org/kb/60/web/web-jpa-part2.html

Thursday, August 02, 2007

Coloring nodes

NetBeans brings platform developers a way to improve node labels using simplified subset of HTML tags.
You can see it in action here in the nodes API tutorial.

Supposing you have a subclass of Node or AbstractNode classes, the only thing you need to do is override the getHtmlDisplayName() method.

@Override
public String getHtmlDisplayName() {
String name;

if(updateState) {
name = "Name";
} else {
name = "RED Name";
}

return name;
}


This peace of code returns the name of the node depending on the 'updateState' attribute. If something is wrong then the name is rendered in red.

Also, the 'updateState' can change in any moment, then only you need to do is to fire that display node name has changed with:

fireDisplayNameChange(oldName, newName);


Take in account that the old and new names must be different. If not, the fire method won't have any effect.

Wednesday, July 25, 2007

Communicating with the User (and Yourself)

(From Geertjan's Weblog)

If you use System.out and the JOptionPane to communicate with your users, or with yourself during debugging or testing, you might be interested in knowing about similar (better!) facilities that the NetBeans APIs make available...

Read...

Wednesday, July 04, 2007

Status line elements order, build number and other thips

Some time ago I read this interview to Emilian Bold
http://platform.netbeans.org/articles/nbm_interview_emilian.html

here you can learn some useful tips (like the build number version I was looking for).

Monday, July 02, 2007

Hiding tabs

Follow the Geertjan post:
http://blogs.sun.com/geertjan/entry/farewell_to_space_consuming_weird
you can know how to hide the tabs on your NB applications.

Hide close button

Here is a little tip about how to hide the close button in the tabs.
As you can read here the sentences are:

System.setProperty("netbeans.tab.close.button.enabled","false");
System.setProperty("nb.tabs.suppressCloseButton","true");


but I put it the 'restored()' method of my ModuleInstall class and it works fine.

Here you can find a more extensive list of options.

Thursday, June 07, 2007

Native libraries in NetBeans modules

The NB wiki and documentation says you can put your dll or so files in the 'lib' directory of your module, but I think a little example can be a more good explanation.

Supose you need a module wrapper for your new NB platform based application. Imagine you want to use the JAI API and distribute your application for Windows and Linux.
Right, after create the module wrapper (with the NB wizard) you'll can see a directory named (see the file view):

jai-wrapper\release\modules\ext

containing the JAR files, but what about the dll/so files?
The answer is put it in the:

jai-wrapper\release\modules\lib

See the previous links to the documentation if you have equal names for diferents SO's in your dll/so files.

Wednesday, June 06, 2007

A note on class loaders

Today I read this post on Nabble about Loading class from an external module.
I think the hierarchy of class loaders is not a common thing for a great number of Java programmers, at least in their every day work.

Although it is for specific for NetBeans, you can find useful information how class loaders works in Java, following the final links.

Tuesday, June 05, 2007

Extending the DataProvider API with VisualWebPack

Until the coming version of NetBeans+VisualWebPack arrive and we can bind a WebService or EJB to a table or other component, here is a little idea to know how to do it in a correct way.

First of all, here are a couple of blogs that can help us in our search:
http://blogs.sun.com/winston
http://jkook.blogspot.com

If the VWP components are enough for your application, I think VWP is a grat tool that helps you top spend much more less time to create an application. The separation of request, session and application beans, the set of components, drag&drop and binding component to data sources: like a DB table.

However, what happens when you want to bind a component to the result of a web service or EJB. For the moment, you can extend the DataProvider API and play requesting data on request or session bean.

ObjectListData Provider work around - Sample Project
Creator Tip: Work around for Object List Data Provider design time problem

Tuesday, April 03, 2007

Looking for EJB3 and JSF integration with Sun webui-jsf

Ok, after a good job you good model business and model layer, that is, you have a set of amazing entities (using JPA) and a set of business objects (EJB session beans).
Now you want to make a beautiful presentation tier. You can immolate or make a precise job using servlets, JSP or by your hand JSF pages.

But no, you are pretty lazy and also you are saw some videos using the new NetBeans VisualWebPack and you want to try its visual component and its easy to use.

What?... you can bind components (tables, labels, text fiels, droplist, ...) to tables or SELECTions, but what about my business layer? what can I do with all my previous job?

I was looking for "binding ejb to jsf" and here are some interesting links which points to some blueprints solutions:
JSF + Session Facade + Entity Bean (EJB3)
JSF combined with EJB, how??

but finally I found (not that I want but at least):
Creator/Vsual Web Pack sources are available in Netbeans repository

It seems in NB 5.5 it is not possible bind an EJB3 to a JSF visual component, but it will be possible in NB6.

Visual Web Development in NetBeans
Visual Web Consumer of Enterprise Java Beans from Services Tab

Finally, I want to point to Project Woodstock: ...an Open Source library of JSF components that provide modern presentations, including the use of AJAX. And borrowed from here: Woodstock is a new Java.net open source project that provides an extensive set of JavaServer Faces (JSF) components for web application developers to build enterprise level applications, and can be fully drag-and-drop enabled within NetBeans 5.5 and its Visual Web Pack.

Sunday, January 28, 2007

Lookup extensions

Thanks to Emilian Bold, who appoints me a reference to this page Lookup and Service Installation, I am able to do a little more things.
The question comes when you have many status line elements and want to show them in a desired order. The answer is in this Emilian's blog entry and also in the later link.

Saturday, December 02, 2006

NetBeans with your preferred LAF

To all that want to change the default LookAndFeel of its NetBeans:
Can I run NetBeans with a custom look and feel?
Also you can take a look at the themes way here.