[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.gmf] Re: Is it possible to create a connection before its source and target elements are created?

hi, Alex:
    thank you for the tips.
    I have tried to modify the NClassItemSemanticEditPolicy class according 
to the NLifeNodeItemSemanticEditPolicy class, and now I have made some 
progress, but have not solve the problem totally.
    The modification I made is as follows:
    (1)by adding the getCreateRelationshipCommand, creation of a NMessage 
connection is enable when the mouse moves on the NClass.
    protected Command getCreateRelationshipCommand(CreateRelationshipRequest 
req) {
          if (SequenceElementTypes.NMessage_4001 == req.getElementType()) {


           return req.getTarget() == null ? 
getCreateStartOutgoingNMessage4001Command(req)
             : getCreateCompleteIncomingNMessage4001Command(req);
          }

          return super.getCreateRelationshipCommand(req);
     }
 (2) add the method getCreateStartOutgoingNMessage4001Command(), and 
getCreateCompleteIncomingNMessage4001Command(), the former one is the same 
as that of the NLifeNodeItemSemanticEditPolicy, just return a new Command, 
and in the later one, I made such modifications
 protected Command getCreateCompleteIncomingNMessage4001Command(
   CreateRelationshipRequest req) {

//here, both getSource() and getTarget() return a NClass object, so I delete 
the checking code.
 //if (!(req.getSource() instanceof NLifeNode)) {
 //  return UnexecutableCommand.INSTANCE;
 // }

//here, I create two NLifeNode object, and set the source and target of the 
request to these newly
//created NLifeNode respectively
  EObject srcContainer = null;
  EObject destContainer = null;
  if(!req.getSource().equals(req.getTarget())) //get the real source and 
target of the request
  {

   final NPackage element = (NPackage) getRelationshipContainer(req
     .getSource(), NPackagePackage.eINSTANCE.getNPackage(), req
     .getElementType());
   if (element == null) {
    return UnexecutableCommand.INSTANCE;
   }

   srcContainer = req.getSource();//now the source of the request is a 
NClass, and I should create a NLifeNode in it.
   destContainer = req.getTarget();

   NLifeNode firstLifeNode = NPackageFactory.eINSTANCE.createNLifeNode();
    NLifeNode secondLifeNode = NPackageFactory.eINSTANCE.createNLifeNode();

   TransactionalEditingDomain domain = 
TransactionUntil.getEditingDomain(getHost().getModel());
    CreateChildCommand firstCommand  = new CreateChildCommand(domain, 
srcContainer, NPackagePackage.eINSTANCE.getNClass_TimeNodes(), 
firstLifeNode, null);
   CreateChildCommand secondCommand = new CreateChildCommand(domain, 
destContainer,  NPackagePackage.eINSTANCE.getNClass_TimeNodes(), 
secondLifeNode, null);

    firstCommand.execute();
    secondCommand.execute();

   req.setSource(firstLifeNode);
   req.setTarget(secondLifeNode);

  if (req.getContainmentFeature() == null) {
   req.setContainmentFeature(NPackagePackage.eINSTANCE
     .getNPackage_Messages());
  }
  return getMSLWrapper(new CreateIncomingNMessage4001Command(req) {

   protected EObject getElementToEdit() {
    return element;
   }
  });
}
else return null;
 }

(4) Just the same as NLifeNodeItemSemanticEditPolicy, an inner class 
CreateIncomingNMessage4001Command is created here.

     I think the key point is before creating the 
CreateIncomingNMessage4001Command, the parameter passed to it (Req) should 
be set correctly. The previous source and target object of the request is 
NClass( I made it by adding the getCreateRelationshipCommand), now it should 
be set to two newly created NLifeNode in the previous source and target 
NClass respectively. I do this job using the CreateChildCommand,.
    Now if I create a NMessage between two NClass, when I push the "save" 
button, an error saying "the newly created NLifeNode can't be saved because 
it is not in a resource set" occurred. When debugging, I found these two 
commands are not executed correctly, so the NLifeNodes are not created 
correctly. I don't know whether the EMF command can be executed here.
    I also try the GMF command CreateElementCommand instead of 
CreateChildCommand , but I find it impossible to execute a 
TransactionalCommand immediately(because only after execution of the 
command, can the NLifeNode be really created, then I call the setSource() 
and setTarget() of the req).  When I call the execute() method of 
CreateElementCommand directly, an error "Cannot activate read/write 
transaction in read-only transaction context" happened, So I put the 
execution code of these two CreateElementCommand in a TransactionalCommand, 
then when should these TransactionalCommand execute?...



"Alex Shatalin" <vano@xxxxxxxxxxx> дÈëÏûÏ¢ÐÂÎÅ:3c3172e6f2ee8c90c4e4b8fa223@xxxxxxxxxxxxxxxxxxx
> Hello Min,
>
> I think you should modify generated SemanticItemEditPolicy of NClass to 
> allow incoming NMessage and return command creating first NLifeNode and 
> then NMessage. You can take a look on the generated SemanticItemEditPolicy 
> for NLifeNode to gather an information on required command structure.
>
> -----------------
> Alex Shatalin
>
>