KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > SaveAllAction


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.actions;
21
22
23 import javax.swing.event.*;
24 import org.openide.LifecycleManager;
25 import org.openide.loaders.DataObject;
26 import org.openide.util.*;
27 import org.openide.util.actions.CallableSystemAction;
28
29 /** Save all open objects.
30 * @see DataObject#getRegistry
31 * @see LifecycleManager#saveAll
32 *
33 * @author Jan Jancura, Ian Formanek
34 */

35 public final class SaveAllAction extends CallableSystemAction {
36     /** to make sure only one instance of this class can run at a time */
37     private static final Object JavaDoc RUNNING = new Object JavaDoc ();
38
39     /** Reference to the change listener
40     * (we treat it weakly, so we have to to prevent it from
41     * being finalized before finalization of this action) */

42     private ChangeListener chl;
43
44     /* Creates new HashMap and inserts some properties to it.
45     * @return the hash map
46     */

47     protected void initialize () {
48         super.initialize ();
49         // false by default
50
putProperty (PROP_ENABLED, Boolean.FALSE);
51         // listen to the changes
52
chl = new ModifiedListL();
53         DataObject.getRegistry().addChangeListener(
54             (ChangeListener)(org.openide.util.WeakListeners.change(chl, DataObject.getRegistry ())));
55     }
56
57     public String JavaDoc getName() {
58         return NbBundle.getMessage(org.openide.loaders.DataObject.class, "SaveAll");
59     }
60
61     public HelpCtx getHelpCtx() {
62         return new HelpCtx (SaveAllAction.class);
63     }
64
65     protected String JavaDoc iconResource () {
66         return "org/openide/loaders/saveAll.gif"; // NOI18N
67
}
68
69     public void performAction() {
70         synchronized (RUNNING) {
71             while (getProperty (RUNNING) != null) {
72                 try {
73                     RUNNING.wait ();
74                 } catch (InterruptedException JavaDoc ex) {
75                     Exceptions.printStackTrace(ex);
76                 }
77             }
78             putProperty (RUNNING, RUNNING);
79         }
80         try {
81             LifecycleManager.getDefault().saveAll();
82         } finally {
83             synchronized (RUNNING) {
84                 putProperty (RUNNING, null);
85                 RUNNING.notifyAll ();
86             }
87             
88         }
89     }
90     
91     protected boolean asynchronous() {
92         return true;
93     }
94
95     /* Listens to the chnages in list of modified data objects
96     * and enables / disables this action appropriately */

97     final class ModifiedListL implements ChangeListener {
98         public void stateChanged(final ChangeEvent evt) {
99             Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
100                 public void run() {
101                     setEnabled(((java.util.Set JavaDoc)evt.getSource()).size() > 0);
102                 }
103             });
104         }
105     } // end of ModifiedListL inner class
106
}
107
Popular Tags