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