KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.ui.internal;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.ui.IPageListener;
17 import org.eclipse.ui.IPerspectiveDescriptor;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PerspectiveAdapter;
21
22 /**
23  * Utility class for tracking the active perspective in a window.
24  *
25  * @since 3.1
26  */

27 public class PerspectiveTracker extends PerspectiveAdapter implements
28         IPageListener {
29
30     private IWorkbenchWindow window;
31
32     private IAction action;
33
34     /**
35      * Creates a perspective tracker for the given window.
36      * Subclasses should override <code>update(IPerspectiveDescriptor)</code>
37      * to get notified of perspective changes.
38      *
39      * @param window the window to track
40      */

41     protected PerspectiveTracker(IWorkbenchWindow window) {
42         Assert.isNotNull(window);
43         this.window = window;
44         window.addPageListener(this);
45         window.addPerspectiveListener(this);
46     }
47
48     /**
49      * Creates a perspective tracker for the given window which will
50      * enable the given action only when there is an active perspective.
51      *
52      * @param window the window to track
53      * @param action the action to enable or disable
54      */

55     public PerspectiveTracker(IWorkbenchWindow window, IAction action) {
56         this(window);
57         this.action = action;
58         update();
59     }
60
61     /**
62      * Disposes the tracker.
63      */

64     public void dispose() {
65         if (window != null) {
66             window.removePageListener(this);
67             window.removePerspectiveListener(this);
68         }
69     }
70
71     public void pageActivated(IWorkbenchPage page) {
72         update();
73     }
74
75     public void pageClosed(IWorkbenchPage page) {
76         update();
77     }
78
79     public void pageOpened(IWorkbenchPage page) {
80         // ignore
81
}
82
83     public void perspectiveActivated(IWorkbenchPage page,
84             IPerspectiveDescriptor perspective) {
85         update();
86     }
87
88     /**
89      * Determines the active perspective in the window
90      * and calls <code>update(IPerspectiveDescriptor)</code>.
91      */

92     private void update() {
93         if (window != null) {
94             IPerspectiveDescriptor persp = null;
95             IWorkbenchPage page = window.getActivePage();
96             if (page != null) {
97                 persp = page.getPerspective();
98             }
99             update(persp);
100         }
101     }
102
103     /**
104      * Performs some function based on the active perspective in the window.
105      * <p>
106      * The default implementation enables the action (if given) if there
107      * is an active perspective, otherwise it disables it.
108      * </p>
109      * <p>
110      * Subclasses may override or extend.
111      * </p>
112      *
113      * @param persp the active perspective in the window, or <code>null</code> if none
114      */

115     protected void update(IPerspectiveDescriptor persp) {
116         if (action != null) {
117             action.setEnabled(persp != null);
118         }
119     }
120
121 }
122
Popular Tags