1 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 27 public class PerspectiveTracker extends PerspectiveAdapter implements 28 IPageListener { 29 30 private IWorkbenchWindow window; 31 32 private IAction action; 33 34 41 protected PerspectiveTracker(IWorkbenchWindow window) { 42 Assert.isNotNull(window); 43 this.window = window; 44 window.addPageListener(this); 45 window.addPerspectiveListener(this); 46 } 47 48 55 public PerspectiveTracker(IWorkbenchWindow window, IAction action) { 56 this(window); 57 this.action = action; 58 update(); 59 } 60 61 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 } 82 83 public void perspectiveActivated(IWorkbenchPage page, 84 IPerspectiveDescriptor perspective) { 85 update(); 86 } 87 88 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 115 protected void update(IPerspectiveDescriptor persp) { 116 if (action != null) { 117 action.setEnabled(persp != null); 118 } 119 } 120 121 } 122 | Popular Tags |