SOURCECODE

How to... define an Action for a Button in a Component


Description:
The reason for adding buttons to your application is of course to let the user interact with it.
By default, buttons don't have any functions, so you have to add a sale.Action to the button either by the method setAction(Action aAction) provided by the button or by already initializing the button with an action.
There are many ways the assemble the button. In this example you can see all the single steps and how to do it in one step.
Remember to put all this into a FormSheetContentCreator in order to have the Actions serialized as you save the Shop's status. Otherwise all the information will be lost after loading and the buttons will be useless.

ToDo's:
  1. initialize the new button without only the caption and buttonID and no action (you could give it one, but it will be changed by the addAction() call)
  2. define the sale.Action. This is an interface and you have to implement the doAction(SaleProcess p, SalesPoint sp) method
  3. call FormSheet.Button.setAction(Action a) with the action created above
  4. add the button to the FormSheet

All this can be done in just one step like in 5

Uses:
FormSheetContentCreator  FormSheet  



SourceCode

import sale.*;
import data.Currency;
import data.ooimpl.*;
import data.stdforms.*;


//The best Place for adding Actions to buttons is the FormSheeContentCreator
//It ensures the serialization of the actions so they won't be lost after saving the Shop's status
public class DefaultCounterFormCreator extends FormSheetContentCreator {
    
    public DefaultCounterFormCreator() {
        super();
    }
    
    //The method you have to implement in order to add the FormSheetContent
    public void createFormSheetContent(FormSheet map) {
    
        CountingStockImpl cs = (CountingStockImpl)Shop.getTheShop().getStock("Video-Countingstock");
        
        FormSheet fs = SingleTableFormSheet.create( "Available Videocassettes",
                                                    cs,
                                                    null,
                                                    new OfferTED(false));
        
        map.setComponent(fs.getComponent());
        
        //remove the buttons to be able to add totally new ones
        map.removeAllButtons();
        
         1
        //define the FormSheetFormButton with its caption and buttonID
        FormSheet.FormButton b = new FormSheet.FormButton("give back", 2, null);
        
         2
        //define a new sale.Action
        sale.Action act = new sale.Action() {
            public void doAction(SaleProcess p, SalesPoint s) {
                s.runProcess(new GiveBackProcess());
            }
        };
        
         3
        //set the Action at the Button
        b.setAction(act);
        
         4
        //add the Button to the FormSheet
        map.addButton (b);
        
         5
        //all the steps above as one method call
        map.addButton ("rent", 1, new sale.Action() { //a new instance of sale.Action
            public void doAction(SaleProcess p, SalesPoint s) { //initialized and implemented at the same time
                //the action itself
                s.runProcess(new RentProcess());
            }
        });
    }
}