This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 77210
Collapse All | Expand All

(-)a/openide.awt/src/org/netbeans/modules/openide/awt/SavableRegistry.java (-5 / +22 lines)
Lines 41-57 Link Here
41
 */
41
 */
42
package org.netbeans.modules.openide.awt;
42
package org.netbeans.modules.openide.awt;
43
43
44
import java.io.IOException;
45
import org.netbeans.api.actions.Savable;
44
import org.netbeans.spi.actions.AbstractSavable;
46
import org.netbeans.spi.actions.AbstractSavable;
47
import org.openide.LifecycleManager;
48
import org.openide.util.Exceptions;
45
import org.openide.util.Lookup;
49
import org.openide.util.Lookup;
46
import org.openide.util.RequestProcessor;
50
import org.openide.util.RequestProcessor;
47
import org.openide.util.lookup.AbstractLookup;
51
import org.openide.util.lookup.AbstractLookup;
48
import org.openide.util.lookup.InstanceContent;
52
import org.openide.util.lookup.InstanceContent;
53
import org.openide.util.lookup.ServiceProvider;
49
54
50
/**
55
@ServiceProvider(service=LifecycleManager.class)
51
 *
56
public final class SavableRegistry extends LifecycleManager {
52
 * @author Jaroslav Tulach <jtulach@netbeans.org>
53
 */
54
public final class SavableRegistry {
55
    private static final RequestProcessor RP = new RequestProcessor("Savable Registry");
57
    private static final RequestProcessor RP = new RequestProcessor("Savable Registry");
56
    private static final InstanceContent IC = new InstanceContent(RP);
58
    private static final InstanceContent IC = new InstanceContent(RP);
57
    private static final Lookup LOOKUP = new AbstractLookup(IC);
59
    private static final Lookup LOOKUP = new AbstractLookup(IC);
Lines 67-70 Link Here
67
    public static void unregister(AbstractSavable as) {
69
    public static void unregister(AbstractSavable as) {
68
        IC.remove(as);
70
        IC.remove(as);
69
    }
71
    }
72
73
    @Override
74
    public void saveAll() {
75
        for (Savable sa : getRegistry().lookupAll(Savable.class)) {
76
            try {
77
                sa.save();
78
            } catch (IOException ex) {
79
                Exceptions.printStackTrace(ex);
80
            }
81
        }
82
    }
83
84
    @Override
85
    public void exit() {
86
    }
70
}
87
}
(-)a/openide.awt/test/unit/src/org/netbeans/api/actions/SavableTest.java (+17 lines)
Lines 45-50 Link Here
45
import java.io.IOException;
45
import java.io.IOException;
46
import org.netbeans.junit.NbTestCase;
46
import org.netbeans.junit.NbTestCase;
47
import org.netbeans.spi.actions.AbstractSavable;
47
import org.netbeans.spi.actions.AbstractSavable;
48
import org.openide.LifecycleManager;
48
import org.openide.util.Lookup.Result;
49
import org.openide.util.Lookup.Result;
49
import org.openide.util.LookupEvent;
50
import org.openide.util.LookupEvent;
50
import org.openide.util.LookupListener;
51
import org.openide.util.LookupListener;
Lines 82-87 Link Here
82
        assertTrue("No other pending saves", Savable.REGISTRY.lookupAll(Savable.class).isEmpty());
83
        assertTrue("No other pending saves", Savable.REGISTRY.lookupAll(Savable.class).isEmpty());
83
    }
84
    }
84
85
86
    public void testLifecycleSafe() throws IOException {
87
        String id = "identity";
88
        DoSave savable = new DoSave(id, null, null);
89
        assertNotNull("Savable created", savable);
90
        
91
        assertTrue(
92
            "Is is among the list of savables that need save", 
93
            Savable.REGISTRY.lookupAll(Savable.class).contains(savable)
94
        );
95
        
96
        LifecycleManager.getDefault().saveAll();
97
        assertTrue("called", savable.save);
98
        
99
        assertTrue("No other pending saves", Savable.REGISTRY.lookupAll(Savable.class).isEmpty());
100
    }
101
85
    public void testTwoSavablesForEqual() throws IOException {
102
    public void testTwoSavablesForEqual() throws IOException {
86
        Object id = new Object();
103
        Object id = new Object();
87
        
104
        
(-)a/openide.util/src/org/openide/LifecycleManager.java (-10 / +30 lines)
Lines 67-79 Link Here
67
     * @return the default instance (never null)
67
     * @return the default instance (never null)
68
     */
68
     */
69
    public static LifecycleManager getDefault() {
69
    public static LifecycleManager getDefault() {
70
        LifecycleManager lm = Lookup.getDefault().lookup(LifecycleManager.class);
70
        return Proxy.INSTANCE;
71
72
        if (lm == null) {
73
            lm = new Trivial();
74
        }
75
76
        return lm;
77
    }
71
    }
78
72
79
    /** Save all opened objects.
73
    /** Save all opened objects.
Lines 96-111 Link Here
96
        throw new UnsupportedOperationException();
90
        throw new UnsupportedOperationException();
97
    }
91
    }
98
92
99
    /** Fallback instance. */
93
    private static final class Proxy extends LifecycleManager {
100
    private static final class Trivial extends LifecycleManager {
94
        private static final Proxy INSTANCE = new Proxy();
101
        public Trivial() {
95
        
96
        private Proxy() {
102
        }
97
        }
103
98
99
        @Override
104
        public void exit() {
100
        public void exit() {
101
            for (LifecycleManager lm : Lookup.getDefault().lookupAll(LifecycleManager.class)) {
102
                lm.exit();
103
            }
105
            System.exit(0);
104
            System.exit(0);
106
        }
105
        }
107
106
107
        @Override
108
        public void saveAll() {
108
        public void saveAll() {
109
            for (LifecycleManager lm : Lookup.getDefault().lookupAll(LifecycleManager.class)) {
110
                lm.saveAll();
111
            }
112
        }
113
114
        @Override
115
        public void markForRestart() throws UnsupportedOperationException {
116
            UnsupportedOperationException thr = null;
117
            for (LifecycleManager lm : Lookup.getDefault().lookupAll(LifecycleManager.class)) {
118
                try {
119
                    lm.markForRestart();
120
                    return;
121
                } catch (UnsupportedOperationException ex) {
122
                    thr = ex;
123
                }
124
            }
125
            if (thr == null) {
126
                thr = new UnsupportedOperationException();
127
            }
128
            throw thr;
109
        }
129
        }
110
    }
130
    }
111
}
131
}

Return to bug 77210