I'm a Civil Engineer in Computation and Computer Science of Andrés Bello University, Santiago - Chile. I'm working as a Software Engineer in Ingerencia, an small company that develop software solutions. In paralell i'm working on a project with a new company that calls ID (Ingeniería Domótica, in english means Home Automation Engineering), where i'm developing a software solution based on Netbeans platform 6.8 and some other interesting technologies like PLCBUS protocol and hardware devices. I love Linux and I love Java. In my other life I'm a heavy weight lifter, 7 years already. What can I say, I'm combining the develop of the mind and body. Daniel has posted 3 posts at DZone. View Full User Profile

How to Create a Custom NetBeans Platform LifecycleManager

06.07.2010
| 4854 views |
  • submit to reddit

One of the things we need when we develop a desktop application is closing control: "What should our application do if we press the exit button? Should we save our work? Should we confirm the exit? Or should we just close the application and lose everything?"

If you are using the NetBeans Platform, it is possible that you have found yourself in this dilemma. The solution is VERY simple.

First, we need to create a class with an annotation for overriding the NetBeans Platform's default org.openide.LifecycleManager. Call the class "MyLifeCycleManager" and then annotate the class as follows:

@ServiceProvider(service=LifecycleManager.class, position=1)

Then we need to add the code for our class, for example:

@ServiceProvider(service=LifecycleManager.class, position=1)
public class MyLifecycleManager extends LifecycleManager {

@Override
public void saveAll() {
}

@Override
public void exit() {
Set<Topcomponent> tcs = TopComponent.getRegistry().getOpened();
Iterator<Topcomponent> it = tcs.iterator();
while (it.hasNext()) {
TopComponent tc = it.next();
if (tc instanceof DepartmentEditorTopComponent) {
tc.close();
}
}
Collection c = Lookup.getDefault().lookup(new Lookup.Template(LifecycleManager.class)).allInstances();
for (Iterator i = c.iterator(); i.hasNext();) {
LifecycleManager lm = (LifecycleManager) i.next();
if (lm != this) {
lm.exit();
}
}
}

}

And this is how it looks in the IDE:

When we extend "LifecycleManager", we need to implement the "exit" method and it is in that implementation where we code what we want to do before closing our application. In this case, I worry about closing properly a TopComponent I made (DepartmentTopComponent, from my Home Automation Application). You MUST write the following piece of code in the last part of exit method, so that your application can close properly (System.exit(0) will not work):

Collection c = Lookup.getDefault().lookup (new Lookup.Template (LifecycleManager.class)).allInstances();
for (Iterator i=c.iterator(); i.hasNext();) {
LifecycleManager lm = (LifecycleManager) i.next();
if (lm != this) {
lm.exit();
}
}

With that, you have your own implementation of LifecycleManager and you can provide specific things that need to be done before closing your application.

 

0
Your rating: None
Published at DZone with permission of its author, Daniel Morales.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Rupert Young replied on Sat, 2014/07/05 - 4:38pm

Hello,

This looks like what I need but when I click exit I get this error:

"java.lang.AssertionError: Window System API is required to be called from AWT thread"

Any ideas for a resolution?

Regards,

Rupert

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.