2

I want to add my custom view to the "Window" menu. This code is adding only opened views

    IMenuManager pencereler = new MenuManager("sample");
    pencereler.add(ContributionItemFactory.VIEWS_SHORTLIST.create(window));
    viewMenu.add(pencereler);

If I open my custom view, it is added to this menu. However, I want to show my custom view always in this menu, not only after it has been opened.

So can I create "ContributionItem" from the view or is there any other way like adding some lines to "plugin.xml" or adding my view's id to any extension?

s.d
  • 4,017
  • 5
  • 35
  • 65
cgrgcn
  • 361
  • 2
  • 6
  • 24
  • Do you want to add your view to the **Show View** menu (Window > Show View), or do you want to add a button to the **toolbar** that opens your view? – s.d Feb 27 '13 at 11:53
  • When I go Windows > Windows > Other (this opens Show View), I can see my view on there but I want to put it on Windows > Windows without choose from Show View. – cgrgcn Feb 27 '13 at 12:09
  • Which version of Eclipse is your RCP based upon? – s.d Feb 27 '13 at 12:13
  • I am using eclipse 3.7.0. – cgrgcn Feb 27 '13 at 12:18

1 Answers1

6

Supposing you have included the Window menu correctly, this is how it works.

Add a command

  1. In plugin.xml, tab Extensions, add the extension point org.eclipse.ui.commands.

  2. To org.eclipse.ui.commands, add a new command (right-click on the extension point > New > command). Give the command an id (e.g., "myplugin.openCustomView"), and a name (e.g., "Open Custom View").

Add a handler for the command

For the newly created command "myplugin.openCustomView", you will have to create a handler, which will programatically open the view.

  1. In plugin.xml, tab Extensions, add the extension point org.eclipse.ui.handlers.

  2. To org.eclipse.ui.handlers, add a handler (right-click on the extension point > New > handler). For the handler's commandId, type the name of your newly created command (myplugin.openCustomView) or browse for it. In the field class, type the name of the handler class to be created (e.g., "OpenCustomViewHandler"), and click on the hyperlinked field name class. This will open the New Class Wizard.

  3. Let the new class (name is already filled in) extend org.eclipse.core.commands.AbstractHandler.

  4. Add the open view logic to the newly created class's execute() method, so that the class will look approximately as follows (with your view ID instead of myplugin.mycustomviewID of course).

    import org.eclipse.core.commands.AbstractHandler;
    import org.eclipse.core.commands.ExecutionEvent;
    import org.eclipse.core.commands.ExecutionException;
    import org.eclipse.ui.PartInitException;
    import org.eclipse.ui.handlers.HandlerUtil;
    
    public class OpenViewHandler extends AbstractHandler {
    
      @Override
      public Object execute(ExecutionEvent event) throws ExecutionException {
        try {
          HandlerUtil.getActiveWorkbenchWindowChecked(event).
            getActivePage().showView("myplugin.mycustomviewID");
        } catch (PartInitException e) {
          e.printStackTrace();
        }
        return null;
      }
    }
    

Add the command to the menu

  1. In plugin.xml, tab Extensions, add the extension point org.eclipse.ui.menus.

  2. To org.eclipse.ui.menus, add a new menuContribution (right-click on the extension point > New > menuContribution).

  3. Give the menuContribution the following locationURI: menu:window?after=additions (this will place the contribution in the Window menu, if you have included it correctly).

  4. To the newly created menuContribution, add a command (right-click on the menuContribution > New > command). Browse for the commandId, or enter it manually (myplugin.openCustomView). Give the menuContribution an id (e.g., "myplugin.menu.openCustomView"), and set style to "push".

s.d
  • 4,017
  • 5
  • 35
  • 65
  • This is really perfect expression thank you very much. I have one more question; I have another _Window_ menu under the _Window_. To add my menu there how should I edit this line **menu:window?after=additions** – cgrgcn Feb 27 '13 at 13:40
  • 1
    If you have added this menu yourself, you should have the **id** for it as well (have a look at the respective extension point in *plugin.xml*). Say this **id** was *myplugin.menus.windowMenu*, you just edit the *locationURI* to *menu:myplugin.menus.windowMenu*. The *?after=additions* part is - in theory - not necessary, but allow you to control where exactly the menu item is placed, cf. http://wiki.eclipse.org/Menu_Contributions. – s.d Feb 27 '13 at 14:08
  • My problem is the menu is generated on ActionBarAdvisor class programtically so I can't see it on plugin.xml or extension org.eclipse.ui.menus. Also my reputation is not enough for the upvote :/ – cgrgcn Feb 27 '13 at 14:18
  • 1
    It is considered a best practice to use Commands rather than actions (cf. [this answer on stackoverflow.com](http://stackoverflow.com/a/552450/731040)). I'd suggest re-implementing the menu via the extension points. This makes it easier to access it from other extensions (see my comment above), and is definitely worth the few minutes of extra work! Just follow the second part of my answer for this menu again, and include all logic you might have had in the `ActionBarContributor` in the menu handler... – s.d Feb 27 '13 at 14:37
  • I have to do it in this way :/ even so thank you for your suggestion. – cgrgcn Feb 27 '13 at 14:51