PDA

View Full Version : action listener for button in spring-rcp using command


gohanz
Sep 21st, 2004, 10:28 PM
is there an action listener for button in spring-rcp that is different from the standard swing's action listener, which using the command or something else?

or i should just use the standard's one?

ulil
Sep 21st, 2004, 10:47 PM
Ya.. can i use action listener for button in spring-rcp? How?
Can somebody help me plz?

Thx

Keith Donald
Sep 21st, 2004, 10:49 PM
If all you need is a single button to invoke a single executable action, just adding a ActionListener to the button is the simplest thing to do.

However, if you need the same action to be invoked by multiple buttons, possibly as part of different button groups (like a button bar vs a popup menu), then the Spring Rich Command framework becomes attractive (org.springframework.richclient.command.*)

For Example:


ActionCommand aExecutableCommand = new ActionCommand() {
protected void doExecuteCommand() {
// command execution logic
}
}
aExecutableCommand.setLabel("&Command Label With Mnemonic and Accelerator@Ctrl M");

JButton aButton = aExecutableCommand.createButton();

JMenuItem aMenuItem = aExecutableCommand.createMenuItem();



Clicking the button or menu item triggers the same aExecutableCommand to be executed. Disabling the command disables all buttons. You get the idea.

If you need a swing Action instance, for example for component action binding, you may call:



Action action = aExecutableCommand.getActionAdapter();

ulil
Sep 21st, 2004, 11:09 PM
Thx for ur help.
Its work

gohanz
Sep 22nd, 2004, 03:02 AM
hi,

i've put the command in the command-context.xml, and i retrieve the command through this :

ActionCommand ac = Application.instance().getActiveWindow().getComman dRegistry().getActionCommand("myCommand");

and when i create a button from it, just like you said, nullpointerexception occured. I create the button inside my view class and call the command from there.
but when i put the command inside one of the bar (menubar, toolbar), the error doesn't occured. while i don't want to put it there. can u help me?

thanks

Keith Donald
Sep 22nd, 2004, 02:51 PM
Is the command truly a global (e.g window-scoped) command? If not, there is no reason to declare it in the commands-context.xml. Commands declared there are registered in the instanting window's command registry. These typically consist of:

1. Targetable commands, who's executing implementation can be changed by registering a command executor. For example, "delete", "selectAll", or "properties."

2. Other commands used by the instantiaing window's command groups (bars): menubars, toolbar, etc.

If you command is local to a view - for example, just a button on a form or a local popup menu, there is no reason to use the global command registry.

If on the other hand, you command IS needed by multiple views - or you want to create a button on a form that, when executed, executes a command accessible in one of the window's command bars, then you need to access the window's command registry. The simplest way to do this is to use the AbstractView helper code:


ActionCommand myCommand = getCommandManager().getActionCommand("myCommand");
myCommand.createButton();


Do you know what is causing the null pointer exception? Is the lookup for "myCommand" returning null in your case? The id you provide must match the bean id of a ActionCommand bean definition in your commands-context.xml. Alternatively, if you command is a global targetable command, declare its definition in the window command manager's globalCommandIds property.

gohanz
Sep 23rd, 2004, 12:21 AM
hi,

thanks a lot for the detail definition. it brights up my life, hehehe :wink:
it really really helps ... :)

skumarsamy
May 28th, 2006, 12:03 PM
Hi,

I am trying to use the Spring RCP, looking into it i am earlier with JGoodies implementation, now looking at RCP, here is have problem with buttons showing commandLabel, how and where i need to set the labels please.

thanks
kumar

lstreepy
May 29th, 2006, 11:43 AM
The text displayed on the command button or in a menu item is controlled by a convention in the platform configuration and properties in the messages.properties file.

By default, the platform tries to find a message property with the key <commandId>.label. If that is found, then the associated text is used for the button text.

This all happens automagically inside the guts of the framework, so you don't have to do anything special to get the button text configured. Just provide the needed message property and you're all set.

Larry.

skumarsamy
Jun 1st, 2006, 04:29 AM
Hi,

If i have the command properties and commandId defined then what you said will be ok. But this one I have with in my form class, and it is not global command that i need to put under the properties and not tied it with commandId. In such case how I can get the message properties to be said for my button.

expecting your help.
thanks
kumar

lstreepy
Jun 1st, 2006, 08:16 AM
Here's an example of creating a command in user code (taken from AbstractDetailForm):

protected ActionCommand createCancelCommand() {
String commandId = getCancelCommandFaceDescriptorId();
if( !StringUtils.hasText( commandId ) ) {
return null;
}
ActionCommand command = new ActionCommand( commandId ) {
protected void doExecuteCommand() {
AbstractDetailForm.this.reset();
AbstractDetailForm.this.setEnabled( false );
setEditingNewFormObject( false );
setEditingFormObjectIndexSilently( -1 );
setEditState( STATE_CLEAR );
setFormObject( null );
}
};
return (ActionCommand) getCommandConfigurer().configure( command );
}


The basic strategy is to construct the ActionCommand with your desired command Id and then invoke the object configurer on the new command. This (call to configure) is necessary for any command (any object actually) that you create outside the application or command contexts. Any object created in the contexts is automatically configured by a bean post-processor.

HTH,
Larry.

skumarsamy
Jun 1st, 2006, 09:33 AM
Hi Larry.

Thanks for your help, here with i have attached my code and messages file, i have done the same way of getting the labels, but it is not showing, also i am not able to get the label text's also.

some were i missing, i am bit confused with the framework, also it bothers me as i have the whole application running in Swing+JGoodies+ejb invokes. I thought of switching the application to this new framework as i need to run behind firewall using the remoting.

Will you please help me get working.

thanks
kumar

skumarsamy
Jun 1st, 2006, 09:38 AM
here are the files

lstreepy
Jun 1st, 2006, 11:29 AM
In your messages.properties file you have this:

search=&Search
help=&Help

You forgot the ".label" on the property key. Try this:

search.label=&Search
help.label=&Help

Larry.

skumarsamy
Jun 3rd, 2006, 07:56 AM
Thanks Larry,
It worked perfectly, but the other labels in the same file doesn't reflect the values from the message.properties. How i can do this. Should i need to pad them with .label i did that too, still it is not coming except the buttons.

thanks
kumar

lstreepy
Jun 3rd, 2006, 08:14 AM
There are a number of issues with the code you posted.

1. You don't seem to be using any bindings in the form. That's ok if you really want it that way, but it's not very common. You should look at the various sample applications for examples of using data bindings to make the form data easier to access.

2. Related to 1, you should look at TableLayoutBuilder for creating your form layout. It's got a lot of helper functions to make things easier. Especially look at things like add() and row(). You might want to examine ContactForm.createFormControl() in the simple sample.

3. If you do 1 and 2, you won't have to construct your labels and text fields, the bindings will take care of that for you.

4. You are currently creating JLabels with names like "ic"+"number" (which is an odd way of constructing the string "icnumber"). Since you use the ComponentFactory to create the label, it will be automatically configured using If you look in the messages file, there is no corresponding "icnumber" entry. This is true for most of the lablel strings you created. Note that the message keys don't use the ".label" suffix because constructing a JLabel directly using the component factory assumes that the the text you passed in is the exact key for the message property. When you use bindings, the message key is constructed from the property name and ".property" (actually the automatic label lookup uses two keys: <formId>.<propertyName>.label and then <propertyName>.label).

5. If you use bindings, as suggested in 1-3, then the message key will be determined from the property name. So, if you call add( "icnumber" ), you will get a label that uses the text from the message key "icnumber.label" and the text field will be created automatically.

Seriously, you should study the sample applications. It will help you understand the mechanics of form construction.

HTH,
Larry.