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.IPageListener;
013: import org.eclipse.ui.IWorkbenchPage;
014: import org.eclipse.ui.IWorkbenchWindow;
015: import org.eclipse.ui.actions.ActionFactory;
016: import org.eclipse.ui.actions.PartEventAction;
017: import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
018:
019: /**
020: * The abstract superclass for actions that listen to page activation and
021: * open/close events. This implementation tracks the active page (see
022: * <code>getActivePage</code>) and provides a convenient place to monitor
023: * page lifecycle events that could affect the availability of the action.
024: * <p>
025: * Subclasses must implement the following <code>IAction</code> method:
026: * <ul>
027: * <li><code>run</code> - to do the action's work</li>
028: * </ul>
029: * </p>
030: * <p>
031: * Subclasses may extend any of the <code>IPartListener</code> methods if the
032: * action availablity needs to be recalculated:
033: * <ul>
034: * <li><code>partActivated</code></li>
035: * <li><code>partDeactivated</code></li>
036: * <li><code>partOpened</code></li>
037: * <li><code>partClosed</code></li>
038: * <li><code>partBroughtToTop</code></li>
039: * </ul>
040: * </p>
041: * <p>
042: * Subclasses may extend any of the <code>IPageListener</code> methods if the
043: * action availablity needs to be recalculated:
044: * <ul>
045: * <li><code>pageActivated</code></li>
046: * <li><code>pageClosed</code></li>
047: * <li><code>pageOpened</code></li>
048: * </ul>
049: * </p>
050: * <p>
051: * This method implements the <code>IPartListener</code> and
052: * <code>IPageListener</code>interfaces, and automatically registers listeners
053: * so that it can keep its enablement state up to date. Ordinarily, the
054: * window's references to these listeners will be dropped automatically when
055: * the window closes. However, if the client needs to get rid of an action
056: * while the window is still open, the client must call
057: * {@link IWorkbenchAction#dispose dispose} to give the action an
058: * opportunity to deregister its listeners and to perform any other cleanup.
059: * </p>
060: */
061: public abstract class PageEventAction extends PartEventAction implements
062: IPageListener, ActionFactory.IWorkbenchAction {
063: /**
064: * The active page, or <code>null</code> if none.
065: */
066: private IWorkbenchPage activePage;
067:
068: /**
069: * The workbench window this action is registered with.
070: */
071: private IWorkbenchWindow workbenchWindow;
072:
073: /**
074: * Creates a new action with the given text. Register this
075: * action with the workbench window for page lifecycle
076: * events.
077: *
078: * @param text the string used as the text for the action,
079: * or <code>null</code> if there is no text
080: * @param window the workbench window this action is
081: * registered with
082: */
083: protected PageEventAction(String text, IWorkbenchWindow window) {
084: super (text);
085: if (window == null) {
086: throw new IllegalArgumentException();
087: }
088: this .workbenchWindow = window;
089: this .activePage = window.getActivePage();
090: this .workbenchWindow.addPageListener(this );
091: this .workbenchWindow.getPartService().addPartListener(this );
092: }
093:
094: /**
095: * Returns the currently active page in the workbench window.
096: *
097: * @return currently active page in the workbench window,
098: * or <code>null</code> in none
099: */
100: public final IWorkbenchPage getActivePage() {
101: return activePage;
102: }
103:
104: /**
105: * Returns the workbench window this action applies to.
106: *
107: * @return the workbench window, or <code>null</code> if this action has been
108: * disposed
109: */
110: public final IWorkbenchWindow getWorkbenchWindow() {
111: return workbenchWindow;
112: }
113:
114: /**
115: * The <code>PageEventAction</code> implementation of this
116: * <code>IPageListener</code> method records that the given page is active.
117: * Subclasses may extend this method if action availability has to be
118: * recalculated.
119: */
120: public void pageActivated(IWorkbenchPage page) {
121: this .activePage = page;
122: }
123:
124: /**
125: * The <code>PageEventAction</code> implementation of this
126: * <code>IPageListener</code> method clears the active page if it just closed.
127: * Subclasses may extend this method if action availability has to be
128: * recalculated.
129: */
130: public void pageClosed(IWorkbenchPage page) {
131: if (page == activePage) {
132: activePage = null;
133: }
134: }
135:
136: /**
137: * The <code>PageEventAction</code> implementation of this
138: * <code>IPageListener</code> method does nothing. Subclasses should extend
139: * this method if action availability has to be recalculated.
140: */
141: public void pageOpened(IWorkbenchPage page) {
142: // do nothing
143: }
144:
145: /**
146: * The <code>PageEventAction</code> implementation of this
147: * <code>ActionFactory.IWorkbenchAction</code> method
148: * deregisters the part and page listener adding by the constructor.
149: * Subclasses should extend this method to do additional
150: * cleanup.
151: *
152: * @since 3.0
153: */
154: public void dispose() {
155: if (workbenchWindow == null) {
156: // action has already been disposed
157: return;
158: }
159: workbenchWindow.removePageListener(this);
160: workbenchWindow.getPartService().removePartListener(this);
161: workbenchWindow = null;
162: }
163: }
|