Home » Modeling » GMF (Graphical Modeling Framework) » Programmatically create and open a GMF diagram
Programmatically create and open a GMF diagram [message #484510] |
Mon, 07 September 2009 21:02 |
Baptiste Mesta Messages: 31 Registered: September 2009 |
Member |
|
|
Hi,
I'm trying to programmatically create and open a diagram based on an emf model that I have created.
This EObject is contained in an other EObject, i create the EOBject using the factory:
xyzFactory.eINSTANCE.create....
Then I add it to the parent object.
All these objects are in the same xml file (including the parent's diagram).
My problem is that I can't create the diagram in the same file.
I have tried using the same code as in performFinish() from the generated xyzNewDiagramFileWizard class but the diagram is created in an other file, if i give him the uri of the EObject it erase my file (even if the diagram is correctly opening after that)
here is the code i use from performFinish:
List affectedFiles = new LinkedList();
//using an other uri
String diagramFile = "/My Processes/"+form.getName()+".form_diagram";
URI diagramModelURI = URI.createPlatformResourceURI(diagramFile, true);
ResourceSet resourceSet = getEditingDomain().getResourceSet();
final Resource diagramResource = resourceSet.createResource(diagramModelURI);
AbstractTransactionalCommand command = new AbstractTransactionalCommand(
getEditingDomain(),
"",
affectedFiles) {
protected CommandResult doExecuteWithResult(
IProgressMonitor monitor, IAdaptable info)
throws ExecutionException {
int diagramVID = ProcessVisualIDRegistry
.getDiagramVisualID(form);
if (diagramVID != FormEditPart.VISUAL_ID) {
return CommandResult
.newErrorCommandResult(Messages.ProcessNewDiagramFileWizard_IncorrectRootError);
}
Diagram diagram = ViewService.createDiagram(
form,
FormEditPart.MODEL_ID,
FormDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
diagramResource.getContents().add(diagram);
diagramResource.getContents().add(diagram.getElement());
return CommandResult.newOKCommandResult();
}
};
try {
OperationHistoryFactory.getOperationHistory().execute(command,
new NullProgressMonitor(), null);
diagramResource.save(ProcessDiagramEditorUtil.getSaveOptions());
ProcessDiagramEditorUtil.openDiagram(diagramResource);
} catch (ExecutionException e) {
FormDiagramEditorPlugin.getInstance().logError(
"Unable to create model and diagram", e); //$NON-NLS-1$
} catch (IOException ex) {
FormDiagramEditorPlugin.getInstance().logError(
"Save operation failed for: " + diagramModelURI, ex); //$NON-NLS-1$
} catch (PartInitException ex) {
FormDiagramEditorPlugin.getInstance().logError(
"Unable to open editor", ex); //$NON-NLS-1$
}
Have you an idea?
Thanks you by advance for your help!
[Updated on: Mon, 07 September 2009 21:05] Report message to a moderator
|
|
| | | | | | |
Re: Programmatically create and open a GMF diagram [message #491279 is a reply to message #486547] |
Tue, 13 October 2009 22:41 |
No real name Messages: 62 Registered: July 2009 |
Member |
|
|
Hi Baptiste,
I am currently trying to implement the following behaviour:
Upon the push of a button, a new model and diagram file shall be created
and the selected diagram elements shall be stored in those files.
It looks like that's what you are doing, although I'm not entirely sure.
Can you help me with implementing the things above? I do not know how to
create new files and store things in them from eithin the run method of
my action.
Regards,
Matthias
Baptiste Mesta wrote:
> "task" is one of my EObjects, i juste add an Element to another one.
>
> I've made 2 functions to do the creation and the opening of the diagram
>
>
>
> public Diagram createDiagram(XXX xxx){
> //create the diagram
> int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(xxx);
> if (diagramVID != FormEditPart.VISUAL_ID) {
> // error
> }
> Diagram diagram = ViewService.createDiagram(xxx,
> XXXEditPart.MODEL_ID,
> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
> xxx.eResource().getContents().add(diagram);
>
> // save the ressource
> try {
> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
> } catch (IOException e) {
> XXXDiagramEditorPlugin.getInstance().logError(
> "Save operation failed for: " + xxx.eResource(), e);
> //$NON-NLS-1$
> }
> refresh();
> return diagram;
> }
>
>
> public void openDiagram(XXX xxx){
> //open the diagram
>
> try {
> //find the diagram in the resource
> Diagram diag = null;
> EList<EObject> resources = xxx.eResource().getContents();
> for (EObject eObject : resources) {
> if(eObject instanceof Diagram){
> if(((Diagram)eObject).getElement()!=null &&
> ((Diagram)eObject).getElement().equals(xxx)){
> diag = (Diagram)eObject;
> break;
> }
> }
> }
> //open it
> URI uri = EcoreUtil.getURI(diag);
> IWorkbenchPage page = PlatformUI.getWorkbench()
> .getActiveWorkbenchWindow().getActivePage();
> page.openEditor(new URIEditorInput(uri,xxx.getName()),
> XXXDiagramEditor.ID);
>
>
> } catch (PartInitException e) {
> XXXDiagramEditorPlugin.getInstance().logError(
> "Unable to open editor", e); //$NON-NLS-1$
> }
>
> It's better to execute them in a command.
>
> tell me if you have troubles with it
|
|
|
Re: Programmatically create and open a GMF diagram [message #491666 is a reply to message #491279] |
Thu, 15 October 2009 12:28 |
Romain Bioteau Messages: 65 Registered: August 2009 Location: Grenoble |
Member |
|
|
Hi Matthias,
Here is a snippet of code doing the thing in the execute method of an
AbstractHandler:
public Object execute(ExecutionEvent event) throws ExecutionException {
try {
IProject project = //retreive your IProject
URI newProcessURI =
URI.createPlatformResourceURI(project.getFile("your file
name").getFullPath().toString(), false);
Resource diagram = XXXDiagramEditorUtil.createDiagram(newProcessURI,
new NullProgressMonitor());
if (diagram != null) {
try {
XXXDiagramEditorUtil.openDiagram(diagram);
} catch (PartInitException e) {
ErrorDialog.openError(Display.getDefault().getActiveShell(),
"Open New Diagram",
null, e.getStatus());
}
}
return null;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
As you can see here you should reuse generated Util functions.
Good Luck
Romain
Matthias Schmeling a écrit :
> Hi Baptiste,
>
> I am currently trying to implement the following behaviour:
>
> Upon the push of a button, a new model and diagram file shall be created
> and the selected diagram elements shall be stored in those files.
>
> It looks like that's what you are doing, although I'm not entirely sure.
> Can you help me with implementing the things above? I do not know how to
> create new files and store things in them from eithin the run method of
> my action.
>
> Regards,
> Matthias
>
> Baptiste Mesta wrote:
>> "task" is one of my EObjects, i juste add an Element to another one.
>>
>> I've made 2 functions to do the creation and the opening of the diagram
>>
>>
>>
>> public Diagram createDiagram(XXX xxx){
>> //create the diagram
>> int diagramVID = ProcessVisualIDRegistry.getDiagramVisualID(xxx);
>> if (diagramVID != FormEditPart.VISUAL_ID) {
>> // error
>> }
>> Diagram diagram = ViewService.createDiagram(xxx,
>> XXXEditPart.MODEL_ID,
>> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
>> xxx.eResource().getContents().add(diagram);
>>
>> // save the ressource
>> try {
>> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
>> } catch (IOException e) {
>> XXXDiagramEditorPlugin.getInstance().logError(
>> "Save operation failed for: " + xxx.eResource(),
>> e); //$NON-NLS-1$
>> }
>> refresh();
>> return diagram;
>> }
>>
>> public void openDiagram(XXX xxx){
>> //open the diagram
>>
>> try {
>> //find the diagram in the resource
>> Diagram diag = null;
>> EList<EObject> resources = xxx.eResource().getContents();
>> for (EObject eObject : resources) {
>> if(eObject instanceof Diagram){
>> if(((Diagram)eObject).getElement()!=null &&
>> ((Diagram)eObject).getElement().equals(xxx)){
>> diag = (Diagram)eObject;
>> break;
>> }
>> }
>> }
>> //open it
>> URI uri = EcoreUtil.getURI(diag);
>> IWorkbenchPage page = PlatformUI.getWorkbench()
>> .getActiveWorkbenchWindow().getActivePage();
>> page.openEditor(new URIEditorInput(uri,xxx.getName()),
>> XXXDiagramEditor.ID);
>> } catch (PartInitException e) {
>> XXXDiagramEditorPlugin.getInstance().logError(
>> "Unable to open editor", e); //$NON-NLS-1$
>> }
>>
>> It's better to execute them in a command.
>>
>> tell me if you have troubles with it
R&D Engineer at BonitaSoft
|
|
|
Re: Programmatically create and open a GMF diagram [message #491792 is a reply to message #491666] |
Thu, 15 October 2009 21:11 |
No real name Messages: 62 Registered: July 2009 |
Member |
|
|
Thanks a lot Romain. I'll try that.
Romain Bioteau wrote:
> Hi Matthias,
>
> Here is a snippet of code doing the thing in the execute method of an
> AbstractHandler:
>
> public Object execute(ExecutionEvent event) throws ExecutionException {
> try {
>
>
> IProject project = //retreive your IProject
>
> URI newProcessURI =
> URI.createPlatformResourceURI(project.getFile("your file
> name").getFullPath().toString(), false);
> Resource diagram =
> XXXDiagramEditorUtil.createDiagram(newProcessURI, new
> NullProgressMonitor());
> if (diagram != null) {
> try {
> XXXDiagramEditorUtil.openDiagram(diagram);
> } catch (PartInitException e) {
>
> ErrorDialog.openError(Display.getDefault().getActiveShell(),
> "Open New Diagram",
> null, e.getStatus());
> }
> }
>
>
>
>
> return null;
> } catch (Exception ex) {
> ex.printStackTrace();
> return null;
> }
> }
>
> As you can see here you should reuse generated Util functions.
> Good Luck
>
> Romain
>
>
> Matthias Schmeling a écrit :
>> Hi Baptiste,
>>
>> I am currently trying to implement the following behaviour:
>>
>> Upon the push of a button, a new model and diagram file shall be
>> created and the selected diagram elements shall be stored in those files.
>>
>> It looks like that's what you are doing, although I'm not entirely
>> sure. Can you help me with implementing the things above? I do not
>> know how to create new files and store things in them from eithin the
>> run method of my action.
>>
>> Regards,
>> Matthias
>>
>> Baptiste Mesta wrote:
>>> "task" is one of my EObjects, i juste add an Element to another one.
>>>
>>> I've made 2 functions to do the creation and the opening of the diagram
>>>
>>>
>>>
>>> public Diagram createDiagram(XXX xxx){
>>> //create the diagram
>>> int diagramVID =
>>> ProcessVisualIDRegistry.getDiagramVisualID(xxx);
>>> if (diagramVID != FormEditPart.VISUAL_ID) {
>>> // error
>>> }
>>> Diagram diagram = ViewService.createDiagram(xxx,
>>> XXXEditPart.MODEL_ID,
>>> XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
>>> xxx.eResource().getContents().add(diagram);
>>>
>>> // save the ressource
>>> try {
>>> xxx.eResource().save(XXXDiagramEditorUtil.getSaveOptions());
>>> } catch (IOException e) {
>>> XXXDiagramEditorPlugin.getInstance().logError(
>>> "Save operation failed for: " + xxx.eResource(),
>>> e); //$NON-NLS-1$
>>> }
>>> refresh();
>>> return diagram;
>>> }
>>> public void openDiagram(XXX xxx){
>>> //open the diagram
>>>
>>> try {
>>> //find the diagram in the resource
>>> Diagram diag = null;
>>> EList<EObject> resources = xxx.eResource().getContents();
>>> for (EObject eObject : resources) {
>>> if(eObject instanceof Diagram){
>>> if(((Diagram)eObject).getElement()!=null &&
>>> ((Diagram)eObject).getElement().equals(xxx)){
>>> diag = (Diagram)eObject;
>>> break;
>>> }
>>> }
>>> }
>>> //open it
>>> URI uri = EcoreUtil.getURI(diag);
>>> IWorkbenchPage page = PlatformUI.getWorkbench()
>>> .getActiveWorkbenchWindow().getActivePage();
>>> page.openEditor(new URIEditorInput(uri,xxx.getName()),
>>> XXXDiagramEditor.ID);
>>> } catch (PartInitException e) {
>>> XXXDiagramEditorPlugin.getInstance().logError(
>>> "Unable to open editor", e); //$NON-NLS-1$
>>> }
>>>
>>> It's better to execute them in a command.
>>>
>>> tell me if you have troubles with it
|
|
| |
Goto Forum:
Current Time: Fri Nov 08 23:02:49 GMT 2024
Powered by FUDForum. Page generated in 0.05057 seconds
|