| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2008 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 | * @noextend This class is not intended to be subclassed by clients. |
| 46 | */ |
| 47 | public class OpenPerspectiveMenu extends PerspectiveMenu { |
| 48 | private IAdaptable pageInput; |
| 49 | |
| 50 | private IMenuManager parentMenuManager; |
| 51 | |
| 52 | private boolean replaceEnabled = true; |
| 53 | |
| 54 | private static String PAGE_PROBLEMS_TITLE = WorkbenchMessages.OpenPerspectiveMenu_pageProblemsTitle; |
| 55 | |
| 56 | private static String PAGE_PROBLEMS_MESSAGE = WorkbenchMessages.OpenPerspectiveMenu_errorUnknownInput; |
| 57 | |
| 58 | /** |
| 59 | * Constructs a new menu. |
| 60 | */ |
| 61 | public OpenPerspectiveMenu(IMenuManager menuManager, IWorkbenchWindow window) { |
| 62 | this(window); |
| 63 | this.parentMenuManager = menuManager; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Constructs a new instance of <code>OpenNewPageMenu</code>. |
| 68 | * <p> |
| 69 | * If this method is used be sure to set the page input by invoking |
| 70 | * <code>setPageInput</code>. The page input is required when the user |
| 71 | * selects an item in the menu. At that point the menu will attempt to |
| 72 | * open a new page with the selected perspective and page input. If there |
| 73 | * is no page input an error dialog will be opened. |
| 74 | * </p> |
| 75 | * |
| 76 | * @param window the window where a new page is created if an item within |
| 77 | * the menu is selected |
| 78 | */ |
| 79 | public OpenPerspectiveMenu(IWorkbenchWindow window) { |
| 80 | this(window, null); |
| 81 | showActive(true); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Constructs a new instance of <code>OpenNewPageMenu</code>. |
| 86 | * |
| 87 | * @param window the window where a new page is created if an item within |
| 88 | * the menu is selected |
| 89 | * @param input the page input |
| 90 | */ |
| 91 | public OpenPerspectiveMenu(IWorkbenchWindow window, IAdaptable input) { |
| 92 | super(window, "Open New Page Menu");//$NON-NLS-1$ |
| 93 | this.pageInput = input; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return whether or not the menu can be run. Answer true unless the current mode |
| 98 | * is replace and the replaceEnabled flag is false. |
| 99 | */ |
| 100 | private boolean canRun() { |
| 101 | if (openPerspectiveSetting().equals( |
| 102 | IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE)) { |
| 103 | return replaceEnabled; |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return the current perspective setting. |
| 110 | */ |
| 111 | private String openPerspectiveSetting() { |
| 112 | return PrefUtil.getAPIPreferenceStore().getString( |
| 113 | IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Runs an action for a particular perspective. Opens the perspective supplied |
| 118 | * in a new window or a new page depending on the workbench preference. |
| 119 | * |
| 120 | * @param desc the selected perspective |
| 121 | */ |
| 122 | protected void run(IPerspectiveDescriptor desc) { |
| 123 | openPage(desc, 0); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Runs an action for a particular perspective. Check for shift or control events |
| 128 | * to decide which event to run. |
| 129 | * |
| 130 | * @param desc the selected perspective |
| 131 | * @param event the event sent along with the selection callback |
| 132 | */ |
| 133 | protected void run(IPerspectiveDescriptor desc, SelectionEvent event) { |
| 134 | openPage(desc, event.stateMask); |
| 135 | } |
| 136 | |
| 137 | /* (non-Javadoc) |
| 138 | * Opens a new page with a particular perspective and input. |
| 139 | */ |
| 140 | private void openPage(IPerspectiveDescriptor desc, int keyStateMask) { |
| 141 | // Verify page input. |
| 142 | if (pageInput == null) { |
| 143 | StatusUtil.handleStatus(PAGE_PROBLEMS_TITLE |
| 144 | + ": " + PAGE_PROBLEMS_MESSAGE, StatusManager.SHOW); //$NON-NLS-1$ |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | // Open the page. |
| 149 | try { |
| 150 | getWindow().getWorkbench().showPerspective(desc.getId(), |
| 151 | getWindow(), pageInput); |
| 152 | } catch (WorkbenchException e) { |
| 153 | StatusUtil.handleStatus( |
| 154 | PAGE_PROBLEMS_TITLE + ": " + e.getMessage(), e, //$NON-NLS-1$ |
| 155 | StatusManager.SHOW); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets the page input. |
| 161 | * |
| 162 | * @param input the page input |
| 163 | */ |
| 164 | public void setPageInput(IAdaptable input) { |
| 165 | pageInput = input; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set whether replace menu item is enabled within its parent menu. |
| 170 | */ |
| 171 | public void setReplaceEnabled(boolean isEnabled) { |
| 172 | if (replaceEnabled != isEnabled) { |
| 173 | replaceEnabled = isEnabled; |
| 174 | if (canRun() && parentMenuManager != null) { |
| 175 | parentMenuManager.update(true); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |