how to access current Project in eclipse?
2005-11-15 14:05
495 查看
在上篇文章,给出来在eclipse plugin 中访问eclipse workspace 从而访问该workspace下所有project的方案,这篇文章提供了如何访问current project的方案。
I 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",
"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;i<rs.length;i++){
IResource r = rs[i];
if(r.getType()==IResource.PROJECT){
project = (IProject) r;
break;
}
}
return project;
}
}
I 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",
"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;i<rs.length;i++){
IResource r = rs[i];
if(r.getType()==IResource.PROJECT){
project = (IProject) r;
break;
}
}
return project;
}
}
相关文章推荐
- How to Create J2ee Project With Maven in Eclipse
- How to install MySQL JDBC driver in Eclipse web project without java.lang.ClassNotFoundexception com
- How to fix Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse---转载
- How to remove native support from an Android Project in eclipse because eclipse is showing errors in
- Java - How to change context root of a dynamic web project in eclipse
- How to create unit test folder separated from src folder in eclipse project?
- How to create a project from existing source in Eclipse and then find it?
- How to change context root of a dynamic web project in Eclipse
- How to get projects list in eclipse
- How to access data/data folder in Android device?
- How to decompile class file in Java and Eclipse - Javap command example(转)
- How do I fix the linker error in project upgraded from VS2010 to VS2013 where the linker is looking
- maven Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- How to get the current time in milliseconds in C Programming?
- Tools.Eclipse.HowToImportAnAndroidLibraryProjectIntoWorkspace
- How to leave the open file in eclipse tab after search?
- CodeProject - How to develop a screen saver in C#(翻译)
- 核心驱动文件操作 How to access file in Kernel Driver
- 1. Introduce how to import the Spring Framework sourcecode into an eclipse project 【analyze spring framework source 】