Greetings Kenneth,

I have experienced the same, but different, angst(s) in the past. I had
ImageWindow(s) that were displayed with zero width and zero height, and, I
created TextWindow(s) but never show(ed) them - but they stayed around
after the plugin exited; both hard to see / deal with using ImageJ GUI
menu commands. I wrote my FindWindow plugin to rectify these, and other,
issues. Its also helpful to work around the problems with the Window menu.

See attached (remove .txt extension as we can not attach .java files)

Enjoy,

Fred

ps: On Linux you can see all the windows that exist. My naive recollection
Windows / MacOS only shows the Application instances that exist, but you
may find a way to find all windows? I would assume that there is some way
to ask the JVM what windows it thinks exists?


On Fri, November 15, 2024 12:00 am, Kenneth R Sloan wrote:
> yes - it works on my machine, too.  but, it fails (silently waiting, with
> no dialog displayed) on three other machines.  it had been workibg on
> those
> machines, too, for months.
>
> i was hoping this had happened to someone else.  i fully expect it to work
>
> i “fixed” it by commenting out this code, removing the user option to
> change these patameters, and all is well - even thought there is another
> NonBlockingGenericDialog further along in the cide.  I can’t remove that
> one!
>
> Behavior that cant be replicated is a bitch to debug!  I was hoping that
> someone else had run into this and knew what it was.
>
> The only thing common to the failing machines seems to be the local
> network.  And it is 1000 miles from me.
>
> even on the failing machines, it was working and then failed only 5
> minutes
> later, with no updates in between, as far as we know.   I’m not sure if
> they  restarted FIJI in between success and failure.
>
> I’m baffled!
>
> Thanks for confirming that it works somewhere other than my personal
> machine.  as near as i can determine, showDialog() is called, but dies
> nothing, and never returns, never displays anything, and does not throw an
> Exception.
>
> -Kenneth Sloan
>
>
> On Thu, Nov 14, 2024 at 22:33 Wayne Rasband <[email protected]> wrote:
>
>> Hi Kenneth,
>>
>> I converted your test code to JavaScript,n fixed problems with missing
>> variable declarations and ran the script successfully on macOS, Linux
>> and
>> Windows, on my MacBook Air.nths
>>
>> -wayne
>>
>>   IJ.log("building dialog");
>>   gd = new NonBlockingGenericDialog("User Paraameters");
>>   IJ.log("created  gd");
>>   caseID = "noCaseID";
>>   gd.addStringField("Case ID:",caseID);
>>   IJ.log("added Case ID:  "+caseID);
>>   markerRoiRadius = 10;
>>   gd.addNumericField("Marker Radius(pixels):", markerRoiRadius,0);
>>   IJ.log("added Marker Radius");
>>   dialogX = 0;
>>   dialogY = 0;
>>   gd.setLocation(dialogX,dialogY);
>>   IJ.log("setLocation "+dialogX+"   "+dialogY);
>>   gd.showDialog();
>>   IJ.log("gd.wasCanceled()  =" + gd.wasCanceled());
>>   IJ.log("getting  caseID");
>>   caseID = gd.getNextString();
>>   IJ.log("caseID = "+caseID);
>>   markerRoiRadius = gd.getNextNumber();
>>   IJ.log("markerRoiRadius = "+markerRoiRadius);
>>
>>
>> > On Nov 14, 2024, at 3:43 PM, Kenneth Sloan <[email protected]>
>> wrote:
>> >
>> > I have a plugin which has been working for 6 months.
>> >
>> > Today, I got a report that it failed, on both Windows and Mac
>> machines.
>> >
>> > I asked for the input, and tried to replicate the problem.  Its works
>> just fine on my MacBook Pro.
>> >
>> > All machines have been updated to the latest FIJI release.
>> >
>> > I added some logging, so the relevant code looks like this:
>> >
>> >       // get user-specified parameters
>> >       IJ.log("building dialog");
>> >       NonBlockingGenericDialog gdParms = new
>> NonBlockingGenericDialog("User Paraameters");
>> >       IJ.log("created  gdParms");
>> >       String caseID = "noCaseID";
>> >       gdParms.addStringField("Case ID:",caseID);
>> >       IJ.log("added Case ID:  "+caseID);
>> >       gdParms.addNumericField(“Marker Radius(pixels):”,
>>      markerRoiRadius,0);
>> >       IJ.log("added Marker Radius");
>> >       gdParms.setLocation(dialogX,dialogY);
>> >       IJ.log("setLocation "+dialogX+"   "+dialogY);
>> >       gdParms.showDialog();
>> >       IJ.log("gdParms.wasCanceled()  =" + gdParms.wasCanceled());
>> >       if(gdParms.wasCanceled()) return;
>> >       IJ.log("getting  caseID");
>> >       caseID = gdParms.getNextString();
>> >       IJ.log("caseID = "+caseID);
>> >       markerRoiRadius = (int)gdParms.getNextNumber();
>> >       IJ.log("markerRoiRadius = "+markerRoiRadius);
>> >
>> > The Log window ends with the line:
>> >
>> >       SetLocation  0     0
>> > So… the statement:
>> >
>> >       gdParms.showDialog();
>> >
>> > Appears to have been executed, but nothing appears on the screen, and
>> the program stalls.
>> >
>> > There is no relevant output in the Console window.
>> >
>> > We have rebooted, updated FIJI, all to no avail.
>> >
>> > It fails on 3 machines (2 Windows and one Mac) in a research lab in a
>> medical center, but works perfectly on my MacBook Pro, at home.
>> >
>> > The initial report claims that they were using the plugin on a series
>> of
>> cases.
>> > It worked on a bunch of cases, and then failed on the next (and has
>> failed at their location every time since).  Ss so…no FIJI update or
>> OS
>> update between the last success and the first failure.
>> >
>> > I’m baffled.   Any ideas?
>> >
>> > Finally, the user reports that when the plugin stalls, all other FIJI
>> functions work properly.  It’s as if “showDialog()” has either
>> terminated
>> the plugin (without anything in the Log or Console window), or is
>> waiting
>> for an input (but has not actually displayed the dialog box).  I asked
>> them
>> to hit ESC, which had no effect.
>> > --
>> > Kenneth Sloan
>> > [email protected]
>> > Vision is the art of seeing what is invisible to others.
>> >
>> > --
>> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.datatransfer.*;
import java.io.*;
import java.util.*;
import ij.plugin.*;
import ij.plugin.frame.*;

public class Find_Window implements PlugIn {

   public void run(String arg) {

      GenericDialog gd = new NonBlockingGenericDialog("Find Windows");

      Vector<String> itv = new Vector<String>();
      itv.add("<choose>");
      itv.add("<ask>");
      int[] ids = WindowManager.getIDList();
      if (ids != null) 
         for(int id : ids)
            itv.add(WindowManager.getImage(id).getTitle());
      gd.addChoice("Find Image:", itv.toArray(new String[0]), "");
      gd.addMessage("Applies only to ImageWindow(s)");
      gd.addCheckbox("Unlock",false);
      gd.addCheckbox("Lock",false);
      gd.addCheckbox("Modified",false);
      gd.addMessage("  ");

      Vector<String> wtv = new Vector<String>();
      wtv.add("<choose>");
      Window[] ws = WindowManager.getAllNonImageWindows();
      if (ws != null)
         for(Window w : ws)
            wtv.add(w instanceof Frame ? ((Frame)w).getTitle() : 
((Dialog)w).getTitle());
      gd.addChoice("Find Frame:", wtv.toArray(new String[0]), "");
      gd.addMessage("Applies to all Window(s)\n ");
      gd.addCheckbox("Here",true);
      gd.addCheckbox("Raise",true);
      gd.addCheckbox("100%",true);
      gd.addCheckbox("Snap",false);
      gd.addCheckbox("Grab",false);
      gd.addCheckbox("Show",false);
      gd.addMessage("  ");

      Vector<String> ptv = new Vector<String>();
      ptv.add("<choose>");
      for(String p : Menus.getPlugins())
         ptv.add(p);
      gd.addChoice("Run Plugin: ",ptv.toArray(new String[0]),"");
      gd.addStringField("arg: ","",20);

      gd.showDialog();
      if (gd.wasCanceled()) return;
      //gd.setVisible(false);

      int iind = gd.getNextChoiceIndex();
      boolean unlock   = gd.getNextBoolean();
      boolean lock     = gd.getNextBoolean();
      boolean modified = gd.getNextBoolean();
      boolean here     = gd.getNextBoolean();
      boolean raise    = gd.getNextBoolean();
      boolean zoom     = gd.getNextBoolean();
      boolean snap     = gd.getNextBoolean();
      boolean grab     = gd.getNextBoolean();
      boolean visible  = gd.getNextBoolean();
      ImagePlus imp = null;
      if (iind > 0) {
         imp = (iind==1) ? WindowManager.getImage(IJ.getString("Title:",""))
                         : WindowManager.getImage(ids[iind-2]);
         ImageWindow win = imp.getWindow();
getInfo(win);
         if (visible) { win.setVisible(true); }
         if (unlock)  imp.unlock();
         if (lock)    imp.lock();
         IJ.log(imp.getTitle()+"  is now  
"+(imp.isLocked()?"Locked":"Unlocked"));
         IJ.log(imp.getTitle()+" was "+(imp.changes?"":"not ")+"modified");
         imp.changes = modified;
         if (here)    win.setLocation(gd.getLocation());
         if (zoom)    win.getCanvas().zoom100Percent();
         if (raise)   IJ.selectWindow(imp.getID()); //win.toFront(); 
         ImageWindow.setNextLocation(gd.getLocation());
         if (snap)    { IJ.wait(1000); snapAndDisplay(win); }
         if (grab)    { IJ.wait(1000); grabAndDisplay(win); }
         if (visible) { win.setVisible(true); }
         }

      int wind = gd.getNextChoiceIndex();
      if (wind > 0) {
         Window win = ws[wind-1];
         if (here)   win.setLocation(gd.getLocation());
         if (zoom)
            if (win instanceof GenericDialog) ((GenericDialog)win).pack();
                                         else win.setSize(500,500);
         ImageWindow.setNextLocation(gd.getLocation());
         if (raise)   win.toFront(); 
         if (snap)    { IJ.wait(1000); snapAndDisplay(win); }
         if (grab)    { IJ.wait(1000); grabAndDisplay(win); }
         if (visible) { win.setVisible(true); }
         }

      int pind = gd.getNextChoiceIndex();
      if (pind > 0) {
         String plugin = ptv.get(pind);
         if (plugin.indexOf("/") != -1) plugin = plugin.split("/")[1];
         if (imp != null) IJ.runPlugIn(imp, plugin, gd.getNextString());
                     else IJ.runPlugIn(plugin, gd.getNextString());
         }

      }

   public static void getInfo(Window win) {
      IJ.log("size = "+win.getSize());
      IJ.log("hieght = "+win.getHeight());
      IJ.log("width = "+win.getWidth());
      IJ.log("location = "+win.getLocation());
      IJ.log("locationOnScreen = "+win.getLocationOnScreen());
      IJ.log("showing = "+win.isShowing());
      IJ.log("valid = "+win.isValid());
      IJ.log("visible = "+win.isVisible());
      IJ.log("VALID = "+((ImageWindow)win).getCanvas().isValid());
      }

   public static void snapAndDisplay(Window win) {
      ImagePlus imp = snap(win);
      if (imp == null) return;
      imp.show();
      clipboard(imp);
      }
   public static ImagePlus snap(Window win) {
      try {
         Rectangle r = new Rectangle(win.getLocation(),win.getSize());
         //new Robot().mouseMove(win.getX()+10,win.getY()+10);
         new Robot().waitForIdle();
         //win.setVisible(true);
         //win.requestFocus();
         //boolean flag = win.isAlwaysOnTop();
         //win.setAlwaysOnTop(true);
         ImagePlus imp = new ImagePlus("Snapshot",new 
Robot().createScreenCapture(r));
         //win.setAlwaysOnTop(flag);
         return imp;
         }
      catch(Exception e) {
         IJ.showStatus("Could not grab snapshot of window");
         }
      return null;
      }
   public static void clipboard(final ImagePlus imp) {
      Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
         new Transferable() {
            @Override public DataFlavor[] getTransferDataFlavors() {
                      return new DataFlavor[] { DataFlavor.imageFlavor };
                      }
            @Override public boolean isDataFlavorSupported(DataFlavor flavor) {
                      return DataFlavor.imageFlavor.equals(flavor);
                      }
            @Override public Object getTransferData(DataFlavor flavor) throws 
UnsupportedFlavorException {
                      if (!isDataFlavorSupported(flavor))
                         throw new UnsupportedFlavorException(flavor);
                      return imp.getImage();
                      }
            }, null);
      }

   public static void grabAndDisplay(Window win) {
      ImagePlus imp = grab(win);
      if (imp == null) return;
      imp.show();
      clipboard(imp);
      }
   public static ImagePlus grab(Window win) {
      Rectangle rec = win.getBounds();
      BufferedImage bi = new BufferedImage(rec.width, rec.height, 
BufferedImage.TYPE_INT_ARGB);
      win.paintAll(bi.getGraphics());
      return new ImagePlus("screenshot",  bi);
      }
}

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Reply via email to