Difference between revisions of "Menu Contributions/TextEditor viewerContribution"

From Eclipsepedia

Jump to: navigation, search
 
(Text editor popup action)
 
Line 7: Line 7:
 
First define the command and its handler.
 
First define the command and its handler.
  
  <extension point="org.eclipse.ui.commands">
+
<source lang="xml">
 +
<extension point="org.eclipse.ui.commands">
 
     <command id="org.eclipse.ui.examples.menus.scramble.text"
 
     <command id="org.eclipse.ui.examples.menus.scramble.text"
            defaultHandler="org.eclipse.ui.examples.menus.internal.ScrambleTextHandler"
+
            defaultHandler="org.eclipse.ui.examples.menus.internal.ScrambleTextHandler"
            name="%ScrambleText.name"
+
            name="%ScrambleText.name"
            description="%ScrambleText.description" />
+
            description="%ScrambleText.description" />
  </extension>
+
</extension>
 +
</source>
  
 
== Menus ==
 
== Menus ==
Line 18: Line 20:
 
Placing the action (which is specifically a menu or button linked to a command) can be accomplished with the org.eclipse.ui.menus extension point.
 
Placing the action (which is specifically a menu or button linked to a command) can be accomplished with the org.eclipse.ui.menus extension point.
  
  <extension point="org.eclipse.ui.menus">
+
<source lang="xml">
 +
<extension point="org.eclipse.ui.menus">
 
     <menuContribution locationURI="popup:#TextEditorContext?after=additions">
 
     <menuContribution locationURI="popup:#TextEditorContext?after=additions">
      <command commandId="org.eclipse.ui.examples.menus.scramble.text"
+
        <command commandId="org.eclipse.ui.examples.menus.scramble.text"
            mnemonic="%ScrambleText.mnemonic"
+
                mnemonic="%ScrambleText.mnemonic"
            icon="$nl$/icons/full/eobj16/scramble.gif" />
+
                icon="$nl$/icons/full/eobj16/scramble.gif" />
 
     </menuContribution>
 
     </menuContribution>
  </extension>
+
</extension>
 +
</source>
  
 
== Menus API ==
 
== Menus API ==
Line 30: Line 34:
 
Programmatically do this, you would have to go through the IMenuService.
 
Programmatically do this, you would have to go through the IMenuService.
  
 
+
<source lang="java">
public static void addTextMenuContribition() {
+
public static void addTextMenuContribition() {
    final IMenuService menuService = (IMenuService) PlatformUI
+
    final IMenuService menuService = (IMenuService) PlatformUI
            .getWorkbench().getService(IMenuService.class);
+
            .getWorkbench().getService(IMenuService.class);
 
   
 
   
    final ImageDescriptor scrambleIcon = AbstractUIPlugin
+
    final ImageDescriptor scrambleIcon = AbstractUIPlugin
            .imageDescriptorFromPlugin("org.eclise.ui.tests",
+
            .imageDescriptorFromPlugin("org.eclise.ui.tests",
                    "icons/full/eobj16/scramble.gif");
+
                    "icons/full/eobj16/scramble.gif");
    AbstractContributionFactory factory = new AbstractContributionFactory(
+
    AbstractContributionFactory factory = new AbstractContributionFactory(
            "popup:#TextEditorContext?after=additions") {
+
            "popup:#TextEditorContext?after=additions") {
        public void createContributionItems(IMenuService menuService,
+
                public void createContributionItems(IMenuService menuService,
                List additions) {
+
                        List additions) {
            CommandContributionItem item = new CommandContributionItem(
+
                    CommandContributionItem item = new CommandContributionItem(
                    "org.eclipse.ui.examples.menus.scramble.text",
+
                            "org.eclipse.ui.examples.menus.scramble.text",
                    "org.eclipse.ui.examples.menus.scramble.text",
+
                            "org.eclipse.ui.examples.menus.scramble.text",
                    null, scrambleIcon, null, null, null, "c", null,
+
                            null, scrambleIcon, null, null, null, "c", null,
                    CommandContributionItem.STYLE_PUSH);
+
                            CommandContributionItem.STYLE_PUSH);
            additions.add(item);
+
                    additions.add(item);
        }
+
                }
 
   
 
   
        public void releaseContributionItems(IMenuService menuService,
+
                public void releaseContributionItems(IMenuService menuService,
                List items) {
+
                        List items) {
        }
+
                }
    };
+
    };
    menuService.addContributionFactory(factory);
+
    menuService.addContributionFactory(factory);
}
+
}
 +
</source>

Latest revision as of 10:24, 15 April 2008

Contents

[edit] Text editor popup action

Popups can be targetted at any registered context menu, or at all of them. This is the Scramble Text command to be added the the standard text editor context menu.

[edit] Commands

First define the command and its handler.

<extension point="org.eclipse.ui.commands">
    <command id="org.eclipse.ui.examples.menus.scramble.text"
            defaultHandler="org.eclipse.ui.examples.menus.internal.ScrambleTextHandler"
            name="%ScrambleText.name"
            description="%ScrambleText.description" />
</extension>

[edit] Menus

Placing the action (which is specifically a menu or button linked to a command) can be accomplished with the org.eclipse.ui.menus extension point.

<extension point="org.eclipse.ui.menus">
    <menuContribution locationURI="popup:#TextEditorContext?after=additions">
        <command commandId="org.eclipse.ui.examples.menus.scramble.text"
                mnemonic="%ScrambleText.mnemonic"
                icon="$nl$/icons/full/eobj16/scramble.gif" />
    </menuContribution>
</extension>

[edit] Menus API

Programmatically do this, you would have to go through the IMenuService.

public static void addTextMenuContribition() {
    final IMenuService menuService = (IMenuService) PlatformUI
            .getWorkbench().getService(IMenuService.class);
 
    final ImageDescriptor scrambleIcon = AbstractUIPlugin
            .imageDescriptorFromPlugin("org.eclise.ui.tests",
                    "icons/full/eobj16/scramble.gif");
    AbstractContributionFactory factory = new AbstractContributionFactory(
            "popup:#TextEditorContext?after=additions") {
                public void createContributionItems(IMenuService menuService,
                        List additions) {
                    CommandContributionItem item = new CommandContributionItem(
                            "org.eclipse.ui.examples.menus.scramble.text",
                            "org.eclipse.ui.examples.menus.scramble.text",
                            null, scrambleIcon, null, null, null, "c", null,
                            CommandContributionItem.STYLE_PUSH);
                    additions.add(item);
                }
 
                public void releaseContributionItems(IMenuService menuService,
                        List items) {
                }
    };
    menuService.addContributionFactory(factory);
}