KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IPropertyListener;
20 import org.eclipse.ui.ISaveablePart;
21 import org.eclipse.ui.IViewPart;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25 import org.eclipse.ui.internal.util.Util;
26
27 /**
28  * The abstract superclass for save actions that depend on the active editor.
29  */

30 public abstract class BaseSaveAction extends ActiveEditorAction {
31     /*
32      * The view-related code below was added to track the view with focus
33      * in order to support save actions from a view (see bug 10234).
34      */

35     
36     /**
37      * List of parts (element type: <code>IWorkbenchPart</code>)
38      * against which this class has outstanding property listeners registered.
39      */

40     private List JavaDoc partsWithListeners = new ArrayList JavaDoc(1);
41
42     private final IPropertyListener propListener = new IPropertyListener() {
43         public void propertyChanged(Object JavaDoc source, int propId) {
44             if (source == getActiveEditor()) {
45                 if (propId == IEditorPart.PROP_DIRTY) {
46                     updateState();
47                 }
48             }
49         }
50     };
51
52     /**
53      * Creates a new action with the given text.
54      *
55      * @param text the string used as the text for the action,
56      * or <code>null</code> if there is no text
57      * @param window the workbench window this action is
58      * registered with.
59      */

60     protected BaseSaveAction(String JavaDoc text, IWorkbenchWindow window) {
61         super(text, window);
62     }
63
64     /* (non-Javadoc)
65      * Method declared on ActiveEditorAction.
66      */

67     protected void editorActivated(IEditorPart part) {
68         if (part != null) {
69             part.addPropertyListener(propListener);
70             partsWithListeners.add(part);
71         }
72     }
73
74     /* (non-Javadoc)
75      * Method declared on ActiveEditorAction.
76      */

77     protected void editorDeactivated(IEditorPart part) {
78         if (part != null) {
79             part.removePropertyListener(propListener);
80             partsWithListeners.remove(part);
81         }
82     }
83
84     private IViewPart activeView;
85
86     private final IPropertyListener propListener2 = new IPropertyListener() {
87         public void propertyChanged(Object JavaDoc source, int propId) {
88             if (source == activeView) {
89                 if (propId == IEditorPart.PROP_DIRTY) {
90                     updateState();
91                 }
92             }
93         }
94     };
95
96     /** the active saveable part is tracked in order to listen to its dirty events */
97     private ISaveablePart activeSaveablePart;
98     
99     private final IPropertyListener propListener3 = new IPropertyListener() {
100         public void propertyChanged(Object JavaDoc source, int propId) {
101             if (source == activeSaveablePart) {
102                 if (propId == IEditorPart.PROP_DIRTY) {
103                     updateState();
104                 }
105             }
106         }
107     };
108     
109     /* (non-Javadoc)
110      * Method declared on PageEventAction.
111      */

112     public void pageActivated(IWorkbenchPage page) {
113         super.pageActivated(page);
114         updateActiveView();
115         updateState();
116     }
117
118     /* (non-Javadoc)
119      * Method declared on PageEventAction.
120      */

121     public void pageClosed(IWorkbenchPage page) {
122         super.pageClosed(page);
123         updateActiveView();
124         updateState();
125     }
126
127     /* (non-Javadoc)
128      * Method declared on PartEventAction.
129      */

130     public void partActivated(IWorkbenchPart part) {
131         super.partActivated(part);
132         if (part instanceof IViewPart) {
133             updateActiveView();
134             updateState();
135         }
136     }
137
138     /* (non-Javadoc)
139      * Method declared on PartEventAction.
140      */

141     public void partClosed(IWorkbenchPart part) {
142         super.partClosed(part);
143         if (part instanceof IViewPart) {
144             updateActiveView();
145             updateState();
146         }
147     }
148
149     /* (non-Javadoc)
150      * Method declared on PartEventAction.
151      */

152     public void partDeactivated(IWorkbenchPart part) {
153         super.partDeactivated(part);
154         if (part instanceof IViewPart) {
155             updateActiveView();
156             updateState();
157         }
158     }
159
160     /**
161      * Update the active view based on the current
162      * active page.
163      */

164     private void updateActiveView() {
165         if (getActivePage() == null) {
166             setActiveView(null);
167         } else {
168             setActiveView(getActivePage().getActivePart());
169         }
170     }
171
172     /**
173      *
174      */

175     private void updateActiveSaveablePart() {
176         if (activeSaveablePart instanceof IWorkbenchPart) {
177             ((IWorkbenchPart)activeSaveablePart).removePropertyListener(propListener3);
178             partsWithListeners.remove(activeSaveablePart);
179         }
180         activeSaveablePart = getSaveableView();
181         if (activeSaveablePart == activeView) {
182             // no need to listen to the same part twice
183
activeSaveablePart = null;
184         }
185         if (activeSaveablePart instanceof IWorkbenchPart) {
186             ((IWorkbenchPart)activeSaveablePart).addPropertyListener(propListener3);
187             partsWithListeners.add(activeSaveablePart);
188         }
189     }
190
191     /**
192      * Set the active editor
193      */

194     private void setActiveView(IWorkbenchPart part) {
195         if (activeView == part) {
196             return;
197         }
198         if (activeView != null) {
199             activeView.removePropertyListener(propListener2);
200             partsWithListeners.remove(activeView);
201         }
202         if (part instanceof IViewPart) {
203             activeView = (IViewPart) part;
204         } else {
205             activeView = null;
206         }
207         if (activeView != null) {
208             activeView.addPropertyListener(propListener2);
209             partsWithListeners.add(activeView);
210         }
211         updateActiveSaveablePart();
212     }
213
214     protected final ISaveablePart getSaveableView() {
215         if (activeView == null) {
216             return null;
217         }
218
219         return (ISaveablePart) Util.getAdapter(activeView, ISaveablePart.class);
220     }
221
222     /* (non-Javadoc)
223      * Method declared on PageEventAction.
224      */

225     public void dispose() {
226         super.dispose();
227         for (Iterator JavaDoc it = partsWithListeners.iterator(); it.hasNext();) {
228             IWorkbenchPart part = (IWorkbenchPart) it.next();
229             part.removePropertyListener(propListener);
230             part.removePropertyListener(propListener2);
231             part.removePropertyListener(propListener3);
232         }
233         partsWithListeners.clear();
234     }
235 }
236
Popular Tags