Coder Social home page Coder Social logo

Comments (5)

lnyng avatar lnyng commented on July 28, 2024

I think the problem can be pinpointed to the DefaultDisplayService. When I set a breakpoint at the 114 line, the problem does not appear; but if I set a breakpoint at the 117 line, the problem appears, and the conditional breakpoint shows that the displayList was empty at the moment the breakpoint is hit, but immediately after that the list is added with one display object by another thread.

Therefore, it seems that the display-activation event is published too slowly for the swing UI when a command is called programmatically, so that the display input is not resolved at that moment. @ctrueden any reason this would happen?

from scijava-ui-swing.

lnyng avatar lnyng commented on July 28, 2024

After playing around with the Eclipse debugger for a while, I think I figure out how most of this process works and where the problem lies.

DefaultUIService#onEvent is invoked due to a DisplayCreatedEvent, which consequently schedule a runnable that will invoke the view(w, d) method of a corresponding DisplayViewer.

In the case of opening an image, the legacy mode uses LegacyImageDisplayViewer, which sets the active display immediately in the view(w, d) call; in contrast, the corresponding DisplayViewer does not set the active display until a display window is constructed, and then the DefaultDisplayService#onEvent finally sets the active display.

In the meanwhile, the main thread is running to preprocess the command, which looks for a suitable active display to inject into the command.

I found that the call stack of constructing a swing window is quite large, so that the main thread always asks for active display before the WinActivatedEvent can invoke the setActiveDisplay method. Since the legacy mode sets the active display before constructing the window, there is no such problem in legacy mode.

If this is actually the problem, then adding a line of code that sets the active display in the view(w, d) will be a hacky solution, but should we also suppose that a display should not be not ready until its window is ready? Then we might need to let other threads wait until the display is active. @ctrueden What do you think?

from scijava-ui-swing.

lnyng avatar lnyng commented on July 28, 2024

For now, this could be a workaround:

ImageJ ij = new ImageJ();
ij.ui().setDefaultUI(ij.ui().getUI("swing"));
ij.ui().showUI();
Dataset blobs = ij.scifio().datasetIO().open("http://imagej.net/images/blobs.gif");
Display<?> dsp = ij.display().createDisplay(blobs);
while (ij.display().getActiveDisplay(dsp.getClass()) == null) {
    Thread.sleep(10);
}
ij.command().run(SubtractFromDataValues.class, true);

But the call to getActiveDisplay might cause ConcurrentModificationException if sleep with smaller numbers, and it looks better if createDisplay() can handle this internally.

from scijava-ui-swing.

lnyng avatar lnyng commented on July 28, 2024

Using @EventHandler also works, but not quite good looking:

ImageJ ij = new ImageJ();
ij.ui().setDefaultUI(ij.ui().getUI("swing"));
ij.ui().showUI();
Dataset blobs = ij.scifio().datasetIO().open("http://imagej.net/images/blobs.gif");
Display<?> dsp = ij.display().createDisplay(blobs);

final Collection<EventSubscriber<?>> subscribers = new ArrayList<EventSubscriber<?>>();
Object obj = new Object() {
    @EventHandler
    private void onEvent(WinActivatedEvent event) {
        if (event.getDisplay().equals(dsp)) {
            ij.command().run(SubtractFromDataValues.class, true);
            ij.event().unsubscribe(subscribers);
        }
    }
};
subscribers.addAll(ij.event().subscribe(obj));

from scijava-ui-swing.

ctrueden avatar ctrueden commented on July 28, 2024

Thanks for the thorough followup, Leon. I'm definitely not a fan of any sleep-based workarounds. IJ1 is full of those and it causes nothing but trouble in the long run. We can make things concurrency safe, I'm sure.

from scijava-ui-swing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.