1 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 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 PAGE_PROBLEMS_TITLE = WorkbenchMessages.OpenPerspectiveMenu_pageProblemsTitle; 54 55 private static String PAGE_PROBLEMS_MESSAGE = WorkbenchMessages.OpenPerspectiveMenu_errorUnknownInput; 56 57 60 public OpenPerspectiveMenu(IMenuManager menuManager, IWorkbenchWindow window) { 61 this(window); 62 this.parentMenuManager = menuManager; 63 } 64 65 78 public OpenPerspectiveMenu(IWorkbenchWindow window) { 79 this(window, null); 80 showActive(true); 81 } 82 83 90 public OpenPerspectiveMenu(IWorkbenchWindow window, IAdaptable input) { 91 super(window, "Open New Page Menu"); this.pageInput = input; 93 } 94 95 99 private boolean canRun() { 100 if (openPerspectiveSetting().equals( 101 IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE)) { 102 return replaceEnabled; 103 } 104 return true; 105 } 106 107 110 private String openPerspectiveSetting() { 111 return PrefUtil.getAPIPreferenceStore().getString( 112 IWorkbenchPreferenceConstants.OPEN_NEW_PERSPECTIVE); 113 } 114 115 121 protected void run(IPerspectiveDescriptor desc) { 122 openPage(desc, 0); 123 } 124 125 132 protected void run(IPerspectiveDescriptor desc, SelectionEvent event) { 133 openPage(desc, event.stateMask); 134 } 135 136 139 private void openPage(IPerspectiveDescriptor desc, int keyStateMask) { 140 if (pageInput == null) { 142 StatusUtil.handleStatus(PAGE_PROBLEMS_TITLE 143 + ": " + PAGE_PROBLEMS_MESSAGE, StatusManager.SHOW); return; 145 } 146 147 try { 149 getWindow().getWorkbench().showPerspective(desc.getId(), 150 getWindow(), pageInput); 151 } catch (WorkbenchException e) { 152 StatusUtil.handleStatus( 153 PAGE_PROBLEMS_TITLE + ": " + e.getMessage(), e, StatusManager.SHOW); 155 } 156 } 157 158 163 public void setPageInput(IAdaptable input) { 164 pageInput = input; 165 } 166 167 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 |