How to hook into start-up and shutdown phase

Warning

This is a very draft version.

 

In the following it is explained how you can initialisation you module during the start-up phase of the application or if you need to clean up some allocated resources when you module is shutdown.

import org.openide.modules.OnStart;
import org.openide.modules.OnStop;
import org.openide.windows.OnShowing;

import java.util.concurrent.Callable;

public class Module {

    private Module() {
    }

    @OnStart
    public static class StartOp implements Runnable {

        @Override
        public void run() {

        }
    }

    @OnShowing
    public static class ShowingOp implements Runnable {

        @Override
        public void run() {

        }
    }

	@OnStop
	public static class MaybeStopOp implements Callable {

    	@Override
	    public Boolean call() {
    	    return Boolean.TRUE;
	}
  
    @OnStop
	public static class StopOp implements Runnable {

    	@Override
    	public void run() {

    }

}

In your maven pom file you will need the following NetBeans dependencies

<dependency>
	// needed for the @OnStart and @OnStop annotations
    <groupId>org.netbeans.api</groupId>
    <artifactId>org-openide-modules</artifactId>
    <version>RELEASE802</version>
</dependency>
<dependency>
	// needed for the @OnShowing annotation
    <groupId>org.netbeans.api</groupId>
    <artifactId>org-openide-windows</artifactId>
    <version>RELEASE802</version>
</dependency>

@OnStart

All OnStart Runnables declared by various modules are invoked in parallel and as soon as possible. Do stuff here which has to be done very early and only once for your module.

@OnShowing

All OnShowing Runnables are invoked when the windowing system is started and ready. Here you can initialise your UI components.

@OnStop

  • Callable
    The OnStop Callables annotated with OnStop are first consulted if shutdown is requested by the user. Callables can prevent the shutdown by returning False. This might be the case if download is still in progress.
  • Runnable
    If all Callables allow the shutdown the Runnables annotated with OnStop are invoked. Here you can clean up allocated resources and shutdown services.