您的位置:首页 > 编程语言 > Java开发

Eclipse 插件开发 - 工具栏(tool bar)增加下来菜单

2016-11-19 16:23 387 查看
在开发Eclipse 插件时, 想在工具栏中增加一个按钮图标是非常容易, 但是想在图标上增加子菜单就会比较麻烦, 例如想实现如下效果: 



具体步骤如下:

首先在扩展点org.eclipse.ui.commands中增加三个指令

[html] view
plain copy

<extension  

    point="org.eclipse.ui.commands">  

       

    <command  

        name="Reference Command"  

        id="top.itart.plugin.smartboot.referenceCommand">  

    </command>  

   

    <command   

        name="Sub1 Command"   

        id="top.itart.plugin.smartboot.sub1Command">  

    </command>  

   

    <command  

        name="Sub2 Command"  

        id="top.itart.plugin.smartboot.sub2Command">  

    </command>  

        

</extension>  

其次在扩展点org.eclipse.ui.menus 增加两个menuContribution

[html] view
plain copy

<menuContribution  

    locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">  

    <toolbar  

       id="top.itart.plugin.smartboot.toolbars">  

        <command  

              commandId="top.itart.plugin.smartboot.referenceCommand"  

              icon="icons/sample.gif"  

              tooltip="Pull down" style="pulldown">  

        </command>  

    </toolbar>  

</menuContribution>  

这个是定义显示在toolbar上的菜单. Command 中的commandId 为第一个指令的ID, 类型 type 为pulldown, 并且放在toolbar标签中

[html] view
plain copy

<menuContribution  

    locationURI="menu:top.itart.plugin.smartboot.referenceCommand">  

    <command  

              commandId="top.itart.plugin.smartboot.sub1Command"  

              icon="icons/sample.gif"  

              label = "Sub 1"  

              tooltip="Sub 1">  

    </command>  

    <command  

        commandId="top.itart.plugin.smartboot.sub2Command"  

        icon="icons/sample.gif"  

        tooltip="Sub2"  

        label = "Sub 2">  

    </command>  

</menuContribution>  

这个menuContribution是定义下拉的子菜单. 注意locationURI的值是menu: <Toolbar上的菜单ID>

最终的plugin.xml如下

[html] view
plain copy

<?xml version="1.0" encoding="UTF-8"?>  

<?eclipse version="3.4"?>  

<plugin>  

  

    <extension  

        point="org.eclipse.ui.commands">  

           

        <command  

            name="Reference Command"  

            id="top.itart.plugin.smartboot.referenceCommand">  

        </command>  

       

        <command   

            name="Sub1 Command"   

            id="top.itart.plugin.smartboot.sub1Command">  

        </command>  

       

        <command  

            name="Sub2 Command"  

            id="top.itart.plugin.smartboot.sub2Command">  

        </command>  

            

    </extension>  

     

     

   <extension  

         point="org.eclipse.ui.menus">  

        

        <menuContribution  

            locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">  

            <toolbar  

               id="top.itart.plugin.smartboot.toolbars">  

                <command  

                      commandId="top.itart.plugin.smartboot.referenceCommand"  

                      icon="icons/sample.gif"  

                      tooltip="Pull down" style="pulldown">  

                </command>  

            </toolbar>  

        </menuContribution>  

        

        <menuContribution  

            locationURI="menu:top.itart.plugin.smartboot.referenceCommand">  

            <command  

                      commandId="top.itart.plugin.smartboot.sub1Command"  

                      icon="icons/sample.gif"  

                      label = "Sub 1"  

                      tooltip="Sub 1">  

            </command>  

            <command  

                commandId="top.itart.plugin.smartboot.sub2Command"  

                icon="icons/sample.gif"  

                tooltip="Sub2"  

                label = "Sub 2">  

            </command>  

        </menuContribution>  

        

    </extension>  

  

</plugin>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eclipse 插件开发