摘要
在开发eclipse pluin的时候某些情况下我们需要访问eclipse workspace例如在插件中以编程的方式调用ant命令访问eclipse workspace中的project等一次在网上偶遇到本文的原创者kobye此人正在进行jsports项目的开发对此颇有心地故在此行文与众人共同探讨之
一基础工作在插件中以编程的方式调用ant命令
在开发eclipse pluin的时候某些情况下我们需要访问eclipse workspace例如在插件中以编程的方式调用ant命令等
如何做到这一点?
public void execute(){
IWorkspace ws = ResourcesPlugingetWorkspace();
IProject[] ps = wsgetRoot()getProjects();
Systemoutprintln(wsgetRoot()getFullPath()makeAbsolute()toOSString());
for(int i=;i<pslength;i++){
IProject p = ps[i];
IPath location = pgetLocation();
IFile ifile = pgetFile(buildxml);
Systemoutprintln(ifilegetLocation()toFile()getAbsolutePath());
File f =new File(ifilegetLocation()toFile()getAbsolutePath());
if(!fexists()){
continue;
}
Project pro = new Project();
prosetBasedir(locationtoFile()getAbsolutePath());
proinit();
ProjectHelper helper = ProjectHelpergetProjectHelper();
helperparse(pro f);
Hashtable tars = progetTargets();
Systemoutprintln(name===+name);
Target t = (Target) tarsget(name);
if(t==null){
return;
}
DefaultLogger consoleLogger = new DefaultLogger();
consoleLoggersetErrorPrintStream(Systemerr);
consoleLoggersetOutputPrintStream(Systemout);
consoleLoggersetMessageOutputLevel(ProjectMSG_INFO);
proaddBuildListener(consoleLogger);
proexecuteTarget(thisname);
break;
}
}
以上代码(单独编译不会通过请把 name换位ant 的target)可以放到插件的代码中
以上代码的含义
获得eclipse workspace的引用对workspace下的pronjects进行循环如果该project下有buildxml并且该文件中有name的target那么就以ant的方式调用并把ant运行的输出输出到eclipse的console
二如何访问current project
上一节给出来在eclipse plugin 中访问eclipse workspace 从而访问该workspace下所有project的方案WorkSpace以及相关的类不提供直接访问current project的方法所以只能走其他途径
在我们的plugin中我们要提供界面入口比如 PopMenuActionMenu 等之类的
这些界面入口是要实现一些接口的例如:PopMenu要实现IObjectActionDelegate
这个接口有几个方法其中 public void selectionChanged(IAction action ISelection
selection) ;
这个方法很早重要可以通过ISelection获得当前选择中的Project
ISelection共有三个子接口分别对应三个实现类那么通过判断ISelection的实际类型可以获得其子接口的引用
然后对其遍历通过getAdaptor方法获得所有的选择的IResource的引用
再进一步对IResource进行类型识别得到IResourcePROJECT类型的元素即为IProject的引用
下面是程序:
import javalangreflectArray;import javautilArrayList;
import javautilIterator;import reresourcesIProject;
import reresourcesIResource;
import reruntimeIAdaptable;
import orgeclipsejfaceactionIAction;
import orgeclipsejfacedialogsMessageDialog;
import orgeclipsejfaceviewersISelection;
import orgeclipsejfaceviewersIStructuredSelection;import orgeclipseswtwidgetsShell;import orgeclipseuiIObjectActionDelegate;
import orgeclipseuiIWorkbenchPart;
/** * @author Kobye */public class TestPopMenu implements IObjectActionDelegate {
private IStructuredSelection selection;
/** * Constructor for Action
*/ 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();
MessageDialogopenInformation(shellPop PluginNewAction was executed);
} public static Object getAdapter(Object adaptable Class c) { if (cisInstance(adaptable)) {return adaptable; } if (adaptable instanceof IAdaptable) {IAdaptable a = (IAdaptable) adaptable;
Object adapter = agetAdapter(c);
if (cisInstance(adapter)) { return adapter;
} } return null;
} /*** * 这个方法和下面的方法很重要
* @param selection * @param c * @return */ private Object[] getSelectedResources(IStructuredSelection selectionClass c) { return getSelectedAdaptables(selection c);
} private static Object[] getSelectedAdaptables(ISelection selection Class c) { ArrayList result = null;
if (!selectionisEmpty()) {result = new ArrayList();
Iterator elements = ((IStructuredSelection) erator();
while (elementshasNext()) { Object adapter = getAdapter(elementsnext() c);
if (cisInstance(adapter)) {resultadd(adapter);
}} } if (result != null && !resultisEmpty()) {return resulttoArray((Object[])ArraynewInstance(c resultsize())); } return (Object[])ArraynewInstance(c );
} /** * 这个方法保存了ISelection的引用 * 请注意ISelection的实际类型因不同的应用其实际类型可能不同 * 共有三种可能请查阅eclipse API * * @see IActionDelegate#selectionChanged(IAction ISelection) */ public void selectionChanged(IAction action ISelection selection) {thisselection = (IStructuredSelection) selection;
Systemoutprintln(current project name===+thisgetProject()getName());
} /** * 这个方法可以得到current project * * @return */ private IProject getProject(){IResource[]rs =(IResource[])getSelectedResources((IStructuredSelection)selectionIResourceclass);
IProject project = null;
for(int i =;i<rslength;i++){
IResource r = rs[i];
if(rgetType()==IResourcePROJECT){
project = (IProject) r;
break;
}
}
return project; }}