cancel
Showing results for 
Search instead for 
Did you mean: 

Action used multiple times

matthew_stroh
Explorer
0 Kudos

I read that you can use an Action more than once on a UI. If this is done what would the handler code look like. I was going to have 2 buttons with the same HandleButtonPush action, but I was unsure how to code the event to determine which button was pushed.

Thanks for any help.

Matthew

Accepted Solutions (0)

Answers (7)

Answers (7)

Former Member
0 Kudos

Hi all,

as Thomas described, we want to have a clear separation between UI and eventhandler. This makes things sometimes a little tricky. But here is a clear solution for the question how to distinguish between 2 buttons when using the same eventhandler:

In method wdDoModifyView (sorry, in Netweaver 05 you will also be able to do the following from within the IDE) you can add parameters to UI events. For a button you can do this as follows:

IWDButton myButton = (IWDButton)view.getElement(“theSaveButton”);

myButton.mappingOfOnAction.addParameter(“command”, “SAVE”);

IWDButton myButton = (IWDButton)view.getElement(“theDeleteButton”);

myButton.mappingOfOnAction.addParameter(“command”, “DELETE”);

Now you can declare your event handler having 1 parameter called command of type java.lang.String. The implementation could be like this

if (command.equals(“SAVE”))

{

// do something for save

}

else if (command.equals(“DELETE”))

{

// do something for delete

}

Former Member
0 Kudos

Hi Reiner,

thank you very much for the answer, but nobody really discusses the UI/Event handler separation. Is an action already tightly coupled to the UI, even more than a plug?

For inbound plugs in an InterfaceView for example i can define a generic event handler and will get the plug name which triggered the event in event.getName().

For actions which use one common event handler i get the event handler name in event.getName()? From my point of view action names will (or at least should) reflect the "functionality" behind it, i'll have a Save-, Delete- and Edit-action for example. This has nothing to do with the UI elements which will probably fire that actions.

Where's the difference in separation here?

  public void onActionGeneric(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    if (wdEvent.getName().equals("Save")) {
    } else if (wdEvent.getName().equals("Delete")) {
    }
  }

against

  public void onActionGeneric(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent, String command )
  {
    if (command.equals("Save")) {
    } else if (command.equals("Delete")) {
    }
  }

The only issue i could imagine is, that the wdEvent.getName() doesn't return the event name, as supposed, but this is also not the case for inbound plugs, so what's wrong now?

But maybe i'm still missing something here and you could point me to the right direction

Best regards

Stefan

Former Member
0 Kudos

Hi Thomas,

Though I don't agree with you completely, what am trying to point out is the value that the method wdEvent.getName() returns. It is suuposed to return the name of the event that was raised. But it seems to be returning the name of the event handler, which is obviously known at that point of time. I think this needs to be rectified.

Thanks

Shakeel

thomas_chadzelek
Participant
0 Kudos

Hello,

I just want to point out that Web Dynpro tries to separate the event handling logic from the UI. Thus the ID of the button that caused the event is intentionally not available to the event handler. Web Dynpro does not consider it a good idea to deal with UI element IDs inside the event handler.

Thus my recommendation is to use something "more semantic" as the additional parameter to your event handler. E.g. the name of the function to perform or such; not the technical ID of that button.

Best regards,

Thomas

Former Member
0 Kudos

Hi Thomas,

sorry, but the point is <b>not</b> to use the button or any other UI element id but the <u>action name</u> as the name transported in IWDCustomEvent in case of action event handlers. The name of the event handler does not really make sense here since i get the name of the handler which i'm already "inside".

Transporting the action name would be a similar behaviour compared to an generic event handler which handles multiple inbound plugs. The event.name of the inbound plug gets the plug name which actually invoked the handler.

I think, this would not break the separation of event handling/UI and the actions names will be sufficiently "semantic" in most cases.

Best regards

Stefan

Former Member
0 Kudos

Thanks Stefan!

This reference does help.

But don't you think the method wdEvent.getName() should be returning the name of the event rather than the event handler? I think it's a bug.

Had this been returning the name of the event then I guess the job would have become much simpler.

Regards

Shakeel

Former Member
0 Kudos

Hi Shakeel

yes, i agree. It would be more straightforward, since you are getting the inbound plug name in case of event handlers for inbound plugs also.

Regards

Stefan

matthew_stroh
Explorer
0 Kudos

I was trying to follow the advice from the author of the pdf "How To Build a Web Dynpro Application". The paragraph below is from that document. I am starting to think I miss interpreted the paragraph. I was thinking he was saying that I could use the same event and action handler for all my buttons on a UI and then interpret which button was pushed in the handler. I guess the sentence that contains "actions can be made generic" is where I missed interpreted what he is saying. Making an event for each button (maybe an handler for each event) is the way to go. I was just curious if my first idea was possible.

Notice that the action is named after the event that triggers it, not the functionality that results as a consequence of its execution. The reason for this is that actions can be made generic. It is perfectly possible to assign the same action to multiple UI elements. This means that the action handler must behave in a generic manner. Although this example only uses one action, it is good to form the habit of naming the action after the type of event that triggers it, rather than the functionality that occurs.

Former Member
0 Kudos

Hi Matthew,

using one action for multiple buttons is also possible, but needs at little programmatic effort. The action handler has to provide an extra parameter (for example a String buttonID) which can be set for each button using button.mappingOfOnAction().addParameter("buttonID", IDofButton); This has to be done in the wdDoModifyView() method, since you can get references to UI elements there only. The event handler will get the parameter and you can check the button ID there and decide what to do.

Here's some official documentation: http://help.sap.com/saphelp_nw04/helpdata/en/25/d41a403233dd5fe10000000a155106/content.htm

Hope that helps.

Regards

Stefan

Former Member
0 Kudos

Hi Matthew,

I would rather use 2 actions in this case, with onAction handlers calling a private method with one parameter.

The nice thing about binding actions to UI element's events is that you can e.g. have a button that triggers an action, and this action is also used when the user hits the Enter key on an input field or chooses a new value on a dropdown.

cheers

Walter

Former Member
0 Kudos

Hi Matthew,

You can use a given action handler for more than UI element by assigning the same action handler to different events. To identify the UI element, use the method wdEvent.getName() to retrieve the name of the event that invoked the action handler. This way you can easily identify the UI element that raised the event.... provided you have created different events and assigned them to different UI elemnts(but with same action handler).

Is this what you are looking at?

Regards

Shakeel

matthew_stroh
Explorer
0 Kudos

When I debuged then handler, wdEvent.getName() returned the name of the handler and not the event. Since this is only my second week of using Web Dynpro maybe somebody else should verify what I saw.

Matthew