我想知道如何使用现有的 emf 资源创建一个新文件。目前,我的 org.eclipse.ui.menus DefaultHandler 中有以下代码,它可以获取现有的 emf 资源并创建一个新的(空)文件:
if (element instanceof IResource) {
IResource pldFile = (IResource) element;
String path = pldFile.getLocation().toString();
URI uri = URI.createFileURI(path);
// Obtain a new resource set
ResourceSet resSet = new ResourceSetImpl();
// Get the existing resource
Resource emfResource = resSet.getResource(uri, true);
IProject project = pldFile.getProject();
String fileName = pldFile.getName().replace(pldFile.getFileExtension(), "plc");
IFile plcFile = project.getFile(new Path(fileName));
byte[] bytes = "".getBytes();
try {
InputStream source = new ByteArrayInputStream(bytes);
if (plcFile.exists()) {
int i = 1;
String tmp = "";
do {
tmp = fileName;
int index = tmp.indexOf(".plc");
tmp = tmp.substring(0, index) + i + tmp.substring(index, tmp.length());
plcFile = project.getFile(new Path(tmp));
i++;
} while (plcFile.exists());
plcFile.create(source, IResource.NONE, null);
} else {
plcFile.create(source, IResource.NONE, null);
}
PlcEditorInput input = new PlcEditorInput(emfResource);
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IWorkbenchPage page = window.getActivePage();
IDE.openEditor(page, plcFile);
} catch (CoreException e) {
e.printStackTrace();
}
我现在如何将现有的 emf 资源分配给我新创建的文件?
干杯,菲尔