Jump to: navigation, search

Difference between revisions of "Graphical Modeling Framework/Tips"

(Image:New-small.gif Sharing single EditingDomain instance across several diagrams)
(Image:New-small.gif Sharing single EditingDomain instance across several diagrams)
Line 83: Line 83:
 
= [[Image:New-small.gif]] Sharing single EditingDomain instance across several diagrams =
 
= [[Image:New-small.gif]] Sharing single EditingDomain instance across several diagrams =
  
Generated code always creates new (own) instance of EditingDomain for each opened diagram editor. Since EditingDomain owns ResourceSet, different instances of domain model elements will be loaded for each opened diagram editor. Sometimes it is important to share the same domain model instances across several diagram editors (for example to get rid of synchronization problems). This leads us to the question of sharing same instance of EditingDomain across several generated diagram editors. Below you can find step by step description of necessary modifications you have to apply to the generated code.
+
Generated code always creates new (own) instance of EditingDomain for each opened diagram editor. Since EditingDomain owns ResourceSet, different instances of domain model elements will be loaded for each opened diagram editor. Sometimes it is important to share the same domain model instances across several diagram editors (for example to get rid of synchronization problems). This leads us to the question of sharing same instance of EditingDomain across several generated diagram editors. Below you can find step by step description of necessary modifications you have to apply to the generated code to share the same EditingDomain instance between two diagrams of different types.
 +
 
 +
1. Make sure you have org.eclipse.gmf.bridge.trace plugin installed as a part of GMF SDK
 +
 
 +
2. Create diagram1.gmfmap, diagram2.gmfmap files and generate diagram1.gmfgen, diagram2.gmfgen. Ensure diagram1.trace and diagram2.trace files created.
 +
 
 +
3. Modify generated diagram1.gmfgen and diagram2.gmfgen files to use different Model IDs, Plugin IDs, Editor IDs. Optionally two different diagrams could has different diagram file extensions as well:
 +
[[Image:Model_ID.PNG]] [[Image:Plugin_ID.PNG]]
 +
 
 +
[[Image:Editor_ID.PNG]]
 +
 
 +
4. Modify diagram2.trace file to use different visualID values then specified in diagram1.trace file - the easiest way is to replace “200”-> “210”, .., “800” -> “810” – and regenerate diagram2.gmfgen from diagram2.gmfmap
 +
 
 +
5. Open diagram2.gmfgen and modify visualID property for GenDiagram to make it different then visualID of GenDiagram in diagram1.gmfgen:
 +
[[Image:Diagram2Visual_ID.PNG]]
 +
 
 +
6. Ensure both diagram1.gmfgen and diagram2.gmfgen are using the same EditingDomain ID:
 +
[[Image:EditingDomain_ID.PNG]]
 +
 
 +
7. Generate code for both diagram plugins.
 +
 
 +
8. In both generated ???DiagramEditor classes modify createEditingDomain() method to simply call to the super. implementation:
 +
 
 +
/**
 +
* @generated NOT
 +
*/
 +
protected TransactionalEditingDomain createEditingDomain() {
 +
return super.createEditingDomain();
 +
}
 +
 
 +
9. Declare EditingDomain with the shared EditingDomain ID (see step 5.) inside plugin.xml for generated for diagram1:
 +
 
 +
<!-- gmf generator persistent region begin -->
 +
    <extension
 +
          point="org.eclipse.emf.transaction.editingDomains">
 +
      <editingDomain
 +
            factory="org.eclipse.emf.workspace.WorkspaceEditingDomainFactory"
 +
            id="SharedEditingDomain"/>
 +
    </extension>
 +
&lt;!-- gmf generator persistent region end -->

Revision as of 12:23, 26 October 2006

I find those tips useful:

Creating New Elements And Corresponding Views

DeviceEditPart selectedElement = ...;

String compartemntsSemanticHint = NetworkVisualIDRegistry.getType(tests.mindmap.network.diagram.edit.parts.Device_ModulesCompartmentEditPart.VISUAL_ID);
Device_ModulesCompartmentEditPart modulesCompartment = (Device_ModulesCompartmentEditPart)selectedElement.getChildBySemanticHint(compartemntsSemanticHint);

IElementType type = NetworkElementTypes.Module_3001;

ViewAndElementDescriptor viewDescriptor = new ViewAndElementDescriptor(
		new CreateElementRequestAdapter(new CreateElementRequest(type)),
		Node.class,
		((IHintedType) type).getSemanticHint(), 
		selectedElement.getDiagramPreferencesHint());

CreateViewAndElementRequest req = new CreateViewAndElementRequest(viewDescriptor);
CompoundCommand cmd = new CompoundCommand("Create 10 modules");
for (int i=0; i<10; i++) {
	cmd.add(modulesCompartment.getCommand(req));
}
selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(cmd);

Change Names Of Newly Created Elements

Collection results = DiagramCommandStack.getReturnValues(cmd);
for (Object res: results) {
	if (res instanceof IAdaptable) {
		IAdaptable adapter = (IAdaptable) res;
		View view = (View) adapter.getAdapter(View.class);
		if (view != null) {
			Module newMod = (Module)view.getElement();
			SetRequest reqSet = new SetRequest(selectedElement.getEditingDomain(),
					newMod, NetworkPackage.eINSTANCE.getNamedElement_Name(),
					"ModX");
			SetValueCommand operation = new SetValueCommand(reqSet);
			selectedElement.getDiagramEditDomain().getDiagramCommandStack().execute(new 
					ICommandProxy(operation));
		}
	}
}

Create New Elements Using RecordingCommand and CanonicalEditPolicies


final Device dev = (Device)((View)selectedElement.getModel()).getElement();
TransactionalEditingDomain editingDomain = selectedElement.getEditingDomain();
editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) {
	@SuppressWarnings("unchecked")
	protected void doExecute() {
		dev.setName("Morda13");
		for (int i = 0; i < 5; i++) {
			Module newMod = NetworkFactory.eINSTANCE.createModule();
			newMod.setName("X26 - " + i);
			dev.getModules().add(newMod);
		}
	}
});

New-small.gif Remove Property Sheet altogether

Add next method to the generated diagram editor class (usually <ModelName>DiagramEditor)

	public Object getAdapter(Class type) {
		if (type == IPropertySheetPage.class) {
			return null;
		}
		return super.getAdapter(type);
	}

New-small.gif Sharing single EditingDomain instance across several diagrams

Generated code always creates new (own) instance of EditingDomain for each opened diagram editor. Since EditingDomain owns ResourceSet, different instances of domain model elements will be loaded for each opened diagram editor. Sometimes it is important to share the same domain model instances across several diagram editors (for example to get rid of synchronization problems). This leads us to the question of sharing same instance of EditingDomain across several generated diagram editors. Below you can find step by step description of necessary modifications you have to apply to the generated code to share the same EditingDomain instance between two diagrams of different types.

1. Make sure you have org.eclipse.gmf.bridge.trace plugin installed as a part of GMF SDK

2. Create diagram1.gmfmap, diagram2.gmfmap files and generate diagram1.gmfgen, diagram2.gmfgen. Ensure diagram1.trace and diagram2.trace files created.

3. Modify generated diagram1.gmfgen and diagram2.gmfgen files to use different Model IDs, Plugin IDs, Editor IDs. Optionally two different diagrams could has different diagram file extensions as well:

Model ID.PNG Plugin ID.PNG
Editor ID.PNG

4. Modify diagram2.trace file to use different visualID values then specified in diagram1.trace file - the easiest way is to replace “200”-> “210”, .., “800” -> “810” – and regenerate diagram2.gmfgen from diagram2.gmfmap

5. Open diagram2.gmfgen and modify visualID property for GenDiagram to make it different then visualID of GenDiagram in diagram1.gmfgen:

Diagram2Visual ID.PNG

6. Ensure both diagram1.gmfgen and diagram2.gmfgen are using the same EditingDomain ID:

EditingDomain ID.PNG

7. Generate code for both diagram plugins.

8. In both generated ???DiagramEditor classes modify createEditingDomain() method to simply call to the super. implementation:

	/**
	 * @generated NOT
	 */
	protected TransactionalEditingDomain createEditingDomain() {
		return super.createEditingDomain();
	}

9. Declare EditingDomain with the shared EditingDomain ID (see step 5.) inside plugin.xml for generated for diagram1:

<!-- gmf generator persistent region begin -->
   <extension
         point="org.eclipse.emf.transaction.editingDomains">
      <editingDomain
            factory="org.eclipse.emf.workspace.WorkspaceEditingDomainFactory"
            id="SharedEditingDomain"/>
   </extension>
<!-- gmf generator persistent region end -->