KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.IEditorPart;
18 import org.eclipse.ui.IEditorReference;
19 import org.eclipse.ui.IPropertyListener;
20 import org.eclipse.ui.IWorkbenchPage;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchWindow;
23
24 /**
25  * Closes all editors except ones with unsaved changes.
26  */

27 public class CloseAllSavedAction extends PageEventAction implements
28         IPropertyListener {
29
30     /**
31      * List of parts (element type: <code>IWorkbenchPart</code>)
32      * against which this class has outstanding property listeners registered.
33      */

34     private List JavaDoc partsWithListeners = new ArrayList JavaDoc(1);
35
36     /**
37      * Create an instance of this class.
38      * @param window the window
39      */

40     public CloseAllSavedAction(IWorkbenchWindow window) {
41         super(WorkbenchMessages.CloseAllSavedAction_text, window);
42         setToolTipText(WorkbenchMessages.CloseAllSavedAction_toolTip);
43         // @issue Should create a ID in IWorkbenchActionConstants when it becames API?
44
setId("closeAllSaved"); //$NON-NLS-1$
45
updateState();
46         window.getWorkbench().getHelpSystem().setHelp(this,
47                 IWorkbenchHelpContextIds.CLOSE_ALL_SAVED_ACTION);
48         setActionDefinitionId("org.eclipse.ui.file.closeAllSaved"); //$NON-NLS-1$
49
}
50
51     /* (non-Javadoc)
52      * Method declared on PageEventAction.
53      */

54     public void pageActivated(IWorkbenchPage page) {
55         super.pageActivated(page);
56         updateState();
57     }
58
59     /* (non-Javadoc)
60      * Method declared on PageEventAction.
61      */

62     public void pageClosed(IWorkbenchPage page) {
63         super.pageClosed(page);
64         updateState();
65     }
66
67     /* (non-Javadoc)
68      * Method declared on PartEventAction.
69      */

70     public void partClosed(IWorkbenchPart part) {
71         super.partClosed(part);
72         if (part instanceof IEditorPart) {
73             part.removePropertyListener(this);
74             partsWithListeners.remove(part);
75             updateState();
76         }
77     }
78
79     /* (non-Javadoc)
80      * Method declared on PartEventAction.
81      */

82     public void partOpened(IWorkbenchPart part) {
83         super.partOpened(part);
84         if (part instanceof IEditorPart) {
85             part.addPropertyListener(this);
86             partsWithListeners.add(part);
87             updateState();
88         }
89     }
90
91     /* (non-Javadoc)
92      * Method declared on IPropertyListener.
93      */

94     public void propertyChanged(Object JavaDoc source, int propID) {
95         if (source instanceof IEditorPart) {
96             if (propID == IEditorPart.PROP_DIRTY) {
97                 updateState();
98             }
99         }
100     }
101
102     /* (non-Javadoc)
103      * Method declared on Action.
104      */

105     public void run() {
106         if (getWorkbenchWindow() == null) {
107             // action has been dispose
108
return;
109         }
110         IWorkbenchPage page = getActivePage();
111         if (page != null) {
112             ((WorkbenchPage) page).closeAllSavedEditors();
113         }
114     }
115
116     /**
117      * Enable the action if there at least one editor open.
118      */

119     private void updateState() {
120         IWorkbenchPage page = getActivePage();
121         if (page == null) {
122             setEnabled(false);
123             return;
124         }
125         IEditorReference editors[] = page.getEditorReferences();
126         for (int i = 0; i < editors.length; i++) {
127             if (!editors[i].isDirty()) {
128                 setEnabled(true);
129                 return;
130             }
131         }
132         setEnabled(false);
133     }
134
135     /* (non-Javadoc)
136      * Method declared on PageEventAction.
137      */

138     public void dispose() {
139         super.dispose();
140         for (Iterator JavaDoc it = partsWithListeners.iterator(); it.hasNext();) {
141             IWorkbenchPart part = (IWorkbenchPart) it.next();
142             part.removePropertyListener(this);
143         }
144         partsWithListeners.clear();
145     }
146
147 }
148
Popular Tags