1 11 package org.eclipse.ui.handlers; 12 13 import java.util.Map ; 14 15 import org.eclipse.core.commands.AbstractHandler; 16 import org.eclipse.core.commands.ExecutionEvent; 17 import org.eclipse.core.commands.ExecutionException; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.jface.window.Window; 20 import org.eclipse.ui.IPerspectiveDescriptor; 21 import org.eclipse.ui.IWorkbench; 22 import org.eclipse.ui.IWorkbenchPage; 23 import org.eclipse.ui.IWorkbenchWindow; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.WorkbenchException; 26 import org.eclipse.ui.internal.WorkbenchPlugin; 27 import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog; 28 29 35 public final class ShowPerspectiveHandler extends AbstractHandler { 36 37 40 private static final String PARAMETER_NAME_VIEW_ID = "org.eclipse.ui.perspectives.showPerspective.perspectiveId"; 42 public final Object execute(final ExecutionEvent event) 43 throws ExecutionException { 44 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); 45 46 final Map parameters = event.getParameters(); 48 final Object value = parameters.get(PARAMETER_NAME_VIEW_ID); 49 if (value == null) { 50 openOther(window); 51 } else { 52 openPerspective((String ) value, window); 53 } 54 55 return null; 56 } 57 58 64 private final void openOther(final IWorkbenchWindow activeWorkbenchWindow) 65 throws ExecutionException { 66 final SelectPerspectiveDialog dialog = new SelectPerspectiveDialog( 67 activeWorkbenchWindow.getShell(), WorkbenchPlugin.getDefault() 68 .getPerspectiveRegistry()); 69 dialog.open(); 70 if (dialog.getReturnCode() == Window.CANCEL) { 71 return; 72 } 73 74 final IPerspectiveDescriptor descriptor = dialog.getSelection(); 75 if (descriptor != null) { 76 openPerspective(descriptor.getId(), activeWorkbenchWindow); 77 } 78 } 79 80 88 private final void openPerspective(final String perspectiveId, 89 final IWorkbenchWindow activeWorkbenchWindow) 90 throws ExecutionException { 91 final IWorkbench workbench = PlatformUI.getWorkbench(); 92 93 IAdaptable input = null; 94 95 final IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage(); 96 if (activePage != null) { 97 input = activePage.getInput(); 98 } 99 100 try { 101 workbench.showPerspective(perspectiveId, activeWorkbenchWindow, 102 input); 103 } catch (WorkbenchException e) { 104 throw new ExecutionException("Perspective could not be opened.", e); } 106 } 107 } 108 | Popular Tags |