如何在eclipse的package explorer中增加自己的右键菜单?重谢!

lotomer 2008-11-30 09:01:16
想自己开发一个eclipse插件,就是不知道怎么在eclipse的package explorer中增加自己的右键菜单?
像MyEclipse就有,easyexplorer也有,但是我不知道怎么做,哪位能手把手教我下?
...全文
2233 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
Five 2010-11-27
  • 打赏
  • 举报
回复
感谢楼主和前面的高手们,通过学习这段代码,获益匪浅。
xiejian880523 2010-03-18
  • 打赏
  • 举报
回复

[Quote=引用 7 楼 danielzhan 的回复:]
1) 添加 Extension: org.eclipse.ui.popupMenu
2) 再添加 ViewerContribution 并给"targetID*"赋值为org.eclipse.jdt.ui.PackageExplorer.
3) 再在此ViewerContribution下添加 Action 或 Menu

具体参考
http://www.eclipse.org/arti……
[/Quote]


这个是好东西,强人啊.
xiejian880523 2010-03-18
  • 打赏
  • 举报
回复
牛人啊,学习了,我一小菜,刚开始学插件开发呢..
lotomer 2008-12-08
  • 打赏
  • 举报
回复
//plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>

<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.core.runtime.IAdaptable"
id="myPop.contribution1">
<action
label="getPath"
class="mypop.popup.actions.getPath"
menubarPath="additions"
enablesFor="1"
id="myPop.newAction">
</action>
</objectContribution>
</extension>
</plugin>
lotomer 2008-12-08
  • 打赏
  • 举报
回复
问题已基本解决,感谢各位的帮助!主要代码如下:
//.java

package mypop.popup.actions;

import java.io.IOException;

import mypop.Activator;

import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;

import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.FileEditorInput;

public class getPath implements IObjectActionDelegate
{
private IStructuredSelection select;

public getPath()
{
super();
}

public void setActivePart(IAction action, IWorkbenchPart targetPart)
{
}

public void run(IAction action)
{
Object obj = select.getFirstElement();
try
{
IResource file = (IResource) obj; //文件和目录
path = file.getLocation().toOSString();
}
catch (ClassCastException e)
{
try
{
IJavaElement element = (IJavaElement) obj; //包及类文件
path = element.getResource().getLocation().toOSString();
}
catch (ClassCastException e1)
{
try
{
FileEditorInput edit = (FileEditorInput)obj; //编辑器
path = edit.getPath().toOSString();
}
catch (ClassCastException e3)
{
//输出对象名(我就是利用他把上面的一个一个试出来的)
path = obj.getClass().toString();
}
}
}
try
{
Runtime runtime = Runtime.getRuntime();
//调用外部命令定位文件或目录
runtime.exec("Explorer.exe /select, " + path);
}
catch (IOException e) {
e.printStackTrace();
}
}


public void selectionChanged(IAction action, ISelection selection)
{
if (selection instanceof IStructuredSelection)
{
select = (IStructuredSelection) selection;
}
}
}

lotomer 2008-12-08
  • 打赏
  • 举报
回复
已基本解决。代码如下:
Object obj = select.getFirstElement();
try
{
IResource file = (IResource) obj; //文件和目录
path = file.getLocation().toOSString();
}
catch (ClassCastException e)
{
try
{
IJavaElement element = (IJavaElement) obj; //包及类文件
path = element.getResource().getLocation().toOSString();
}
catch (ClassCastException e1)
{
try
{
FileEditorInput edit = (FileEditorInput)obj; //编辑器
path = edit.getPath().toOSString();
}
catch (ClassCastException e3)
{
//输出对象名(我就是利用他把上面的一个一个试出来的)
path = obj.getClass().toString();
}
}
}

try
{
Runtime runtime = Runtime.getRuntime();
//调用外部命令打开文件或目录
runtime.exec("Explorer.exe /select, " + path);
}
catch (IOException e) {
e.printStackTrace();
}
danielzhan 2008-12-08
  • 打赏
  • 举报
回复
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

public class ViewAction1 implements IViewActionDelegate {

String filePath = "";

public void init(IViewPart view) {

}

public void run(IAction action) {
MessageDialog.openInformation(new Shell(), "View Action", filePath);
}

public void selectionChanged(IAction action, ISelection selection) {
if(selection!=null && selection instanceof IStructuredSelection){
IStructuredSelection se = (IStructuredSelection)selection;
if(se.getFirstElement()!=null && se.getFirstElement() instanceof CompilationUnit){

filePath = ((CompilationUnit)se.getFirstElement()).getResource().getLocation().toOSString();
return;
}
}
filePath = "";
}

}
saighost 2008-12-07
  • 打赏
  • 举报
回复
如果你想在任意选择文件的时候都能弹出来你的plugin.xml应该是这样
<extension
point="org.eclipse.ui.popupMenus">
<objectContribution
objectClass="org.eclipse.core.resources.IFile"
id="test.contribution1">
<action
label="MyAction"
class="test.popup.actions.NewAction"
menubarPath="additions"
enablesFor="1"
id="test.newAction">
</action>
</objectContribution>
</extension>
如果你只想的在package explorer中显示plugin.xml应该是
<extension
point="org.eclipse.ui.popupMenus">
<viewerContribution
id="test.viewerContribution1"
targetID="org.eclipse.jdt.ui.PackageExplorer">
<action
class="test.popup.actions.NewAction1"
enablesFor="1"
id="test.action1"
label="MyAction"
menubarPath="additions">
<enablement>
<objectClass
name="org.eclipse.core.resources.IFile">
</objectClass>
</enablement>
</action>
</viewerContribution>
</extension>

eclipse常用的扩展点都有示例,做前最好看看。像这里的Action就不用你去全部实现或从Action去继承,只要实现IObjectActionDelegate接口就可以了。也就是只需要关心处理过程就可以了。
saighost 2008-12-07
  • 打赏
  • 举报
回复
Extension: org.eclipse.ui.popupMenu 这个是有自带例子的,推荐在其基础上改一下就方便很多。

package test.popup.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class NewAction implements IObjectActionDelegate {

private IStructuredSelection select;
/**
* Constructor for Action1.
*/
public NewAction() {
super();
}

/**
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}

/**
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
IFile file = (IFile)select.getFirstElement(); //假设你只需要选择一个对象
IPath path = file.getFullPath(); //文件路径
Shell shell = new Shell();
MessageDialog.openInformation(
shell,
"文件路径",
path.toOSString());

}

/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
if(selection instanceof IStructuredSelection){
select = (IStructuredSelection)selection;
}
}

}
lihan6415151528 2008-12-07
  • 打赏
  • 举报
回复
学习了
lotomer 2008-12-07
  • 打赏
  • 举报
回复
我已经试出来了,可以在右键看到我的选项了,不过怎么获取被选文件的本地路径?
Gordon.Shao 2008-12-07
  • 打赏
  • 举报
回复
要自己生成插件
lotomer 2008-12-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 danielzhan 的回复:]
1) 添加 Extension: org.eclipse.ui.popupMenu
2) 再添加 ViewerContribution 并给"targetID*"赋值为org.eclipse.jdt.ui.PackageExplorer.
3) 再在此ViewerContribution下添加 Action 或 Menu

具体参考
http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html
[/Quote]

这里只有配置文件的写法,没有说action要从哪继承,运行需要哪些库。我试了没成功
muyu114 2008-12-07
  • 打赏
  • 举报
回复
lotomer 2008-12-07
  • 打赏
  • 举报
回复
非常感谢,有点眉目了
不过还有个问题:
1、为什么不能得到目录的路径?
2、为什么在package explorer中的java文件的路径也得不到?
lotomer 2008-12-06
  • 打赏
  • 举报
回复
写插件是为了辅助自己开发,提交开发效率
呵呵
我用批处理把功能实现了,但是想把该功能直接做成eclipse的插件,更方便,所以想学
谢谢各位的指点
我去试验一下
顺便说明一下,我是做C++开发的,现在在兼做java,所以比较菜,见笑啦
lotomer 2008-12-06
  • 打赏
  • 举报
回复
哪位能做个简单的demo给我就最好了,能直接看源码的
lotomer@163.com
JJZHK 2008-12-03
  • 打赏
  • 举报
回复
自己写插件是肯定的了,写插件挺有意思的。
danielzhan 2008-12-02
  • 打赏
  • 举报
回复
1) 添加 Extension: org.eclipse.ui.popupMenu
2) 再添加 ViewerContribution 并给"targetID*"赋值为org.eclipse.jdt.ui.PackageExplorer.
3) 再在此ViewerContribution下添加 Action 或 Menu

具体参考
http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html
JJZHK 2008-12-02
  • 打赏
  • 举报
回复
工作空间中的右键菜单的扩展点,好像是org.eclipse.ui.popupMenus,然后你可以判断是否点在project explore上,然后再进行你的操作。以前做过,有点记不清了。你可以去找一本<Contributing to Eclipse>看看,里面讲的很清楚。
加载更多回复(7)

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧