KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > SaveAllAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ui.IPropertyListener;
18 import org.eclipse.ui.ISaveablePart;
19 import org.eclipse.ui.ISaveablesLifecycleListener;
20 import org.eclipse.ui.ISaveablesSource;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.IWorkbenchWindow;
24 import org.eclipse.ui.Saveable;
25
26 /**
27  * Global action that saves all targets in the
28  * workbench that implement ISaveTarget interface.
29  * The action keeps track of opened save targets
30  * and their 'save' state. If none of the currently
31  * opened targets needs saving, it will disable.
32  * This action is somewhat different from all
33  * other global actions in that it works on
34  * multiple targets at the same time i.e. it
35  * does not disconnect from the target when it
36  * becomes deactivated.
37  */

38 public class SaveAllAction extends PageEventAction implements IPropertyListener {
39     /**
40      * List of parts (element type: <code>IWorkbenchPart</code>)
41      * against which this class has outstanding property listeners registered.
42      */

43     private List JavaDoc partsWithListeners = new ArrayList JavaDoc(1);
44     private IWorkbenchPart openPart;
45
46     /**
47      * The default constructor.
48      *
49      * @param window the window
50      */

51     public SaveAllAction(IWorkbenchWindow window) {
52         super(WorkbenchMessages.SaveAll_text, window);
53         setToolTipText(WorkbenchMessages.SaveAll_toolTip);
54         setId("saveAll"); //$NON-NLS-1$
55
setEnabled(false);
56         window.getWorkbench().getHelpSystem().setHelp(this,
57                 IWorkbenchHelpContextIds.SAVE_ALL_ACTION);
58         setImageDescriptor(WorkbenchImages
59                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVEALL_EDIT));
60         setDisabledImageDescriptor(WorkbenchImages
61                 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_SAVEALL_EDIT_DISABLED));
62         setActionDefinitionId("org.eclipse.ui.file.saveAll"); //$NON-NLS-1$
63
}
64
65     /* (non-Javadoc)
66      * Method declared on PageEventAction.
67      */

68     public void pageActivated(IWorkbenchPage page) {
69         super.pageActivated(page);
70         updateState();
71     }
72
73     /* (non-Javadoc)
74      * Method declared on PageEventAction.
75      */

76     public void pageClosed(IWorkbenchPage page) {
77         super.pageClosed(page);
78         updateState();
79     }
80
81     /* (non-Javadoc)
82      * Method declared on PartEventAction.
83      */

84     public void partClosed(IWorkbenchPart part) {
85         super.partClosed(part);
86         if (part instanceof ISaveablePart) {
87             part.removePropertyListener(this);
88             partsWithListeners.remove(part);
89             updateState();
90         }
91     }
92
93     /* (non-Javadoc)
94      * Method declared on PartEventAction.
95      */

96     public void partOpened(IWorkbenchPart part) {
97         super.partOpened(part);
98         if (part instanceof ISaveablePart) {
99             part.addPropertyListener(this);
100             partsWithListeners.add(part);
101             // We need to temporarily cache the opened part
102
// because saveable views are not registered
103
// with a perspective until after this method
104
// is called. We can't pass it through to
105
// update because it's protected. An async
106
// call to update may be a better approach.
107
// See bug 93784 [WorkbenchParts] View not yet added to perspective when partOpened sent
108
openPart = part;
109             updateState();
110             openPart = null;
111         }
112     }
113
114     /* (non-Javadoc)
115      * Method declared on IPropertyListener.
116      */

117     public void propertyChanged(Object JavaDoc source, int propID) {
118         if (source instanceof ISaveablePart) {
119             if (propID == ISaveablePart.PROP_DIRTY) {
120                 updateState();
121             }
122         }
123     }
124
125     /* (non-Javadoc)
126      * Method declared on Action.
127      */

128     public void run() {
129         if (getWorkbenchWindow() == null) {
130             // action has been disposed
131
return;
132         }
133         WorkbenchPage page = (WorkbenchPage) getActivePage();
134         if (page != null) {
135             // The second parameter is true to also save saveables from non-part
136
// sources, see bug 139004.
137
page.saveAllEditors(false, true);
138             updateState();
139         }
140     }
141
142     /**
143      * Updates availability depending on number of
144      * targets that need saving.
145      */

146     protected void updateState() {
147         // Workaround for bug 93784 [WorkbenchParts] View not yet added to perspective when partOpened sent
148
if (openPart != null && openPart.getSite().getPage().equals(getActivePage()) && ((ISaveablePart) openPart).isDirty()) {
149             setEnabled(true);
150         }
151         else {
152             WorkbenchPage page = (WorkbenchPage) getActivePage();
153             if (page == null) {
154                 setEnabled(false);
155             } else {
156                 if (page.getDirtyParts().length > 0) {
157                     setEnabled(true);
158                 } else {
159                     // Since Save All also saves saveables from non-part sources,
160
// look if any such saveables exist and are dirty.
161
SaveablesList saveablesList = (SaveablesList) page
162                             .getWorkbenchWindow().getWorkbench().getService(
163                                     ISaveablesLifecycleListener.class);
164                     ISaveablesSource[] nonPartSources = saveablesList.getNonPartSources();
165                     for (int i = 0; i < nonPartSources.length; i++) {
166                         Saveable[] saveables = nonPartSources[i].getSaveables();
167                         for (int j = 0; j < saveables.length; j++) {
168                             if (saveables[j].isDirty()) {
169                                 setEnabled(true);
170                                 return;
171                             }
172                         }
173                     }
174                     setEnabled(false);
175                 }
176             }
177         }
178     }
179
180     /* (non-Javadoc)
181      * Method declared on PageEventAction.
182      */

183     public void dispose() {
184         super.dispose();
185         for (Iterator JavaDoc it = partsWithListeners.iterator(); it.hasNext();) {
186             IWorkbenchPart part = (IWorkbenchPart) it.next();
187             part.removePropertyListener(this);
188         }
189         partsWithListeners.clear();
190     }
191 }
192
Popular Tags