#58918: LifeCycleManager should be in utilities version-2-3-26
authorjtulach@netbeans.org
Sat, 21 May 2005 15:43:17 +0000
changeset 214355e763eb90
parent 20 3789d4d84884
child 22 54762d3b0642
#58918: LifeCycleManager should be in utilities
openide.util/src/org/openide/LifecycleManager.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/openide.util/src/org/openide/LifecycleManager.java	Sat May 21 15:43:17 2005 +0000
     1.3 @@ -0,0 +1,70 @@
     1.4 +/*
     1.5 + *                 Sun Public License Notice
     1.6 + *
     1.7 + * The contents of this file are subject to the Sun Public License
     1.8 + * Version 1.0 (the "License"). You may not use this file except in
     1.9 + * compliance with the License. A copy of the License is available at
    1.10 + * http://www.sun.com/
    1.11 + *
    1.12 + * The Original Code is NetBeans. The Initial Developer of the Original
    1.13 + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
    1.14 + * Microsystems, Inc. All Rights Reserved.
    1.15 + */
    1.16 +package org.openide;
    1.17 +
    1.18 +import org.openide.util.Lookup;
    1.19 +
    1.20 +
    1.21 +/** Manages major aspects of the NetBeans lifecycle - currently saving all objects and exiting.
    1.22 + * @author Jesse Glick
    1.23 + * @since 3.14
    1.24 + */
    1.25 +public abstract class LifecycleManager {
    1.26 +    /** Subclass constructor. */
    1.27 +    protected LifecycleManager() {
    1.28 +    }
    1.29 +
    1.30 +    /**
    1.31 +     * Get the default lifecycle manager.
    1.32 +     * Normally this is found in {@link Lookup#getDefault} but if no instance is
    1.33 +     * found there, a fallback instance is returned which behaves as follows:
    1.34 +     * <ol>
    1.35 +     * <li>{@link #saveAll} does nothing
    1.36 +     * <li>{@link #exit} calls {@link System#exit} with an exit code of 0
    1.37 +     * </ol>
    1.38 +     * This is useful for unit tests and perhaps standalone library usage.
    1.39 +     * @return the default instance (never null)
    1.40 +     */
    1.41 +    public static LifecycleManager getDefault() {
    1.42 +        LifecycleManager lm = (LifecycleManager) Lookup.getDefault().lookup(LifecycleManager.class);
    1.43 +
    1.44 +        if (lm == null) {
    1.45 +            lm = new Trivial();
    1.46 +        }
    1.47 +
    1.48 +        return lm;
    1.49 +    }
    1.50 +
    1.51 +    /** Save all opened objects.
    1.52 +     */
    1.53 +    public abstract void saveAll();
    1.54 +
    1.55 +    /** Exit NetBeans.
    1.56 +     * This method will return only if {@link java.lang.System#exit} fails, or if at least one component of the
    1.57 +     * system refuses to exit (because it cannot be properly shut down).
    1.58 +     */
    1.59 +    public abstract void exit();
    1.60 +
    1.61 +    /** Fallback instance. */
    1.62 +    private static final class Trivial extends LifecycleManager {
    1.63 +        public Trivial() {
    1.64 +        }
    1.65 +
    1.66 +        public void exit() {
    1.67 +            System.exit(0);
    1.68 +        }
    1.69 +
    1.70 +        public void saveAll() {
    1.71 +        }
    1.72 +    }
    1.73 +}