[ImageJ-devel] Please don't quit

Curtis Rueden ctrueden at wisc.edu
Wed Nov 5 11:33:33 CST 2014


Hi Lee,

> Please don't quit

I would suggest avoiding a custom service. There are two other (hopefully
simpler!) ways you can achieve your goal -- see below.

First, a summary of the architecture:

The current DefaultAppService responds to AppQuitEvents as follows:

protected void onEvent(@SuppressWarnings("unused") final AppQuitEvent
event) {
getApp().quit();
}

Which delegates the operation to the App plugin with the highest priority.

The following App plugins ship with net.imagej:imagej:

* org.scijava.app.SciJavaApp [LOW priority] -- bundled with
org.scijava:scijava-common
* io.scif.SCIFIOApp [NORMAL priority] -- bundled with io.scif:scifio
* net.imagej.legacy.LegacyImageJApp [NORMAL priority] - bundled with
net.imagej:imagej-legacy
* net.imagej.app.ImageJApp [HIGH priority] -- bundled with
net.imagej:imagej-common
* net.imagej.app.ToplevelImageJApp [HIGH+1 priority] -- bundled with
net.imagej:imagej

So:
* If net.imagej:imagej is on the classpath, the ToplevelImageJApp will take
precedence.
Otherwise:
* If net.imagej:imagej-common is on the classpath, the ImageJApp takes
precedence.

However, ToplevelImageJApp extends ImageJApp and does not override quit().
So either way, the quit behavior will be the same:

  public void quit() {
    commandService.run(QuitProgram.class, true);
  }

https://github.com/imagej/imagej-common/blob/imagej-common-0.10.1/src/main/java/net/imagej/app/QuitProgram.java#L79-L96

Note that the devastating System.exit(0) is only called when
exitWhenQuitting is set to true. This is part of OptionsMisc:

https://github.com/imagej/imagej-common/blob/imagej-common-0.10.1/src/main/java/net/imagej/options/OptionsMisc.java#L75-L76

So, there are two potential ways I would suggest to achieve your goal:

1) Set the OptionsMisc exitWhenQuitting to false. This is handy if you
don't need a lot of control over exactly what happens when quit() is
called, but you just want to stop the System.exit(0) from firing.

2) Create a CellProfilerApp plugin and set it to a higher priority than
ImageJ. This solution makes the most sense to me because it gives you more
flexibility over the behavior of a couple other operations, too: about()
and prefs().

https://github.com/scijava/scijava-common/blob/scijava-common-2.35.0/src/main/java/org/scijava/app/App.java

Regards,
Curtis

On Wed, Nov 5, 2014 at 10:23 AM, Lee Kamentsky <leek at broadinstitute.org>
wrote:

> Oops never mind, what I tried actually worked. AppQuitEvent.consume() did
> the trick. Go figure - you guys really threw the whole kitchen sink into
> it, didn't you?
>
> @Plugin(type = Service.class, priority = Priority.HIGH_PRIORITY)
> public class CellProfilerAppService extends DefaultAppService {
> static boolean canQuit = false;
> public static void allowQuit() {
> canQuit = true;
> }
> public static void preventQuit() {
> canQuit = false;
> }
> @EventHandler
> public void onEvent(final AppQuitEvent event) {
> if (canQuit) {
> super.onEvent(event);
> } else {
> final UIService uiService = getContext().getService(UIService.class);
> final LogService logService = getContext().getService(LogService.class);
> if (uiService.isVisible()) {
> UserInterface ui = uiService.getDefaultUI();
> logService.info("Quit action: hide the application frame");
> ui.getApplicationFrame().setVisible(false);
> } else {
> logService.info("Quit action: do nothing");
> }
> event.consume();
> }
> }
>
> }
>
>
> On Wed, Nov 5, 2014 at 11:15 AM, Lee Kamentsky <leek at broadinstitute.org>
> wrote:
>
>> Hi all,
>> I had a class (
>> https://github.com/CellProfiler/CellProfiler/blob/master/java/src/main/java/org/cellprofiler/ijutils/CellProfilerAppEventService.java)
>> that implemented the deprecated AppEventService whose purpose was to
>> prevent ImageJ from quitting if a user closed its window by overriding
>> AppEventService.quit(). Quitting is pretty devastating for CellProfiler
>> since the process closes when the user's probable intent was to hide the
>> window.
>>
>> I'm hoping someone can give me a hint about how to do it now - I thought
>> I'd cheat by asking instead of figuring it out myself.
>>
>> Thanks in advance,
>> --Lee
>>
>
>
> _______________________________________________
> ImageJ-devel mailing list
> ImageJ-devel at imagej.net
> http://imagej.net/mailman/listinfo/imagej-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20141105/15046c70/attachment.html>


More information about the ImageJ-devel mailing list