Skip to main content



      Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Best code pattern using listeners in RCP views.
Best code pattern using listeners in RCP views. [message #463376] Fri, 09 February 2007 06:03 Go to next message
Eclipse UserFriend
Originally posted by: jdamas.capgemini.fr

Hello.

I'm wondering which is the best code pattern to use when working with
listeners in RCP views (from Workbench SelectionService as well as from
code specific event providers) :

Can the ViewPart implement directly the listeners interfaces and then
"add" and "remove" itself as a listener ?

or

Shall the ViewPart define and use as many "listener" attributes (inner
classes) that implements the listeners interfaces and "add" and "remove"
this classes as listeners ?

The #1 solution bothers me because I will have to code the unregistering
of the view while it is being disposed by the Workbench ... Like this :

public void dispose() {
// unregister the view when the view is disposed
xxx.removeListener(this);
super.dispose();
}

I've found sample code (from eclipse.org and ibm.com) using this 2 code
patterns, may be it's not a concern ??

JM.D
Re: Best code pattern using listeners in RCP views. [message #463390 is a reply to message #463376] Fri, 09 February 2007 16:51 Go to previous messageGo to next message
Eclipse UserFriend
The dispose() code you mention, ...removeListener(this), is no problem, I think; anyway I do do it
myself. As to whether you want your listeners to be fields or whether you want your view(s) to
implement N listener-interfaces -- up to you, I'd say. Implementing clutters the public interface of
your View or whatever; having fields means you have to remember to create them on every path, plus
if they need data from the view, they either need to be non-static, or you have to pass data when
creating them.

Paul
Re: Best code pattern using listeners in RCP views. [message #463449 is a reply to message #463376] Mon, 12 February 2007 08:46 Go to previous messageGo to next message
Eclipse UserFriend
The pattern I see most often is anonymous inner classes remembered in a
field. For example:

private IPartListener2 partListener = null;

private IPartListener2 getPartListener() {
if (partListener==null) {
partListener = new IPartListener2() {
// methods here
];
}
return partListener;
}

and then getPartListener() is used to add the listener to the page at
the appropriate time.


public void dispose() {
if (partListener!=null) {
getSite().getPage().removePartListener(partListener);
partListener = null;
}
super.dispose();
}


There are pluses and minuses to this approach (you don't expose
IPartListener2 on your view, but you are creating another object). If
you have the opportunity, I'd advise you to pick one style and stick
with it :-)

Later,
PW
Re: Best code pattern using listeners in RCP views. [message #463902 is a reply to message #463449] Tue, 20 February 2007 03:20 Go to previous message
Eclipse UserFriend
And then there are the hybrid, where you have a single anonymous class
that implements all the relevant listener interfaces (as long as there
are no conflicts between the interfaces)...

This method lowers the number of class classes in the system, yet keep
the different relevant interfaces apart...

/tonny

Paul Webster wrote:
> The pattern I see most often is anonymous inner classes remembered in a
> field. For example:
>
> private IPartListener2 partListener = null;
>
> private IPartListener2 getPartListener() {
> if (partListener==null) {
> partListener = new IPartListener2() {
> // methods here
> ];
> }
> return partListener;
> }
>
> and then getPartListener() is used to add the listener to the page at
> the appropriate time.
>
>
> public void dispose() {
> if (partListener!=null) {
> getSite().getPage().removePartListener(partListener);
> partListener = null;
> }
> super.dispose();
> }
>
>
> There are pluses and minuses to this approach (you don't expose
> IPartListener2 on your view, but you are creating another object). If
> you have the opportunity, I'd advise you to pick one style and stick
> with it :-)
>
> Later,
> PW
Previous Topic:DataBinding
Next Topic:Tooltips display duration
Goto Forum:
  


Current Time: Thu Mar 27 13:42:33 EDT 2025

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

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

Back to the top