KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ChangeToPerspectiveMenu


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.ui.internal;
12
13 import org.eclipse.core.runtime.IAdaptable;
14 import org.eclipse.jface.dialogs.ErrorDialog;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.ui.IPerspectiveDescriptor;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.WorkbenchException;
21 import org.eclipse.ui.actions.PerspectiveMenu;
22 import org.eclipse.ui.internal.util.PrefUtil;
23
24 /**
25  * Change the perspective of the active page in the window
26  * to the selected one.
27  */

28 public class ChangeToPerspectiveMenu extends PerspectiveMenu {
29
30     /**
31      * Constructor for ChangeToPerspectiveMenu.
32      *
33      * @param window the workbench window this action applies to.
34      */

35     public ChangeToPerspectiveMenu(IWorkbenchWindow window, String JavaDoc id) {
36         super(window, id);
37         // indicate that a open perspectives submenu has been created
38
((WorkbenchWindow) window)
39                 .addSubmenu(WorkbenchWindow.OPEN_PERSPECTIVE_SUBMENU);
40         showActive(true);
41     }
42
43     /**
44      * Returns the available list of perspectives to display in the menu.
45      * Extends the super implementation by ensuring that the current perspective
46      * is included in the list.
47      *
48      * @return an <code>ArrayList<code> of perspective items <code>IPerspectiveDescriptor</code>
49      */

50     /*
51      protected ArrayList getPerspectiveItems() {
52      ArrayList list = super.getPerspectiveItems();
53      IWorkbenchWindow window = getWindow();
54      IWorkbenchPage page = window.getActivePage();
55      if (page != null) {
56      IPerspectiveDescriptor desc = page.getPerspective();
57      if (desc != null) {
58      if (!list.contains(desc)) {
59      list.add(desc);
60      }
61      }
62      }
63      return list;
64      }
65      */

66
67     /* (non-Javadoc)
68      * @see PerspectiveMenu#run(IPerspectiveDescriptor)
69      */

70     protected void run(IPerspectiveDescriptor desc) {
71         IPreferenceStore store = PrefUtil.getInternalPreferenceStore();
72         int mode = store.getInt(IPreferenceConstants.OPEN_PERSP_MODE);
73         IWorkbenchPage page = getWindow().getActivePage();
74         IPerspectiveDescriptor persp = null;
75         if (page != null) {
76             persp = page.getPerspective();
77         }
78
79         // Only open a new window if user preference is set and the window
80
// has an active perspective.
81
if (IPreferenceConstants.OPM_NEW_WINDOW == mode && persp != null) {
82             try {
83                 IWorkbench workbench = getWindow().getWorkbench();
84                 IAdaptable input = ((Workbench) workbench)
85                         .getDefaultPageInput();
86                 workbench.openWorkbenchWindow(desc.getId(), input);
87             } catch (WorkbenchException e) {
88                 handleWorkbenchException(e);
89             }
90         } else {
91             if (page != null) {
92                 page.setPerspective(desc);
93             } else {
94                 try {
95                     IWorkbench workbench = getWindow().getWorkbench();
96                     IAdaptable input = ((Workbench) workbench)
97                             .getDefaultPageInput();
98                     getWindow().openPage(desc.getId(), input);
99                 } catch (WorkbenchException e) {
100                     handleWorkbenchException(e);
101                 }
102             }
103         }
104     }
105
106     /**
107      * Handles workbench exception
108      */

109     private void handleWorkbenchException(WorkbenchException e) {
110         ErrorDialog.openError(getWindow().getShell(), WorkbenchMessages.ChangeToPerspectiveMenu_errorTitle,
111                 e.getMessage(), e.getStatus());
112     }
113 }
114
Popular Tags