cancel
Showing results for 
Search instead for 
Did you mean: 

Get the current view in a non static method

Former Member
0 Kudos

Hello,

I was wondering if the solution i implemented might cause problems with a multi-user environment.

The situation is that i want to dynamically create some UI elements when a certain button is pushed for example. I have to lookup the container to which the buttons have to be added. For this i use the following line:

Group buttonContainer =	(Group) currentView.getElement("topicsButtonGroup");

As u can see, i need an instance of IWDView to use the getElement method. I couldnt find it anywhere so i saved it from the

public static void wdDoModifyView(IPrivateTaskListView wdThis, IPrivateTaskListView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)

method like this:


if (firstTime) {
//keep a reference to the current view
currentView = view;
}

This is done in a static method so the variable is gonna be

private static IWDView currentView

.

Now i can use the view from non-static methods to lookup UI elements.

My only question is then...is this gonna cause any problems or is there another way to solve this?

Much thanks,

Hugo Hendriks

Accepted Solutions (1)

Accepted Solutions (1)

pravesh_verma
Active Contributor
0 Kudos

Hi Hugo,

I have another approach of doing the same thing.

1) Create a attribute in the View context of type: IWDView.

2) In the wdDoModifyView, set this attribute value to the view value of the wdDoModifyView function.

Use this code:

if(firstTime)
  	{
  		wdContext.currentContextElement().setView(view);
..
..
}

now where ever you require the view object just use this value from the context attribute. It is present there globally for all methods of this view.

Code to get the value of this view:


IWDView view = wdContext.currentContextElement().getView();

I hope this will help you. This will also solve your problem of this view object being static and private to wdDoModifyView function.

Regards

Pravesh

Former Member
0 Kudos

Thanks for the fast reply,

Armin, that kinda looks like a strange way to handle this, in my humble opion. You create nice and clean eventhandlers for buttons and then still put all of the functionality into the wdDoModifyView method.

I like Pravesh method more then but doesnt this also create problems? Im just startign with WD so i apologize for asking so much questions but im trying to figure how it all works.

regards,

Hugo Hendriks

pravesh_verma
Active Contributor
0 Kudos

Hi Hugo,

I think this will not create any problem for Non static methods as well. In my project I am creating all my UI elements Dynamically and I am able to use this approach efficienly.

As far as I think, you will not not encounter any probelm using this code. I hope this helps you!!

Regards

Pravesh

PS: Kindly consider rewarding points if helpful or solved.

Former Member
0 Kudos

Hi Pravesh,

ok thanks. Ill change my approach then and try it your way. Thanks you both for the help.

regards,

Hugo

Former Member
0 Kudos

Storing the view reference in the context allows accessing and changing the view in any phase of the request cycle, which may lead to subtle errors.

I would not recommend this.

Armin

Answers (1)

Answers (1)

Former Member
0 Kudos

Don't do this, it will definitely lead to problems.

Instead you can set a context attribute in the button's action handler, say "UpdateView", and query and reset it inside method wdDoModifyView().

void wdDoModifyView(...)
{
  if (wdContext.currentContextElement().getUpdateView())
  {
    /* modify view as needed */
    wdContext.currentContextElement().setUpdateView(false);
  }
}

Armin