KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > OpenPerspectiveMenu


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
12 package org.eclipse.ui.actions;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.swt.events.SelectionEvent;
17 import org.eclipse.ui.IPerspectiveDescriptor;
18 import org.eclipse.ui.IWorkbenchPreferenceConstants;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.WorkbenchException;
21 import org.eclipse.ui.internal.WorkbenchMessages;
22 import org.eclipse.ui.internal.misc.StatusUtil;
23 import org.eclipse.ui.internal.util.PrefUtil;
24 import org.eclipse.ui.statushandlers.StatusManager;
25
26 /**
27  * A menu for window creation in the workbench.
28  * <p>
29  * An <code>OpenPerspectiveMenu</code> is used to populate a menu with
30  * actions that will open a new perspective. If the user selects one of
31  * these items either a new page is added to the workbench, a new
32  * workbench window is created with the chosen perspective or the current
33  * perspective will be replaced with the new onw.
34  * </p><p>
35  * The visible perspectives within the menu may also be updated dynamically to
36  * reflect user preference.
37  * </p><p>
38  * The input for the page is determined by the value of <code>pageInput</code>.
39  * The input should be passed into the constructor of this class or set using
40  * the <code>setPageInput</code> method.
41  * </p><p>
42  * This class may be instantiated; it is not intended to be subclassed.
43  * </p>
44  * @deprecated See IWorkbench.showPerspective methods.
45  */

46 public class OpenPerspectiveMenu extends PerspectiveMenu {
47     private IAdaptable pageInput;
48
49     private IMenuManager parentMenuManager;
50
51     private boolean replaceEnabled = true;
52
53     private static String JavaDoc PAGE_PROBLEMS_TITLE = WorkbenchMessages.OpenPerspectiveMenu_pageProblemsTitle;
54
55     private static String JavaDoc PAGE_PROBLEMS_MESSAGE = WorkbenchMessages.OpenPerspectiveMenu_errorUnknownInput;
56
57     /**
58      * Constructs a new menu.
59      */

60     public OpenPerspectiveMenu(IMenuManager menuManager, IWorkbenchWindow window) {
61         this(window);
62         this.parentMenuManager = menuManager;
63     }
64
65     /**
66      * Constructs a new instance of <code>OpenNewPageMenu</code>.
67      * <p>
68      * If this method is used be sure to set the page input by invoking
69      * <code>setPageInput</code>. The page input is required when the user
70      * selects an item in the menu. At that point the menu will attempt to
71      * open a new page with the selected perspective and page input. If there
72      * is no page input an error dialog will be opened.
73      * </p>
74      *
75      * @param window the window where a new page is created if an item within
76      * the menu is selected
77      */

78     public OpenPerspectiveMenu(IWorkbenchWindow window) {
79         this(window, null);
80         showActive(true);
81     }
82
83     /**
84      * Constructs a new instance of <code>OpenNewPageMenu</code>.
85      *
86      * @param window the window where a new page is created if an item within
87      * the menu is selected
88      * @param input the page input
89      */

90     public OpenPerspectiveMenu(IWorkbenchWindow window, IAdaptable input) {
91         super(window, "Open New Page Menu");//$NON-NLS-1$
92
this.pageInput = input;
93     }
94
95     /**
96      * Return whether or not the menu can be run. Answer true unless the current mode
97      * is replace and the replaceEnabled flag is false.
98      */

99     private boolean canRun() {
100         if (openPerspectiveSetting().equals(
101                 IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE)) {
102             return replaceEnabled;
103         }
104         return true;
105     }
106
107     /**
108      * Return the current perspective setting.
109      */

110     private String JavaDoc openPerspectiveSetting() {
111         return PrefUtil.getAPIPreferenceStore().getString(
112                 IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE);
113     }
114
115     /**
116      * Runs an action for a particular perspective. Opens the perspective supplied
117      * in a new window or a new page depending on the workbench preference.
118      *
119      * @param desc the selected perspective
120      */

121     protected void run(IPerspectiveDescriptor desc) {
122         openPage(desc, 0);
123     }
124
125     /**
126      * Runs an action for a particular perspective. Check for shift or control events
127      * to decide which event to run.
128      *
129      * @param desc the selected perspective
130      * @param event the event sent along with the selection callback
131      */

132     protected void run(IPerspectiveDescriptor desc, SelectionEvent event) {
133         openPage(desc, event.stateMask);
134     }
135
136     /* (non-Javadoc)
137      * Opens a new page with a particular perspective and input.
138      */

139     private void openPage(IPerspectiveDescriptor desc, int keyStateMask) {
140         // Verify page input.
141
if (pageInput == null) {
142             StatusUtil.handleStatus(PAGE_PROBLEMS_TITLE
143                     + ": " + PAGE_PROBLEMS_MESSAGE, StatusManager.SHOW); //$NON-NLS-1$
144
return;
145         }
146
147         // Open the page.
148
try {
149             getWindow().getWorkbench().showPerspective(desc.getId(),
150                     getWindow(), pageInput);
151         } catch (WorkbenchException e) {
152             StatusUtil.handleStatus(
153                     PAGE_PROBLEMS_TITLE + ": " + e.getMessage(), e, //$NON-NLS-1$
154
StatusManager.SHOW);
155         }
156     }
157
158     /**
159      * Sets the page input.
160      *
161      * @param input the page input
162      */

163     public void setPageInput(IAdaptable input) {
164         pageInput = input;
165     }
166
167     /**
168      * Set whether replace menu item is enabled within its parent menu.
169      */

170     public void setReplaceEnabled(boolean isEnabled) {
171         if (replaceEnabled != isEnabled) {
172             replaceEnabled = isEnabled;
173             if (canRun() && parentMenuManager != null) {
174                 parentMenuManager.update(true);
175             }
176         }
177     }
178 }
179
Popular Tags