Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Menu Contributions/Problems View Example"

(Use <source>.)
 
Line 7: Line 7:
 
First define commands that are specific to the view.  Since these are view commands, we can specify a default handler ... we're unlikely to replace it.
 
First define commands that are specific to the view.  Since these are view commands, we can specify a default handler ... we're unlikely to replace it.
  
  <extension point="org.eclipse.ui.commands">
+
<source lang="xml">
      <category id="org.eclipse.ui.views.problems"
+
<extension point="org.eclipse.ui.commands">
                name="%ProblemView.category.name">
+
    <category id="org.eclipse.ui.views.problems"
      </category>
+
            name="%ProblemView.category.name">
      <command categoryId="org.eclipse.ui.views.problems"
+
    </category>
              defaultHandler="org.eclipse.ui.views.markers.internal.TableSortHandler"
+
    <command categoryId="org.eclipse.ui.views.problems"
              description="%ProblemView.Sorting.description"
+
            defaultHandler="org.eclipse.ui.views.markers.internal.TableSortHandler"
              id="org.eclipse.ui.views.problems.sorting"
+
            description="%ProblemView.Sorting.description"
              name="%ProblemView.Sorting.name">
+
            id="org.eclipse.ui.views.problems.sorting"
      </command>
+
            name="%ProblemView.Sorting.name">
      &lt;!-- the view preference command would probably be defined once
+
    </command>
      with the other preference contributions -->
+
    <!-- the view preference command would probably be defined once
      <command categoryId="org.eclipse.ui.views.problems"
+
        with the other preference contributions -->
              defaultHandler="org.eclipse.ui.preferences.ViewPreferencesHandler"
+
    <command categoryId="org.eclipse.ui.views.problems"
              description="%ViewPreferences.description"
+
            defaultHandler="org.eclipse.ui.preferences.ViewPreferencesHandler"
              id="org.eclipse.ui.preferences.viewPreferences"
+
            description="%ViewPreferences.description"
              name="%ViewPreferences.name">
+
            id="org.eclipse.ui.preferences.viewPreferences"
        <commandParameter id="markerEnablementName"
+
            name="%ViewPreferences.name">
                          name="%ViewPreferences.markerEnablementName.name"
+
        <commandParameter id="markerEnablementName"
                          optional="false" />
+
                name="%ViewPreferences.markerEnablementName.name"
        <commandParameter id="markerLimitName"
+
                optional="false" />
                          name="%ViewPreferences.markerLimitName.name"
+
        <commandParameter id="markerLimitName"
                          optional="false" />
+
                name="%ViewPreferences.markerLimitName.name"
      </command>
+
                optional="false" />
      <command categoryId="org.eclipse.ui.views.problems"
+
    </command>
              defaultHandler="org.eclipse.ui.views.markers.internal.FiltersHandler"
+
    <command categoryId="org.eclipse.ui.views.problems"
              description="%ProblemView.ConfigureFilters.description"
+
            defaultHandler="org.eclipse.ui.views.markers.internal.FiltersHandler"
              id="org.eclipse.ui.views.problems.configureFilters"
+
            description="%ProblemView.ConfigureFilters.description"
              name="%ProblemView.ConfigureFilters.name">
+
            id="org.eclipse.ui.views.problems.configureFilters"
      </command>
+
            name="%ProblemView.ConfigureFilters.name">
      <command categoryId="org.eclipse.ui.views.problems"
+
    </command>
              defaultHandler="org.eclipse.ui.views.markers.internal.OpenMarkerHandler"
+
    <command categoryId="org.eclipse.ui.views.problems"
              description="%ProblemView.GoTo.description"
+
            defaultHandler="org.eclipse.ui.views.markers.internal.OpenMarkerHandler"
              id="org.eclipse.ui.views.problems.goTo"
+
            description="%ProblemView.GoTo.description"
              name="%ProblemView.GoTo.name" />
+
            id="org.eclipse.ui.views.problems.goTo"
  </extension>
+
            name="%ProblemView.GoTo.name" />
 +
</extension>
 +
</source>
  
 
== Handlers ==
 
== Handlers ==
Line 48: Line 50:
 
We can also use a number of global commands, like copy, paste, delete, quick fix, and properties.  For these, we just need to define our handlers.  We need to add them with <activeWhen/> clauses to restrict them to being active when the view is active.
 
We can also use a number of global commands, like copy, paste, delete, quick fix, and properties.  For these, we just need to define our handlers.  We need to add them with <activeWhen/> clauses to restrict them to being active when the view is active.
  
  <extension point="org.eclipse.ui.handlers">
+
<source lang="xml">
      <handler commandId="org.eclipse.ui.edit.copy"
+
<extension point="org.eclipse.ui.handlers">
              class="org.eclipse.ui.views.markers.internal.CopyMarkerHandler">
+
    <handler commandId="org.eclipse.ui.edit.copy"
        <enabledWhen>
+
            class="org.eclipse.ui.views.markers.internal.CopyMarkerHandler">
 +
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
      <handler commandId="org.eclipse.ui.edit.paste"
+
    <handler commandId="org.eclipse.ui.edit.paste"
              class="org.eclipse.ui.views.markers.internal.PasteMarkerHandler">
+
            class="org.eclipse.ui.views.markers.internal.PasteMarkerHandler">
        <enabledWhen>
+
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
      <handler commandId="org.eclipse.ui.edit.delete"
+
    <handler commandId="org.eclipse.ui.edit.delete"
              class="org.eclipse.ui.views.markers.internal.RemoveMarkerHandler">
+
            class="org.eclipse.ui.views.markers.internal.RemoveMarkerHandler">
        <enabledWhen>
+
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
      <handler commandId="org.eclipse.ui.edit.selectAll"
+
    <handler commandId="org.eclipse.ui.edit.selectAll"
              class="org.eclipse.ui.views.markers.internal.SelectAllMarkersHandler">
+
            class="org.eclipse.ui.views.markers.internal.SelectAllMarkersHandler">
        <enabledWhen>
+
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
      <handler commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
+
    <handler commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
              class="org.eclipse.ui.views.markers.internal.ResolveMarkerHandler">
+
            class="org.eclipse.ui.views.markers.internal.ResolveMarkerHandler">
        <enabledWhen>
+
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
      <handler commandId="org.eclipse.ui.file.properties"
+
    <handler commandId="org.eclipse.ui.file.properties"
              class="org.eclipse.ui.views.markers.internal.ProblemPropertiesHandler">
+
            class="org.eclipse.ui.views.markers.internal.ProblemPropertiesHandler">
        <enabledWhen>
+
        <enabledWhen>
 
             <not>
 
             <not>
              <count value="0" />
+
                <count value="0" />
 
             </not>
 
             </not>
        </enabledWhen>
+
        </enabledWhen>
        <activeWhen>
+
        <activeWhen>
 
             <with variable="activePartId">
 
             <with variable="activePartId">
              <equals value="org.eclipse.ui.views.ProblemView" />
+
                <equals value="org.eclipse.ui.views.ProblemView" />
 
             </with>
 
             </with>
        </activeWhen>
+
        </activeWhen>
      </handler>
+
    </handler>
  </extension>
+
</extension>
 
+
</source>
  
 
Or we can programmatically activate them through the IHandlerService which we would retrieve from the ProblemView site.
 
Or we can programmatically activate them through the IHandlerService which we would retrieve from the ProblemView site.
  
IHandlerService handlerServ = (IHandlerService)getSite().getService(IHandlerService.class);
+
<source lang="java">
copy = new CopyMarkerHandler();
+
IHandlerService handlerServ = (IHandlerService)getSite().getService(IHandlerService.class);
handlerServ.activateHandler("org.eclipse.ui.edit.copy", copy);
+
CopyMarkerHandler copy = new CopyMarkerHandler();
 +
handlerServ.activateHandler("org.eclipse.ui.edit.copy", copy);
 +
</source>
  
 
Using the ProblemView site to access the IHandlerService handles the <activeWhen/> clause for us, and our programmatic handler would manage its own enablement state.
 
Using the ProblemView site to access the IHandlerService handles the <activeWhen/> clause for us, and our programmatic handler would manage its own enablement state.
 
  
 
== Menus ==
 
== Menus ==
 
 
 
Then we would define the ProblemView menu structures.  We are using 3 <b>roots</b>:  the view menu, the view toolbar, and the view context menu.  This is an example of an "in-place" menu definition.  The <menuContribution/> location attribute is a URI that defines the starting point for inserting the menu elements. The XML hierarchy mirrors the menu hierarchy, in that you can define items and menus within the body of other menus.
 
Then we would define the ProblemView menu structures.  We are using 3 <b>roots</b>:  the view menu, the view toolbar, and the view context menu.  This is an example of an "in-place" menu definition.  The <menuContribution/> location attribute is a URI that defines the starting point for inserting the menu elements. The XML hierarchy mirrors the menu hierarchy, in that you can define items and menus within the body of other menus.
  
  <extension point="org.eclipse.ui.menus">
+
<source lang="xml"><extension point="org.eclipse.ui.menus">
      <menuContribution locationURI="menu:org.eclipse.ui.views.ProblemView">
+
    <menuContribution locationURI="menu:org.eclipse.ui.views.ProblemView">
        <command commandId="org.eclipse.ui.views.problems.sorting"
+
        <command commandId="org.eclipse.ui.views.problems.sorting"
                  label="%ProblemView.Sorting.name"
+
                label="%ProblemView.Sorting.name"
                  mnemonic="%ProblemView.Sorting.mnemonic"
+
                mnemonic="%ProblemView.Sorting.mnemonic"
                  style="push"
+
                style="push"
                  tooltip="%ProblemView.Sorting.tooltip">
+
                tooltip="%ProblemView.Sorting.tooltip">
        </command>
+
        </command>
        <menu id="org.eclipse.ui.views.problems.groupBy.menu"
+
        <menu id="org.eclipse.ui.views.problems.groupBy.menu"
              label="%ProblemView.GroupBy.label"
+
                label="%ProblemView.GroupBy.label"
              mnemonic="%ProblemView.GroupBy.mnemonic">
+
                mnemonic="%ProblemView.GroupBy.mnemonic">
 
             <dynamic class="org.eclipse.ui.views.markers.internal.GroupByItems"
 
             <dynamic class="org.eclipse.ui.views.markers.internal.GroupByItems"
                    id="org.eclipse.ui.views.problems.groupBy.items">
+
                    id="org.eclipse.ui.views.problems.groupBy.items">
 
             </dynamic>
 
             </dynamic>
        </menu>
+
        </menu>
        <separator name="group.filter" visible="true" />
+
        <separator name="group.filter" visible="true" />
        <menu id="org.eclipse.ui.views.problems.filters.menu"
+
        <menu id="org.eclipse.ui.views.problems.filters.menu"
              label="%ProblemView.Filters.label"
+
                label="%ProblemView.Filters.label"
              mnemonic="%ProblemView.Filters.mnemonic">
+
                mnemonic="%ProblemView.Filters.mnemonic">
 
             <dynamic class="org.eclipse.ui.views.markers.internal.FilterItems"
 
             <dynamic class="org.eclipse.ui.views.markers.internal.FilterItems"
                    id="org.eclipse.ui.views.problems.filters.items" />
+
                    id="org.eclipse.ui.views.problems.filters.items" />
        </menu>
+
        </menu>
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
+
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
                  icon="$nl$/elcl16/filter_ps.gif"
+
                icon="$nl$/elcl16/filter_ps.gif"
                  label="%ProblemView.ConfigureFilters.name"
+
                label="%ProblemView.ConfigureFilters.name"
                  mnemonic="%ProblemView.ConfigureFilters.mnemonic"
+
                mnemonic="%ProblemView.ConfigureFilters.mnemonic"
                  tooltip="%ProblemView.ConfigureFilters.tooltip" />
+
                tooltip="%ProblemView.ConfigureFilters.tooltip" />
        <command commandId="org.eclipse.ui.preferences.viewPreferences"
+
        <command commandId="org.eclipse.ui.preferences.viewPreferences"
                  label="%ViewPreferences.name"
+
                label="%ViewPreferences.name"
                  mnemonic="%ViewPreferences.mnemonic">
+
                mnemonic="%ViewPreferences.mnemonic">
             <parameter name="markerEnablementName" value="LIMIT_PROBLEMS" />
+
             <parameter name="markerEnablementName"
             <parameter name="markerLimitName" value="PROBLEMS_LIMIT" />
+
                        value="LIMIT_PROBLEMS" />
        </command>
+
             <parameter name="markerLimitName"
      </menuContribution>
+
                        value="PROBLEMS_LIMIT" />
      <menuContribution locationURI="toolbar:org.eclipse.ui.views.ProblemView">
+
        </command>
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
+
    </menuContribution>
                  icon="$nl$/elcl16/filter_ps.gif"
+
    <menuContribution locationURI="toolbar:org.eclipse.ui.views.ProblemView">
                  tooltip="%ProblemView.ConfigureFilters.tooltip" />
+
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
      </menuContribution>
+
                icon="$nl$/elcl16/filter_ps.gif"
      <menuContribution locationURI="popup:org.eclipse.ui.views.ProblemView">
+
                tooltip="%ProblemView.ConfigureFilters.tooltip" />
        <command commandId="org.eclipse.ui.views.problems.goTo"
+
    </menuContribution>
                  mnemonic="%ProblemView.GoTo.mnemonic"
+
    <menuContribution locationURI="popup:org.eclipse.ui.views.ProblemView">
                  icon="$nl$/elcl16/gotoobj_tsk.gif"
+
        <command commandId="org.eclipse.ui.views.problems.goTo"
                  disabledIcon="$nl$/dlcl16/gotoobj_tsk.gif"
+
                mnemonic="%ProblemView.GoTo.mnemonic"
                  tooltip="%ProblemView.GoTo.tooltip" />
+
                icon="$nl$/elcl16/gotoobj_tsk.gif"
        <separator name="group.showIn" visible="true" />
+
                disabledIcon="$nl$/dlcl16/gotoobj_tsk.gif"
        <menu id="org.eclipse.ui.views.problems.showIn.menu"
+
                tooltip="%ProblemView.GoTo.tooltip" />
              label="%ProblemView.ShowIn.label"
+
        <separator name="group.showIn"
              mnemonic="%ProblemView.ShowIn.mnemonic">
+
                visible="true" />
 +
        <menu id="org.eclipse.ui.views.problems.showIn.menu"
 +
                label="%ProblemView.ShowIn.label"
 +
                mnemonic="%ProblemView.ShowIn.mnemonic">
 
             <dynamic class="org.eclipse.ui.actions.ShowInContributions"
 
             <dynamic class="org.eclipse.ui.actions.ShowInContributions"
                    id="org.eclipse.ui.views.problems.showIn.items" />
+
                    id="org.eclipse.ui.views.problems.showIn.items" />
        </menu>
+
        </menu>
        <separator name="group.edit" visible="true" />
+
        <separator name="group.edit"
        <command commandId="org.eclipse.ui.edit.copy"
+
                visible="true" />
                  mnemonic="%ProblemView.copy.mnemonic"
+
        <command commandId="org.eclipse.ui.edit.copy"
                  icon="$nl$/icons/full/etool16/copy_edit.gif"
+
                mnemonic="%ProblemView.copy.mnemonic"
                  disabledIcon="$nl$/icons/full/dtool16/copy_edit.gif" />
+
                icon="$nl$/icons/full/etool16/copy_edit.gif"
        <command commandId="org.eclipse.ui.edit.paste"
+
                disabledIcon="$nl$/icons/full/dtool16/copy_edit.gif" />
                  mnemonic="%ProblemView.paste.mnemonic"
+
        <command commandId="org.eclipse.ui.edit.paste"
                  icon="$nl$/icons/full/etool16/paste_edit.gif"
+
                mnemonic="%ProblemView.paste.mnemonic"
                  disabledIcon="$nl$/icons/full/dtool16/paste_edit.gif" />
+
                icon="$nl$/icons/full/etool16/paste_edit.gif"
        <command commandId="org.eclipse.ui.edit.delete"
+
                disabledIcon="$nl$/icons/full/dtool16/paste_edit.gif" />
                  mnemonic="%ProblemView.delete.mnemonic"
+
        <command commandId="org.eclipse.ui.edit.delete"
                  icon="$nl$/icons/full/etool16/delete_edit.gif"
+
                mnemonic="%ProblemView.delete.mnemonic"
                  disabledIcon="$nl$/icons/full/dtool16/delete_edit.gif">
+
                icon="$nl$/icons/full/etool16/delete_edit.gif"
 +
                disabledIcon="$nl$/icons/full/dtool16/delete_edit.gif">
 
             <visibleWhen>
 
             <visibleWhen>
              <not>
+
                <not>
                  <with variable="activePartId">
+
                    <with variable="activePartId">
                    <equals value="org.eclipse.ui.views.ProblemView" />
+
                        <equals value="org.eclipse.ui.views.ProblemView" />
                  </with>
+
                    </with>
              </not>
+
                </not>
 
             </visibleWhen>
 
             </visibleWhen>
        </command>
+
        </command>
        <command commandId="org.eclipse.ui.edit.selectAll"
+
        <command commandId="org.eclipse.ui.edit.selectAll"
                  mnemonic="%ProblemView.selectAll.mnemonic" />
+
                mnemonic="%ProblemView.selectAll.mnemonic" />
        <separator name="group.resolve" visible="true" />
+
        <separator name="group.resolve"
        <command commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
+
                visible="true" />
                  mnemonic="%ProblemView.Resolve.mnemonic"
+
        <command commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
                  icon="$nl$/icons/full/elcl16/smartmode_co.gif"
+
                mnemonic="%ProblemView.Resolve.mnemonic"
                  disabledIcon="$nl$/icons/full/dlcl16/smartmode_co.gif" />
+
                icon="$nl$/icons/full/elcl16/smartmode_co.gif"
        <separator name="additions" visible="false" />
+
                disabledIcon="$nl$/icons/full/dlcl16/smartmode_co.gif" />
        <separator name="group.properties" visible="true" />
+
        <separator name="additions"
        <command commandId="org.eclipse.ui.file.properties"
+
                visible="false" />
                  mnemonic="%ProblemView.Properties.mnemonic" />
+
        <separator name="group.properties"
      </menuContribution>
+
                visible="true" />
  </extension>
+
        <command commandId="org.eclipse.ui.file.properties"
 
+
                mnemonic="%ProblemView.Properties.mnemonic" />
 +
    </menuContribution>
 +
</extension>
 +
</source>
  
 
== Menus API ==
 
== Menus API ==
 
 
We can contribute menu definitions through the IMenuService API.
 
We can contribute menu definitions through the IMenuService API.
  
 
The above example can be done for the view menus:
 
The above example can be done for the view menus:
  
    public void addProblemsViewMenuContribution() {
+
<source lang="java">
        IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
+
public void addProblemsViewMenuContribution() {
                .getService(IMenuService.class);
+
    IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
 +
            .getService(IMenuService.class);
 +
 
 +
    AbstractContributionFactory viewMenuAddition = new AbstractContributionFactory(
 +
            "menu:org.eclipse.ui.views.ProblemView?after=additions") {
 +
        public void createContributionItems(IMenuService menuService,
 +
                List additions) {
 +
            CommandContributionItem item = new CommandContributionItem(
 +
                    null, "org.eclipse.ui.views.problems.sorting", null,
 +
                    null, null, null, "Sorting...", "S",
 +
                    "Change the Sort order",
 +
                    CommandContributionItem.STYLE_PUSH);
 +
            additions.add(item);
 
   
 
   
        AbstractContributionFactory viewMenuAddition = new AbstractContributionFactory(
+
            MenuManager submenu = new MenuManager("Group &By",
                "menu:org.eclipse.ui.views.ProblemView?after=additions") {
+
                    "org.eclipse.ui.views.problems.groupBy.menu");
             public void createContributionItems(IMenuService menuService,
+
             IContributionItem dynamicItem = new CompoundContributionItem(
                     List additions) {
+
                     "org.eclipse.ui.views.problems.groupBy.items") {
                 CommandContributionItem item = new CommandContributionItem(
+
                 protected IContributionItem[] getContributionItems() {
                        null, "org.eclipse.ui.views.problems.sorting", null,
+
                    // Here's where you would dynamically generate your list
                        null, null, null, "Sorting...", "S",
+
                    IContributionItem[] list = new IContributionItem[2];
                        "Change the Sort order",
+
                    Map parms = new HashMap();
                        CommandContributionItem.STYLE_PUSH);
+
                    parms.put("groupBy", "Severity");
                additions.add(item);
+
                    list[0] = new CommandContributionItem(null,
 +
                            "org.eclipse.ui.views.problems.grouping",
 +
                            parms, null, null, null, "Severity", null,
 +
                            null, CommandContributionItem.STYLE_PUSH);
 
   
 
   
                MenuManager submenu = new MenuManager("Group &By",
+
                     parms = new HashMap();
                        "org.eclipse.ui.views.problems.groupBy.menu");
+
                    parms.put("groupBy", "None");
                IContributionItem dynamicItem = new CompoundContributionItem(
+
                    list[1] = new CommandContributionItem(null,
                        "org.eclipse.ui.views.problems.groupBy.items") {
+
                            "org.eclipse.ui.views.problems.grouping",
                     protected IContributionItem[] getContributionItems() {
+
                            parms, null, null, null, "None", null, null,
                        // Here's where you would dynamically generate your list
+
                            CommandContributionItem.STYLE_PUSH);
                        IContributionItem[] list = new IContributionItem[2];
+
                    return list;
                        Map parms = new HashMap();
+
                }
                        parms.put("groupBy", "Severity");
+
            };
                        list[0] = new CommandContributionItem(null,
+
            submenu.add(dynamicItem);
                                "org.eclipse.ui.views.problems.grouping",
+
                                parms, null, null, null, "Severity", null,
+
                                null, CommandContributionItem.STYLE_PUSH);
+
 
   
 
   
                        parms = new HashMap();
+
            additions.add(submenu);
                        parms.put("groupBy", "None");
+
            additions.add(new Separator("group.filter"));
                        list[1] = new CommandContributionItem(null,
+
                                "org.eclipse.ui.views.problems.grouping",
+
                                parms, null, null, null, "None", null, null,
+
                                CommandContributionItem.STYLE_PUSH);
+
                        return list;
+
                    }
+
                };
+
                submenu.add(dynamicItem);
+
 
   
 
   
                additions.add(submenu);
+
            submenu = new MenuManager("&Filters",
                 additions.add(new Separator("group.filter"));
+
                    "org.eclipse.ui.views.problems.filters.menu");
 +
            dynamicItem = new CompoundContributionItem(
 +
                    "org.eclipse.ui.views.problems.filters.items") {
 +
                 protected IContributionItem[] getContributionItems() {
 +
                    // Here's where you would dynamically generate your list
 +
                    IContributionItem[] list = new IContributionItem[1];
 +
                    Map parms = new HashMap();
 +
                    parms.put("filter", "Default");
 +
                    list[0] = new CommandContributionItem(null,
 +
                            "org.eclipse.ui.views.problems.filters", parms,
 +
                            null, null, null, "Default", null, null,
 +
                            CommandContributionItem.STYLE_PUSH);
 +
                    return list;
 +
                }
 +
            };
 +
            submenu.add(dynamicItem);
 
   
 
   
                submenu = new MenuManager("&Filters",
+
            additions.add(submenu);
                        "org.eclipse.ui.views.problems.filters.menu");
+
                dynamicItem = new CompoundContributionItem(
+
                        "org.eclipse.ui.views.problems.filters.items") {
+
                    protected IContributionItem[] getContributionItems() {
+
                        // Here's where you would dynamically generate your list
+
                        IContributionItem[] list = new IContributionItem[1];
+
                        Map parms = new HashMap();
+
                        parms.put("filter", "Default");
+
                        list[0] = new CommandContributionItem(null,
+
                                "org.eclipse.ui.views.problems.filters", parms,
+
                                null, null, null, "Default", null, null,
+
                                CommandContributionItem.STYLE_PUSH);
+
                        return list;
+
                    }
+
                };
+
                submenu.add(dynamicItem);
+
 
   
 
   
                additions.add(submenu);
+
            ImageDescriptor filterIcon = PlatformUI.getWorkbench()
 +
                    .getSharedImages().getImageDescriptor(
 +
                            "elcl16/filter_ps.gif");
 +
            item = new CommandContributionItem(null,
 +
                    "org.eclipse.ui.views.problems.configureFilters", null,
 +
                    filterIcon, null, null, "Configure Filters...", "C",
 +
                    "Configure the filters to be applied to this view",
 +
                    CommandContributionItem.STYLE_PUSH);
 +
            additions.add(item);
 
   
 
   
                ImageDescriptor filterIcon = PlatformUI.getWorkbench()
+
            Map parms = new HashMap();
                        .getSharedImages().getImageDescriptor(
+
            parms.put("markerEnablementName", "LIMIT_PROBLEMS");
                                "elcl16/filter_ps.gif");
+
            parms.put("markerLimitName", "PROBLEMS_LIMIT");
                item = new CommandContributionItem(null,
+
            item = new CommandContributionItem(null,
                        "org.eclipse.ui.views.problems.configureFilters", null,
+
                    "org.eclipse.ui.preferences.viewPreferences", parms,
                        filterIcon, null, null, "Configure Filters...", "C",
+
                    null, null, null, "Preference", "P",
                        "Configure the filters to be applied to this view",
+
                    "Open the preference dialog",
                        CommandContributionItem.STYLE_PUSH);
+
                    CommandContributionItem.STYLE_PUSH);
                additions.add(item);
+
            additions.add(item);
+
         }
                Map parms = new HashMap();
+
                parms.put("markerEnablementName", "LIMIT_PROBLEMS");
+
                parms.put("markerLimitName", "PROBLEMS_LIMIT");
+
                item = new CommandContributionItem(null,
+
                        "org.eclipse.ui.preferences.viewPreferences", parms,
+
                        null, null, null, "Preference", "P",
+
                        "Open the preference dialog",
+
                        CommandContributionItem.STYLE_PUSH);
+
                additions.add(item);
+
            }
+
+
            public void releaseContributionItems(IMenuService menuService,
+
                    List items) {
+
                // for us this is a no-op
+
            }
+
         };
+
        menuService.addContributionFactory(viewMenuAddition);
+
    }
+
 
+
 
+
 
   
 
   
 +
        public void releaseContributionItems(IMenuService menuService,
 +
                List items) {
 +
            // for us this is a no-op
 +
        }
 +
    };
 +
    menuService.addContributionFactory(viewMenuAddition);
 +
}
 +
</source>
  
 
The <code>AbstractContributionFactory</code> creates new contribution items every time <code>createContributionItems(List)</code> is called.  The factory location tells the framework where to insert the contributions when populating <code>ContributionManager</code>s.
 
The <code>AbstractContributionFactory</code> creates new contribution items every time <code>createContributionItems(List)</code> is called.  The factory location tells the framework where to insert the contributions when populating <code>ContributionManager</code>s.

Latest revision as of 08:52, 13 October 2008

Add ProblemView menus

Add the Problems view menus. The Problems view has one toolbar action and in the view menu, 3 actions and 2 dynamic submenus. It also has a dynamic menu and another bunch of actions in its context menu.

Commands

First define commands that are specific to the view. Since these are view commands, we can specify a default handler ... we're unlikely to replace it.

<extension point="org.eclipse.ui.commands">
    <category id="org.eclipse.ui.views.problems"
            name="%ProblemView.category.name">
    </category>
    <command categoryId="org.eclipse.ui.views.problems"
            defaultHandler="org.eclipse.ui.views.markers.internal.TableSortHandler"
            description="%ProblemView.Sorting.description"
            id="org.eclipse.ui.views.problems.sorting"
            name="%ProblemView.Sorting.name">
    </command>
    <!-- the view preference command would probably be defined once
         with the other preference contributions -->
    <command categoryId="org.eclipse.ui.views.problems"
            defaultHandler="org.eclipse.ui.preferences.ViewPreferencesHandler"
            description="%ViewPreferences.description"
            id="org.eclipse.ui.preferences.viewPreferences"
            name="%ViewPreferences.name">
        <commandParameter id="markerEnablementName"
                name="%ViewPreferences.markerEnablementName.name"
                optional="false" />
        <commandParameter id="markerLimitName"
                name="%ViewPreferences.markerLimitName.name"
                optional="false" />
    </command>
    <command categoryId="org.eclipse.ui.views.problems"
            defaultHandler="org.eclipse.ui.views.markers.internal.FiltersHandler"
            description="%ProblemView.ConfigureFilters.description"
            id="org.eclipse.ui.views.problems.configureFilters"
            name="%ProblemView.ConfigureFilters.name">
    </command>
    <command categoryId="org.eclipse.ui.views.problems"
            defaultHandler="org.eclipse.ui.views.markers.internal.OpenMarkerHandler"
            description="%ProblemView.GoTo.description"
            id="org.eclipse.ui.views.problems.goTo"
            name="%ProblemView.GoTo.name" />
</extension>

Handlers

We can also use a number of global commands, like copy, paste, delete, quick fix, and properties. For these, we just need to define our handlers. We need to add them with <activeWhen/> clauses to restrict them to being active when the view is active.

<extension point="org.eclipse.ui.handlers">
    <handler commandId="org.eclipse.ui.edit.copy"
            class="org.eclipse.ui.views.markers.internal.CopyMarkerHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
    <handler commandId="org.eclipse.ui.edit.paste"
            class="org.eclipse.ui.views.markers.internal.PasteMarkerHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
    <handler commandId="org.eclipse.ui.edit.delete"
            class="org.eclipse.ui.views.markers.internal.RemoveMarkerHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
    <handler commandId="org.eclipse.ui.edit.selectAll"
            class="org.eclipse.ui.views.markers.internal.SelectAllMarkersHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
    <handler commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
            class="org.eclipse.ui.views.markers.internal.ResolveMarkerHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
    <handler commandId="org.eclipse.ui.file.properties"
            class="org.eclipse.ui.views.markers.internal.ProblemPropertiesHandler">
        <enabledWhen>
            <not>
                <count value="0" />
            </not>
        </enabledWhen>
        <activeWhen>
            <with variable="activePartId">
                <equals value="org.eclipse.ui.views.ProblemView" />
            </with>
        </activeWhen>
    </handler>
</extension>

Or we can programmatically activate them through the IHandlerService which we would retrieve from the ProblemView site.

IHandlerService handlerServ = (IHandlerService)getSite().getService(IHandlerService.class);
CopyMarkerHandler copy = new CopyMarkerHandler();
handlerServ.activateHandler("org.eclipse.ui.edit.copy", copy);

Using the ProblemView site to access the IHandlerService handles the <activeWhen/> clause for us, and our programmatic handler would manage its own enablement state.

Menus

Then we would define the ProblemView menu structures. We are using 3 roots: the view menu, the view toolbar, and the view context menu. This is an example of an "in-place" menu definition. The <menuContribution/> location attribute is a URI that defines the starting point for inserting the menu elements. The XML hierarchy mirrors the menu hierarchy, in that you can define items and menus within the body of other menus.

<extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="menu:org.eclipse.ui.views.ProblemView">
        <command commandId="org.eclipse.ui.views.problems.sorting"
                label="%ProblemView.Sorting.name"
                mnemonic="%ProblemView.Sorting.mnemonic"
                style="push"
                tooltip="%ProblemView.Sorting.tooltip">
        </command>
        <menu id="org.eclipse.ui.views.problems.groupBy.menu"
                label="%ProblemView.GroupBy.label"
                mnemonic="%ProblemView.GroupBy.mnemonic">
            <dynamic class="org.eclipse.ui.views.markers.internal.GroupByItems"
                    id="org.eclipse.ui.views.problems.groupBy.items">
            </dynamic>
        </menu>
        <separator name="group.filter" visible="true" />
        <menu id="org.eclipse.ui.views.problems.filters.menu"
                label="%ProblemView.Filters.label"
                mnemonic="%ProblemView.Filters.mnemonic">
            <dynamic class="org.eclipse.ui.views.markers.internal.FilterItems"
                    id="org.eclipse.ui.views.problems.filters.items" />
        </menu>
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
                icon="$nl$/elcl16/filter_ps.gif"
                label="%ProblemView.ConfigureFilters.name"
                mnemonic="%ProblemView.ConfigureFilters.mnemonic"
                tooltip="%ProblemView.ConfigureFilters.tooltip" />
        <command commandId="org.eclipse.ui.preferences.viewPreferences"
                label="%ViewPreferences.name"
                mnemonic="%ViewPreferences.mnemonic">
            <parameter name="markerEnablementName"
                        value="LIMIT_PROBLEMS" />
            <parameter name="markerLimitName"
                        value="PROBLEMS_LIMIT" />
        </command>
    </menuContribution>
    <menuContribution locationURI="toolbar:org.eclipse.ui.views.ProblemView">
        <command commandId="org.eclipse.ui.views.problems.configureFilters"
                icon="$nl$/elcl16/filter_ps.gif"
                tooltip="%ProblemView.ConfigureFilters.tooltip" />
    </menuContribution>
    <menuContribution locationURI="popup:org.eclipse.ui.views.ProblemView">
        <command commandId="org.eclipse.ui.views.problems.goTo"
                mnemonic="%ProblemView.GoTo.mnemonic"
                icon="$nl$/elcl16/gotoobj_tsk.gif"
                disabledIcon="$nl$/dlcl16/gotoobj_tsk.gif"
                tooltip="%ProblemView.GoTo.tooltip" />
        <separator name="group.showIn"
                visible="true" />
        <menu id="org.eclipse.ui.views.problems.showIn.menu"
                label="%ProblemView.ShowIn.label"
                mnemonic="%ProblemView.ShowIn.mnemonic">
            <dynamic class="org.eclipse.ui.actions.ShowInContributions"
                    id="org.eclipse.ui.views.problems.showIn.items" />
        </menu>
        <separator name="group.edit"
                visible="true" />
        <command commandId="org.eclipse.ui.edit.copy"
                mnemonic="%ProblemView.copy.mnemonic"
                icon="$nl$/icons/full/etool16/copy_edit.gif"
                disabledIcon="$nl$/icons/full/dtool16/copy_edit.gif" />
        <command commandId="org.eclipse.ui.edit.paste"
                mnemonic="%ProblemView.paste.mnemonic"
                icon="$nl$/icons/full/etool16/paste_edit.gif"
                disabledIcon="$nl$/icons/full/dtool16/paste_edit.gif" />
        <command commandId="org.eclipse.ui.edit.delete"
                mnemonic="%ProblemView.delete.mnemonic"
                icon="$nl$/icons/full/etool16/delete_edit.gif"
                disabledIcon="$nl$/icons/full/dtool16/delete_edit.gif">
            <visibleWhen>
                <not>
                    <with variable="activePartId">
                        <equals value="org.eclipse.ui.views.ProblemView" />
                    </with>
                </not>
            </visibleWhen>
        </command>
        <command commandId="org.eclipse.ui.edit.selectAll"
                mnemonic="%ProblemView.selectAll.mnemonic" />
        <separator name="group.resolve"
                visible="true" />
        <command commandId="org.eclipse.jdt.ui.edit.text.java.correction.assist.proposals"
                mnemonic="%ProblemView.Resolve.mnemonic"
                icon="$nl$/icons/full/elcl16/smartmode_co.gif"
                disabledIcon="$nl$/icons/full/dlcl16/smartmode_co.gif" />
        <separator name="additions"
                visible="false" />
        <separator name="group.properties"
                visible="true" />
        <command commandId="org.eclipse.ui.file.properties"
                mnemonic="%ProblemView.Properties.mnemonic" />
    </menuContribution>
</extension>

Menus API

We can contribute menu definitions through the IMenuService API.

The above example can be done for the view menus:

public void addProblemsViewMenuContribution() {
    IMenuService menuService = (IMenuService) PlatformUI.getWorkbench()
            .getService(IMenuService.class);
 
    AbstractContributionFactory viewMenuAddition = new AbstractContributionFactory(
            "menu:org.eclipse.ui.views.ProblemView?after=additions") {
        public void createContributionItems(IMenuService menuService,
                List additions) {
            CommandContributionItem item = new CommandContributionItem(
                    null, "org.eclipse.ui.views.problems.sorting", null,
                    null, null, null, "Sorting...", "S",
                    "Change the Sort order",
                    CommandContributionItem.STYLE_PUSH);
            additions.add(item);
 
            MenuManager submenu = new MenuManager("Group &By",
                    "org.eclipse.ui.views.problems.groupBy.menu");
            IContributionItem dynamicItem = new CompoundContributionItem(
                    "org.eclipse.ui.views.problems.groupBy.items") {
                protected IContributionItem[] getContributionItems() {
                    // Here's where you would dynamically generate your list
                    IContributionItem[] list = new IContributionItem[2];
                    Map parms = new HashMap();
                    parms.put("groupBy", "Severity");
                    list[0] = new CommandContributionItem(null,
                            "org.eclipse.ui.views.problems.grouping",
                            parms, null, null, null, "Severity", null,
                            null, CommandContributionItem.STYLE_PUSH);
 
                    parms = new HashMap();
                    parms.put("groupBy", "None");
                    list[1] = new CommandContributionItem(null,
                            "org.eclipse.ui.views.problems.grouping",
                            parms, null, null, null, "None", null, null,
                            CommandContributionItem.STYLE_PUSH);
                    return list;
                }
            };
            submenu.add(dynamicItem);
 
            additions.add(submenu);
            additions.add(new Separator("group.filter"));
 
            submenu = new MenuManager("&Filters",
                    "org.eclipse.ui.views.problems.filters.menu");
            dynamicItem = new CompoundContributionItem(
                    "org.eclipse.ui.views.problems.filters.items") {
                protected IContributionItem[] getContributionItems() {
                    // Here's where you would dynamically generate your list
                    IContributionItem[] list = new IContributionItem[1];
                    Map parms = new HashMap();
                    parms.put("filter", "Default");
                    list[0] = new CommandContributionItem(null,
                            "org.eclipse.ui.views.problems.filters", parms,
                            null, null, null, "Default", null, null,
                            CommandContributionItem.STYLE_PUSH);
                    return list;
                }
            };
            submenu.add(dynamicItem);
 
            additions.add(submenu);
 
            ImageDescriptor filterIcon = PlatformUI.getWorkbench()
                    .getSharedImages().getImageDescriptor(
                            "elcl16/filter_ps.gif");
            item = new CommandContributionItem(null,
                    "org.eclipse.ui.views.problems.configureFilters", null,
                    filterIcon, null, null, "Configure Filters...", "C",
                    "Configure the filters to be applied to this view",
                    CommandContributionItem.STYLE_PUSH);
            additions.add(item);
 
            Map parms = new HashMap();
            parms.put("markerEnablementName", "LIMIT_PROBLEMS");
            parms.put("markerLimitName", "PROBLEMS_LIMIT");
            item = new CommandContributionItem(null,
                    "org.eclipse.ui.preferences.viewPreferences", parms,
                    null, null, null, "Preference", "P",
                    "Open the preference dialog",
                    CommandContributionItem.STYLE_PUSH);
            additions.add(item);
        }
 
        public void releaseContributionItems(IMenuService menuService,
                List items) {
            // for us this is a no-op
        }
    };
    menuService.addContributionFactory(viewMenuAddition);
}

The AbstractContributionFactory creates new contribution items every time createContributionItems(List) is called. The factory location tells the framework where to insert the contributions when populating ContributionManagers.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.