001: /*******************************************************************************
002: * Copyright (c) 2005, 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.core.runtime.Assert;
013: import org.eclipse.jface.action.IAction;
014: import org.eclipse.ui.IPageListener;
015: import org.eclipse.ui.IPerspectiveDescriptor;
016: import org.eclipse.ui.IWorkbenchPage;
017: import org.eclipse.ui.IWorkbenchWindow;
018: import org.eclipse.ui.PerspectiveAdapter;
019:
020: /**
021: * Utility class for tracking the active perspective in a window.
022: *
023: * @since 3.1
024: */
025: public class PerspectiveTracker extends PerspectiveAdapter implements
026: IPageListener {
027:
028: private IWorkbenchWindow window;
029:
030: private IAction action;
031:
032: /**
033: * Creates a perspective tracker for the given window.
034: * Subclasses should override <code>update(IPerspectiveDescriptor)</code>
035: * to get notified of perspective changes.
036: *
037: * @param window the window to track
038: */
039: protected PerspectiveTracker(IWorkbenchWindow window) {
040: Assert.isNotNull(window);
041: this .window = window;
042: window.addPageListener(this );
043: window.addPerspectiveListener(this );
044: }
045:
046: /**
047: * Creates a perspective tracker for the given window which will
048: * enable the given action only when there is an active perspective.
049: *
050: * @param window the window to track
051: * @param action the action to enable or disable
052: */
053: public PerspectiveTracker(IWorkbenchWindow window, IAction action) {
054: this (window);
055: this .action = action;
056: update();
057: }
058:
059: /**
060: * Disposes the tracker.
061: */
062: public void dispose() {
063: if (window != null) {
064: window.removePageListener(this );
065: window.removePerspectiveListener(this );
066: }
067: }
068:
069: public void pageActivated(IWorkbenchPage page) {
070: update();
071: }
072:
073: public void pageClosed(IWorkbenchPage page) {
074: update();
075: }
076:
077: public void pageOpened(IWorkbenchPage page) {
078: // ignore
079: }
080:
081: public void perspectiveActivated(IWorkbenchPage page,
082: IPerspectiveDescriptor perspective) {
083: update();
084: }
085:
086: /**
087: * Determines the active perspective in the window
088: * and calls <code>update(IPerspectiveDescriptor)</code>.
089: */
090: private void update() {
091: if (window != null) {
092: IPerspectiveDescriptor persp = null;
093: IWorkbenchPage page = window.getActivePage();
094: if (page != null) {
095: persp = page.getPerspective();
096: }
097: update(persp);
098: }
099: }
100:
101: /**
102: * Performs some function based on the active perspective in the window.
103: * <p>
104: * The default implementation enables the action (if given) if there
105: * is an active perspective, otherwise it disables it.
106: * </p>
107: * <p>
108: * Subclasses may override or extend.
109: * </p>
110: *
111: * @param persp the active perspective in the window, or <code>null</code> if none
112: */
113: protected void update(IPerspectiveDescriptor persp) {
114: if (action != null) {
115: action.setEnabled(persp != null);
116: }
117: }
118:
119: }
|