分享
 
 
 

如何加入到eclipseworkspace方式

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

摘要:

在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令、访问eclipse workspace中的project等。一次在网上偶遇到本文的原创者kobye,此人正在进行jsports项目的开发,对此颇有心地,故在此行文与众人共同探讨之。

一、基础工作-在插件中以编程的方式调用ant命令:

在开发eclipse pluin的时候,某些情况下我们需要访问eclipse workspace,例如:在插件中以编程的方式调用ant命令等。

如何做到这一点?

public void execute(){

IWorkspace ws = ResourcesPlugin.getWorkspace();

IProject[] ps = ws.getRoot().getProjects();

System.out.println(ws.getRoot().getFullPath().makeAbsolute().toOSString());

for(int i=0;i<ps.length;i++){

IProject p = ps[i];

IPath location = p.getLocation();

IFile ifile = p.getFile("build.xml");

System.out.println(ifile.getLocation().toFile().getAbsolutePath());

File f =

new File(ifile.getLocation().toFile().getAbsolutePath());

if(!f.exists()){

continue;

}

Project pro = new Project();

pro.setBasedir(location.toFile().getAbsolutePath());

pro.init();

ProjectHelper helper = ProjectHelper.getProjectHelper();

helper.parse(pro, f);

Hashtable tars = pro.getTargets();

System.out.println("name==="+name);

Target t = (Target) tars.get(name);

if(t==null){

return;

}

DefaultLogger consoleLogger = new DefaultLogger();

consoleLogger.setErrorPrintStream(System.err);

consoleLogger.setOutputPrintStream(System.out);

consoleLogger.setMessageOutputLevel(Project.MSG_INFO);

pro.addBuildListener(consoleLogger);

pro.executeTarget(this.name);

break;

}

}

以上代码(单独编译不会通过,请把 name换位ant 的target)可以放到插件的代码中。

以上代码的含义:

获得eclipse workspace的引用,对workspace下的pronjects进行循环,如果该project下有build.xml并且该文件中有name的target那么就以ant的方式调用,并把ant运行的输出输出到eclipse的console。

二、如何访问current project:

上一节给出来在eclipse plugin 中访问eclipse workspace, 从而访问该workspace下所有project的方案,WorkSpace以及相关的类不提供直接访问current project的方法,所以只能走其他途径.

在我们的plugin中,我们要提供界面入口,比如 PopMenu

ActionMenu 等之类的,

这些界面入口是要实现一些接口的,例如:PopMenu要实现IObjectActionDelegate,

这个接口有几个方法,其中 public void selectionChanged(IAction action, ISelection

selection) ;

这个方法很早重要,可以通过ISelection获得当前选择中的Project.

ISelection共有三个子接口,分别对应三个实现类,那么通过判断ISelection的实际类型可以获得其子接口的引用,

然后对其遍历,通过getAdaptor方法获得所有的选择的IResource的引用,

再进一步对IResource进行类型识别,得到IResource.PROJECT类型的元素即为IProject的引用.

下面是程序:

import java.lang.reflect.Array;import java.util.ArrayList;

import java.util.Iterator;import org.eclipse.core.resources.IProject;

import org.eclipse.core.resources.IResource;

import org.eclipse.core.runtime.IAdaptable;

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.IObjectActionDelegate;

import org.eclipse.ui.IWorkbenchPart;

/** * @author Kobye */public class TestPopMenu implements IObjectActionDelegate {

private IStructuredSelection selection;

/** * Constructor for Action1.

*/ public TestPopMenu () { super();

} /** * @see IObjectActionDelegate#setActivePart(IAction IWorkbenchPart)

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

/**

* @see IActionDelegate#run(IAction) */ public void run(IAction action) { Shell shell = new Shell();

MessageDialog.openInformation(

shell,

"Pop Plug-in",

"NewAction was executed.");

} public static Object getAdapter(Object adaptable, Class c) { if (c.isInstance(adaptable)) {

return adaptable; } if (adaptable instanceof IAdaptable) {

IAdaptable a = (IAdaptable) adaptable;

Object adapter = a.getAdapter(c);

if (c.isInstance(adapter)) {

return adapter;

} } return null;

} /*** * 这个方法和下面的方法很重要。

* @param selection * @param c * @return */ private Object[] getSelectedResources(IStructuredSelection selection,Class c) { return getSelectedAdaptables(selection, c);

} private static Object[] getSelectedAdaptables(ISelection selection, Class c) { ArrayList result = null;

if (!selection.isEmpty()) {

result = new ArrayList();

Iterator elements = ((IStructuredSelection) selection).iterator();

while (elements.hasNext()) {

Object adapter = getAdapter(elements.next(), c);

if (c.isInstance(adapter)) {

result.add(adapter);

}

} } if (result != null && !result.isEmpty()) {

return result.toArray((Object[])Array.newInstance(c, result.size())); } return (Object[])Array.newInstance(c, 0);

} /** * 这个方法保存了ISelection的引用, * 请注意:ISelection的实际类型因不同的应用,其实际类型可能不同, * 共有三种可能,请查阅eclipse API。 * * @see IActionDelegate#selectionChanged(IAction, ISelection) */ public void selectionChanged(IAction action, ISelection selection) {

this.selection = (IStructuredSelection) selection;

System.out.println("current project name==="+this.getProject().getName());

} /** * 这个方法可以得到current project。 * * @return */ private IProject getProject(){

IResource[]rs =(IResource[])getSelectedResources((IStructuredSelection)selection,IResource.class);

IProject project = null;

for(int i =0;i<rs.length;i++){

IResource r = rs[i];

if(r.getType()==IResource.PROJECT){

project = (IProject) r;

break;

}

}

return project; }}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有