|
|
|
Re: Model View Presenter: how to use with a command [message #487099 is a reply to message #486928] |
Mon, 21 September 2009 20:53 |
Marc Schlegel 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
|
|
|
Powered by
FUDForum. Page generated in 0.03026 seconds