我必须编写一个 Eclipse-Plugin 程序,但我以前从未这样做过,所以我有一些问题。
当您在 Eclipse 项目资源管理器中右键单击 Java 项目时,该插件应该出现在上下文菜单中。它应该打开一个对话框,用户可以在其中输入他在所选项目中查找的文件名,然后该文件会突出显示(如果存在具有该名称的文件)。
到目前为止我所做的是设置插件开发项目、插件的扩展点和对话框。
但现在我不知道如何访问所选项目。您能告诉我这是如何完成的或者相应 API 的链接吗?
预先感谢:)
最佳答案
我假设您的插件中有一个用于右键单击操作的 Handler 类。 Handler 扩展了 AbstractHandler 并重写了方法execute(..)。
然后你可以这样做:
public class YourHandler extends AbstractHandler {
private ExecutionEvent event;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
// First get the tree of the right-clicked project.
ISelection sel = HandlerUtil.getActiveMenuSelection(event);
IResource resource = null;
IProject project = null;
try {
IStructuredSelection selection = (IStructuredSelection) sel;
// Get the first element of the tree (return type Object).
Object firstElement = selection.getFirstElement();
// Get the IResource and from this the IProject of the selection.
if (firstElement instanceof IAdaptable) {
IResource resource = (IResource) (((IAdaptable) firstElement)
.getAdapter(IResource.class));
project = res.getProject();
}
} catch (ClassCastException e) {
// Do nothing.
}
// Then you can do something with the project.
return project;
}
另请参阅 IProject 的 Eclipse API,了解您可以执行的操作: http://help.eclipse.org/kepler/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/resources/IProject.html
例如从名称获取文件:
IFile getFile(String name)
Returns a handle to the file with the given name in this project.
希望这有帮助。
顺便说一句:如果您需要一些有关开发 Eclipse 插件的精彩教程,我可以推荐这个网站 http://www.vogella.com/eclipse.html
干杯。
关于java - 编程 Eclipse 插件用于在项目中搜索文件(需要基本帮助),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20466388/