Unregistering listeners using CyServiceRegistrar

82 views
Skip to first unread message

Omar Wagih

unread,
Sep 15, 2012, 7:28:37 PM9/15/12
to cytoscap...@googlegroups.com
I have my custom event class which implements several listeners
The handleEvent methods of the class depends on a JTree which is created after files are loaded through a custom JFrame. Thus, I can't register the listeners in CyActivator.
I instantiated the class, and passed references of the class + a reference of CyServiceRegistrar through to where my JFrame is being created. 
After my files are loaded and my JTree is created, I set the JTree in my custom event listener class like this: myListeners.setTree(myJTree) and register the listeners.

However, I run into this issue: 
In the case that someone loads JFrame file loader from the app menu to load the the files a second time, the listeners are registered again and thus my handleEvent methods are called more than once.

I tried unregistering all services when my custom file loader JFrame is opened like this: serviceRegistrar.unregisterAllServices(myListeners);, but it fails

Any idea what's going on?

Thanks

Jason Montojo

unread,
Sep 17, 2012, 11:09:42 AM9/17/12
to cytoscap...@googlegroups.com
Hi Omar,

I usually get around that issue by reusing the JFrame instance.  For example, your menu item could be a CyAction that lazily instantiates the JFrame.  If the JFrame already exists, it just re-displays it.  That way, your JFrame is created exactly once and the services are registered only once.

Hope this helps,
Jason


--
You received this message because you are subscribed to the Google Groups "cytoscape-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cytoscape-discuss/-/JrM2K7WTPQYJ.
To post to this group, send email to cytoscap...@googlegroups.com.
To unsubscribe from this group, send email to cytoscape-disc...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cytoscape-discuss?hl=en.

Omar Wagih

unread,
Sep 17, 2012, 2:15:38 PM9/17/12
to cytoscap...@googlegroups.com
Hi Jason,

I dont think this will work in terms of debugging/developing my app.
Every time I rebuild my project and update my bundle in the cytoscape terminal, I want the old listeners to be removed/unregistered and the new one registered. Does this make sense?
I am constantly updating my listeners throughout the development of my app and would hate having to restart Cytoscape in order to see the effect of my new listeners without the redundancy of the old ones

Any alternatives?

Omar

Jason Montojo

unread,
Sep 17, 2012, 2:31:30 PM9/17/12
to cytoscap...@googlegroups.com
Hi Omar,

When you update your bundle, OSGi unloads the old one and loads the new one (i.e. you end up with a new JFrame).  That causes all of the OSGi services registered by your bundle to be forcefully unregistered.  You shouldn't need to restart Cytoscape for your changes to take effect.  If Cytoscape is holding onto references to your old class, then this will definitely not work; for example, if you grab Cytoscape's JMenuBar and add things to that directly.  However, if you're extending Cytoscape strictly using OSGi services (e.g. CyAction, *TaskFactory), you won't run into this problem.

I use the approach I described in the GeneMANIA app and I haven't run into instances where I've had to restart Cytoscape when updating my app.

Hope this helps,
Jason

Omar Wagih

unread,
Sep 18, 2012, 12:24:18 AM9/18/12
to cytoscap...@googlegroups.com
Hi Jason,

I took your exact same approach. 
My JFrame class is instantiated in CyActivator and I have a class which extends AbstractCyAction and on actionPerformed it calls frame.setVisible(true)
This works fine.

Now, In my CyActivator class, I instantiate my listeners class and register my listeners:

myListeners = new AppListeners();
//Register listeners
registerService(bc, myListeners, NetworkViewAddedListener.class, new Properties());

My listeners class prints the following:
public void handleEvent(NetworkViewAddedEvent nvae) {
        System.out.println("Network view add: registering->"+nvae.getNetworkView().getModel().getSUID());
}

If I build my app, launch cytoscape, update and start, when my network is generated I see the output:

Network view add: registering->71

Which is great. Now, If I rebuild changing the print statement to include more arrows, update my bundle and launch my app again, on a network view added event I see 

Cytoscape 3.0.0-M5> update 182
Network view add: registering->>>>71
Network view add: registering->71

Both the old and the new listeners are still there. i.e. the old listeners were not removed which causes the redundancy…

Am I missing something?

Omar

Omar Wagih

unread,
Sep 18, 2012, 1:42:37 AM9/18/12
to cytoscap...@googlegroups.com
Nevermind, I got it to work! 
Just needed one final clean of Cyoscape. 
Thanks so much for all your help!! Greatly appreciated

Omar
On 2012-09-17, at 2:31 PM, Jason Montojo <jrr...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages