[导入]Eclipse运行(launcher)框架三


网站: JavaEye  作者: liugang594  链接:http://liugang594.javaeye.com/blog/152353  发表时间: 2008年01月02日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

ShortCuts的增加

我们要运行java的时候,我们在java文件上点右键,就有一个“run as”菜单,可以到里面选择java的运行方式;在“debug as”菜单里可以选择调试方式。所以我们也想有一个这个怎么办?

这里就要用到另一个扩展点:org.eclipse.debug.ui.launchShortcuts。这是和UI相关的,所以放到我们的UI插件里。我的实现如下:

   <extension

         point="org.eclipse.debug.ui.launchShortcuts">

      <shortcut

            class="com.tibco.cdc.liugang.launcher.ui.shortcut.LiugangLaunchShortcut"

            icon="icons/sample.gif"

            id="com.tibco.cdc.liugang.launcher.ui.shortcut"

            label="Liugang Application"

            modes="run,debug">

         <configurationType

               id="com.tibco.cdc.liugang.launcher.type">

         </configurationType>

           <perspective id="org.eclipse.jdt.ui.JavaPerspective"/>

            <perspective id="org.eclipse.jdt.ui.JavaHierarchyPerspective"/>

            <perspective id="org.eclipse.jdt.ui.JavaBrowsingPerspective"/>

            <perspective id="org.eclipse.debug.ui.DebugPerspective"/>

                  <contextualLaunch>

            <enablement>

               <with variable="selection">

                  <count value="1"/>

                  <iterate>

                     <instanceof value="org.eclipse.core.resources.IFile"/>

                     <test

                           value="*.liugang"

                           property="org.eclipse.debug.ui.matchesPattern"/>

                  </iterate>

               </with>

            </enablement>

         </contextualLaunch>

      </shortcut>

   </extension>

 

这里有几个地方:首先当然是那个实现类是最重要的了。其次我加了一个出现在条件,如果选择的是一个后缀名为“liugang”的文件,则出现。所以现在运行如下:

 

tu6

图六

所以这个时候需要自己去生成对应的configuration去运行。通常要做的事如下:首先查找是否这个文件对应的launchConfiguration已经存在,如果存在,则直接用就可以;否则,新建一个configuration然后运行。

 

补充

我们再回过头去看一下LiugangLaunchConfigurationTypelaunch()方法里的launch参数。在开始之前,我们把运行产生的eclipse的透视图切换到调试视图,然后运行一个:

 

tu7

图七

我们看红色圈住的地方。每运行一次,会产生一个这样的configuration。不过我们再看看java

 

tu8

图八

我们看到它比我们生成的要多一次。如果是在调试情况下,那就更多了:

 

tu9

图九

这就是之前我们介绍参数:launch,时说到了,运行完以后可以把运行的结果加到这个launch里去。这里有两种加的方式:一个是加IProcess对象,直接运行的情况下一般是加这个;另一个是IDebugTarget对象,调试的情况下用这个,它有一个层次结构。现在我们先不讲调试。看看运行的情况下怎么加上吧。例如我们有一个取IP的操作,就可以如下完成:

public class LiugangLaunchConfigurationType implements

            ILaunchConfigurationDelegate {

 

      public void launch(ILaunchConfiguration configuration, String mode,

                  ILaunch launch, IProgressMonitor monitor) throws CoreException {

            System.out.println("You selected liugang launchconfiguration type to run");

            Process process;

            try {

                  process = Runtime.getRuntime().exec("ipconfig");

                  RuntimeProcess runtimeProcess = new RuntimeProcess(launch,process,"Liugang Process",null);

                  launch.addProcess(runtimeProcess);

            } catch (IOException e) {

                  e.printStackTrace();

            }

      }

 

}

我们再运行试试:

 

tu10

图十

好玩吧?好了,先讲这么多了。下次就要开讲调试器了。不过这个比较复杂,可能要长一些时间的准备。等着就行了!

 

[参考]

http://www.eclipse.org/articles/Article-Launch-Framework/launch.html


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐




文章来源:http://liugang594.javaeye.com/blog/152353

posted on 2008-01-02 15:05  刘刚  阅读(826)  评论(1)    收藏  举报

导航