Pages

Thursday, 3 June 2010

Stop Connection from moving when Nodes are moved

For this to take place we need to understand the concept of fixedconnectionAnchor. Try to extend the class AbstractConnectionAnchor and override the methods getLocation() and the getOwner() method to suit to your needs.

Then go to the editpart you wish the connection to stick to and just write the following code -

public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connEditPart) {
     if (fixedAnchor == null) 
       {
         fixedAnchor = new FixedConnectionAnchor(getNodeFigure());
         getNodeFigure().addAncestorListener(fixedAnchor);
       }
  return fixedAnchor;
}

public ConnectionAnchor getTargetConnectionAnchor(Request request) {
    if (fixedAnchor == null) 
      {
        fixedAnchor = new FixedConnectionAnchor(getNodeFigure());
        getNodeFigure().addAncestorListener(fixedAnchor);
      }
  return fixedAnchor;
}

Cascaded menus in GMF

To get nested menus while dragging a connection out from a node in GMF we need to make our own command that extends PopupMenuCommand.

The Steps will be -
1. Override the method doExecuteWithResult().
2. Inside the method we would need to call another method createPopupMenu().
3. To populate the contents of the popupmenu we need the connection and endMenu contents.
4. To get the endMenu Contents write a method similar to :-

protected List getEndMenuContent(Object connectionItem)
{
  ModelingAssistantService mInstance = ModelingAssistantService.getInstance();
  if(connectionItem instanceof IElementType)
   {
     IElementType connectionType = (IElementType) connectionItem;
     List returneMe = new ArrayList();
     List menuContent = mInstance.getTypesForTarget(getKnownEnd(), connectionType);
     for (int i = 0; i <>
      {
         if(menuContent.get(i) instanceof IElementType)
                     returneMe.add(menuContent.get(i));
         else {
               returneMe.add(new PopupMenu.CascadingMenu("{name of the catrgory}", new PopupMenu(
               (List) (menuContent.get(i)), getConnectionAndEndLabelProvider(menuContent.get(i)))));
              }
      }
      returneMe.add(EXISTING_ELEMENT);
      return returneMe;
 }
return Collections.EMPTY_LIST;
}

Once you are done with these stpes then go ahead with the normal 
procedure to call this command in the ContainerNodeEditPolicy of your projetct.