001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.ui.IEditorPart;
013: import org.eclipse.ui.IWorkbenchPage;
014: import org.eclipse.ui.IWorkbenchPart;
015: import org.eclipse.ui.IWorkbenchWindow;
016: import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
017:
018: /**
019: * The abstract superclass for actions that depend on the active editor.
020: * This implementation tracks the active editor (see <code>getActiveEditor</code>)
021: * and updates the availability of the action when an editor becomes
022: * active.
023: * <p>
024: * Subclasses must implement the following <code>IAction</code> method:
025: * <ul>
026: * <li><code>run</code> - to do the action's work</li>
027: * </ul>
028: * </p>
029: * <p>
030: * Subclasses may extend any of the <code>IPartListener</code> methods if the
031: * action availablity needs to be recalculated:
032: * <ul>
033: * <li><code>partActivated</code></li>
034: * <li><code>partDeactivated</code></li>
035: * <li><code>partOpened</code></li>
036: * <li><code>partClosed</code></li>
037: * <li><code>partBroughtToTop</code></li>
038: * </ul>
039: * </p>
040: * <p>
041: * Subclasses may extend any of the <code>IPageListener</code> methods if the
042: * action availablity needs to be recalculated:
043: * <ul>
044: * <li><code>pageActivated</code></li>
045: * <li><code>pageClosed</code></li>
046: * <li><code>pageOpened</code></li>
047: * </ul>
048: * </p>
049: * <p>
050: * This method implements the <code>IPartListener</code> and
051: * <code>IPageListener</code>interfaces, and automatically registers listeners
052: * so that it can keep its enablement state up to date. Ordinarily, the
053: * window's references to these listeners will be dropped automatically when
054: * the window closes. However, if the client needs to get rid of an action
055: * while the window is still open, the client must call
056: * {@link IWorkbenchAction#dispose dispose} to give the action an
057: * opportunity to deregister its listeners and to perform any other cleanup.
058: * </p>
059: */
060: public abstract class ActiveEditorAction extends PageEventAction {
061:
062: private IEditorPart activeEditor;
063:
064: /**
065: * Creates a new action with the given text.
066: *
067: * @param text the string used as the text for the action,
068: * or <code>null</code> if there is no text
069: * @param window the workbench window this action is
070: * registered with.
071: */
072: protected ActiveEditorAction(String text, IWorkbenchWindow window) {
073: super (text, window);
074: updateState();
075: }
076:
077: /**
078: * Notification that the active editor tracked
079: * by the action is being activated.
080: *
081: * Subclasses may override.
082: */
083: protected void editorActivated(IEditorPart part) {
084: }
085:
086: /**
087: * Notification that the active editor tracked
088: * by the action is being deactivated.
089: *
090: * Subclasses may override.
091: */
092: protected void editorDeactivated(IEditorPart part) {
093: }
094:
095: /**
096: * Return the active editor
097: *
098: * @return the page's active editor, and <code>null</code>
099: * if no active editor or no active page.
100: */
101: public final IEditorPart getActiveEditor() {
102: return activeEditor;
103: }
104:
105: /* (non-Javadoc)
106: * Method declared on PageEventAction.
107: */
108: public void pageActivated(IWorkbenchPage page) {
109: super .pageActivated(page);
110: updateActiveEditor();
111: updateState();
112: }
113:
114: /* (non-Javadoc)
115: * Method declared on PageEventAction.
116: */
117: public void pageClosed(IWorkbenchPage page) {
118: super .pageClosed(page);
119: updateActiveEditor();
120: updateState();
121: }
122:
123: /* (non-Javadoc)
124: * Method declared on PartEventAction.
125: */
126: public void partActivated(IWorkbenchPart part) {
127: super .partActivated(part);
128: if (part instanceof IEditorPart) {
129: updateActiveEditor();
130: updateState();
131: }
132: }
133:
134: /* (non-Javadoc)
135: * Method declared on PartEventAction.
136: */
137: public void partBroughtToTop(IWorkbenchPart part) {
138: super .partBroughtToTop(part);
139: if (part instanceof IEditorPart) {
140: updateActiveEditor();
141: updateState();
142: }
143: }
144:
145: /* (non-Javadoc)
146: * Method declared on PartEventAction.
147: */
148: public void partClosed(IWorkbenchPart part) {
149: super .partClosed(part);
150: if (part instanceof IEditorPart) {
151: updateActiveEditor();
152: updateState();
153: }
154: }
155:
156: /* (non-Javadoc)
157: * Method declared on PartEventAction.
158: */
159: public void partDeactivated(IWorkbenchPart part) {
160: super .partDeactivated(part);
161: if (part instanceof IEditorPart) {
162: updateActiveEditor();
163: updateState();
164: }
165: }
166:
167: /**
168: * Set the active editor
169: */
170: private void setActiveEditor(IEditorPart part) {
171: if (activeEditor == part) {
172: return;
173: }
174: if (activeEditor != null) {
175: editorDeactivated(activeEditor);
176: }
177: activeEditor = part;
178: if (activeEditor != null) {
179: editorActivated(activeEditor);
180: }
181: }
182:
183: /**
184: * Update the active editor based on the current
185: * active page.
186: */
187: private void updateActiveEditor() {
188: if (getActivePage() == null) {
189: setActiveEditor(null);
190: } else {
191: setActiveEditor(getActivePage().getActiveEditor());
192: }
193: }
194:
195: /**
196: * Update the state of the action. By default, the action
197: * is enabled if there is an active editor.
198: *
199: * Subclasses may override or extend this method.
200: */
201: protected void updateState() {
202: setEnabled(getActiveEditor() != null);
203: }
204:
205: }
|