I am writing a plugin (in Java) in which I click on places in an image and the plugin makes changes to the image. It is modeled after the Mouse Listener programming example on the plugins page.
Unfortunately, when I click on an image, not only does my plugin get invoked, but whatever tool happens to be selected on the toolbar also reacts. Thus, I find myself unexpectedly zooming or selecting/deselecting the ROI while using my plugin. In some cases, the current tool seems to block my mouse listener. For example, if there is a rectangular selection showing, and any of the selection tools is set, after I start my plugin, the first mouse click deselects the ROI but does not get to my plugin. Subsequent mouse clicks also do not get seen by my plugin. However, if no ROI has been drawn and a selection tool is set, my plugin works correctly. So the question is, can my plugin suspend toolbar tools while it is running? The closest thing I've found is in the Toolbar class. There are getToolID and setTool functions which would allow me to remember the tool selected when my plugin starts and restore it when my plugin exits. Perhaps one of the valid values for setTool would correspond to "None"? If so, it is not listed in the documentation. There is also a restorePreviousTool, but other than its name, it is undocumented. It's not clear how or when the previous tool is set. |
Hi Bob,
my procedure to disable tools conflicting with a MouseListener and/or MouseMotionListener is this: if (Toolbar.getToolId() > Toolbar.CROSSHAIR) IJ.setTool(Toolbar.RECTANGLE); //deselect tools that would interfere ic.addMouseListener(this); ic.addMouseMotionListener(this); //ic is the ImageCanvas and in public void mousePressed(MouseEvent e): imp.setRoi((Roi)null); // intercept roi tools Michael ________________________________________________________________ On 20 Sep 2011, at 18:27, Bob Loushin wrote: > I am writing a plugin (in Java) in which I click on places in an > image and the plugin makes changes to the image. It is modeled > after the Mouse Listener programming example on the plugins page. > > Unfortunately, when I click on an image, not only does my plugin > get invoked, but whatever tool happens to be selected on the > toolbar also reacts. Thus, I find myself unexpectedly zooming or > selecting/deselecting the ROI while using my plugin. In some cases, > the current tool seems to block my mouse listener. For example, if > there is a rectangular selection showing, and any of the selection > tools is set, after I start my plugin, the first mouse click > deselects the ROI but does not get to my plugin. Subsequent mouse > clicks also do not get seen by my plugin. However, if no ROI has > been drawn and a selection tool is set, my plugin works correctly. > > So the question is, can my plugin suspend toolbar tools while it is > running? > > The closest thing I've found is in the Toolbar class. There are > getToolID and setTool functions which would allow me to remember > the tool selected when my plugin starts and restore it when my > plugin exits. Perhaps one of the valid values for setTool would > correspond to "None"? If so, it is not listed in the documentation. > There is also a restorePreviousTool, but other than its name, it is > undocumented. It's not clear how or when the previous tool is set. |
Thanks! I'll give that a shot tomorrow. How do I undo it? I know to save
the tool ID it was on and then reset it when my plugin exits. But how do I undo the setRoi((Roi)null)? At the end of my plugin, I do a removeMouseListener and removeMouseMotionListener. Is that enough? Thank you, Bob -----Original Message----- From: Michael Schmid Sent: Tuesday, September 20, 2011 5:04 PM To: [hidden email] Subject: Re: In a PlugInFilter, how do I suspend the current tool? Hi Bob, my procedure to disable tools conflicting with a MouseListener and/or MouseMotionListener is this: if (Toolbar.getToolId() > Toolbar.CROSSHAIR) IJ.setTool(Toolbar.RECTANGLE); //deselect tools that would interfere ic.addMouseListener(this); ic.addMouseMotionListener(this); //ic is the ImageCanvas and in public void mousePressed(MouseEvent e): imp.setRoi((Roi)null); // intercept roi tools Michael ________________________________________________________________ On 20 Sep 2011, at 18:27, Bob Loushin wrote: > I am writing a plugin (in Java) in which I click on places in an image > and the plugin makes changes to the image. It is modeled after the Mouse > Listener programming example on the plugins page. > > Unfortunately, when I click on an image, not only does my plugin get > invoked, but whatever tool happens to be selected on the toolbar also > reacts. Thus, I find myself unexpectedly zooming or selecting/deselecting > the ROI while using my plugin. In some cases, the current tool seems to > block my mouse listener. For example, if there is a rectangular selection > showing, and any of the selection tools is set, after I start my plugin, > the first mouse click deselects the ROI but does not get to my plugin. > Subsequent mouse clicks also do not get seen by my plugin. However, if no > ROI has been drawn and a selection tool is set, my plugin works > correctly. > > So the question is, can my plugin suspend toolbar tools while it is > running? > > The closest thing I've found is in the Toolbar class. There are getToolID > and setTool functions which would allow me to remember the tool selected > when my plugin starts and restore it when my plugin exits. Perhaps one of > the valid values for setTool would correspond to "None"? If so, it is not > listed in the documentation. There is also a restorePreviousTool, but > other than its name, it is undocumented. It's not clear how or when the > previous tool is set. |
Michael's suggestion has provided a partial solution. It deals with all cases but the following: if a rectangular ROI has been selected before running the plugin, then when I run my plugin, the first click erases the box but is ignored by my plugin. Subsequent clicks behave stangely--sometimes my plugin ignores them (although it is possible the plugin receives them and incorrectly calculates a null result, which it would choose not to report. Clicking in some parts of the image should give such a null result), sometimes it appears to receive the clicks but gives the wrong answer. I'm not sure why.
The relevant portions of my code are below. The way it functions is that there is an image open, the user starts the plugin, clicks on places in the image, calculations are done and the results are shown in a overlay of the image and a results table. If an ROI is selected before the plugin starts, it is ignored (calculations are not limited to the ROI or influenced by it). public class My_Plug implements PlugInFilter, MouseListener, MouseMotionListener { ImagePlus img; ImagePlus dup; //Calculations are actually made on a thresholded duplicate of the original image ImageCanvas canvas; static Vector images = new Vector(); int count; String toolName; Overlay over = new Overlay(); Color overlayColor = Color.MAGENTA; ResultsTable out; public int setup(String arg, ImagePlus img) { if (img == null) { IJ.noImage(); return(DONE); } this.img = img; count = 1; out = new ResultsTable(); img.setOverlay(over); img.setHideOverlay(false); IJ.register(My_Plug.class); return DOES_ALL+NO_CHANGES; } public void run(ImageProcessor ip) { Integer id = new Integer(img.getID()); if (images.contains(id)) { IJ.log("Already listening to this image"); return; } else { ImageWindow win = img.getWindow(); canvas = win.getCanvas(); toolName = IJ.getToolName(); // A slight modification of Michael's recommendation, but shouldn't make a difference. IJ.setTool(Toolbar.RECTANGLE); canvas.addMouseListener(this); canvas.addMouseMotionListener(this); images.addElement(id); } dup = img.duplicate(); dup.getProcessor().threshold(128); // I added this so the user could tell me when (s)he is done with the plugin. It just sits off to the side while the user clicks in the image. NonBlockingGenericDialog stopper = new NonBlockingGenericDialog("Finished?"); stopper.addMessage("Select positions to measure. Click \"Done\" when finished measuring."); stopper.setOKLabel("Done"); stopper.hideCancelButton(); stopper.showDialog(); //Runs after the dialog above is ended. Clean up, try to restore the tools and image to the way they were before I started, as much as possible, with the addition of the overlay. canvas.removeMouseListener(this); canvas.removeMouseMotionListener(this); IJ.setTool(toolName); images.remove(id); dup.close(); out.show("Answers"); } public void mousePressed(MouseEvent e) { img.setRoi((Roi)null); //Michael's suggestion int x = e.getX(); int y = e.getY(); int offscreenX = canvas.offScreenX(x); int offscreenY = canvas.offScreenY(y); handlePixel(offscreenX, offscreenY); //Does calculations, writes in the overlay, and records stuff in the results table. } //Other code, not relevant here. Does not influence input side of the UI, just does calculations and populates the overlay and results table. } ----- Original Message ----- From: "Bob" <[hidden email]> To: [hidden email] Sent: Tuesday, September 20, 2011 6:07:17 PM Subject: Re: In a PlugInFilter, how do I suspend the current tool? Thanks! I'll give that a shot tomorrow. How do I undo it? I know to save the tool ID it was on and then reset it when my plugin exits. But how do I undo the setRoi((Roi)null)? At the end of my plugin, I do a removeMouseListener and removeMouseMotionListener. Is that enough? Thank you, Bob -----Original Message----- From: Michael Schmid Sent: Tuesday, September 20, 2011 5:04 PM To: [hidden email] Subject: Re: In a PlugInFilter, how do I suspend the current tool? Hi Bob, my procedure to disable tools conflicting with a MouseListener and/or MouseMotionListener is this: if (Toolbar.getToolId() > Toolbar.CROSSHAIR) IJ.setTool(Toolbar.RECTANGLE); //deselect tools that would interfere ic.addMouseListener(this); ic.addMouseMotionListener(this); //ic is the ImageCanvas and in public void mousePressed(MouseEvent e): imp.setRoi((Roi)null); // intercept roi tools Michael ________________________________________________________________ On 20 Sep 2011, at 18:27, Bob Loushin wrote: > I am writing a plugin (in Java) in which I click on places in an image > and the plugin makes changes to the image. It is modeled after the Mouse > Listener programming example on the plugins page. > > Unfortunately, when I click on an image, not only does my plugin get > invoked, but whatever tool happens to be selected on the toolbar also > reacts. Thus, I find myself unexpectedly zooming or selecting/deselecting > the ROI while using my plugin. In some cases, the current tool seems to > block my mouse listener. For example, if there is a rectangular selection > showing, and any of the selection tools is set, after I start my plugin, > the first mouse click deselects the ROI but does not get to my plugin. > Subsequent mouse clicks also do not get seen by my plugin. However, if no > ROI has been drawn and a selection tool is set, my plugin works > correctly. > > So the question is, can my plugin suspend toolbar tools while it is > running? > > The closest thing I've found is in the Toolbar class. There are getToolID > and setTool functions which would allow me to remember the tool selected > when my plugin starts and restore it when my plugin exits. Perhaps one of > the valid values for setTool would correspond to "None"? If so, it is not > listed in the documentation. There is also a restorePreviousTool, but > other than its name, it is undocumented. It's not clear how or when the > previous tool is set. |
In reply to this post by Bob Loushin
Hi Bob,
On Tue, 20 Sep 2011, Bob Loushin wrote: > I am writing a plugin (in Java) in which I click on places in an image > and the plugin makes changes to the image. It is modeled after the Mouse > Listener programming example on the plugins page. > > Unfortunately, when I click on an image, not only does my plugin get > invoked, but whatever tool happens to be selected on the toolbar also > reacts. So you should register a new tool. Actually, in Fiji there is a whole framework called "AbstractTool" to implement new tools. The big advantage is that you can switch to other tools and then back to your tool, and it will only get mouse events when active. Probably the easiest way to play with it is by starting the Script Editor (File>New>Script, or simply press the '{' key), and then select Templates>Java>Bare Tool. To run it, simply save it and call Run>Run. That should get you going within 1 minutes. Ciao, Johannes |
In reply to this post by Bob Loushin
Hi Bob,
no such problem for me. I forgot to mention that you have to do imp.setRoi((Roi)null); also when entering the plugin, not only each time the mouse is pressed. You can use the following when entering the plugin to save the current roi, so you can restore it later: boolean roiSaved; //class (global) variable if (imp.getRoi()!=null) { //remove any roi that would interfere with input imp.saveRoi(); roiSaved = true; imp.setRoi((Roi)null); } and when leaving if (roiSaved) imp.restoreRoi(); Michael ________________________________________________________________ On 21 Sep 2011, at 17:20, Bob Loushin wrote: > Michael's suggestion has provided a partial solution. It deals with > all cases but the following: if a rectangular ROI has been selected > before running the plugin, then when I run my plugin, the first > click erases the box but is ignored by my plugin. Subsequent clicks > behave stangely--sometimes my plugin ignores them (although it is > possible the plugin receives them and incorrectly calculates a null > result, which it would choose not to report. Clicking in some parts > of the image should give such a null result), sometimes it appears > to receive the clicks but gives the wrong answer. I'm not sure why. > > The relevant portions of my code are below. The way it functions is > that there is an image open, the user starts the plugin, clicks on > places in the image, calculations are done and the results are > shown in a overlay of the image and a results table. If an ROI is > selected before the plugin starts, it is ignored (calculations are > not limited to the ROI or influenced by it). > > public class My_Plug implements PlugInFilter, MouseListener, > MouseMotionListener { > ImagePlus img; > ImagePlus dup; //Calculations are actually made on a thresholded > duplicate of the original image > ImageCanvas canvas; > static Vector images = new Vector(); > int count; > String toolName; > Overlay over = new Overlay(); > Color overlayColor = Color.MAGENTA; > ResultsTable out; > > public int setup(String arg, ImagePlus img) { > if (img == null) > { > IJ.noImage(); > return(DONE); > } > this.img = img; > count = 1; > out = new ResultsTable(); > img.setOverlay(over); > img.setHideOverlay(false); > IJ.register(My_Plug.class); > return DOES_ALL+NO_CHANGES; > } > > public void run(ImageProcessor ip) { > Integer id = new Integer(img.getID()); > if (images.contains(id)) { > IJ.log("Already listening to this image"); > return; > } else { > ImageWindow win = img.getWindow(); > canvas = win.getCanvas(); > toolName = IJ.getToolName(); // A slight modification of Michael's > recommendation, but shouldn't make a difference. > IJ.setTool(Toolbar.RECTANGLE); > canvas.addMouseListener(this); > canvas.addMouseMotionListener(this); > images.addElement(id); > } > dup = img.duplicate(); > dup.getProcessor().threshold(128); > > // I added this so the user could tell me when (s)he is done with > the plugin. It just sits off to the side while the user clicks in > the image. > NonBlockingGenericDialog stopper = new NonBlockingGenericDialog > ("Finished?"); > stopper.addMessage("Select positions to measure. Click \"Done\" > when finished measuring."); > stopper.setOKLabel("Done"); > stopper.hideCancelButton(); > stopper.showDialog(); > > //Runs after the dialog above is ended. Clean up, try to restore > the tools and image to the way they were before I started, as much > as possible, with the addition of the overlay. > canvas.removeMouseListener(this); > canvas.removeMouseMotionListener(this); > IJ.setTool(toolName); > images.remove(id); > dup.close(); > out.show("Answers"); > } > > public void mousePressed(MouseEvent e) { > img.setRoi((Roi)null); //Michael's suggestion > int x = e.getX(); > int y = e.getY(); > int offscreenX = canvas.offScreenX(x); > int offscreenY = canvas.offScreenY(y); > handlePixel(offscreenX, offscreenY); //Does calculations, writes in > the overlay, and records stuff in the results table. > } > > //Other code, not relevant here. Does not influence input side of > the UI, just does calculations and populates the overlay and > results table. > } > > ----- Original Message ----- > From: "Bob" <[hidden email]> > To: [hidden email] > Sent: Tuesday, September 20, 2011 6:07:17 PM > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Thanks! I'll give that a shot tomorrow. How do I undo it? I know to > save > the tool ID it was on and then reset it when my plugin exits. But > how do I > undo the setRoi((Roi)null)? At the end of my plugin, I do a > removeMouseListener and removeMouseMotionListener. Is that enough? > > Thank you, > > Bob > > -----Original Message----- > From: Michael Schmid > Sent: Tuesday, September 20, 2011 5:04 PM > To: [hidden email] > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Hi Bob, > > my procedure to disable tools conflicting with a MouseListener and/or > MouseMotionListener is this: > > if (Toolbar.getToolId() > Toolbar.CROSSHAIR) > IJ.setTool(Toolbar.RECTANGLE); //deselect tools that > would interfere > ic.addMouseListener(this); > ic.addMouseMotionListener(this); > //ic is the ImageCanvas > > and in public void mousePressed(MouseEvent e): > imp.setRoi((Roi)null); // intercept roi tools > > > Michael > ________________________________________________________________ > > On 20 Sep 2011, at 18:27, Bob Loushin wrote: > >> I am writing a plugin (in Java) in which I click on places in an >> image >> and the plugin makes changes to the image. It is modeled after the >> Mouse >> Listener programming example on the plugins page. >> >> Unfortunately, when I click on an image, not only does my plugin get >> invoked, but whatever tool happens to be selected on the toolbar also >> reacts. Thus, I find myself unexpectedly zooming or selecting/ >> deselecting >> the ROI while using my plugin. In some cases, the current tool >> seems to >> block my mouse listener. For example, if there is a rectangular >> selection >> showing, and any of the selection tools is set, after I start my >> plugin, >> the first mouse click deselects the ROI but does not get to my >> plugin. >> Subsequent mouse clicks also do not get seen by my plugin. >> However, if no >> ROI has been drawn and a selection tool is set, my plugin works >> correctly. >> >> So the question is, can my plugin suspend toolbar tools while it is >> running? >> >> The closest thing I've found is in the Toolbar class. There are >> getToolID >> and setTool functions which would allow me to remember the tool >> selected >> when my plugin starts and restore it when my plugin exits. Perhaps >> one of >> the valid values for setTool would correspond to "None"? If so, it >> is not >> listed in the documentation. There is also a restorePreviousTool, but >> other than its name, it is undocumented. It's not clear how or >> when the >> previous tool is set. |
Thanks, Michael, I'll give that a try.
Bob -----Original Message----- From: Michael Schmid Sent: Thursday, September 22, 2011 2:30 AM To: [hidden email] Subject: Re: In a PlugInFilter, how do I suspend the current tool? Hi Bob, no such problem for me. I forgot to mention that you have to do imp.setRoi((Roi)null); also when entering the plugin, not only each time the mouse is pressed. You can use the following when entering the plugin to save the current roi, so you can restore it later: boolean roiSaved; //class (global) variable if (imp.getRoi()!=null) { //remove any roi that would interfere with input imp.saveRoi(); roiSaved = true; imp.setRoi((Roi)null); } and when leaving if (roiSaved) imp.restoreRoi(); Michael ________________________________________________________________ On 21 Sep 2011, at 17:20, Bob Loushin wrote: > Michael's suggestion has provided a partial solution. It deals with > all cases but the following: if a rectangular ROI has been selected > before running the plugin, then when I run my plugin, the first > click erases the box but is ignored by my plugin. Subsequent clicks > behave stangely--sometimes my plugin ignores them (although it is > possible the plugin receives them and incorrectly calculates a null > result, which it would choose not to report. Clicking in some parts > of the image should give such a null result), sometimes it appears > to receive the clicks but gives the wrong answer. I'm not sure why. > > The relevant portions of my code are below. The way it functions is > that there is an image open, the user starts the plugin, clicks on > places in the image, calculations are done and the results are > shown in a overlay of the image and a results table. If an ROI is > selected before the plugin starts, it is ignored (calculations are > not limited to the ROI or influenced by it). > > public class My_Plug implements PlugInFilter, MouseListener, > MouseMotionListener { > ImagePlus img; > ImagePlus dup; //Calculations are actually made on a thresholded > duplicate of the original image > ImageCanvas canvas; > static Vector images = new Vector(); > int count; > String toolName; > Overlay over = new Overlay(); > Color overlayColor = Color.MAGENTA; > ResultsTable out; > > public int setup(String arg, ImagePlus img) { > if (img == null) > { > IJ.noImage(); > return(DONE); > } > this.img = img; > count = 1; > out = new ResultsTable(); > img.setOverlay(over); > img.setHideOverlay(false); > IJ.register(My_Plug.class); > return DOES_ALL+NO_CHANGES; > } > > public void run(ImageProcessor ip) { > Integer id = new Integer(img.getID()); > if (images.contains(id)) { > IJ.log("Already listening to this image"); > return; > } else { > ImageWindow win = img.getWindow(); > canvas = win.getCanvas(); > toolName = IJ.getToolName(); // A slight modification of Michael's > recommendation, but shouldn't make a difference. > IJ.setTool(Toolbar.RECTANGLE); > canvas.addMouseListener(this); > canvas.addMouseMotionListener(this); > images.addElement(id); > } > dup = img.duplicate(); > dup.getProcessor().threshold(128); > > // I added this so the user could tell me when (s)he is done with > the plugin. It just sits off to the side while the user clicks in > the image. > NonBlockingGenericDialog stopper = new NonBlockingGenericDialog > ("Finished?"); > stopper.addMessage("Select positions to measure. Click \"Done\" > when finished measuring."); > stopper.setOKLabel("Done"); > stopper.hideCancelButton(); > stopper.showDialog(); > > //Runs after the dialog above is ended. Clean up, try to restore > the tools and image to the way they were before I started, as much > as possible, with the addition of the overlay. > canvas.removeMouseListener(this); > canvas.removeMouseMotionListener(this); > IJ.setTool(toolName); > images.remove(id); > dup.close(); > out.show("Answers"); > } > > public void mousePressed(MouseEvent e) { > img.setRoi((Roi)null); //Michael's suggestion > int x = e.getX(); > int y = e.getY(); > int offscreenX = canvas.offScreenX(x); > int offscreenY = canvas.offScreenY(y); > handlePixel(offscreenX, offscreenY); //Does calculations, writes in > the overlay, and records stuff in the results table. > } > > //Other code, not relevant here. Does not influence input side of > the UI, just does calculations and populates the overlay and > results table. > } > > ----- Original Message ----- > From: "Bob" <[hidden email]> > To: [hidden email] > Sent: Tuesday, September 20, 2011 6:07:17 PM > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Thanks! I'll give that a shot tomorrow. How do I undo it? I know to > save > the tool ID it was on and then reset it when my plugin exits. But > how do I > undo the setRoi((Roi)null)? At the end of my plugin, I do a > removeMouseListener and removeMouseMotionListener. Is that enough? > > Thank you, > > Bob > > -----Original Message----- > From: Michael Schmid > Sent: Tuesday, September 20, 2011 5:04 PM > To: [hidden email] > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Hi Bob, > > my procedure to disable tools conflicting with a MouseListener and/or > MouseMotionListener is this: > > if (Toolbar.getToolId() > Toolbar.CROSSHAIR) > IJ.setTool(Toolbar.RECTANGLE); //deselect tools that > would interfere > ic.addMouseListener(this); > ic.addMouseMotionListener(this); > //ic is the ImageCanvas > > and in public void mousePressed(MouseEvent e): > imp.setRoi((Roi)null); // intercept roi tools > > > Michael > ________________________________________________________________ > > On 20 Sep 2011, at 18:27, Bob Loushin wrote: > >> I am writing a plugin (in Java) in which I click on places in an >> image >> and the plugin makes changes to the image. It is modeled after the >> Mouse >> Listener programming example on the plugins page. >> >> Unfortunately, when I click on an image, not only does my plugin get >> invoked, but whatever tool happens to be selected on the toolbar also >> reacts. Thus, I find myself unexpectedly zooming or selecting/ >> deselecting >> the ROI while using my plugin. In some cases, the current tool >> seems to >> block my mouse listener. For example, if there is a rectangular >> selection >> showing, and any of the selection tools is set, after I start my >> plugin, >> the first mouse click deselects the ROI but does not get to my >> plugin. >> Subsequent mouse clicks also do not get seen by my plugin. >> However, if no >> ROI has been drawn and a selection tool is set, my plugin works >> correctly. >> >> So the question is, can my plugin suspend toolbar tools while it is >> running? >> >> The closest thing I've found is in the Toolbar class. There are >> getToolID >> and setTool functions which would allow me to remember the tool >> selected >> when my plugin starts and restore it when my plugin exits. Perhaps >> one of >> the valid values for setTool would correspond to "None"? If so, it >> is not >> listed in the documentation. There is also a restorePreviousTool, but >> other than its name, it is undocumented. It's not clear how or >> when the >> previous tool is set. |
In reply to this post by Michael Schmid
Michael,
Finally got to give this a try (I've been home from work, sick). Putting in the extra imp.setRoi((Roi)null); really helped! The problems I was having have been cleared up. Thank you! Bob -----Original Message----- From: Michael Schmid Sent: Thursday, September 22, 2011 2:30 AM To: [hidden email] Subject: Re: In a PlugInFilter, how do I suspend the current tool? Hi Bob, no such problem for me. I forgot to mention that you have to do imp.setRoi((Roi)null); also when entering the plugin, not only each time the mouse is pressed. You can use the following when entering the plugin to save the current roi, so you can restore it later: boolean roiSaved; //class (global) variable if (imp.getRoi()!=null) { //remove any roi that would interfere with input imp.saveRoi(); roiSaved = true; imp.setRoi((Roi)null); } and when leaving if (roiSaved) imp.restoreRoi(); Michael ________________________________________________________________ On 21 Sep 2011, at 17:20, Bob Loushin wrote: > Michael's suggestion has provided a partial solution. It deals with all > cases but the following: if a rectangular ROI has been selected before > running the plugin, then when I run my plugin, the first click erases the > box but is ignored by my plugin. Subsequent clicks behave > stangely--sometimes my plugin ignores them (although it is possible the > plugin receives them and incorrectly calculates a null result, which it > would choose not to report. Clicking in some parts of the image should > give such a null result), sometimes it appears to receive the clicks but > gives the wrong answer. I'm not sure why. > > The relevant portions of my code are below. The way it functions is that > there is an image open, the user starts the plugin, clicks on places in > the image, calculations are done and the results are shown in a overlay > of the image and a results table. If an ROI is selected before the plugin > starts, it is ignored (calculations are not limited to the ROI or > influenced by it). > > public class My_Plug implements PlugInFilter, MouseListener, > MouseMotionListener { > ImagePlus img; > ImagePlus dup; //Calculations are actually made on a thresholded > duplicate of the original image > ImageCanvas canvas; > static Vector images = new Vector(); > int count; > String toolName; > Overlay over = new Overlay(); > Color overlayColor = Color.MAGENTA; > ResultsTable out; > > public int setup(String arg, ImagePlus img) { > if (img == null) > { > IJ.noImage(); > return(DONE); > } > this.img = img; > count = 1; > out = new ResultsTable(); > img.setOverlay(over); > img.setHideOverlay(false); > IJ.register(My_Plug.class); > return DOES_ALL+NO_CHANGES; > } > > public void run(ImageProcessor ip) { > Integer id = new Integer(img.getID()); > if (images.contains(id)) { > IJ.log("Already listening to this image"); > return; > } else { > ImageWindow win = img.getWindow(); > canvas = win.getCanvas(); > toolName = IJ.getToolName(); // A slight modification of Michael's > recommendation, but shouldn't make a difference. > IJ.setTool(Toolbar.RECTANGLE); > canvas.addMouseListener(this); > canvas.addMouseMotionListener(this); > images.addElement(id); > } > dup = img.duplicate(); > dup.getProcessor().threshold(128); > > // I added this so the user could tell me when (s)he is done with the > plugin. It just sits off to the side while the user clicks in the image. > NonBlockingGenericDialog stopper = new NonBlockingGenericDialog > ("Finished?"); > stopper.addMessage("Select positions to measure. Click \"Done\" when > finished measuring."); > stopper.setOKLabel("Done"); > stopper.hideCancelButton(); > stopper.showDialog(); > > //Runs after the dialog above is ended. Clean up, try to restore the > tools and image to the way they were before I started, as much as > possible, with the addition of the overlay. > canvas.removeMouseListener(this); > canvas.removeMouseMotionListener(this); > IJ.setTool(toolName); > images.remove(id); > dup.close(); > out.show("Answers"); > } > > public void mousePressed(MouseEvent e) { > img.setRoi((Roi)null); //Michael's suggestion > int x = e.getX(); > int y = e.getY(); > int offscreenX = canvas.offScreenX(x); > int offscreenY = canvas.offScreenY(y); > handlePixel(offscreenX, offscreenY); //Does calculations, writes in the > overlay, and records stuff in the results table. > } > > //Other code, not relevant here. Does not influence input side of the UI, > just does calculations and populates the overlay and results table. > } > > ----- Original Message ----- > From: "Bob" <[hidden email]> > To: [hidden email] > Sent: Tuesday, September 20, 2011 6:07:17 PM > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Thanks! I'll give that a shot tomorrow. How do I undo it? I know to save > the tool ID it was on and then reset it when my plugin exits. But how do > I > undo the setRoi((Roi)null)? At the end of my plugin, I do a > removeMouseListener and removeMouseMotionListener. Is that enough? > > Thank you, > > Bob > > -----Original Message----- > From: Michael Schmid > Sent: Tuesday, September 20, 2011 5:04 PM > To: [hidden email] > Subject: Re: In a PlugInFilter, how do I suspend the current tool? > > Hi Bob, > > my procedure to disable tools conflicting with a MouseListener and/or > MouseMotionListener is this: > > if (Toolbar.getToolId() > Toolbar.CROSSHAIR) > IJ.setTool(Toolbar.RECTANGLE); //deselect tools that > would interfere > ic.addMouseListener(this); > ic.addMouseMotionListener(this); > //ic is the ImageCanvas > > and in public void mousePressed(MouseEvent e): > imp.setRoi((Roi)null); // intercept roi tools > > > Michael > ________________________________________________________________ > > On 20 Sep 2011, at 18:27, Bob Loushin wrote: > >> I am writing a plugin (in Java) in which I click on places in an image >> and the plugin makes changes to the image. It is modeled after the Mouse >> Listener programming example on the plugins page. >> >> Unfortunately, when I click on an image, not only does my plugin get >> invoked, but whatever tool happens to be selected on the toolbar also >> reacts. Thus, I find myself unexpectedly zooming or selecting/ >> deselecting >> the ROI while using my plugin. In some cases, the current tool seems to >> block my mouse listener. For example, if there is a rectangular >> selection >> showing, and any of the selection tools is set, after I start my plugin, >> the first mouse click deselects the ROI but does not get to my plugin. >> Subsequent mouse clicks also do not get seen by my plugin. However, if >> no >> ROI has been drawn and a selection tool is set, my plugin works >> correctly. >> >> So the question is, can my plugin suspend toolbar tools while it is >> running? >> >> The closest thing I've found is in the Toolbar class. There are >> getToolID >> and setTool functions which would allow me to remember the tool selected >> when my plugin starts and restore it when my plugin exits. Perhaps one >> of >> the valid values for setTool would correspond to "None"? If so, it is >> not >> listed in the documentation. There is also a restorePreviousTool, but >> other than its name, it is undocumented. It's not clear how or when the >> previous tool is set. |
Free forum by Nabble | Edit this page |