0
我为eclipse中的插件创建了一个NewFile-Wizard。我想将新文件正好放在转发IStructuredSelection
指向的地方。我的问题是,我只知道如何处理的IResource
但不是其它资源实例:Eclipse:在INewWizard中使用IStructuredSelection
public class WizardNewShader extends Wizard implements INewWizard {
... implementations omitted...
IContainer fileLocation;
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
Object s = selection.getFirstElement();
if (s instanceof IResource) {
if (s instanceof IContainer) {
fileLocation = (IContainer) s;
} else {
fileLocation = ((IResource) s).getParent();
}
} else {
System.out.println("what shall we do with a " + s.getClass() + "?");
}
}
}
一些容器类型很好的工作,因为他们的IResource
实例,但其他人不承认:
what shall we do with a class org.eclipse.jdt.internal.core.PackageFragment?
what shall we do with a class org.eclipse.jdt.internal.core.JavaProject?
what shall we do with a class org.eclipse.cdt.internal.core.model.SourceRoot?
是否有任何其他方式来处理这些资源,而不是一个大的instanceof开关,它包含每种资源类型的不同代码?
谢谢,这工作像一个魅力。尽管如此,我不太喜欢这种设计模式,我不明白为什么这比普通的接口实现更受欢迎。 – tly 2014-11-01 18:17:41