ImageListener: Adding imageFocused/Activated event would be nice

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

ImageListener: Adding imageFocused/Activated event would be nice

Wolfgang Gross
Hi all,

plugins that implement the ImageListener interface are notified when an image
is Opened/Closed/Updated. In addition, it would be nice to get notified
immediately when an image gets the focus so plugins can keep track of the
current image.
Calling WindowManager.getCurrentImage() when the plugin itself gains focus is
not what I need.

Regards
Wolfgang
--
Dr. W. Gross, Chirurgische Klinik I, Exp. Chirurgie, Uniklinik
Heidelberg
Im Neuenheimer Feld 365, D-69120 Heidelberg, Germany
Tel. ++49 (0)6221/566392, Fax: ++49 (0)6221/564208
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

Michael Ellis
Dear Wolfgang, I wanted to to do this too for my plugi. I solved the  
problem with the AWT toolkit AWTEventListener

In my plugIn's constructor I have:

this.getToolkit().addAWTEventListener(this,  
AWTEvent.WINDOW_FOCUS_EVENT_MASK);

When my PlugIn finishes I call

this.getToolkit().removeAWTEventListener(this);


My plugin implements the AWTEventListener interface with:


        /**
         * Invoked by AWT when windows are opened, closed, made active etc
         *
         * @see  
java.awt.event.AWTEventListener#eventDispatched(java.awt.AWTEvent)
         */
        public void eventDispatched(AWTEvent e) {
                try {
                        if (e.getID() == WindowEvent.WINDOW_GAINED_FOCUS) {
                        // do things
                        }
                } catch (Throwable t) {
                }
        }

Hope this is of some help.

Regards -- Michael Ellis

Does anyone know of a more ImageJ specific way to tackle this situation?

On 1 Jul 2010, at 09:27, Wolfgang Gross wrote:

> Hi all,
>
> plugins that implement the ImageListener interface are notified when  
> an image
> is Opened/Closed/Updated. In addition, it would be nice to get  
> notified
> immediately when an image gets the focus so plugins can keep track  
> of the
> current image.
> Calling WindowManager.getCurrentImage() when the plugin itself gains  
> focus is
> not what I need.
>
> Regards
> Wolfgang
> --
> Dr. W. Gross, Chirurgische Klinik I, Exp. Chirurgie, Uniklinik
> Heidelberg
> Im Neuenheimer Feld 365, D-69120 Heidelberg, Germany
> Tel. ++49 (0)6221/566392, Fax: ++49 (0)6221/564208
> [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

dscho
In reply to this post by Wolfgang Gross
Hi,

On Thu, 1 Jul 2010, Wolfgang Gross wrote:

> plugins that implement the ImageListener interface are notified when an
> image is Opened/Closed/Updated. In addition, it would be nice to get
> notified immediately when an image gets the focus so plugins can keep
> track of the current image. Calling WindowManager.getCurrentImage() when
> the plugin itself gains focus is not what I need.

Why don't you just register a WindowListener upon imageOpened() which you
unregister upon imageClosed()?

That would be backwards compatible, even if a newer ImageJ version
acquires imageFocused()/imageActivated() methods in the ImageListener
interface: older ImageJ versions would just throw an exception because
_their_ ImageListener interface knows nothing about the new methods.

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

Michael Ellis
In reply to this post by Wolfgang Gross
As a followup I here's a simple but complete plugin that illustrates  
things

package DSUKplugins;

import java.awt.AWTEvent;
import java.awt.event.AWTEventListener;
import java.awt.event.WindowEvent;

import ij.IJ;
import ij.plugin.frame.PlugInFrame;

/**
  * Test_PlunginFrame Illustrate how a PlugIn can detect when there is a
  * change in window focus
  *
  * @author Michael Ellis, DSUK Ltd.
  */
public class Test_PlunginFrame extends PlugInFrame implements  
AWTEventListener {

        public Test_PlunginFrame() {
                super("Test_PluginFrame");
                this.getToolkit().addAWTEventListener(this,  
AWTEvent.WINDOW_FOCUS_EVENT_MASK);
                this.setBounds(100, 100, 300, 100);
                this.setVisible(true);
        }

        public void close() {
                this.getToolkit().removeAWTEventListener(this);
                super.close();
        }

        public void eventDispatched(AWTEvent event) {
                try {
                        if (event.getID() == WindowEvent.WINDOW_GAINED_FOCUS) {
                                IJ.log("event " + event.getID() + " " + event.getSource());
                        }
                } catch (Throwable t) {
                }
        }

}



On 1 Jul 2010, at 09:27, Wolfgang Gross wrote:

> Hi all,
>
> plugins that implement the ImageListener interface are notified when  
> an image
> is Opened/Closed/Updated. In addition, it would be nice to get  
> notified
> immediately when an image gets the focus so plugins can keep track  
> of the
> current image.
> Calling WindowManager.getCurrentImage() when the plugin itself gains  
> focus is
> not what I need.
>
> Regards
> Wolfgang
> --
> Dr. W. Gross, Chirurgische Klinik I, Exp. Chirurgie, Uniklinik
> Heidelberg
> Im Neuenheimer Feld 365, D-69120 Heidelberg, Germany
> Tel. ++49 (0)6221/566392, Fax: ++49 (0)6221/564208
> [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

Wolfgang Gross
Thanks to Michael Ellis and Johannes Schindelin for their rapid replies.

As a summary:
As Johannes has pointed out, listening to all AWT events might have a rather
heavy performance footprint. But then, most PCs today stay in in idle mode for
more than 99%.
Adding a special windowslistener on opening new images still leaves the task of
handling all images already open when the plugin gets invoked.

Adding a ImageFocused event to the ImageListener interface as supposed in the
OP gives problems with backward compatibility, ok, but lets get "kicking and
screaming into the century of the fruitbat" (copyright with Terry Pratchett).

Thanks again
Wolfgang
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

Michael Schmid
Hi Wolfgang,

maybe I don't understand the problem correctly, but what about this:
When the plugin is opened, get the list of open windows from the  
WindowManager and register as a java.awt.event.WindowFocusListener  
for each of them.
Then use the ImageJ WindowListener interface to get WindowOpened/
closed events; register as WindowFocusListener for images that will  
be opened, and deregister on windows that will be closed.

Michael
________________________________________________________________

On 1 Jul 2010, at 17:44, Wolfgang Gross wrote:


> Thanks to Michael Ellis and Johannes Schindelin for their rapid  
> replies.
>
> As a summary:
> As Johannes has pointed out, listening to all AWT events might have  
> a rather
> heavy performance footprint. But then, most PCs today stay in in  
> idle mode for
> more than 99%.
> Adding a special windowslistener on opening new images still leaves  
> the task of
> handling all images already open when the plugin gets invoked.
>
> Adding a ImageFocused event to the ImageListener interface as  
> supposed in the
> OP gives problems with backward compatibility, ok, but lets get  
> "kicking and
> screaming into the century of the fruitbat" (copyright with Terry  
> Pratchett).
>
> Thanks again
> Wolfgang
Reply | Threaded
Open this post in threaded view
|

Re: ImageListener: Adding imageFocused/Activated event would be nice

Wolfgang Gross
In reply to this post by Wolfgang Gross
Hi Michael

thanks for your reply. For comments see below

On Thu, 1 Jul 2010 21:08:54 +0200, Michael Schmid <[hidden email]> wrote:

>Hi Wolfgang,
>
>maybe I don't understand the problem correctly, but what about this:
>When the plugin is opened, get the list of open windows from the
>WindowManager and register as a java.awt.event.WindowFocusListener
>for each of them.
Yes, not too much work
>Then use the ImageJ WindowListener interface to get WindowOpened/
>closed events; register as WindowFocusListener for images that will
>be opened, and deregister on windows that will be closed.
Could do this, but then I don't need the ImageListener. But the nice thing with ImageListener is that it uses the static field listeners in modul ImagePlus and hence works automatically on all images, be they open at plugin invcation or not.
Thats why I came up, rather naively, with the idea of proposing the ImageFocused event to be added.
I did it low-key, though (see subject line).

Wolfgang