01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.actions;
11:
12: import org.eclipse.core.runtime.Assert;
13: import org.eclipse.core.runtime.IAdaptable;
14: import org.eclipse.jface.action.Action;
15: import org.eclipse.jface.window.Window;
16: import org.eclipse.ui.IPerspectiveDescriptor;
17: import org.eclipse.ui.IWorkbench;
18: import org.eclipse.ui.IWorkbenchWindow;
19: import org.eclipse.ui.WorkbenchException;
20: import org.eclipse.ui.actions.ActionFactory;
21: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
22: import org.eclipse.ui.internal.Workbench;
23: import org.eclipse.ui.internal.WorkbenchImages;
24: import org.eclipse.ui.internal.WorkbenchMessages;
25: import org.eclipse.ui.internal.WorkbenchPlugin;
26: import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog;
27:
28: /**
29: * Action to open the Open Perspective dialog.
30: *
31: * @since 3.1
32: */
33: public class OpenPerspectiveDialogAction extends Action implements
34: ActionFactory.IWorkbenchAction {
35:
36: private IWorkbenchWindow workbenchWindow;
37:
38: /**
39: * Creates a new open perspective dialog action.
40: *
41: * @param window the window containing the action
42: */
43: public OpenPerspectiveDialogAction(IWorkbenchWindow window) {
44: Assert.isNotNull(window);
45: this .workbenchWindow = window;
46: setText(WorkbenchMessages.OpenPerspectiveDialogAction_text);
47: setToolTipText(WorkbenchMessages.OpenPerspectiveDialogAction_tooltip);
48: setImageDescriptor(WorkbenchImages
49: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_NEW_PAGE));
50: }
51:
52: /*
53: * (non-Javadoc)
54: *
55: * @see org.eclipse.jface.action.Action#run()
56: */
57: public void run() {
58: if (workbenchWindow == null) {
59: return;
60: }
61: SelectPerspectiveDialog dlg = new SelectPerspectiveDialog(
62: workbenchWindow.getShell(), workbenchWindow
63: .getWorkbench().getPerspectiveRegistry());
64: dlg.open();
65: if (dlg.getReturnCode() == Window.CANCEL) {
66: return;
67: }
68: IPerspectiveDescriptor desc = dlg.getSelection();
69: if (desc != null) {
70: try {
71: IWorkbench workbench = workbenchWindow.getWorkbench();
72: IAdaptable input = ((Workbench) workbench)
73: .getDefaultPageInput();
74: workbenchWindow.openPage(desc.getId(), input);
75: } catch (WorkbenchException e) {
76: WorkbenchPlugin.log("Error opening perspective ", e); //$NON-NLS-1$
77: }
78: }
79: }
80:
81: /* (non-Javadoc)
82: * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
83: */
84: public void dispose() {
85: workbenchWindow = null;
86: }
87: }
|