社区
Eclipse
帖子详情
如何在eclipse的package explorer中增加自己的右键菜单?重谢!
lotomer
2008-11-30 09:01:16
想自己开发一个eclipse插件,就是不知道怎么在eclipse的package explorer中增加自己的右键菜单?
像MyEclipse就有,easyexplorer也有,但是我不知道怎么做,哪位能手把手教我下?
...全文
2287
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
自定义
右键
菜单
选项
用
eclipse
开发android时,集成好环境之后,发现
右键
没有新建android项目选项,也没有新建布局文件选项等,反正关于android的选项都没有,如果用到的话还得从"other"里去找,很麻烦, 如果能把这些功能集成到
右键
菜单
就好了.... 也很简单 以目前最新版的
eclipse
为例,这里为4.5.1版本, Windows->Perspective->Customize Pers
Eclipse
中
的
Package
Explorer
不见了
原文链接:https://www.fearlazy.com/index.php/post/301.html 现象: 用
Eclipse
打开了尘封很久的spring项目,想看看
Package
,发现
Package
Explorer
不见了。 解决办法: Window->Show View->Other 接着Java ->
Package
Explorer
,藏的还挺深。 --文章最终版本更新在我的独立博客fearlazy
中
,博客
中
我们尽量只聊编程技术本身。 ...
eclipse
右键
弹不出来、崩溃的问题
eclipse
右键
弹不出来的问题, 在虚拟机
中
搭建的环境,搭建完成后,
eclipse
右键
就有问题,点击
右键
eclipse
右键
菜单
弹不出来,时间一长必崩溃,从网上查了下,去工作空间内删除.metadata文件夹,然后重新配置下工作空间然后就好了! ...
修改
Eclipse
中
package
explore
中
字体
修改
Eclipse
中
package
explore
中
字体 标签:
eclipse
package
explore 字体 分类: 非技术 1. 设置workspace
中
的字体 Preference - Colors and Font - Basic - Text Font , 这里设置的字体只是代码区的字体,不能改变project
explorer
中
的字体 2. 设置
package
Eclipse
添加
右键
菜单
项
主要有下图两步:
Eclipse
58,445
社区成员
49,459
社区内容
发帖
与我相关
我的任务
Eclipse
Java Eclipse
复制链接
扫一扫
分享
社区描述
Java Eclipse
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章