Code snippet for event registering in MM 2.0 plugin

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Code snippet for event registering in MM 2.0 plugin

MatthL
Hello,

I am currently writting a MM plug-in for controlling a custom microscope.

I am a bit lost with the event registering  process in MM 2.0.

For what I have understood from the website MM 2.0 part and from the API, I have to register an event via
// Studio mm_;
mm_.events().registerForEvents( ?? );

but I don't know what to put as an argument and I don't know also how to use the registered event.

I can't find any example in the plugin repertory of the MM github.

Would you please give me a working example ?

Thanks
Matthieu L
University of Angers - France

PS : my goal is to detect an user's double click on a image in order to trigger a movement related to the position of this click.



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Code snippet for event registering in MM 2.0 plugin

Peterbauer Thomas
On 2017-01-26 14:39, MatthL wrote:

I am a bit lost with the event registering  process in MM 2.0.

For what I have understood from the website MM 2.0 part and from the API, I
have to register an event via
// Studio mm_;
mm_.events().registerForEvents( ?? );

but I don't know what to put as an argument and I don't know also how to use
the registered event.

I can't find any example in the plugin repertory of the MM github.

Would you please give me a working example ?



A snippet for a menu plugin listening to startup and shutdown events:

import com.google.common.eventbus.Subscribe;
import org.micromanager.MenuPlugin;
import org.micromanager.Studio;
import org.micromanager.events.ShutdownCommencingEvent;
import org.micromanager.events.StartupCompleteEvent;
import org.scijava.plugin.Plugin;
import org.scijava.plugin.SciJavaPlugin;

@Plugin(type = MenuPlugin.class)
public class OptionsPlugin implements MenuPlugin, SciJavaPlugin {

   @Override
   public void setContext(Studio studio) {
      //will be called prior to loading a configuration
      studio.getEventManager().registerForEvents(this);
   }

   @Subscribe
   public void OnStartup(StartupCompleteEvent ev) {
      //do something;
   }

   @Subscribe
   public void OnShutdown(ShutdownCommencingEvent ev) {
      //clean up something
   }

   //Other methods of the plugin interface here

For each event, the object subscribing as listener has to implement a method without return value (void)
and accepting the event as the sole argument. It has to be annotated with @Subscribe. The name of
the method can be freely chosen.

Best,
Thomas


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Code snippet for event registering in MM 2.0 plugin

Nico Stuurman-2
In reply to this post by MatthL
Hi Matthieu,


> I am currently writting a MM plug-in for controlling a custom microscope.
>
> I am a bit lost with the event registering  process in MM 2.0.
>
>
> PS : my goal is to detect an user's double click on a image in order to
> trigger a movement related to the position of this click.

I don't think that the MM2 api provides an event for that.  However, you
can attach a listener to the display canvas.  You can find an example in
the code for the CenterAndDragListener in
org.micromanager.internal.navigation.  That code is used by the "Mouse
moves stage" menu command.

Best,

Nico



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Code snippet for event registering in MM 2.0 plugin

Peterbauer Thomas


On 2017-01-27 00:39, Nico Stuurman wrote:
>> I am currently writting a MM plug-in for controlling a custom microscope.
>>
>> I am a bit lost with the event registering  process in MM 2.0.
>>
>>
>> PS : my goal is to detect an user's double click on a image in order to
>> trigger a movement related to the position of this click.
> I don't think that the MM2 api provides an event for that.

Something along the line of the following should work with public API
events:

    @Subscribe
    public void OnDisplayAboutToShow(DisplayAboutToShowEvent ev) {
       DisplayWindow dw = studio.getDisplayManager().getCurrentWindow();
       dw.getImagePlus().getWindow().getCanvas().addMouseListener(new
MouseAdapter() {

          @Override
          public void mousePressed(MouseEvent e) {
             double zoom =
studio.getDisplayManager().getCurrentWindow().getZoom();
             int x = (int) (e.getX()/zoom);
             int y = (int) (e.getY()/zoom);
             System.out.println("Mouse pressed at image coordinate x
="+x+" y = "+y);
          }
       });
    }

Most likely, one would like to further narrow down the event to, say,
image snaps (excluding live windows, active acquistions...) and to cache
the DisplayWindow to avoid confusion with others, which may have been
opened between mouse listener registration and mouse click.
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Code snippet for event registering in MM 2.0 plugin

MatthL
Thank you all for this prompt an precise answers

Here is a working example for a basic MM plugin (in two files) implementing the dbl click "callback" on a image :


------------------------------
Plug_TestDblClickImage.java
-------------------------------

package TestDblClickImage;

import mmcorej.CMMCore;
import org.micromanager.MenuPlugin;
import org.micromanager.Studio;

import org.scijava.plugin.Plugin;
import org.scijava.plugin.SciJavaPlugin;



@Plugin(type = MenuPlugin.class)
public class Plug_TestDblClickImage implements MenuPlugin, SciJavaPlugin {

   public static final String menuName = "TestDblClickImage";
   public static final String tooltipDescription = "TestDblClickImage";

   private Studio mm_;
   private CMMCore core_;              
   private JFrame_test myFrame_;

   
    @Override
    public void setContext(Studio studio) {
        mm_ = studio;
        core_ = studio.getCMMCore();          
    }  
   
   @Override
   public String getVersion() {
      return "1.0";
   }
   
   @Override
   public String getCopyright() {
      return "";
   }

    @Override
    public String getName() {
        return "Event Registration Test";
    }

    @Override
    public String getHelpText() {
        return "Event Registration Test";
    }

    @Override
    public String getSubMenu() {
        return "";
    }

    @Override
    public void onPluginSelected() {
      if (myFrame_ == null)
         myFrame_ = new JFrame_test(mm_);
      myFrame_.setVisible(true);
    }
}




------------------------------
JFrame_test.java
-------------------------------
package TestDblClickImage;

import com.google.common.eventbus.Subscribe;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import mmcorej.CMMCore;
import org.micromanager.Studio;
import org.micromanager.display.DisplayWindow;
import org.micromanager.events.DisplayAboutToShowEvent;


public class JFrame_test extends javax.swing.JFrame  {

    private Studio mm_ = null;
    private CMMCore core_ = null;


    public JFrame_test(Studio studio) {
        mm_ = studio;
        core_ = mm_.getCMMCore();
        initComponents();
        mm_.events().registerForEvents(this);
    }


    @Subscribe
    public void OnDisplayAboutToShow(DisplayAboutToShowEvent ev) {
        //Is called One time when a DisplayWindow is just about to show its GUI, which happens once there's at least one image for it to display.
        addTxtToLog("New DisplayAboutToShowEvent event \n");

        DisplayWindow dw = mm_.getDisplayManager().getCurrentWindow();
        dw.getImagePlus().getWindow().getCanvas().addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    double zoom
                            = mm_.getDisplayManager().getCurrentWindow().getZoom();
                    int x = (int) (e.getX() / zoom);
                    int y = (int) (e.getY() / zoom);
                    addTxtToLog(
                            "Dbl Click at image coordinate x= " + x + " y = " + y + "\n");
                }
                /*
                else
                {
                     double zoom
                            = mm_.getDisplayManager().getCurrentWindow().getZoom();
                    int x = (int) (e.getX() / zoom);
                    int y = (int) (e.getY() / zoom);
                    addTxtToLog(
                            "Mouse pressed at image coordinate x= " + x + " y = " + y + "\n");                  
                }
                */                
            }
        });
    }

    public void addTxtToLog(String txt) {
        jTextArea_Log.append(txt);
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_Log = new javax.swing.JTextArea();

        jTextArea_Log.setColumns(20);
        jTextArea_Log.setRows(5);
        jScrollPane1.setViewportView(jTextArea_Log);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                       


    // Variables declaration - do not modify                    
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea_Log;
    // End of variables declaration                  

}



Once again, many thanks !

Matthieu L

Loading...