01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal;
11:
12: import org.eclipse.core.runtime.Assert;
13: import org.eclipse.jface.action.Action;
14: import org.eclipse.ui.IPerspectiveDescriptor;
15: import org.eclipse.ui.IWorkbenchPage;
16: import org.eclipse.ui.IWorkbenchWindow;
17: import org.eclipse.ui.actions.ActionFactory;
18:
19: /**
20: * Abstract superclass of actions which are enabled iff there is an active perspective
21: * in the window.
22: *
23: * @since 3.1
24: */
25: public abstract class PerspectiveAction extends Action implements
26: ActionFactory.IWorkbenchAction {
27:
28: /**
29: * The workbench window containing this action.
30: */
31: private IWorkbenchWindow workbenchWindow;
32:
33: /**
34: * Tracks perspective activation, to update this action's
35: * enabled state.
36: */
37: private PerspectiveTracker tracker;
38:
39: /**
40: * Constructs a new perspective action for the given window.
41: *
42: * @param window the window
43: */
44: protected PerspectiveAction(IWorkbenchWindow window) {
45: Assert.isNotNull(window);
46: this .workbenchWindow = window;
47: tracker = new PerspectiveTracker(window, this );
48: }
49:
50: /**
51: * Returns the window, or <code>null</code> if the action has been disposed.
52: *
53: * @return the window or <code>null</code>
54: */
55: protected IWorkbenchWindow getWindow() {
56: return workbenchWindow;
57: }
58:
59: /* (non-Javadoc)
60: * Method declared on IAction.
61: */
62: public void run() {
63: if (workbenchWindow == null) {
64: // action has been disposed
65: return;
66: }
67: IWorkbenchPage page = workbenchWindow.getActivePage();
68: if (page != null && page.getPerspective() != null) {
69: run(page, page.getPerspective());
70: }
71: }
72:
73: /**
74: * Runs the action, passing the active page and perspective.
75: *
76: * @param page the active page
77: * @param persp the active perspective
78: */
79: protected abstract void run(IWorkbenchPage page,
80: IPerspectiveDescriptor persp);
81:
82: /* (non-Javadoc)
83: * Method declared on ActionFactory.IWorkbenchAction.
84: */
85: public void dispose() {
86: if (workbenchWindow == null) {
87: // already disposed
88: return;
89: }
90: tracker.dispose();
91: workbenchWindow = null;
92: }
93:
94: }
|