Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Graphical Modeling Framework/Recipes
< Graphical Modeling Framework
Revision as of 11:26, 22 February 2008 by Thomas.kenner.acoveo.com (Talk | contribs) (→Recipe: HowTo reorder children in a GMF compartment with Drag & Drop)
Recipe: HowTo reorder children in a GMF compartment with Drag & Drop
Problem: You have created a node class which can contain other nodes. E.g. a class bookshelf that contains objects of the class book. Therefore you created a compartment. After adding books to the bookshelf in your editor, you want to rearrange the books by drag & drop. But it doesn't work...
Solution:
In the xxxCompartmentEditPart class rewrite the createFigure() method:
public IFigure createFigure() { ResizableCompartmentFigure rcf = (ResizableCompartmentFigure) super.createFigure(); FlowLayout layout = new FlowLayout(); layout.setMajorSpacing(getMapMode().DPtoLP(5)); layout.setMinorSpacing(getMapMode().DPtoLP(5)); layout.setHorizontal(false); rcf.getContentPane().setLayoutManager(layout); rcf.setTitleVisibility(false); return rcf; }
In the createDefaultEditPolicies() method of the xxxCompartmentEditPart class add an EditPolicy:
installEditPolicy(EditPolicy.LAYOUT_ROLE, new CompartmentEditPolicy(xxxPackage.Literals.xxx));
Note: xxxPackage.Literals.xxx has to be the EStructuralFeature of the EList Attribute that contains the Objects which should be reordered.
Use the classes CompartmentEditPolicy and CompartmentRepositionEObjectCommand:
public class CompartmentEditPolicy extends org.eclipse.gef.editpolicies.FlowLayoutEditPolicy { private EStructuralFeature feature = null; protected Command createAddCommand(EditPart child, EditPart after) { int index = getHost().getChildren().indexOf(after); TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain(); AddCommand command = new AddCommand(editingDomain, new EObjectAdapter((View)getHost().getModel()), new EObjectAdapter((View)child.getModel()), index); return new ICommandProxy(command); } protected EditPolicy createChildEditPolicy(EditPart child) { ResizableEditPolicyEx policy = new ResizableEditPolicyEx(); policy.setResizeDirections(0); return policy; } protected Command createMoveChildCommand(EditPart child, EditPart after) { int newIndex; int displacement; int childIndex = getHost().getChildren().indexOf(child); int afterIndex = getHost().getChildren().indexOf(after); if(afterIndex == -1) { newIndex = getHost().getChildren().size()-1; displacement = newIndex - childIndex; } else { newIndex = afterIndex; displacement = afterIndex - childIndex; if (childIndex <= afterIndex) { newIndex--; displacement--; } } TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain(); RepositionEObjectCommand command = new CompartmentRepositionEObjectCommand(child, editingDomain, "", (EList)((View)child.getParent().getModel()).getElement().eGet(feature), ((View)child.getModel()).getElement(), displacement, newIndex); //TODO ev. reintroduce target feedback (actual problem: line is not deleted after dropping) eraseLayoutTargetFeedback(null); return new ICommandProxy(command); } protected Command getCreateCommand(CreateRequest request) { return null; } protected Command getDeleteDependantCommand(Request request) { return null; } protected Command getOrphanChildrenCommand(Request request) { return null; } /** * @param feature has to be an EList */ public CompartmentEditPolicy(EStructuralFeature feature) { super(); this.feature = feature; } }
public class CompartmentRepositionEObjectCommand extends RepositionEObjectCommand { EditPart childToMove = null; int newIndex = 0; public CompartmentRepositionEObjectCommand( TransactionalEditingDomain editingDomain, String label, EList elements, EObject element, int displacement) { super(editingDomain, label, elements, element, displacement); } public CompartmentRepositionEObjectCommand(EditPart childToMove, TransactionalEditingDomain editingDomain, String label, EList elements, EObject element, int displacement, int newIndex) { super(editingDomain, label, elements, element, displacement); this.childToMove = childToMove; this.newIndex = newIndex; } public CommandResult doExecuteWithResult( IProgressMonitor progressMonitor, IAdaptable info) throws ExecutionException { CommandResult rs = super.doExecuteWithResult(progressMonitor, info); EditPart compartment = childToMove.getParent(); ViewUtil.repositionChildAt((View)compartment.getModel(), (View)childToMove.getModel(), newIndex); compartment.refresh(); return rs; } }