我正在开发一个Eclipse Plug-In,它应该自动创建一个工作集,通过使用它们的路径添加项目。我很难理解IAdaptable类是如何工作的。
public static void createWorkingSet(List<Path> projectPaths, String workingSetName){
IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet newSet = manager.createWorkingSet(workingSetName, new IAdaptable[0]);
manager.addWorkingSet(newSet);
}
作为参数给出的列表包含如下路径:
Path p = Paths.get("C:", "Users", "Me", "workspace", "ProjectName");
现在编写代码的方式,它只生成一个空的工作集,因为我用“new IAdaptable [0]”调用createWorkingSet方法。如何填写IAdaptable数组来存储有关我要添加的项目的信息?
答案 0 :(得分:0)
您应该将projectPaths
传递给方法createWorkingSet
,如:
manager.createWorkingSet( workingSetName, projectPaths.toArray( new IAdaptable[projectPaths.size()] );
但是因为你的projectPaths不是IAdaptable
,所以它不起作用。因此,您应该创建自己的实现IAdaptable
的包装器类,并使用它来创建工作集。示例代码如下:
AdaptablePath p1 = new AdaptablePath( Paths.get("C:", "Users", "Me", "workspace", "ProjectName") );
AdaptablePath p2 = new AdaptablePath( Paths.get("C:", "Users", "Me", "workspace", "ProjectName2") );
List<AdaptablePath> paths = new ArrayList<AdaptablePath>();
paths.add(p1);
paths.add(p2);
public static void createWorkingSet(List<AdaptablePath> paths, String workingSetName)
{
IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager();
IWorkingSet newSet = manager.createWorkingSet(workingSetName, paths.toArray( new IAdaptable[paths.size()] ) );
manager.addWorkingSet(newSet);
printElements( newSet );
}
private static void printElements( IWorkingSet set )
{
IAdaptable[] elements = set.getElements();
if ( elements != null && elements.length > 0 )
{
for ( IAdaptable element : elements )
{
Path p = (Path)element.getAdapter(Path.class);
if ( p != null )
{
System.out.println(p.getFileName());
}
}
}
}
static class AdaptablePath implements IAdaptable
{
private Path path;
public AdaptablePath( Path p )
{
this.path = p;
}
@Override
public Object getAdapter(Class adapter)
{
if( adapter == Path.class )
{
return path;
}
return null;
}
}
答案 1 :(得分:0)
createWorkingSet
方法要求您在elements
参数中传递的对象实现IAdaptable
接口。
如果您尝试将Eclipse工作区项目传递给createWorkingSet
,您几乎肯定需要使用实现IProject
的Eclipse IAdaptable
类。