KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > handlers > ShowPerspectiveHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 package org.eclipse.ui.handlers;
12
13 import java.util.Map JavaDoc;
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 /**
30  * Shows the given perspective. If no perspective is specified in the
31  * parameters, then this opens the perspective selection dialog.
32  *
33  * @since 3.1
34  */

35 public final class ShowPerspectiveHandler extends AbstractHandler {
36
37     /**
38      * The name of the parameter providing the perspective identifier.
39      */

40     private static final String JavaDoc PARAMETER_NAME_VIEW_ID = "org.eclipse.ui.perspectives.showPerspective.perspectiveId"; //$NON-NLS-1$
41

42     public final Object JavaDoc execute(final ExecutionEvent event)
43             throws ExecutionException {
44         IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
45         
46         // Get the view identifier, if any.
47
final Map JavaDoc parameters = event.getParameters();
48         final Object JavaDoc value = parameters.get(PARAMETER_NAME_VIEW_ID);
49         if (value == null) {
50             openOther(window);
51         } else {
52             openPerspective((String JavaDoc) value, window);
53         }
54
55         return null;
56     }
57
58     /**
59      * Opens a view selection dialog, allowing the user to chose a view.
60      *
61      * @throws ExecutionException
62      * If the perspective could not be opened.
63      */

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     /**
81      * Opens the perspective with the given identifier.
82      *
83      * @param perspectiveId
84      * The perspective to open; must not be <code>null</code>
85      * @throws ExecutionException
86      * If the perspective could not be opened.
87      */

88     private final void openPerspective(final String JavaDoc 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); //$NON-NLS-1$
105
}
106     }
107 }
108
Popular Tags