RCP关于配置方式在菜单栏创建菜单

来源:互联网 发布:物流软件系统试用 编辑:程序博客网 时间:2024/05/22 03:01
Java代码 复制代码
  1. RCP关于配置方式在菜单栏创建菜单  
Java代码 复制代码
  1. <extension   
  2.          point="org.eclipse.ui.commands">   
  3.       <category   
  4.             id="com.vnvntrip.plugin.dev.commands.category"  
  5.             name="Sample Category">   
  6.       </category>  
Java代码 复制代码
  1. <!---创建一个command命令-->   
  2.    <command   
  3.          categoryId="com.vnvntrip.plugin.dev.commands.category"  
  4.          id="com.vnvntrip.plugin.dev.commands.sampleCommand"  
  5.          name="编辑">   
  6.    </command>  
Java代码 复制代码
  1.    <command   
  2.          name="Open Mailbox"  
  3.          description="Opens a mailbox"  
  4.          categoryId="com.vnvntrip.plugin.dev.commands.category"  
  5.          id="com.vnvntrip.plugin.dev.commands.category.open">   
  6.    </command>   
  7.    <command   
  8.          name="Open Message Dialog"  
  9.          description="Open a message dialog"  
  10.          categoryId="com.vnvntrip.plugin.dev.commands.category"  
  11.          id="com.vnvntrip.plugin.dev.commands.category.openMessage">   
  12.    </command>   
  13.       
  14.       
  15. </extension>  
Java代码 复制代码
  1. <!---使用处理器(Handler)绑定Command--->   
  2.    <extension   
  3.          point="org.eclipse.ui.handlers">   
  4.       <handler   
  5.             class="com.vnvntrip.plugin.dev.handlers.SampleHandler"  
  6.             commandId="com.vnvntrip.plugin.dev.commands.sampleCommand">   
  7.       </handler>   
  8.    </extension>  
Java代码 复制代码
  1. <extension   
  2.       point="org.eclipse.ui.bindings">  
Java代码 复制代码
  1. <!---绑定相应的快捷键-->   
  2.       <key   
  3.             commandId="com.vnvntrip.plugin.dev.commands.sampleCommand"  
  4.             contextId="org.eclipse.ui.contexts.window"  
  5.             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  6.             sequence="M1+6">   
  7.       </key>   
  8.          
  9.        <key   
  10.             commandId="com.vnvntrip.plugin.dev.commands.category.open"  
  11.             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  12.             sequence="CTRL+2">   
  13.       </key>   
  14.       <key   
  15.             commandId="com.vnvntrip.plugin.dev.commands.category.openMessage"  
  16.             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  17.             sequence="CTRL+3">   
  18.       </key>   
  19.       <key   
  20.             commandId="org.eclipse.ui.file.exit"  
  21.             schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"  
  22.             sequence="CTRL+X">   
  23.       </key>   
  24.    </extension>   
  25.       
  26.    <extension   
  27.          point="org.eclipse.ui.menus">   
  28.       <menuContribution   
  29.             locationURI="menu:org.eclipse.ui.main.menu?after=additions">  
Java代码 复制代码
  1. <!---创建相关的菜单对象-->   
  2.       <menu   
  3.             id="com.vnvntrip.plugin.dev.menus.sampleMenu"  
  4.             label="编辑"  
  5.             mnemonic="M">  
Java代码 复制代码
  1.   <!---菜单绑定的命令-->   
  2.    <command   
  3.          commandId="com.vnvntrip.plugin.dev.commands.sampleCommand"  
  4.          id="com.vnvntrip.plugin.dev.menus.sampleCommand"  
  5.          mnemonic="S">   
  6.    </command>   
  7. </menu>   
  8. ;/menuContribution>  
Java代码 复制代码
  1.    <menuContribution   
  2.          locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">   
  3.       <toolbar   
  4.             id="com.vnvntrip.plugin.dev.toolbars.sampleToolbar">   
  5.          <command   
  6.                commandId="com.vnvntrip.plugin.dev.commands.sampleCommand"  
  7.                icon="icons/sample.gif"  
  8.                id="com.vnvntrip.plugin.dev.toolbars.sampleCommand"  
  9.                tooltip="Say hello world">   
  10.          </command>   
  11.       </toolbar>   
  12.    </menuContribution>   
  13. </extension>  

 

  在编码是仅仅需要编写Handler中的代码:

 


 

Java代码 复制代码
  1. package com.vnvntrip.plugin.dev.handlers;   
  2.   
  3. import org.eclipse.core.commands.AbstractHandler;   
  4. import org.eclipse.core.commands.ExecutionEvent;   
  5. import org.eclipse.core.commands.ExecutionException;   
  6. import org.eclipse.swt.widgets.Shell;   
  7. import org.eclipse.ui.handlers.HandlerUtil;   
  8. import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceDialog;   
  9.   
  10. /**  
  11.  *   
  12.  * @author longgangbai  
  13.  *  
  14.  */  
  15. public class SampleHandler extends AbstractHandler {   
  16.     private static String CUSTOMPROPERTYPAGE = "com.vnvntrip.plugin.dev.properties.CustomPropertyPage";   
  17.   
  18.     /**  
  19.      * The constructor.  
  20.      */  
  21.     public SampleHandler() {   
  22.     }   
  23.   
  24.     /**  
  25.      * the command has been executed, so extract extract the needed information  
  26.      * from the application context.  
  27.      */  
  28.     public Object execute(ExecutionEvent event) throws ExecutionException {   
  29.     Shell shell = HandlerUtil.getActiveShell(event);   
  30.     // JfaceDialog dialog=new JfaceDialog(shell);   
  31.     // dialog.open();   
  32.     //WorkbenchPreferenceDialog.createDialogOn(shell, preferencePageId)   
  33.     WorkbenchPreferenceDialog dialog = WorkbenchPreferenceDialog   
  34.         .createDialogOn(shell, CUSTOMPROPERTYPAGE);   
  35.     dialog.showOnly(new String[] { CUSTOMPROPERTYPAGE });   
  36.     dialog.open();   
  37.     return null;   
  38.     }   
  39. }