KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.ui.IEditorPart;
14 import org.eclipse.ui.IWorkbenchPage;
15 import org.eclipse.ui.IWorkbenchPart;
16 import org.eclipse.ui.IWorkbenchWindow;
17 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
18
19 /**
20  * The abstract superclass for actions that depend on the active editor.
21  * This implementation tracks the active editor (see <code>getActiveEditor</code>)
22  * and updates the availability of the action when an editor becomes
23  * active.
24  * <p>
25  * Subclasses must implement the following <code>IAction</code> method:
26  * <ul>
27  * <li><code>run</code> - to do the action's work</li>
28  * </ul>
29  * </p>
30  * <p>
31  * Subclasses may extend any of the <code>IPartListener</code> methods if the
32  * action availablity needs to be recalculated:
33  * <ul>
34  * <li><code>partActivated</code></li>
35  * <li><code>partDeactivated</code></li>
36  * <li><code>partOpened</code></li>
37  * <li><code>partClosed</code></li>
38  * <li><code>partBroughtToTop</code></li>
39  * </ul>
40  * </p>
41  * <p>
42  * Subclasses may extend any of the <code>IPageListener</code> methods if the
43  * action availablity needs to be recalculated:
44  * <ul>
45  * <li><code>pageActivated</code></li>
46  * <li><code>pageClosed</code></li>
47  * <li><code>pageOpened</code></li>
48  * </ul>
49  * </p>
50  * <p>
51  * This method implements the <code>IPartListener</code> and
52  * <code>IPageListener</code>interfaces, and automatically registers listeners
53  * so that it can keep its enablement state up to date. Ordinarily, the
54  * window's references to these listeners will be dropped automatically when
55  * the window closes. However, if the client needs to get rid of an action
56  * while the window is still open, the client must call
57  * {@link IWorkbenchAction#dispose dispose} to give the action an
58  * opportunity to deregister its listeners and to perform any other cleanup.
59  * </p>
60  */

61 public abstract class ActiveEditorAction extends PageEventAction {
62
63     private IEditorPart activeEditor;
64
65     /**
66      * Creates a new action with the given text.
67      *
68      * @param text the string used as the text for the action,
69      * or <code>null</code> if there is no text
70      * @param window the workbench window this action is
71      * registered with.
72      */

73     protected ActiveEditorAction(String JavaDoc text, IWorkbenchWindow window) {
74         super(text, window);
75         updateState();
76     }
77
78     /**
79      * Notification that the active editor tracked
80      * by the action is being activated.
81      *
82      * Subclasses may override.
83      */

84     protected void editorActivated(IEditorPart part) {
85     }
86
87     /**
88      * Notification that the active editor tracked
89      * by the action is being deactivated.
90      *
91      * Subclasses may override.
92      */

93     protected void editorDeactivated(IEditorPart part) {
94     }
95
96     /**
97      * Return the active editor
98      *
99      * @return the page's active editor, and <code>null</code>
100      * if no active editor or no active page.
101      */

102     public final IEditorPart getActiveEditor() {
103         return activeEditor;
104     }
105
106     /* (non-Javadoc)
107      * Method declared on PageEventAction.
108      */

109     public void pageActivated(IWorkbenchPage page) {
110         super.pageActivated(page);
111         updateActiveEditor();
112         updateState();
113     }
114
115     /* (non-Javadoc)
116      * Method declared on PageEventAction.
117      */

118     public void pageClosed(IWorkbenchPage page) {
119         super.pageClosed(page);
120         updateActiveEditor();
121         updateState();
122     }
123
124     /* (non-Javadoc)
125      * Method declared on PartEventAction.
126      */

127     public void partActivated(IWorkbenchPart part) {
128         super.partActivated(part);
129         if (part instanceof IEditorPart) {
130             updateActiveEditor();
131             updateState();
132         }
133     }
134
135     /* (non-Javadoc)
136      * Method declared on PartEventAction.
137      */

138     public void partBroughtToTop(IWorkbenchPart part) {
139         super.partBroughtToTop(part);
140         if (part instanceof IEditorPart) {
141             updateActiveEditor();
142             updateState();
143         }
144     }
145
146     /* (non-Javadoc)
147      * Method declared on PartEventAction.
148      */

149     public void partClosed(IWorkbenchPart part) {
150         super.partClosed(part);
151         if (part instanceof IEditorPart) {
152             updateActiveEditor();
153             updateState();
154         }
155     }
156
157     /* (non-Javadoc)
158      * Method declared on PartEventAction.
159      */

160     public void partDeactivated(IWorkbenchPart part) {
161         super.partDeactivated(part);
162         if (part instanceof IEditorPart) {
163             updateActiveEditor();
164             updateState();
165         }
166     }
167
168     /**
169      * Set the active editor
170      */

171     private void setActiveEditor(IEditorPart part) {
172         if (activeEditor == part) {
173             return;
174         }
175         if (activeEditor != null) {
176             editorDeactivated(activeEditor);
177         }
178         activeEditor = part;
179         if (activeEditor != null) {
180             editorActivated(activeEditor);
181         }
182     }
183
184     /**
185      * Update the active editor based on the current
186      * active page.
187      */

188     private void updateActiveEditor() {
189         if (getActivePage() == null) {
190             setActiveEditor(null);
191         } else {
192             setActiveEditor(getActivePage().getActiveEditor());
193         }
194     }
195
196     /**
197      * Update the state of the action. By default, the action
198      * is enabled if there is an active editor.
199      *
200      * Subclasses may override or extend this method.
201      */

202     protected void updateState() {
203         setEnabled(getActiveEditor() != null);
204     }
205
206 }
207
Popular Tags