SOURCECODE |
How to... change the standard ok or cancel button behaviour
Description:
A FormSheet initially has two buttons, one labeled "ok" and one with "cancel" on it. But they don't do anything by default, so you have to define their behaviour.
There are mainly three ways to define the behaviour of the buttons. One is to create your own FormSheet and implement the methods ok()
and cancel()
and the other one is to remove the buttons and add your own ones with a FormSheetContentCreator
The second one is more commonly used, because it is less effort to add two buttons instead of creating lots of new FormSheets just to define the behaviour of a single click on a button.
The third one is almost as common but due to the lack of influence on the button's look and feel less used. It's because here you only set an action to the standard button by resolving it with getButton(int id)
and using setAction(Action action)
on it. The button's ids are stored as static int in the FormSheet, where BTNID_CANCEL stands for the cancel button and BTNID_OK for the ok button.
In order to make the behaviour serializable you have to define it within a FormSheetContentCreator. Otherwise the information will be lost after loading. It is also possible to alter the buttons design and caption when adding new ones.
Our example is taken from the EditCustomersProcess of "How to... display Users in a JUserListBox"
ToDo's:
- Initialize a new FormSheet.
- Add a new FormSheetContentCreator. It can be defined within the addContentCreator-method like in our example. The FormSheet in the CreateFormSheetContent(FormSheet fs) method will be the one you add the ContentCreator to. As you can see, it is possible to use one FormSheetContentCreator for several FormSheets.
- Remove the OK button with the method
removeButton(int id)
- Add a new one with the addButton method. You can use it like in this example to crate a new button or with a FormSheet.FormButton that you initialized before.
- And set a new sale.Action to the CANCEL button by resolving it with the
getButton(int id)
The action defined in that button is the most important thing. Here you add the behaviour of it. In our case we assigned a Transition to a Gate.
Uses:
FormSheet
1
FormSheet fs = new FormSheet("UserEditor", null);
2
// Add the buttons with a FormSheetContentCreator
fs.addContentCreator(new FormSheetContentCreator() {
protected void createFormSheetContent(FormSheet fs) {
3
//remove the OK button for demonstration purpose
//it is being added with a new sale.Action below
fs.removeButton(fs.BTNID_OK);
4
// add a new "Ok"-button
fs.addButton ("Ok", 102, new sale.Action() { //the behaviour is defined rigth here
public void doAction (SaleProcess p, SalesPoint sp) {
uig.setNextTransition(GateChangeTransition.CHANGE_TO_COMMIT_GATE);
}
});
5
// add a new action to the CANCEL button
fs.getButton(fs.BTNID_CANCEL).setAction(new sale.Action() {//the behaviour is defined rigth here
public void doAction (SaleProcess p, SalesPoint sp) {
uig.setNextTransition(GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
}
});
}
});