ITeye博客

RCP、PDE、Eclipse插件、模态、阻塞Eclipse平台、扩展点知识

feng88724 · 2009-03-13 12:41 · Eclipse Plugin · Markdown

  1. org.eclipse.ui.actionSets --
    给 Eclipse 增加主菜单和工具栏 (Workbench Window Actions) Action需要实现的接口 implements IWorkbenchWindowActionDelegate

    这种情况下,如果需要打开的窗口是模态窗口,并且阻塞整个Eclipse平台: 那么:使用IWorkbenchWindow window 并且在 init方法中初始化(注:此方法在IWorkbenchWindowActionDelegate已声明)


	public void init(IWorkbenchWindow window) {
		this.window = window;
	}

此时,再在run方法中启动伱的界面

	public void run(IAction action) {
		MessageDialog.openInformation(window.getShell(),"Hello Plug-in","Hello, Eclipse world");
	}

注意上面的window.getShell()。

工具栏、菜单栏扩展点示例:


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
   <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            label="Sample Action Set"
            visible="true"
            id="hello.actionSet">
         <menu
               label="Sample &amp;Menu"
               id="sampleMenu">
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
         <action
               label="&amp;Sample Action"
               icon="icons/sample.gif"
               class="hello.actions.SampleAction"	<!-- 指定Action类-->
               tooltip="Hello, Eclipse world"
               menubarPath="sampleMenu/sampleGroup"
               toolbarPath="sampleGroup"
               id="hello.actions.SampleAction">
         </action>
      </actionSet>
   </extension>
</plugin>

  1. org.eclipse.ui.popupMenus 给 Eclipse 增加上下文右键菜单 --
    针对选择的对象不同,显示上下文菜单 (Object Action) Action需要实现的接口 implements IViewActionDelegate --
    针对在不同的View里面,显示上下文菜单 (View Actions)

     伱可以自己定义这个右键菜单出现在上面编辑器中。
              比如: targetID – “org.eclipse.jdt.ui.MembersView” 
              此时,Action需要实现的接口       implements IViewActionDelegate
    
      比如: targetID – “#CompilationUnitEditorContext”  (Java Editor)
              此时,Action需要实现的接口      implements IEditorActionDelegate
    
          另外还有个专门用于文件编辑器的右键菜单的,targetID 记不得了!好像是“#TextEditorContext”
    
      Java Editor中,如果需要打开的窗口是模态窗口,并且阻塞整个Eclipse平台:
      需要使用到IEditorSite editorSite 或者 IWorkbenchPartSite partsite
      在setActiveEditor方法中初始化这两个对象(此方法在IEditorActionDelegate接口中已声明)
    

			public void setActiveEditor(IAction action, IEditorPart targetEditor) {
				partsite = targetEditor.getSite();
				editorSite = targetEditor.getEditorSite();
			}

	此时,再在run方法中启动伱的界面

	public void run(IAction action) {
		new PPMEAddCommentGUI(editorSite.getShell(),SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);	//1
		new PPMEAddCommentGUI(partsite.getShell(),SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);	//2
	}
注意上面的editorSite.getShell() 和partsite.getShell()

Java Editor右键上下文菜单扩展点示例:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension point="org.eclipse.ui.popupMenus">
   <viewerContribution
      id="org.eclipse.ui.articles.action.contribution.popup.editor"
  	  targetID="#CompilationUnitEditorContext"> 
  	  	<menu
               label="Comment Edit"
               path="additions"
               id="second">
            <groupMarker
            	name="contents">
            </groupMarker>
            <separator
                  name="sampleGroup">
            </separator>
         </menu>
   		<action
         id="org.eclipse.ui.articles.action.contribution.editor.action1"
         label="Delete"
         icon="icons/sample.gif"
     	 menubarPath="second/contents"
         class="OldAction3">		<!-- 指定Action类-->
        </action>   		
	    <action
	     id="org.eclipse.ui.articles.action.contribution.editor.action2"
	     label="Edit"
	     icon="icons/sample.gif"
	 	 menubarPath="second/contents"
	     class="OldAction2">	<!-- 指定Action类-->
	    </action>
	    <action
	     id="org.eclipse.ui.articles.action.contribution.editor.action3"
	     label="Create"
	     icon="icons/sample.gif"
	 	 menubarPath="second/contents"
	     class="OldAction2">	<!-- 指定Action类-->
	    </action>
   </viewerContribution> 
 </extension>
</plugin>

  1. org.eclipse.ui.viewActions 给 Eclipse 的 View 上面的工具栏和菜单栏增加 View 系统菜单和工具栏

  2. org.eclipse.ui.editorActions 针对不同的编辑器环境(比如JavaEditor, JSPEditor),在Eclipse主菜单上面增加不同的菜单和工具栏 也就是说,给 JavaEditor 在 Eclipse 主菜单上面增加的菜单和工具栏,在 JSPEditor 打开的时候就看不到

评论(2)

  1. #2 楼 · feng88724 · 2009-03-23 10:24

    samwalt writes:

    请问通过eclipse的插件向导生成一个上下文菜单的向导,作为eclipse project运行的时候,为什么上下文菜单不显示呢?

    不好意思,没能理解你的意思。你所说的“eclipse的插件向导生成一个上下文菜单的向导”是指在Eclipse中的实例吗?

    另外说明一下,需要以Eclipse Application的方式来运行插件项目

  2. #1 楼 · samwalt · 2009-03-19 22:58

    请问通过eclipse的插件向导生成一个上下文菜单的向导,作为eclipse project运行的时候,为什么上下文菜单不显示呢?

评论功能维护中,当前仅展示历史评论。