KEMBAR78
Create a Customized GMF DnD Framework | DOC
Customizing GMF DnD Framework

Step 1 : Contributing custom edit policy provider to GMF editor

In our DnD handler plugin, we contribute
com.tibco.xpd.cm.boma.dnd.core.BOMADnDEditPolicyProvider to the
org.eclipse.gmf.runtime.diagram.ui.editpolicyProviders.

This Presentation Edit Policy Provider extension point is used to define editpolicy providers
for the editpolicy service.
The editpolicy service allows plug-ins to modify or enhance the behavior of an editpart via an
editpolicy without modifying the editpart code.



Step 2 : Attach a custom edit policy and a drop target listener to the edit part

public void createEditPolicies(EditPart editPart) {
                             if (isEditPartValid(editPart))         {

/** if the edit part is valid,
    install the dnd edit policy
    editPart.installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE,

dndPolicy);
    editPart.getViewer().addDropTargetListener(dndListner);
**/
}
}

public boolean provides(IOperation operation) {
                             if(operation instanceof CreateEditPoliciesOperation){
                                           EditPart editPart =
((CreateEditPoliciesOperation)
             operation).getEditPart();
                                return isEditPartValid(editPart);
                             }
                             return false;

// Custom Edit Policy
public class BOMADragDropEditPolicy extends DragDropEditPolicy {

//------

}
Step 3 : Prepare the list of objects to be dropped and set it on the Drop Request
@Override
                protected List<Object> getObjectsBeingDropped() {

                                ISelection selection =
LocalSelectionTransfer.getInstance().getSelection();
                            if (selection instanceof IStructuredSelection) {
                                          sourceObjectList =
((IStructuredSelection) selection).toList();
                            }

                            return sourceObjectList;
              }

@Override
              protected Request createTargetRequest() {
      DropObjectsRequest req = new DropObjectsRequest();
            req.setObjects(getObjectsBeingDropped());
                            return req;
              }


Step 4: Return an edit part that can produce an executable command for the
target        request

              private EditPart calculateTargetEditPart() {
                            updateTargetRequest();
                            EditPart ep = getViewer()
                                          .findObjectAtExcluding(
                                                        getDropLocation(),
                                                        getExclusionSet(),
                                                        new
EditPartViewer.Conditional() {
                                                                      public
boolean evaluate(EditPart editpart) {

Command command =
      editpart.getCommand(getTargetRequest());


return command != null;
                                                                          }
                                                         });
                            /*if (ep != null) {
                                          Command command =
ep.getCommand(getTargetRequest());
                                          return (command != null &&
command.canExecute())? ep : null;
                            }*/
                            return ep;
              }


Step 5: Return the required DnD command from the corresponding Edit Part

/**
              * @generated NOT
              */
              @Override
              public Command getCommand(Request _request) {
                             if(_request instanceof DropObjectsRequest)
{
return
getDnDEditPolicy().getCommand(_request);
                            }else {
                                            return
super.getCommand(_request);
                              }
              }

              /**
              * @generated NOT
              * @return
              */
              private EditPolicy getDnDEditPolicy() {
                             EditPolicy dndPolicy = null;
                             EditPolicyIterator i =
getEditPolicyIterator();
                             while (i.hasNext()) {
                                           EditPolicy tempPolicy =
i.next();
                                           if (tempPolicy instanceof
DragDropEditPolicy) {
                                                          dndPolicy =
tempPolicy;
                                                          break;

                                            }
                              }
                              return dndPolicy;
              }

// Custom Edit Policy
  public class BOMADragDropEditPolicy extends DragDropEditPolicy {


@Override
              protected Command getDropElementCommand(EObject element,
DropObjectsRequest
            request) {
                            EObject modelObj = getHostObject();


                            TransactionalEditingDomain editDomain =
(TransactionalEdit
      ingDomain)WorkingCopyUtil.getWorkingCopyFor(modelObj).getEditingDomain();
                            return getGMFWrapper(new
            BOMADragDropCommand(editDomain,request,modelObj,element));
                            }

              private Command getGMFWrapper(ICommand gmfCommand){
                            return new ICommandProxy(gmfCommand);
              }
//------
}


Step 6 : Execute the DnD Command to create the Concepts through corresponding
Generator class
public class BOMADragDropCommand extends AbstractTransactionalCommand{

              private EObject modelObj;
              private EObject element;
              private IDataModelGenerator modelGen;

              /**
              *
              * @param editDomain
              * @param request
              * @param modelObj
              * @param element
              */
              public BOMADragDropCommand (TransactionalEditingDomain
editDomain,DropObjectsRequest request,EObject modelObj,EObject element){
                             super(editDomain,"DND",getAffectedFiles(request));
                             this.modelObj = modelObj;
                             this.element = element;

              }

              @Override
              protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
                            Model model = null;
                            //
                            if(modelObj != null){
                                          if(modelObj instanceof Model){
                                                         model = (Model)modelObj;
                                                int elementType =
                        DropElementTypeResolver.getElementType(element);
                                                         switch(elementType){
                                                                       case
DropElementTypeResolver.CONCEPT_TYPE:
      modelGen = new
      ConceptModelGenerator((Model)modelObj);

modelGen.generateUMLModel(element);

modelGen.setRelationships();

break;
                                                                         case
DropElementTypeResolver.DB_TYPE:
      modelGen = new
      DB2UMLModelGenerator((Model)modelObj);

modelGen.generateUMLModel(element);

modelGen.setRelationships();

break;
                                                                         case
DropElementTypeResolver.JAVA_TYPE:

break;
                                                                         case
DropElementTypeResolver.XSD_TYPE:
break;
                                                   }

                                     }
                       }
                       return CommandResult.newOKCommandResult();
         }

         private static List getAffectedFiles(Request request){

                       return null;
         }
}

Create a Customized GMF DnD Framework

  • 1.
    Customizing GMF DnDFramework Step 1 : Contributing custom edit policy provider to GMF editor In our DnD handler plugin, we contribute com.tibco.xpd.cm.boma.dnd.core.BOMADnDEditPolicyProvider to the org.eclipse.gmf.runtime.diagram.ui.editpolicyProviders. This Presentation Edit Policy Provider extension point is used to define editpolicy providers for the editpolicy service. The editpolicy service allows plug-ins to modify or enhance the behavior of an editpart via an editpolicy without modifying the editpart code. Step 2 : Attach a custom edit policy and a drop target listener to the edit part public void createEditPolicies(EditPart editPart) { if (isEditPartValid(editPart)) { /** if the edit part is valid, install the dnd edit policy editPart.installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, dndPolicy); editPart.getViewer().addDropTargetListener(dndListner); **/ } } public boolean provides(IOperation operation) { if(operation instanceof CreateEditPoliciesOperation){ EditPart editPart = ((CreateEditPoliciesOperation) operation).getEditPart(); return isEditPartValid(editPart); } return false; // Custom Edit Policy public class BOMADragDropEditPolicy extends DragDropEditPolicy { //------ } Step 3 : Prepare the list of objects to be dropped and set it on the Drop Request @Override protected List<Object> getObjectsBeingDropped() { ISelection selection =
  • 2.
    LocalSelectionTransfer.getInstance().getSelection(); if (selection instanceof IStructuredSelection) { sourceObjectList = ((IStructuredSelection) selection).toList(); } return sourceObjectList; } @Override protected Request createTargetRequest() { DropObjectsRequest req = new DropObjectsRequest(); req.setObjects(getObjectsBeingDropped()); return req; } Step 4: Return an edit part that can produce an executable command for the target request private EditPart calculateTargetEditPart() { updateTargetRequest(); EditPart ep = getViewer() .findObjectAtExcluding( getDropLocation(), getExclusionSet(), new EditPartViewer.Conditional() { public boolean evaluate(EditPart editpart) { Command command = editpart.getCommand(getTargetRequest()); return command != null; } }); /*if (ep != null) { Command command = ep.getCommand(getTargetRequest()); return (command != null && command.canExecute())? ep : null; }*/ return ep; } Step 5: Return the required DnD command from the corresponding Edit Part /** * @generated NOT */ @Override public Command getCommand(Request _request) { if(_request instanceof DropObjectsRequest) {
  • 3.
    return getDnDEditPolicy().getCommand(_request); }else { return super.getCommand(_request); } } /** * @generated NOT * @return */ private EditPolicy getDnDEditPolicy() { EditPolicy dndPolicy = null; EditPolicyIterator i = getEditPolicyIterator(); while (i.hasNext()) { EditPolicy tempPolicy = i.next(); if (tempPolicy instanceof DragDropEditPolicy) { dndPolicy = tempPolicy; break; } } return dndPolicy; } // Custom Edit Policy public class BOMADragDropEditPolicy extends DragDropEditPolicy { @Override protected Command getDropElementCommand(EObject element, DropObjectsRequest request) { EObject modelObj = getHostObject(); TransactionalEditingDomain editDomain = (TransactionalEdit ingDomain)WorkingCopyUtil.getWorkingCopyFor(modelObj).getEditingDomain(); return getGMFWrapper(new BOMADragDropCommand(editDomain,request,modelObj,element)); } private Command getGMFWrapper(ICommand gmfCommand){ return new ICommandProxy(gmfCommand); } //------ } Step 6 : Execute the DnD Command to create the Concepts through corresponding Generator class
  • 4.
    public class BOMADragDropCommandextends AbstractTransactionalCommand{ private EObject modelObj; private EObject element; private IDataModelGenerator modelGen; /** * * @param editDomain * @param request * @param modelObj * @param element */ public BOMADragDropCommand (TransactionalEditingDomain editDomain,DropObjectsRequest request,EObject modelObj,EObject element){ super(editDomain,"DND",getAffectedFiles(request)); this.modelObj = modelObj; this.element = element; } @Override protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { Model model = null; // if(modelObj != null){ if(modelObj instanceof Model){ model = (Model)modelObj; int elementType = DropElementTypeResolver.getElementType(element); switch(elementType){ case DropElementTypeResolver.CONCEPT_TYPE: modelGen = new ConceptModelGenerator((Model)modelObj); modelGen.generateUMLModel(element); modelGen.setRelationships(); break; case DropElementTypeResolver.DB_TYPE: modelGen = new DB2UMLModelGenerator((Model)modelObj); modelGen.generateUMLModel(element); modelGen.setRelationships(); break; case DropElementTypeResolver.JAVA_TYPE: break; case DropElementTypeResolver.XSD_TYPE:
  • 5.
    break; } } } return CommandResult.newOKCommandResult(); } private static List getAffectedFiles(Request request){ return null; } }