KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > LifecycleManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide;
21
22 import org.openide.util.Lookup;
23
24 /** Manages major aspects of the NetBeans lifecycle - currently saving all objects and exiting.
25  * @author Jesse Glick
26  * @since 3.14
27  */

28 public abstract class LifecycleManager {
29     /** Subclass constructor. */
30     protected LifecycleManager() {
31     }
32
33     /**
34      * Get the default lifecycle manager.
35      * Normally this is found in {@link Lookup#getDefault} but if no instance is
36      * found there, a fallback instance is returned which behaves as follows:
37      * <ol>
38      * <li>{@link #saveAll} does nothing
39      * <li>{@link #exit} calls {@link System#exit} with an exit code of 0
40      * </ol>
41      * This is useful for unit tests and perhaps standalone library usage.
42      * @return the default instance (never null)
43      */

44     public static LifecycleManager getDefault() {
45         LifecycleManager lm = Lookup.getDefault().lookup(LifecycleManager.class);
46
47         if (lm == null) {
48             lm = new Trivial();
49         }
50
51         return lm;
52     }
53
54     /** Save all opened objects.
55      */

56     public abstract void saveAll();
57
58     /** Exit NetBeans.
59      * This method will return only if {@link java.lang.System#exit} fails, or if at least one component of the
60      * system refuses to exit (because it cannot be properly shut down).
61      */

62     public abstract void exit();
63
64     /** Fallback instance. */
65     private static final class Trivial extends LifecycleManager {
66         public Trivial() {
67         }
68
69         public void exit() {
70             System.exit(0);
71         }
72
73         public void saveAll() {
74         }
75     }
76 }
77
Popular Tags