|
|
Re: How to change the diagram's model at runtime? [message #662182 is a reply to message #662176] |
Tue, 29 March 2011 15:50 |
Missing name Missing name Messages: 7 Registered: March 2011 |
Junior Member |
|
|
The relative path is no problem for GMF.
I solved my problem by myself The following code works for me.
package testecorediagrameditor;
import java.io.*;
import java.util.*;
import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.*;
import org.eclipse.emf.ecore.resource.*;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.ecore.edit.parts.EPackageEditPart;
import org.eclipse.gmf.ecore.part.*;
import org.eclipse.gmf.runtime.diagram.core.services.ViewService;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueCommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.ui.*;
import org.eclipse.ui.ide.IDE;
public class StartUp implements IStartup {
@Override
public void earlyStartup() {
final IWorkbench workbench = PlatformUI.getWorkbench();
workbench.getDisplay().asyncExec(new TestECoreDiagramEditor());
}
private class TestECoreDiagramEditor extends TimerTask implements Runnable {
private IEditorPart editorPart = null;
private Timer timer = new Timer();
@Override
public void run() {
try {
URI ePackageURI = URI.createFileURI("temp_model.ecore");
URI ePackageDiagramURI = URI.createFileURI("temp_model.ecore_diagram");
if (this.editorPart == null) {
createTempECoreModelWithDiagram(ePackageURI, ePackageDiagramURI);
// start the editor
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.closeAllEditors(false);
String editorId = EcoreDiagramEditor.ID;
this.editorPart = IDE.openEditor(page, new URIEditorInput(ePackageDiagramURI), editorId);
this.timer.scheduleAtFixedRate(this, 1000, 1000);
}
// try to add an element to the diagram
DiagramDocumentEditor editor = (DiagramDocumentEditor) this.editorPart;
TransactionalEditingDomain domain = editor.getEditingDomain();
EPackage ePackage = (EPackage) editor.getDiagram().getElement();
SetRequest setReq = new SetRequest(domain, ePackage, EcorePackage.eINSTANCE.getEPackage_EClassifiers(),
EcoreFactory.eINSTANCE.createEClass());
new SetValueCommand(setReq).execute(null, editor);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createTempECoreModelWithDiagram(URI ePackageURI, URI ePackageDiagramURI) throws IOException {
ResourceSet resourceSet = new ResourceSetImpl();
// create an EPackage
EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
ePackage.setName("main package");
Resource ePackageResource = resourceSet.createResource(ePackageURI);
ePackageResource.getContents().add(ePackage);
ePackageResource.save(Collections.EMPTY_MAP);
new File(ePackageURI.toFileString()).deleteOnExit();
// create an EPackage diagram
Diagram diagram = ViewService.createDiagram(ePackage, EPackageEditPart.MODEL_ID,
EcoreDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
Resource ePackageDiagramResource = resourceSet.createResource(ePackageDiagramURI);
ePackageDiagramResource.getContents().add(diagram);
ePackageDiagramResource.save(Collections.EMPTY_MAP);
new File(ePackageDiagramURI.toFileString()).deleteOnExit();
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03370 seconds