Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Create new element and prefill it
Create new element and prefill it [message #1231137] Mon, 13 January 2014 22:53 Go to next message
Ralph Bergmann is currently offline Ralph BergmannFriend
Messages: 10
Registered: July 2009
Location: Berlin / Germany
Junior Member
Hi,


I have to kinds of elements: Dataobject and Field.

In the create feature of the Dataobject feature I want to fill this new object with a new Field element. I can create this new Field element and can add this to the Dataobject element but I is not visible on the diagram Sad

I think I have to call addGraphicalRepresentation from the Field element but it happens in the Dataobject create feature. How to call the addGraphicalRepresentation method from the Field?

How to create a new Dataobject element with a prefilled Filed element?



Ralph
Re: Create new element and prefill it [message #1232213 is a reply to message #1231137] Thu, 16 January 2014 12:51 Go to previous messageGo to next message
Michael Wenz is currently offline Michael WenzFriend
Messages: 1921
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Ralph,

not sure, but aren't you mixing up concepts here? In the create feature for
the dataobject you should also create the field (both on domain model
level), in the add feature of the data object you should delegate via the
addGraphicalRepresentation call to the add feature of the field.

Michael
Re: Create new element and prefill it [message #1232305 is a reply to message #1232213] Thu, 16 January 2014 16:41 Go to previous messageGo to next message
Ralph Bergmann is currently offline Ralph BergmannFriend
Messages: 10
Registered: July 2009
Location: Berlin / Germany
Junior Member
Hi Michael,


Michael Wenz wrote on Thu, 16 January 2014 07:51
not sure, but aren't you mixing up concepts here? In the create feature for the dataobject you should also create the field (both on domain model level), in the add feature of the data object you should delegate via the addGraphicalRepresentation call to the add feature of the field.


I hope that I did it the right way Smile

It works for me, here my code:

public class DataobjectPattern extends AbstractPattern implements IPattern {


   @Override
   public Object[] create(final ICreateContext context) {

      final Dataobject newDataObject = ModelFactory.eINSTANCE.createDataobject();
      newDataObject.setName(newDataObjectName);

      final Field newField = ModelFactory.eINSTANCE.createField();
      // ... settters

      newDataObject.addField(newField);

      final ContainerShape target = context.getTargetContainer();
      if (target instanceof Diagram) {
         getDiagram().eResource().getContents().add(newDataObject);
      } else {
         final PictogramLink link = target.getLink();
         final EList<EObject> businessObjects = link.getBusinessObjects();
         for (final EObject bo : businessObjects) {
            if (bo instanceof Database) {
               final Database db = (Database) bo;
               final EList<Version> versions = db.getVersions();
               final Version version = versions.get(versions.size() - 1);
               version.addTable(newDataObject);
            }
         }
      }

      addGraphicalRepresentation(context, newDataObject);

      return new Object[] { newDataObject, newField };
   }

   @Override
   public PictogramElement add(final IAddContext context) {

      final Dataobject newDataObject = (Dataobject) context.getNewObject();

      final IPeCreateService peCreateService = Graphiti.getPeCreateService();
      final IGaService gaService = Graphiti.getGaService();

      final ContainerShape target = context.getTargetContainer();
      final ContainerShape containerShape = peCreateService.createContainerShape(target, true);
      link(containerShape, newDataObject);

      // ... graphics

      // peCreateService.createChopboxAnchor(containerShape);
      layoutPictogramElement(containerShape);

      // add ID field to diagram
      final EList<Field> fields = newDataObject.getFields();
      if (fields.size() > 0) {

         final CreateContext createCtx = new CreateContext();
         createCtx.setTargetContainer(containerShape);

         addGraphicalRepresentation(createCtx, fields.get(0));
      }

      return containerShape;
   }
}
Re: Create new element and prefill it [message #1232589 is a reply to message #1232305] Fri, 17 January 2014 09:34 Go to previous messageGo to next message
Martin Hanysz is currently offline Martin HanyszFriend
Messages: 30
Registered: November 2013
Member
Dear Ralph,

It seems you are indeed mixing up the responsibilities of the add and create methods.
In the create method you should only deal with business objects (your domain model). So code like
final Dataobject newDataObject = ModelFactory.eINSTANCE.createDataobject();

is supposed to be in this method, code that deals with pictogram elements is not. That is what the add method is for. By calling
addGraphicalRepresentation(context, newDataObject);

you get from the create to the add method in order to create the graphical representation for the business objects you created in the create method.

I suggest you try the following:


  • in the create method: create your two business objects (DataObject and Field)
  • in the create method: call addGraphicalRepresentation for each business object you created
  • in the add method: create the pictogram elements you need to visualize your business objects in the diagram


Best regards,
Martin

[Updated on: Fri, 17 January 2014 09:35]

Report message to a moderator

Re: Create new element and prefill it [message #1232602 is a reply to message #1232589] Fri, 17 January 2014 09:59 Go to previous message
Ralph Bergmann is currently offline Ralph BergmannFriend
Messages: 10
Registered: July 2009
Location: Berlin / Germany
Junior Member
Hi Martin,

Martin Hanysz wrote on Fri, 17 January 2014 04:34


  • in the create method: create your two business objects (DataObject and Field)
  • in the create method: call addGraphicalRepresentation for each business object you created
  • in the add method: create the pictogram elements you need to visualize your business objects in the diagram



I had this before with the result that the Field Object was not visible Sad

I think the problem with this solution is that the Field canAdd method is called with the wrong context.

My structure is like this:

Diagram
Diagram has one or more Databases
Database has one or more Dataobjects
Dataobject has one or more Fields

Now when I call addGraphicalRepresentation within the Dataobject create method the target for the Field canAdd method is the Database but it has to be the Dataobject.

To fix this I created a new CreateContext with the right target. The right target is the ContainerShape of the Dataobject and this shape is created in the add method. Thats why I call addGraphicalRepresentation from the add method and not from the create method.


Ralph
Previous Topic:Connection to rounded Shapes
Next Topic:Force Connection refresh/redraw
Goto Forum:
  


Current Time: Fri Aug 14 02:48:04 GMT 2020

Powered by FUDForum. Page generated in 0.01618 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top