社区
Eclipse
帖子详情
如何在eclipse的package explorer中增加自己的右键菜单?重谢!
lotomer
2008-11-30 09:01:16
想自己开发一个eclipse插件,就是不知道怎么在eclipse的package explorer中增加自己的右键菜单?
像MyEclipse就有,easyexplorer也有,但是我不知道怎么做,哪位能手把手教我下?
...全文
2279
27
打赏
收藏
如何在eclipse的package explorer中增加自己的右键菜单?重谢!
想自己开发一个eclipse插件,就是不知道怎么在eclipse的package explorer中增加自己的右键菜单? 像MyEclipse就有,easyexplorer也有,但是我不知道怎么做,哪位能手把手教我下?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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)
eclipse
更改
右键
菜单
设置、设置登陆时可以切换workspace
更改原因:在使用
eclipse
的时候,进行
右键
new
菜单
选择某些功能时,
菜单
中
很多功能时用不到的,想用的却没有 操作:Window——Perspective——Customize Perspective 进入: 点击Menu Visibility, 再点击...
Eclipse
中
定制自己的
右键
new
菜单
使用
Eclipse
进行Java开发,经常
右键
新建但是初始的
右键
菜单
有的选项很少用到,需要的选项又必须点击other重新选择。其实我们可以定制自己的
右键
new
菜单
,使开发更加得心应手。 1.
菜单
栏Window-->Perspective-->...
eclipse
自定义
右键
菜单
选项
用
eclipse
开发android时,集成好环境之后,发现
右键
没有新建android项目选项,也没有新建布局文件选项等,反正关于android的选项都没有,如果用到的话还得从"other"里去找,很麻烦, 如果能把这些功能集成到
右键
菜单
就好了...
eclipse
右键
菜单
无show in
explorer
若需在
右键
菜单
中
添加open in
explorer
,需先下载该插件,下载地址为https://github.com/samsonw/Open
Explorer
/downloads之后将下载好的jar包拷贝到
eclipse
安装目录\plugins下,重启
eclipse
即可。...
Eclipse
Package
Explorer
相关问题及解决方法
用例描述:
右键
package
explorer
里一个项目,选“open in new window”,然后关掉之前那个
eclipse
窗口,再关掉新打开的窗口 产生问题:这时打开
eclipse
之后
package
explorer
始终会在open in new window的那个...
Eclipse
58,446
社区成员
49,461
社区内容
发帖
与我相关
我的任务
Eclipse
Java Eclipse
复制链接
扫一扫
分享
社区描述
Java Eclipse
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章