[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[整理]RCP开发,如何定义Menu和ToolBar的位置。

上一篇:[备忘]小巧好用的flash music player地址~~
下一篇:[备忘]一个简单好用的,免费的,支持命令行的,图片批量缩略图工具~~~

添加日期:2012/6/7 16:50:51 快速返回   返回列表 阅读6712次
RCP开发中,通过actionSet定义菜单和工具栏后,会发现菜单被放在了help的后边。
那么如何定义它们的位置呢~~

(1)如在actionSet下有一个menu的定义。
<menu
    id="xxxx.yyyyMenu"
    label="我的菜单"
    path="myMenu">
</menu>
通过menubarPath,可以指定menu出现的位置。
运行发现,菜单加在了最后。

(2)那么应该用什么名字呢?

一个RCP 程序的标准菜单通常包含以下几个:
file
edit
window
help

每个菜单都有一个名字,可以引用。(org.eclipse.ui.IWorkbenchActionConstants有更多的常量定义。)
如果用menubarPath="file",那么菜单就会被加到file菜单的右边。

(3)但通常的做法是,在创建菜单时,预留一个空位。
如以下代码:
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
menuBar.add(showWindowsMenu);
menuBar.add(helpMenu);
其中,
menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
就创建了一个空位,它的名称就是additions。
Eclipse本身也是这样实现的,所以,这招值得学习。

那我们只要menubarPath="additions"就行了。
如果有多个菜单,那么后定义的会在左边,先定义的在右边,

(4)当menubarPath未指定时,默认就往additions里放,如果additions不存在,则放到最后。

(5)当然,你也可以预留一个其他名字的空位。但最好在文档中说明,否则别人可不知道。

(6)至于工具栏的位置,类似。
<action
    class="xxxxx.xxxAction"
    icon="icons/ftpStart.gif"
    id="xxxxxxxxxxxxxxxx"
    label="xxxxx"
    menubarPath="additions"
    style="push"
    toolbarPath="my tool">
</action>
使用toolbarPath来定义,格式可以如AAA/BBB这个样子。
有一些内置的定义,还是参看org.eclipse.ui.IWorkbenchActionConstants这个类。
如toolbarPath="Normal/save.ext"
Normal表示默认工具栏,save.ext表示“扩展保存功能”那个group。
反正就是,toolbarPath值一样的,会放在一起。
这个值可以随便写,无所谓有没有,没有会自动创建一个group,不过就是放在最后了。

怎么控制精确位置,还没学会~~~~

这个文章可以看看:
http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html
---------------------------------------------------

下面看一下,如何使用分组。
<menu
     id="xxxx.subMenu"
     label="View Sub Menu"
     path="additions">         <----这里

    <separator name="group1"/>    <----这里
    <separator name="group2"/>    <----这里
</menu>
<action
     id="xxxx.action2" 
     label="View Action 2"
     icon="icons/red_dot.gif"
    menubarPath="xxxx.subMenu/group1"  <----这里
     class="xxxx.ViewAction2Delegate"
    enablesFor="1">
</action> 
<action
     id="xxxx.action3" 
     label="View Action 3"
     icon="icons/red_dot.gif"
    menubarPath="xxxx.subMenu/group2"  <----这里
     class="xxxx.ViewAction3Delegate"
    enablesFor="2+">
</action> 

首先,菜单被放在additions之后,然后定义了group1和group2。
然后两个action分别放入两个组,
将menubarPath指定为“menu的id/组名”就行了。
实际效果就是菜单里有分隔线了,呵呵
-------------------------------------------------------
创建子菜单的一个例子,留着参考吧。


<extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="tool"
            visible="true"
            id="com.anrainie.ide.workbench.actionSet">
         <menu
               id="toolmenu"                          
               label="%Tool.title"
               >
            <separator
                  name="slot1">                   
            </separator>
            <separator
                name="slot2">                   
            </separator>
         </menu>
               
         <menu
               id="exportdoc"
               label="%Tool.exportDocumentTool"
               path="toolmenu/slot1">                  
            <separator 
             name="slot3">
            </separator>
         </menu>
         <action
               class="com.anrainie.ide.workbench.actions.ExportTrdAction"
               icon="icons/trd.gif"
               id="com.anrainie.ide.workbench.actions.ExportTrdAction"
               label="%Tool.exportDocumentTool.Trd"
               menubarPath="toolmenu/exportdoc/slot3"
               tooltip="%Tool.exportDocumentTool.Trd">
         </action>          
         <action
               class="com.anrainie.ide.workbench.actions.ExportTwfAction"
               icon="icons/twf.gif"
               id="com.anrainie.ide.workbench.actions.ExportTwfAction"
               label="%Tool.exportDocumentTool.Twf"
               menubarPath="toolmenu/exportdoc/slot3"
               tooltip="%Tool.exportDocumentTool.Trd">
         </action>
         <action
               class="com.anrainie.ide.workbench.actions.ExportBcdAction"
               icon="icons/bcd.gif"
               id="com.anrainie.ide.workbench.actions.ExportBcdAction"
               label="%Tool.exportDocumentTool.Bcd"
               menubarPath="toolmenu/exportdoc/slot3"
               style="push"
               tooltip="%Tool.exportDocumentTool.Bcd">
         </action>
 <action
       class="com.anrainie.ide.workbench.actions.ExportTcdAction"
       icon="icons/tcd.gif"
       id="com.anrainie.ide.workbench.actions.ExportTcdAction"
       label="%Tool.exportDocumentTool.Tcd"
       menubarPath="toolmenu/exportdoc/slot3"
       style="push"
       tooltip="%Tool.exportDocumentTool.Bcd">
         </action>
               
      </actionSet>


---------------------------------------------------------------
一个创建程序菜单的例子,留着参考:


package org.codehaus.timtam;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

public class TimTamActionBarAdvisor extends ActionBarAdvisor {

    public TimTamActionBarAdvisor(IActionBarConfigurer configurer) {
        super(configurer);
    }

    @Override
    protected void fillCoolBar(ICoolBarManager coolBar) {
        // no contributions to the cool bar so far
        super.fillCoolBar(coolBar);
    }

    @Override
    protected void fillMenuBar(IMenuManager menuBar) {
        menuBar.add(getFileMenu());
        menuBar.add(getEditMenu());
        menuBar.add(getWindowMenu());
        menuBar.add(getHelpMenu());
    }
    
    @Override
    protected void fillStatusLine(IStatusLineManager statusLine) {
        // no contributions to the status line so far
        super.fillStatusLine(statusLine);
    }

    @Override
    protected void makeActions(IWorkbenchWindow window) {
        // File menu
        register(ActionFactory.QUIT.create(window));

        // Edit menu
        register(ActionFactory.UNDO.create(window));
        register(ActionFactory.REDO.create(window));
        register(ActionFactory.CUT.create(window));
        register(ActionFactory.COPY.create(window));
        register(ActionFactory.PASTE.create(window));
        register(ActionFactory.DELETE.create(window));
        register(ActionFactory.SELECT_ALL.create(window));

        // Windows menu
        register(ActionFactory.OPEN_NEW_WINDOW.create(window));
        register(ActionFactory.PREFERENCES.create(window));        
        
        // Help menu
        register(ActionFactory.HELP_CONTENTS.create(window));
        register(ActionFactory.ABOUT.create(window));
    }

    private MenuManager getFileMenu() {
        MenuManager menu = new MenuManager("File", //$NON-NLS-1$
                IWorkbenchActionConstants.M_FILE);
        menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
        menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        menu.add(getAction(ActionFactory.QUIT.getId()));
        menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
        return menu;
    }

    private MenuManager getEditMenu() {

        MenuManager menu = new MenuManager("Edit", //$NON-NLS-1$
                IWorkbenchActionConstants.M_EDIT);
        menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));

        menu.add(getAction(ActionFactory.UNDO.getId()));
        menu.add(getAction(ActionFactory.REDO.getId()));
        menu.add(new GroupMarker(IWorkbenchActionConstants.UNDO_EXT));
        menu.add(new Separator());

        menu.add(getAction(ActionFactory.CUT.getId()));
        menu.add(getAction(ActionFactory.COPY.getId()));
        menu.add(getAction(ActionFactory.PASTE.getId()));
        menu.add(new GroupMarker(IWorkbenchActionConstants.CUT_EXT));
        menu.add(new Separator());

        menu.add(getAction(ActionFactory.DELETE.getId()));
        menu.add(getAction(ActionFactory.SELECT_ALL.getId()));
        menu.add(new Separator());

        menu.add(new GroupMarker(IWorkbenchActionConstants.ADD_EXT));

        menu.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
        menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
        return menu;
    }

    private MenuManager getWindowMenu() {
        MenuManager menu = new MenuManager("Window", //$NON-NLS-1$
                IWorkbenchActionConstants.M_WINDOW);

        menu.add(getAction(ActionFactory.OPEN_NEW_WINDOW.getId()));

        menu.add(new Separator());
        MenuManager perspectiveMenu = new MenuManager(
                "Open_Perspective", "openPerspective"); //$NON-NLS-1$ //$NON-NLS-2$
        IContributionItem perspectiveList = ContributionItemFactory.PERSPECTIVES_SHORTLIST
                .create(getActionBarConfigurer().getWindowConfigurer().getWindow());
        perspectiveMenu.add(perspectiveList);
        menu.add(perspectiveMenu);

        MenuManager viewMenu = new MenuManager("Show_View"); //$NON-NLS-1$
        IContributionItem viewList = ContributionItemFactory.VIEWS_SHORTLIST
                .create(getActionBarConfigurer().getWindowConfigurer().getWindow());
        viewMenu.add(viewList);
        menu.add(viewMenu);

        menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
        
        menu.add(getAction(ActionFactory.PREFERENCES.getId()));
        
        menu.add(ContributionItemFactory.OPEN_WINDOWS.create(getActionBarConfigurer().getWindowConfigurer().getWindow()));

        return menu;
    }

    private MenuManager getHelpMenu() {
        MenuManager menu = new MenuManager(
                "Help", IWorkbenchActionConstants.M_HELP); //$NON-NLS-1$
        // Welcome or intro page would go here
        menu.add(getAction(ActionFactory.HELP_CONTENTS.getId()));
        // Tips and tricks page would go here
        menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
        menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
        menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
        // About should always be at the bottom
        // To use the real RCP About dialog uncomment these lines
        menu.add(new Separator());
        menu.add(getAction(ActionFactory.ABOUT.getId()));

        return menu;
    }
}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved