001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.handlers;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.commands.AbstractHandler;
015: import org.eclipse.core.commands.ExecutionEvent;
016: import org.eclipse.core.commands.ExecutionException;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.jface.window.Window;
019: import org.eclipse.ui.IPerspectiveDescriptor;
020: import org.eclipse.ui.IWorkbench;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.IWorkbenchWindow;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.WorkbenchException;
025: import org.eclipse.ui.internal.WorkbenchPlugin;
026: import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog;
027:
028: /**
029: * Shows the given perspective. If no perspective is specified in the
030: * parameters, then this opens the perspective selection dialog.
031: *
032: * @since 3.1
033: */
034: public final class ShowPerspectiveHandler extends AbstractHandler {
035:
036: /**
037: * The name of the parameter providing the perspective identifier.
038: */
039: private static final String PARAMETER_NAME_VIEW_ID = "org.eclipse.ui.perspectives.showPerspective.perspectiveId"; //$NON-NLS-1$
040:
041: public final Object execute(final ExecutionEvent event)
042: throws ExecutionException {
043: IWorkbenchWindow window = HandlerUtil
044: .getActiveWorkbenchWindowChecked(event);
045:
046: // Get the view identifier, if any.
047: final Map parameters = event.getParameters();
048: final Object value = parameters.get(PARAMETER_NAME_VIEW_ID);
049: if (value == null) {
050: openOther(window);
051: } else {
052: openPerspective((String) value, window);
053: }
054:
055: return null;
056: }
057:
058: /**
059: * Opens a view selection dialog, allowing the user to chose a view.
060: *
061: * @throws ExecutionException
062: * If the perspective could not be opened.
063: */
064: private final void openOther(
065: final IWorkbenchWindow activeWorkbenchWindow)
066: throws ExecutionException {
067: final SelectPerspectiveDialog dialog = new SelectPerspectiveDialog(
068: activeWorkbenchWindow.getShell(), WorkbenchPlugin
069: .getDefault().getPerspectiveRegistry());
070: dialog.open();
071: if (dialog.getReturnCode() == Window.CANCEL) {
072: return;
073: }
074:
075: final IPerspectiveDescriptor descriptor = dialog.getSelection();
076: if (descriptor != null) {
077: openPerspective(descriptor.getId(), activeWorkbenchWindow);
078: }
079: }
080:
081: /**
082: * Opens the perspective with the given identifier.
083: *
084: * @param perspectiveId
085: * The perspective to open; must not be <code>null</code>
086: * @throws ExecutionException
087: * If the perspective could not be opened.
088: */
089: private final void openPerspective(final String perspectiveId,
090: final IWorkbenchWindow activeWorkbenchWindow)
091: throws ExecutionException {
092: final IWorkbench workbench = PlatformUI.getWorkbench();
093:
094: IAdaptable input = null;
095:
096: final IWorkbenchPage activePage = activeWorkbenchWindow
097: .getActivePage();
098: if (activePage != null) {
099: input = activePage.getInput();
100: }
101:
102: try {
103: workbench.showPerspective(perspectiveId,
104: activeWorkbenchWindow, input);
105: } catch (WorkbenchException e) {
106: throw new ExecutionException(
107: "Perspective could not be opened.", e); //$NON-NLS-1$
108: }
109: }
110: }
|