Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Create a Customized GMF DnD Framework


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Create a Customized GMF DnD Framework

  1. 1. Customizing GMF DnD FrameworkStep 1 : Contributing custom edit policy provider to GMF editorIn our DnD handler plugin, we to theorg.eclipse.gmf.runtime.diagram.ui.editpolicyProviders.This Presentation Edit Policy Provider extension point is used to define editpolicy providersfor the editpolicy service.The editpolicy service allows plug-ins to modify or enhance the behavior of an editpart via aneditpolicy without modifying the editpart code.Step 2 : Attach a custom edit policy and a drop target listener to the edit partpublic 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 Policypublic 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. 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 thetarget request private EditPart calculateTargetEditPart() { updateTargetRequest(); EditPart ep = getViewer() .findObjectAtExcluding( getDropLocation(), getExclusionSet(), newEditPartViewer.Conditional() { publicboolean 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. 3. returngetDnDEditPolicy().getCommand(_request); }else { returnsuper.getCommand(_request); } } /** * @generated NOT * @return */ private EditPolicy getDnDEditPolicy() { EditPolicy dndPolicy = null; EditPolicyIterator i =getEditPolicyIterator(); while (i.hasNext()) { EditPolicy tempPolicy; if (tempPolicy instanceofDragDropEditPolicy) { 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 correspondingGenerator class
  4. 4. public class BOMADragDropCommand extends AbstractTransactionalCommand{ private EObject modelObj; private EObject element; private IDataModelGenerator modelGen; /** * * @param editDomain * @param request * @param modelObj * @param element */ public BOMADragDropCommand (TransactionalEditingDomaineditDomain,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){ caseDropElementTypeResolver.CONCEPT_TYPE: modelGen = new ConceptModelGenerator((Model)modelObj);modelGen.generateUMLModel(element);modelGen.setRelationships();break; caseDropElementTypeResolver.DB_TYPE: modelGen = new DB2UMLModelGenerator((Model)modelObj);modelGen.generateUMLModel(element);modelGen.setRelationships();break; caseDropElementTypeResolver.JAVA_TYPE:break; caseDropElementTypeResolver.XSD_TYPE:
  5. 5. break; } } } return CommandResult.newOKCommandResult(); } private static List getAffectedFiles(Request request){ return null; }}