Skip to main content
 首页 » 编程设计

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

2022年07月19日152三少

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.       运行截图

 


本文参考链接:https://blog.csdn.net/neweastsun/article/details/46980331