KickJava   Java API By Example, From Geeks To Geeks.

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


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

62 public abstract class PageEventAction extends PartEventAction implements
63         IPageListener, ActionFactory.IWorkbenchAction {
64     /**
65      * The active page, or <code>null</code> if none.
66      */

67     private IWorkbenchPage activePage;
68
69     /**
70      * The workbench window this action is registered with.
71      */

72     private IWorkbenchWindow workbenchWindow;
73
74     /**
75      * Creates a new action with the given text. Register this
76      * action with the workbench window for page lifecycle
77      * events.
78      *
79      * @param text the string used as the text for the action,
80      * or <code>null</code> if there is no text
81      * @param window the workbench window this action is
82      * registered with
83      */

84     protected PageEventAction(String JavaDoc text, IWorkbenchWindow window) {
85         super(text);
86         if (window == null) {
87             throw new IllegalArgumentException JavaDoc();
88         }
89         this.workbenchWindow = window;
90         this.activePage = window.getActivePage();
91         this.workbenchWindow.addPageListener(this);
92         this.workbenchWindow.getPartService().addPartListener(this);
93     }
94
95     /**
96      * Returns the currently active page in the workbench window.
97      *
98      * @return currently active page in the workbench window,
99      * or <code>null</code> in none
100      */

101     public final IWorkbenchPage getActivePage() {
102         return activePage;
103     }
104
105     /**
106      * Returns the workbench window this action applies to.
107      *
108      * @return the workbench window, or <code>null</code> if this action has been
109      * disposed
110      */

111     public final IWorkbenchWindow getWorkbenchWindow() {
112         return workbenchWindow;
113     }
114
115     /**
116      * The <code>PageEventAction</code> implementation of this
117      * <code>IPageListener</code> method records that the given page is active.
118      * Subclasses may extend this method if action availability has to be
119      * recalculated.
120      */

121     public void pageActivated(IWorkbenchPage page) {
122         this.activePage = page;
123     }
124
125     /**
126      * The <code>PageEventAction</code> implementation of this
127      * <code>IPageListener</code> method clears the active page if it just closed.
128      * Subclasses may extend this method if action availability has to be
129      * recalculated.
130      */

131     public void pageClosed(IWorkbenchPage page) {
132         if (page == activePage) {
133             activePage = null;
134         }
135     }
136
137     /**
138      * The <code>PageEventAction</code> implementation of this
139      * <code>IPageListener</code> method does nothing. Subclasses should extend
140      * this method if action availability has to be recalculated.
141      */

142     public void pageOpened(IWorkbenchPage page) {
143         // do nothing
144
}
145
146     /**
147      * The <code>PageEventAction</code> implementation of this
148      * <code>ActionFactory.IWorkbenchAction</code> method
149      * deregisters the part and page listener adding by the constructor.
150      * Subclasses should extend this method to do additional
151      * cleanup.
152      *
153      * @since 3.0
154      */

155     public void dispose() {
156         if (workbenchWindow == null) {
157             // action has already been disposed
158
return;
159         }
160         workbenchWindow.removePageListener(this);
161         workbenchWindow.getPartService().removePartListener(this);
162         workbenchWindow = null;
163     }
164 }
165
Popular Tags