KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > actions > OpenPerspectiveDialogAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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
12 package org.eclipse.ui.internal.actions;
13
14 import org.eclipse.core.runtime.Assert;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.ui.IPerspectiveDescriptor;
19 import org.eclipse.ui.IWorkbench;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.WorkbenchException;
22 import org.eclipse.ui.actions.ActionFactory;
23 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
24 import org.eclipse.ui.internal.Workbench;
25 import org.eclipse.ui.internal.WorkbenchImages;
26 import org.eclipse.ui.internal.WorkbenchMessages;
27 import org.eclipse.ui.internal.WorkbenchPlugin;
28 import org.eclipse.ui.internal.dialogs.SelectPerspectiveDialog;
29
30 /**
31  * Action to open the Open Perspective dialog.
32  *
33  * @since 3.1
34  */

35 public class OpenPerspectiveDialogAction extends Action implements
36         ActionFactory.IWorkbenchAction {
37
38     private IWorkbenchWindow workbenchWindow;
39     
40     /**
41      * Creates a new open perspective dialog action.
42      *
43      * @param window the window containing the action
44      */

45     public OpenPerspectiveDialogAction(IWorkbenchWindow window) {
46         Assert.isNotNull(window);
47         this.workbenchWindow = window;
48         setText(WorkbenchMessages.OpenPerspectiveDialogAction_text);
49         setToolTipText(WorkbenchMessages.OpenPerspectiveDialogAction_tooltip);
50         setImageDescriptor(WorkbenchImages.getImageDescriptor(
51               IWorkbenchGraphicConstants.IMG_ETOOL_NEW_PAGE));
52     }
53
54     /*
55      * (non-Javadoc)
56      *
57      * @see org.eclipse.jface.action.Action#run()
58      */

59     public void run() {
60         if (workbenchWindow == null) {
61             return;
62         }
63         SelectPerspectiveDialog dlg = new SelectPerspectiveDialog(workbenchWindow
64                 .getShell(), workbenchWindow.getWorkbench().getPerspectiveRegistry());
65         dlg.open();
66         if (dlg.getReturnCode() == Window.CANCEL) {
67             return;
68         }
69         IPerspectiveDescriptor desc = dlg.getSelection();
70         if (desc != null) {
71             try {
72                 IWorkbench workbench = workbenchWindow.getWorkbench();
73                 IAdaptable input = ((Workbench) workbench)
74                         .getDefaultPageInput();
75                 workbenchWindow.openPage(desc.getId(), input);
76             } catch (WorkbenchException e) {
77                 WorkbenchPlugin.log("Error opening perspective ", e); //$NON-NLS-1$
78
}
79         }
80     }
81     
82     /* (non-Javadoc)
83      * @see org.eclipse.ui.actions.ActionFactory.IWorkbenchAction#dispose()
84      */

85     public void dispose() {
86         workbenchWindow = null;
87     }
88 }
89
Popular Tags