我正在尝试向Package Explorer弹出上下文菜单添加一个条目,该菜单在选择条目时在编辑器中打开文件的修改版本。
我在Fedora 15笔记本电脑上使用Eclipse Indigo。
这是一个非常具体的问题,可能会非常集中,并可能提供解决我遇到的一般问题所需的帮助。
Eclipse提供了一个名为org.eclipse.ui.examples.readmetool的示例插件,可以从Help-> Welcome页面进行设置。
此插件定义了一个名为ReadmeEditor.java的编辑器。它还在plugin.xml中定义了一个扩展点“org.eclipse.ui.popupMenus”。当您在带有后缀“.readme”的文件的Eclipse Eclipse Project视图中使用此操作时,您只需获得一个小弹出窗口。
如何更改readmetool插件,以便在foo.readme文件中选择上面定义的Project Explorer菜单项时,在eclipse中获取一个新的ReadmeEditor,其中包含foo.readme?
我正在努力解决的具体项目如下:
从Package Explorer中导航到文件。
右键单击该文件以获取弹出上下文菜单
查看新条目“版本视图”
选择该条目后,找到该文件,通过外部程序运行该文件以创建临时文件,并在新的TextEditor窗口中显示该临时文件的内容。 (并且,不再需要孤立的临时文件。)
我有一些上面的部分;我扩充了MultiPageEditor示例,以便它执行运行外部程序,创建临时文件以及在文本编辑器中打开临时文件的工作。
但是,我当前可以访问该功能的唯一方法是在Project Explorer的弹出上下文窗口中选择“打开方式”选项,然后转到该窗口中的“其他”并选择我的新的编辑选项。
所以,我现在陷入困境的是如何为plugin.xml添加正确的魔法,以便在Project Manager弹出上下文窗口中获得一个新选项,并将其与新的TextEditor窗口连接。 / p>
这看起来非常像香草“开放”选项会做什么,而且看起来这应该是在eclipse中做的非常基本的事情。任何示例和/或指导将不胜感激。
格雷格
答案 0 :(得分:6)
这是设置和使用项目的简洁但完整的示例。 (在网上搜索,有很多关于日食的建议,但它往往是分散的,而且是零碎的,当我努力解决我的问题时,拥有一个完整的,独立的HOWTO会很有帮助。所以,这是该..)
此配方假设您使用Eclipse Indigo或Juno:
要在eclipse中确认上述内容,请参阅
Help -> About Eclipse -> Installation Details -> Installed Software
启动Ecipse Indigo,带有一个清新的空白工作区:
you, before starting eclipse:
move or delete ~/eclipse.d/popup_workspace.d
mkdir -p ~/eclipse.d/popup_workspace.d
start eclipse: eclipse
eclipse: "Select a workspace"
you: "Browse", navigate to above-created popup_workspace.d; "OK" "OK"
eclipse: "Welcome to Eclipse IDE for Java Developers"
you: select "Workbench"
创建一个新项目,使用Plug-in Project作为一个方便的起点:
you: File -> New -> Project
eclipse: "Select a wizard:
you: Plug-in Development -> Plug-in Project; Next>
eclipse: "Plug-in Project"
you: Project name: org.mypopup.popup_editor Next>
eclipse: "Content"
you: Next>
eclipse: "Templates"
you: Plug-in with a popup menu Finish
eclipse: "This kind of project is associated with the
Plug-in Development perspective..."
you: Yes
在“包资源管理器”中,导航到NewActions.java并双击打开:
org.mypopup.popup_editor
-> src
-> org.mypopup.popup_editor.popup.actions
-> NewAction.java
delete entire contents and replace with:
package org.mypopup.popup_editor.popup.actions;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
public class NewAction implements IObjectActionDelegate {
public NewAction() {
super();
}
public void run(IAction action) {
try {
IWorkbenchWindow window =
PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISelection selection = window.getSelectionService()
.getSelection("org.eclipse.ui.navigator.ProjectExplorer");
TreePath[] paths = ((TreeSelection) selection).getPaths();
TreePath p = paths[0];
Object last = p.getLastSegment();
if (last instanceof IFile) {
IPath ipath = ((IFile) last).getLocation();
IWorkbenchPage page = window.getActivePage();
IDE.openEditorOnFileStore(page,
EFS.getLocalFileSystem().getStore(ipath));
}
} catch (Exception blah) {
}
}
public void setActivePart(IAction action, IWorkbenchPart targetPart) { }
public void selectionChanged(IAction action, ISelection selection) { }
}
为项目添加依赖项以便能够解析导入:
in Package Explorer, navigate to and open META-INF -> MANIFEST.MF
Select "Dependencies" tab along the bottom of the resulting
"org.mypopup.plugin_editor" window
"Add..."; "Select a Plug-in:" enter org.eclipse.core.filesystem; "OK"
"Add..."; "Select a Plug-in:" enter org.eclipse.ui.ide; "OK"
File -> Save my.popup.popup_editor
使用新的空目标目录运行新项目:
outside of eclipse, browse to the directory containing popup_workspace.d,
in this case ~/eclipse.d, and if a folder "runtime-EclipseApplication"
exists, move it or delete it.
right-click META-INF -> MANIFEST.MF, select Run As -> Eclipse Application
in new eclipse window:
eclipse: "Welcome to Eclipse"
you: select "Workbench"
you: File -> New -> Project
eclipse: "Select a wizard"
you: General -> Project Next>
eclipse: "Project"
you: project name popup_test Finish
you: in Project Explorer, right-click popup_test, select New -> File
eclipse: "File"
you: File name: test_file1 Finish
you: enter a little text in test_file1, File -> Save; File -> Close
重要时刻:使用新的弹出菜单条目打开文件:
in Project Explorer, right-click popup_test -> test_file1
select New Submenu -> New Action
答案 1 :(得分:2)
readmetool
示例已经为.readme文件提供了上下文菜单操作:
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.core.resources.IFile"
nameFilter="*.readme"
id="org.eclipse.ui.examples.readmetool">
<action
label="%PopupMenus.action"
icon="$nl$/icons/ctool16/openbrwsr.gif"
helpContextId="org.eclipse.ui.examples.readmetool.open_browser_action_context"
class="org.eclipse.ui.examples.readmetool.PopupMenuActionDelegate"
menubarPath="additions"
enablesFor="1"
id="org.eclipse.ui.examples.readmetool.action1">
</action>
</objectContribution>
...
</extension>
它名为Show Readme Action
,但您可以通过编辑包含相应标签名称的plugin.properties将其重命名为Version view
:
PopupMenus.action = &Version view
然后您只需要编辑PopupMenuActionDelegate
并更改其实现以执行您想要的操作:
public class PopupMenuActionDelegate implements IObjectActionDelegate {
public void run(IAction action) {
// invoke the external tool and open the result in a new editor
}
...
}
答案 2 :(得分:1)
这是一个小型,自包含的示例,用于响应选择弹出窗口选项而打开编辑器窗口。作为测试,它会打开一个带有硬连线名称的文件。
public void run(IAction action) {
try {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
File f = new File("/tmp/testum");
IPath ipath = new Path(f.getAbsolutePath());
IWorkbenchPage page = window.getActivePage();
IDE.openEditorOnFileStore(page, EFS.getLocalFileSystem().getStore(ipath));
} catch (Exception blah) {
System.err.println("Exception!");
}
}