当前页面: 开发资料首页 → Java 专题 → How to access eclipse workspace?
摘要: How to access eclipse workspace?
public void execute(){
IWorkspace ws = ResourcesPlugin.getWorkspace();
IProject[] ps = ws.getRoot().getProjects();
System.out.println(ws.getRoot().getFullPath().makeAbsolute().toOSString());
for(int i=0;iIProject 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;
}
}
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",
"New Action 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;iIResource r = rs[i];
if(r.getType()==IResource.PROJECT){
project = (IProject) r;
break;
}
}
return project;
}
}