Getting started with non EMF Domain Models, need an example [message #765844] |
Wed, 14 December 2011 13:59  |
Eclipse User |
|
|
|
I am trying desperately to make a very simple example with POJO classes and Graphiti. (Based on the EMF tutorial in the documentation).
It looks to me that Graphiti can only work with EMF:(
I have two very simple Domain Objects: BusinessClass and Association.
public class BusinessClass {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Association {
private BusinessClass fromClass;
private BusinessClass toClass;
private String name;
public BusinessClass getFromClass() {
return fromClass;
}
public void setFromClass(BusinessClass fromClass) {
this.fromClass = fromClass;
}
public BusinessClass getToClass() {
return toClass;
}
public void setToClass(BusinessClass toClass) {
this.toClass = toClass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
In the CreateBusinessClassFeature I have following code:
@Override
public Object[] create(ICreateContext context) {
// ask user for BusinessClass name
String newClassName = ExampleUtil.askString(TITLE, USER_QUESTION, "");
if (newClassName == null || newClassName.trim().length() == 0) {
return EMPTY;
}
// create EClass
BusinessClass newClass = new BusinessClass();
// Add model element to resource.
// We add the model element to the resource of the diagram for
// simplicity's sake. Normally, a customer would use its own
// model persistence layer for storing the business model separately.
// getDiagram().eResource().getContents().add(newClass);
newClass.setName(newClassName);
// do the add
addGraphicalRepresentation(context, newClass);
// return newly created business object(s)
return new Object[] { newClass };
}
If I start the Editor and create a new BusinessClass, then an exception happens:
java.lang.ClassCastException: test.graphiti.nonemf.domainmodel.BusinessClass cannot be cast to org.eclipse.emf.ecore.EObject
at org.eclipse.graphiti.features.impl.AbstractFeatureProvider.link(AbstractFeatureProvider.java:668)
..
...
at org.eclipse.graphiti.features.impl.AbstractFeature.addGraphicalRepresentation(AbstractFeature.java:108)
at test.graphiti.nonemf.diagram.features.CreateBusinessClassFeature.create(CreateBusinessClassFeature.java:49)
The reason is that the linkage of the diagramm can only work with EObjects ...
EObject bo = (EObject) businessObjects[i]; // KABUMM!!
It is very frustrating for me, to create without prior knowledge a small and clear example.
So please, can someone help me create such an example with only pojo objects?
It would also help a lot for Graphiti distribution. The world is not just about EMF ...
|
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #766352 is a reply to message #766111] |
Thu, 15 December 2011 10:50   |
Eclipse User |
|
|
|
Hello Tim,
many thanks for that tip! Should it eventually be mentioned here: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.graphiti.doc%2Fresources%2Fdocu%2Fgfw%2FNon-EMF+domain+objects.htm ?
My implementation now looks like this (first try, I'm happy, because I can draw after hours of despair a rectangle 
import org.eclipse.graphiti.features.impl.IIndependenceSolver;
public class POJOIndependenceSolver implements IIndependenceSolver {
private static Map<String, Object> objectMap = new HashMap<String, Object>();
@Override
public String getKeyForBusinessObject(Object bo) {
String result = null;
if(bo != null) {
result = String.valueOf(bo.hashCode());
if(!objectMap.containsKey(result))
objectMap.put(result, bo);
}
return result;
}
@Override
public Object getBusinessObjectForKey(String key) {
return objectMap.get(key);
}
}
public class DiagramFeatureProvider extends DefaultFeatureProvider {
POJOIndependenceSolver pojoIndependenceSolver;
public DiagramFeatureProvider(IDiagramTypeProvider dtp) {
super(dtp);
pojoIndependenceSolver = new POJOIndependenceSolver();
setIndependenceSolver(pojoIndependenceSolver);
}
@Override
public IAddFeature getAddFeature(IAddContext context) {
// is object for add request a TermClass?
if (context.getNewObject() instanceof BusinessClass) {
return new AddBusinessClassFeature(this);
}
return super.getAddFeature(context);
}
@Override
public ICreateFeature[] getCreateFeatures() {
return new ICreateFeature[] { new CreateBusinessClassFeature(this) };
}
}
I keep trying. I'll also post my further questions on this topic, if you don't mind.
Best regards,
Nikolai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #900272 is a reply to message #900268] |
Mon, 06 August 2012 05:50   |
Eclipse User |
|
|
|
Hi,
I get it done in a follow way (with drag & drop): I have a TreeView with my custom, Non-EMF Objects and an editor. If I drag & drop an object from the TreeView into the editor, I create a new "Binding" with AddFeature to the diagram.
Unfortunately, my "Example" does not work anymore, after updating Graphiti to the 0.9x version. I need some time, to correct the code and test it. After that, I will upload it here.
Best regards,
Nikolai
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #900278 is a reply to message #900272] |
Mon, 06 August 2012 06:23   |
Eclipse User |
|
|
|
In my context menu action class I do the following to open up the editor.
final String diagramTypeId = "test.graphiti.nonemf.diagram.DiagramType";
final Diagram diagram = Graphiti.getPeCreateService()
.createDiagram(diagramTypeId,
"test.graphiti.nonemf.diagram.DiagramType", false);
final String editorID = NonEmfDiagramEditor.DIAGRAM_EDITOR_ID;
IFile diagramFile = project.getFile("Test" + "." + "diagram");
URI uri = URI.createPlatformResourceURI(diagramFile.getFullPath()
.toString(), true);
TransactionalEditingDomain editingDomain = FileService
.createEmfFileForDiagram(uri, diagram);
String providerId = "test.graphiti.nonemf.diagram.DiagramTypeProvider";
DiagramEditorInput editorInput = new DiagramEditorInput(
EcoreUtil.getURI(diagram), editingDomain, providerId, true);
IDiagramTypeProvider dtyProvider = ExtensionManager.getSingleton()
.createDiagramTypeProvider(providerId);
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().openEditor(editorInput, editorID);
this.createExampleStructure(dtyProvider, editingDomain, diagram,
doaminObject);
And in the createExampleStructure function, I call the Add feature for the domain object as follows:
private void createExampleStructure(final IDiagramTypeProvider ddtp,
final TransactionalEditingDomain editingDomain,
final Diagram diagram, DomainObject domainObject) {
int x = 20;
int y = 20;
AddContext addcontext = new AddContext();
IFeatureProvider featureprovider = ddtp.getFeatureProvider();
AddDomainClassFeature addFvFeature = new AddDomainClassFeature(
featureprovider);
addcontext.setNewObject(domainObject);
addcontext.setTargetContainer(diagram);
addcontext.setX(x);
addcontext.setY(y);
if (addFvFeature.canAdd(addcontext)) {
addFvFeature.add(addcontext);
}
}
With this I get an Error "Cannot modify resource set without a write transaction"
Is this the right way to do or am I missing something?
[Updated on: Thu, 09 August 2012 05:11] by Moderator
|
|
|
|
|
|
|
|
|
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #985568 is a reply to message #983820] |
Thu, 15 November 2012 08:57   |
Eclipse User |
|
|
|
Hi Hemlata,
I am happy that my example has helped you.
In fact, the code from the example should be migrated to Graphiti version 0.9.x.
In the Github version of my example (https://github.com/kumarunster/phirea.public/tree/master/test.graphiti.nonemf) you can see the current state of the example, and it runs on 0.9 version of Graphiti, but unfortunately I cannot exactly say (on the fly), what you should change...
In the whole example I'm trying to show, how is it possible to use Graphiti based editors without EMF lock-in of the domain model. If you look at ApplicationWorkbenchWindowAdvisor you have a Tip, how to get the right EditorInput for your Graphiti-Editor.
About my case: In my application I have a datamodel, that is stored in a database and a lot of graphiti-xml-files, that are stored in a database too. The graphiti-xml's are all created programatically from some template-files, and all the referenced objects in that diagram files are drag-n-dropped from my "Repository". That files are copied, if a EditorInput is produced, in a Temp-Folder from my Application every time the new User-Session is started and a diagram is requested. On Save-Actions they are stored in the DB as BLOB.
If you have any questions, so feel free to ask me and look to the github version of my example!
Best regards,
Nikolai
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #985772 is a reply to message #985568] |
Fri, 16 November 2012 01:16   |
Eclipse User |
|
|
|
Hi Nikolai,
Thanks for that valuable info.
I use the github version of your example which works well with Graphiti 0.9.1 as well.
I do get EditorInput for my Graphiti-Editor the way you get it in ApplicationWorkbenchWindowAdvisor.
In my case I do not want to persist graphiti related info in the database.Right now I am just trying to add the pictogram representation of the domain object to the diagram editor with version 0.9.1
I try to create my own Graphiti diagram as follows:
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
IEditorInput input = RepositoryUtils.getEditorInput();
// Create the pictogram for selected Functional Variable
final Diagram diagram = Graphiti.getPeCreateService()
.createDiagram(DIAGRAM_TYPE, sourceVariable_.getName(),
false);
DiagramEditor editor = new DiagramEditor();
final DiagramTypeProvider dtyProvider = (DiagramTypeProvider) ExtensionManager
.getSingleton().createDiagramTypeProvider(
DIAGRAM_TYPE_PROVIDER_ID);
dtyProvider.init(diagram, editor);
page.openEditor(input, DiagramEditor.DIAGRAM_EDITOR_ID);
With this blank graphical editor opens up.And now I try to add the buisness object to this diaram using following stuff:
AddContext addContext = new AddContext();
addContext.setNewObject(object);
addContext.setLocation(200, 300);
addContext.setSize(100, 200);
addContext.setTargetContainer(diagram);
DiagramFeatureProvider fp = (DiagramFeatureProvider) dtyProvider
.getFeatureProvider();
AddFunctionalVariableFeature feature = (AddFunctionalVariableFeature)
dtyProvider.getFeatureProvider().getAddFeature(addContext);
dtyProvider.getFeatureProvider().addIfPossible(addContext);
When using this ,internally it gives Nullpointer exception for getEditDomain() in addIfPossible method as follows:
org.eclipse.core.runtime.AssertionFailedException: null argument:
at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:85)
at org.eclipse.core.runtime.Assert.isNotNull(Assert.java:73)
at org.eclipse.graphiti.ui.editor.DiagramEditor.executeFeature(DiagramEditor.java:1946)
at org.eclipse.graphiti.features.impl.AbstractFeatureProvider.addIfPossible(AbstractFeatureProvider.java:331)
I suppose that is because my editor is not getting initialised properly.
On debugging I see that my Editor has no TransactionalEditingDomain.With version 0.8.2 I created my own TransactionalEditingDomain and passed that to constructor of DiagramEditor. But in version 0.9 there is no constructor which accepts TransactionalEditingDomain.
I am now stuck at point as how to add the pictogram on the diagram with the new version of Graphiti.
|
|
|
Re: Getting started with non EMF Domain Models, need an example [message #985810 is a reply to message #985772] |
Fri, 16 November 2012 04:07  |
Eclipse User |
|
|
|
Hemlata,
in case the editing domain is the issue, that is indeed changed between 0.8
and 0.9: instead of passing a domain into the input (FWIW: which caused the
input object to be not as light-weight as it should be - that's the reason
why we removed it) you should now be able to create a special editing domain
in your own subclass of DefaultUpdateBehavior (override
DiagramEditor.createUpdateBehavior to hook in your own instance of that
class). DefaultUpdateBehavior offers a method createEditingDomain you could
override.
HTH,
Michael
|
|
|