1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.rcp.application;
  34 
  35 import org.eclipse.core.runtime.Platform;
  36 import org.eclipse.jface.action.GroupMarker;
  37 import org.eclipse.jface.action.IContributionItem;
  38 import org.eclipse.jface.action.IMenuManager;
  39 import org.eclipse.jface.action.MenuManager;
  40 import org.eclipse.jface.action.Separator;
  41 import org.eclipse.ui.IWorkbenchActionConstants;
  42 import org.eclipse.ui.IWorkbenchCommandConstants;
  43 import org.eclipse.ui.IWorkbenchWindow;
  44 import org.eclipse.ui.actions.ActionFactory;
  45 import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
  46 import org.eclipse.ui.actions.ContributionItemFactory;
  47 import org.eclipse.ui.application.ActionBarAdvisor;
  48 import org.eclipse.ui.application.IActionBarConfigurer;
  49 import org.eclipse.ui.menus.CommandContributionItem;
  50 import org.eclipse.ui.menus.CommandContributionItemParameter;
  51 import org.eclipse.ui.services.IServiceLocator;
  52 
  53 import org.openjdk.jmc.rcp.application.actions.OpenFileAction;
  54 import org.openjdk.jmc.rcp.application.scripting.ShellViewCoommand;
  55 import org.openjdk.jmc.ui.common.util.Environment;
  56 import org.openjdk.jmc.ui.common.util.Environment.OSType;
  57 import org.openjdk.jmc.ui.handlers.ExternalUrlAction;
  58 
  59 public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
  60         private static final String FORUM_URL = "https://community.oracle.com/community/java/java_hotspot_virtual_machine/java_mission_control"; //$NON-NLS-1$
  61         private static final String OTN_URL = "http://www.oracle.com/missioncontrol"; //$NON-NLS-1$
  62 
  63         private IWorkbenchAction exitAction;
  64         private IWorkbenchAction preferenceAction;
  65         private IWorkbenchAction aboutAction;
  66         private IWorkbenchAction closeAction;
  67         private IWorkbenchAction closeAllAction;
  68         private IWorkbenchAction saveAction;
  69         private IWorkbenchAction saveAsAction;
  70         private IWorkbenchAction introAction;
  71         private IWorkbenchAction helpAction;
  72         private IWorkbenchAction helpSearchAction;
  73         private IWorkbenchAction dynamicHelpAction;
  74         private OpenFileAction openAction;
  75         private IWorkbenchAction backwardAction;
  76         private IWorkbenchAction forwardAction;
  77 
  78         public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
  79                 super(configurer);
  80         }
  81 
  82         @Override
  83         protected void makeActions(final IWorkbenchWindow window) {
  84                 openAction = new OpenFileAction(window);
  85                 openAction.setActionDefinitionId("org.openjdk.jmc.rcp.application.file.open"); //$NON-NLS-1$
  86 
  87                 saveAction = ActionFactory.SAVE.create(window);
  88                 saveAsAction = ActionFactory.SAVE_AS.create(window);
  89 
  90                 closeAction = ActionFactory.CLOSE.create(window);
  91                 register(closeAction);
  92 
  93                 closeAllAction = ActionFactory.CLOSE_ALL.create(window);
  94                 register(closeAction);
  95 
  96                 if (!(Environment.getOSType() == OSType.MAC)) {
  97                         preferenceAction = ActionFactory.PREFERENCES.create(window);
  98                         register(preferenceAction);
  99                 }
 100                 aboutAction = ActionFactory.ABOUT.create(window);
 101                 register(aboutAction);
 102                 exitAction = ActionFactory.QUIT.create(window);
 103                 register(exitAction);
 104                 if (Platform.getProduct() != null) {
 105                         introAction = ActionFactory.INTRO.create(window);
 106                         register(introAction);
 107                 }
 108                 register(new ShellViewCoommand());
 109                 helpAction = ActionFactory.HELP_CONTENTS.create(window);
 110                 helpAction.setText(Messages.ApplicationActionBarAdvisor_MENU_ITEM_HELP_TEXT);
 111                 helpSearchAction = ActionFactory.HELP_SEARCH.create(window);
 112                 register(helpSearchAction);
 113                 dynamicHelpAction = ActionFactory.DYNAMIC_HELP.create(window);
 114                 register(dynamicHelpAction);
 115 
 116                 backwardAction = ActionFactory.BACKWARD_HISTORY.create(window);
 117                 backwardAction.setId(IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY);
 118                 register(backwardAction);
 119                 forwardAction = ActionFactory.FORWARD_HISTORY.create(window);
 120                 forwardAction.setId(IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY);
 121                 register(forwardAction);
 122         }
 123 
 124         private void createDebug(IMenuManager manager) {
 125                 MenuManager menu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_DEBUG_TEXT, "mcdebug"); //$NON-NLS-1$
 126                 menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
 127                 menu.setVisible(Environment.isDebug());
 128                 manager.add(menu);
 129         }
 130 
 131         @Override
 132         protected void fillMenuBar(IMenuManager menuBar) {
 133                 IWorkbenchWindow window = getActionBarConfigurer().getWindowConfigurer().getWindow();
 134                 menuBar.add(createFileMenu(window));
 135                 menuBar.add(createEditMenu(window));
 136                 menuBar.add(createNavigateMenu(window));
 137                 menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
 138                 menuBar.add(createWindowMenu(window));
 139                 menuBar.add(createHelpMenu(window));
 140 
 141         }
 142 
 143         private MenuManager createHelpMenu(IWorkbenchWindow window) {
 144                 MenuManager helpMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_HELP_TEXT,
 145                                 IWorkbenchActionConstants.M_HELP);
 146 
 147                 if (introAction != null) {
 148                         helpMenu.add(introAction);
 149                         helpMenu.add(new Separator());
 150                 }
 151 
 152                 helpMenu.add(helpAction);
 153                 helpMenu.add(helpSearchAction);
 154                 helpMenu.add(dynamicHelpAction);
 155                 helpMenu.add(new Separator());
 156 
 157                 helpMenu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
 158                 helpMenu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
 159                 helpMenu.add(new Separator());
 160 
 161                 helpMenu.add(new ExternalUrlAction(OTN_URL, Messages.ApplicationActionBarAdvisor_MENU_ITEM_OTN_TEXT));
 162                 helpMenu.add(new ExternalUrlAction(FORUM_URL, Messages.ApplicationActionBarAdvisor_MENU_ITEM_FORUM_TEXT));
 163                 helpMenu.add(new Separator());
 164 
 165                 helpMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
 166                 helpMenu.add(new Separator());
 167 
 168                 helpMenu.add(aboutAction);
 169                 return helpMenu;
 170         }
 171 
 172         private MenuManager createWindowMenu(IWorkbenchWindow window) {
 173                 MenuManager windowMenu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_WINDOW_TEXT,
 174                                 IWorkbenchActionConstants.M_WINDOW);
 175 
 176                 windowMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
 177                 windowMenu.add(new GroupMarker("mcadditions")); //$NON-NLS-1$
 178                 windowMenu.add(new Separator());
 179                 windowMenu.add(createViewMenu(window));
 180                 windowMenu.add(createPerspectiveMenu(window));
 181                 windowMenu.add(new Separator());
 182                 windowMenu.add(ActionFactory.RESET_PERSPECTIVE.create(window));
 183                 if (!(Environment.getOSType() == OSType.MAC)) {
 184                         windowMenu.add(new Separator());
 185                         windowMenu.add(preferenceAction);
 186                 }
 187 
 188                 return windowMenu;
 189         }
 190 
 191         private MenuManager createEditMenu(IWorkbenchWindow window) {
 192                 MenuManager menu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_EDIT_TEXT,
 193                                 IWorkbenchActionConstants.M_EDIT);
 194                 menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));
 195                 menu.add(createItem(window, IWorkbenchCommandConstants.EDIT_CUT));
 196                 menu.add(createItem(window, IWorkbenchCommandConstants.EDIT_COPY));
 197                 menu.add(createItem(window, IWorkbenchCommandConstants.EDIT_PASTE));
 198                 menu.add(new Separator());
 199                 menu.add(createItem(window, IWorkbenchCommandConstants.EDIT_DELETE));
 200                 menu.add(createItem(window, IWorkbenchCommandConstants.EDIT_SELECT_ALL));
 201                 menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
 202                 return menu;
 203         }
 204 
 205         private static IContributionItem createItem(IServiceLocator sl, String commandId) {
 206                 return new CommandContributionItem(
 207                                 new CommandContributionItemParameter(sl, commandId, commandId, CommandContributionItem.STYLE_PUSH));
 208         }
 209 
 210         private IContributionItem createPerspectiveMenu(IWorkbenchWindow window) {
 211                 MenuManager perspectiveMenuMgr = new MenuManager(
 212                                 Messages.ApplicationActionBarAdvisor_SHOW_PERSPECTIVE_MENU_TEXT, "showPerspective"); //$NON-NLS-1$
 213                 IContributionItem perspectiveMenu = ContributionItemFactory.PERSPECTIVES_SHORTLIST.create(window);
 214                 perspectiveMenuMgr.add(perspectiveMenu);
 215 
 216                 return perspectiveMenuMgr;
 217         }
 218 
 219         private MenuManager createViewMenu(IWorkbenchWindow window) {
 220                 MenuManager showViewMenuMgr = new MenuManager(Messages.ApplicationActionBarAdvisor_SHOW_VIEW_MENU_TEXT,
 221                                 "showView"); //$NON-NLS-1$
 222                 IContributionItem showViewMenu = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
 223                 showViewMenuMgr.add(showViewMenu);
 224 
 225                 return showViewMenuMgr;
 226         }
 227 
 228         private MenuManager createFileMenu(IWorkbenchWindow window) {
 229                 MenuManager menu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_FILE_TEXT,
 230                                 IWorkbenchActionConstants.M_FILE);
 231                 menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
 232 
 233                 // create the New submenu, using the same id for it as the New action
 234                 String newText = Messages.ApplicationActionBarAdvisor_MENU_NEW_TEXT;
 235                 String newId = ActionFactory.NEW.getId();
 236                 MenuManager newMenu = new MenuManager(newText, newId);
 237                 newMenu.add(new Separator(newId));
 238                 newMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
 239                 menu.add(newMenu);
 240                 menu.add(openAction);
 241                 menu.add(saveAction);
 242                 menu.add(saveAsAction);
 243                 createDebug(menu);
 244 
 245                 menu.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
 246                 menu.add(new Separator());
 247                 menu.add(closeAction);
 248                 menu.add(closeAllAction);
 249                 menu.add(new GroupMarker(IWorkbenchActionConstants.CLOSE_EXT));
 250                 menu.add(new Separator());
 251                 menu.add(ActionFactory.EXPORT.create(window));
 252                 menu.add(ActionFactory.IMPORT.create(window));
 253                 menu.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
 254                 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
 255 
 256                 // this is where OPEN_CONNECTION is placed via plugin.xml from org.openjdk.jmc.browser
 257                 menu.add(new Separator());
 258 
 259                 // create a submenu for the list of Recent Files
 260                 String recentText = Messages.ApplicationActionBarAdvisor_MENU_RECENT_FILES_TEXT;
 261                 String recentId = ContributionItemFactory.REOPEN_EDITORS.getId();
 262                 MenuManager recentMenu = new MenuManager(recentText, recentId);
 263                 recentMenu.add(ContributionItemFactory.REOPEN_EDITORS.create(window));
 264                 recentMenu.add(new GroupMarker(IWorkbenchActionConstants.MRU));
 265                 menu.add(recentMenu);
 266                 menu.add(new Separator());
 267 
 268                 menu.add(getAction(ActionFactory.QUIT.getId()));
 269                 menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
 270                 return menu;
 271         }
 272 
 273         private MenuManager createNavigateMenu(IWorkbenchWindow window) {
 274                 MenuManager menu = new MenuManager(Messages.ApplicationActionBarAdvisor_MENU_NAVIGATE_TEXT,
 275                                 IWorkbenchActionConstants.M_NAVIGATE);
 276                 menu.add(backwardAction);
 277                 menu.add(forwardAction);
 278                 return menu;
 279         }
 280 }