码迷,mamicode.com
首页 > 系统相关 > 详细

eclipse插件开发入门——使用command实现在资源管理器中定位资源

时间:2015-07-21 09:08:27      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:eclipse   插件   command   eclipse插件   

Eclipse插件开发之command

 

Eclipse提供了三种命令与操作的方式:动作ActionSets、弹出菜单popupmenus、命令Command,其中前两种因为耦合过于紧密,在未来版本中可能弃用,不建议使用。

ActionSets方式使用的是扩展点org.eclipse.ui.actionsets;

popupmenus方式使用了一个扩展点:org.eclipse.ui.popupmenus;

Command方式使用了三个扩展点:org.eclipse.ui.menus; org.eclipse.ui.commands;org.eclipse.ui.handlers;

命令代表了外观和行为之间的抽象。命令本身不代表外观和行为,其捆绑一个或多个有一个或多个处理程序(行为)的Menu共享(外观)。

技术分享

 

下面开发一个在资源管理中打开资源的插件,如在java包或文件中右键点击,打开资源文件。通过command定义扩展点。

1.       增加依赖

技术分享

2.       添加扩展点

技术分享

xml配置如下:

<extension

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

      <category

            name="Sample Category"

            id="com.cnofe.explorer.commands.category">

      </category>

      <command

            name="Explorer(定位资源位置)"

            categoryId="com.cnofe.explorer.commands.category"

            id="com.cnofe.explorer.commands.sampleCommand">

      </command>

   </extension>

   <extension

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

      <handler

            commandId="com.cnofe.explorer.commands.sampleCommand"

            class="com.cnofe.explorer.handlers.OpenExplorerHandler">

      </handler>

   </extension>

   <extension

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

      <key

            commandId="com.cnofe.explorer.commands.sampleCommand"

            contextId="org.eclipse.ui.contexts.window"

            sequence="ALT+O"

            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">

      </key>

   </extension>

   <extension

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

      <menuContribution

            allPopups="false"

            locationURI="popup:org.eclipse.ui.popup.any?after=additions">

         <separator

               name="com.cnofe.explorer.separator1">

         </separator>

         <command

               commandId="com.cnofe.explorer.commands.sampleCommand"

               icon="icons/folderlink.png"

               style="push">

            <visibleWhen>

               <iterateifEmpty="false">

               <or>

                  <instanceof

                        value="org.eclipse.team.ui.synchronize.ISynchronizeModelElement">

                  </instanceof>

                  <instanceof

                        value="org.eclipse.jdt.core.IJavaElement">

                  </instanceof>

                  <instanceof

                        value="org.eclipse.core.resources.IResource">

                  </instanceof>

               </or>

               </iterate>

            </visibleWhen>

         </command>

      </menuContribution>

   </extension>

3.       实现handler

publicclass OpenExplorerHandler extends AbstractHandler {

 

    public Object execute(ExecutionEvent event) throws ExecutionException {

        IWorkbenchWindow window = HandlerUtil

                .getActiveWorkbenchWindowChecked(event);

        ISelection sel = window.getSelectionService().getSelection();

        if (selinstanceof IStructuredSelection) {

            Object obj = ((IStructuredSelection) sel).getFirstElement();

            IResource resource = null;

            String path = null;

            // common resource file

            if (objinstanceof IFile) {

                resource = (IResource) obj;

                path = resource.getLocation().toOSString();

                path = path.substring(0, path.lastIndexOf(File.separator));

            }

            // other resource such as folder,project

            elseif (objinstanceof IResource) {

                resource = (IResource) obj;

                path = resource.getLocation().toOSString();

            }

            // explorer java element, containfield,method,package

            elseif (objinstanceof IJavaElement) {

                // jar resource is null

                if (objinstanceof JarPackageFragmentRoot) {

                    path = ((IPackageFragmentRoot) obj).getPath().toOSString();

                    // get folder

                    path = path.substring(0, path.lastIndexOf(File.separator));

                } elseif (objinstanceof IPackageFragmentRoot) {

                    // src folder

                    String prjPath = ((IPackageFragmentRoot) obj)

                            .getJavaProject().getProject().getParent()

                            .getLocation().toOSString();

                    path = prjPath

                            +((IPackageFragmentRoot) obj).getPath()

                                    .toOSString();

                } elseif (objinstanceof IPackageFragment) {// other : package

                    resource = ((IPackageFragment) obj).getResource();

                    path = resource.getLocation().toOSString();

                } else {// member:filed:

                    resource = ((IJavaElement) obj).getResource();

                    path = resource.getLocation().toOSString();

                    // get folder

                    path = path.substring(0, path.lastIndexOf(File.separator));

                }

            }

            // explorer team ui resource

            elseif (objinstanceof ISynchronizeModelElement) {

                resource = ((ISynchronizeModelElement) obj).getResource();

            }

            if (path != null) {

                try {

                    Runtime.getRuntime().exec("explorer " + path); //$NON-NLS-1$

                } catch (IOException e) {

                    //

                }

            }

        }

        returnnull;

    }

}

 

4.       运行截图

技术分享

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

eclipse插件开发入门——使用command实现在资源管理器中定位资源

标签:eclipse   插件   command   eclipse插件   

原文地址:http://blog.csdn.net/neweastsun/article/details/46980331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!