Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Model View Presenter: how to use with a command
Model View Presenter: how to use with a command [message #486816] Sat, 19 September 2009 13:41 Go to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 92
Registered: July 2009
Member
Hello everyone

I am still working on a reference implementation for myself using the
model-view-controller pattern in a rcp. So I set up a couple of projects

rcp.mvp.view // the actual rcp
rcp.mvp.presenter
rcp.mvp.data // eclipselink and DAOs
rcp.mvp.data.mysql // fragment providing the mysql driver
rcp.mvp.model // just my model pojos

My question now gos for the view and the presenter. According to what I know
about MVP, when a view (that implements a interface the presenter also knows)
gets created, it creates a presenter object and passes itself to it (so the
presenter can call the view as well, using the interface).
So now my eclipse-views are having a reference a particular presenter.

Now comes the problem: when I use a command, I specify a command handler which
executes the command. This is usually a separate class that has now reference to
my view.
How can I call my presenter from the commandhandler, in a best-practise way,
since I dont want to have any businesslogic in my view-project?

The only idea I got, is making my view implementing IHandler2 and use it as
commandhandler for all commands that can occur for it. But this doesnt sound
right to me.

regards
Marc
Re: Model View Presenter: how to use with a command [message #486911 is a reply to message #486816] Sun, 20 September 2009 22:15 Go to previous messageGo to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 92
Registered: July 2009
Member
Ok, I found one way which works for now.

From the CommandHandler I just get a reference to my view which has a public
method getPresenter.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IViewDescriptor viewDesc =
PlatformUI.getWorkbench().getViewRegistry().find(CustomerExp lorerView.ID);
try {
CustomerExplorerView view = (CustomerExplorerView)viewDesc.createView();
view.getPresenter().createNewCustomer();
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello from NewCustomerHandler");
return null;
}

I am not sure if this can be called best practise but I havent found another way
yet. I tried Parameterized Commands as well but not very successful. The only
thing I currently dont like about my approach is that the presenter can be
called from anywhere in the workbench just by getting a reference to the view.

I was reading a lot about MVP recently and that people are using it successful
with an rcp application. I am keen to know how they are handling this.

regards
Marc

Marc Schlegel schrieb:
> Hello everyone
>
> I am still working on a reference implementation for myself using the
> model-view-controller pattern in a rcp. So I set up a couple of projects
>
> rcp.mvp.view // the actual rcp
> rcp.mvp.presenter
> rcp.mvp.data // eclipselink and DAOs
> rcp.mvp.data.mysql // fragment providing the mysql driver
> rcp.mvp.model // just my model pojos
>
> My question now gos for the view and the presenter. According to what I know
> about MVP, when a view (that implements a interface the presenter also knows)
> gets created, it creates a presenter object and passes itself to it (so the
> presenter can call the view as well, using the interface).
> So now my eclipse-views are having a reference a particular presenter.
>
> Now comes the problem: when I use a command, I specify a command handler which
> executes the command. This is usually a separate class that has now reference to
> my view.
> How can I call my presenter from the commandhandler, in a best-practise way,
> since I dont want to have any businesslogic in my view-project?
>
> The only idea I got, is making my view implementing IHandler2 and use it as
> commandhandler for all commands that can occur for it. But this doesnt sound
> right to me.
>
> regards
> Marc
Re: Model View Presenter: how to use with a command [message #486928 is a reply to message #486911] Mon, 21 September 2009 05:56 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
Marc Schlegel wrote:
> Ok, I found one way which works for now.
>
> From the CommandHandler I just get a reference to my view which has a public
> method getPresenter.
>
> @Override
> public Object execute(ExecutionEvent event) throws ExecutionException {
> IViewDescriptor viewDesc =
> PlatformUI.getWorkbench().getViewRegistry().find(CustomerExp lorerView.ID);
> try {
> CustomerExplorerView view = (CustomerExplorerView)viewDesc.createView();
> view.getPresenter().createNewCustomer();
> } catch (CoreException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> System.out.println("Hello from NewCustomerHandler");
> return null;
> }
>
> I am not sure if this can be called best practise but I havent found another way
> yet. I tried Parameterized Commands as well but not very successful. The only
> thing I currently dont like about my approach is that the presenter can be
> called from anywhere in the workbench just by getting a reference to the view.

You might consider to take advantage of the ISourceProvider:

http://blog.eclipse-tips.com/2009/02/commands-part-5-authent ication-in-rcp.html

You can access this via the ISourceProviderService inside the
AbstractHandler.execute method.

HTH && Greetings from Bremen,

Daniel Krügler
Re: Model View Presenter: how to use with a command [message #487099 is a reply to message #486928] Mon, 21 September 2009 20:53 Go to previous message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 92
Registered: July 2009
Member
Daniel Krügler schrieb:
>
> You might consider to take advantage of the ISourceProvider:
>
> http://blog.eclipse-tips.com/2009/02/commands-part-5-authent ication-in-rcp.html
>
>
> You can access this via the ISourceProviderService inside the
> AbstractHandler.execute method.
>
> HTH && Greetings from Bremen,
>
> Daniel Krügler
>

Thanks for the idea. I implemented a SourceProvider as follows

public class PresenterSourceProvider extends AbstractSourceProvider {

public static final String PRESENTERS = "rcp.mvp.view.presenters";
private Map <String, Object>presenters;

public PresenterSourceProvider() {
presenters = new HashMap<String, Object>();
}

@SuppressWarnings("unchecked")
@Override
public Map getCurrentState() {
return presenters;
}

@Override
public String[] getProvidedSourceNames() {
return new String[]{PRESENTERS};
}

public void addPresenter(String id, Object o){
presenters.put(id, o);
}

public Object getPresenter(String id){
return presenters.get(id);
}

public void removePresenter(String id){
presenters.remove(id);
}

@Override
public void dispose() {
}
}

In the Handler

IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
ISourceProviderService service = (ISourceProviderService)
window.getService(ISourceProviderService.class);
PresenterSourceProvider presenterSourceProvider = (PresenterSourceProvider)
service.getSourceProvider(PresenterSourceProvider.PRESENTERS );
CustomerPresenter p =
(CustomerPresenter)presenterSourceProvider.getPresenter(Cust omerExplorerView.ID);
p.createNewCustomer();

In the view I am overriding dispose, so the presenter can be removed from the
sourceprovider when the view gets destroyed.

This works, but its quite a lot of work to get a simple reference.
As far as I understand, the SourceProviders are more important when you want to
use Expressions with the defined variables.

A probably easier way would be to define a global (thread-safe) singleton with
the same Map. But I doubt that this would be best-practise since I am reading a
lot about "bad" singeltons.

It would be really nice to get some feedback from people that have successfully
adopted the MVP together with the command framework.
In my opinion it is important to have a easy-to-understand solution, so that in
a team, new developers can work right away with the designed structure. The
SourceProvider approach seems more difficult (create presenter in view, add to
the sourceprovider, dont forget to remove it on view-dispose, ...).

regards
Marc
Previous Topic:How to disable built-in shortcut keys provided by eclipse
Next Topic:RCP hangs
Goto Forum:
  


Current Time: Wed Apr 24 22:27:43 GMT 2024

Powered by FUDForum. Page generated in 0.02992 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top