|
Added
Link Here
|
| 1 |
/** |
| 2 |
* Copyright (c) 2008, 2009 Anyware Technologies and others |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: |
| 10 |
* Anyware Technologies - initial API and implementation |
| 11 |
*/ |
| 12 |
package org.eclipse.emf.ecoretools.diagram.edit.policies; |
| 13 |
|
| 14 |
import org.eclipse.emf.common.util.EList; |
| 15 |
import org.eclipse.emf.ecore.EObject; |
| 16 |
import org.eclipse.emf.ecore.EStructuralFeature; |
| 17 |
import org.eclipse.emf.ecoretools.diagram.edit.commands.CompartmentReorderEObjectCommand; |
| 18 |
import org.eclipse.emf.ecoretools.diagram.part.EcoreVisualIDRegistry; |
| 19 |
import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| 20 |
import org.eclipse.gef.EditPart; |
| 21 |
import org.eclipse.gef.EditPolicy; |
| 22 |
import org.eclipse.gef.Request; |
| 23 |
import org.eclipse.gef.commands.Command; |
| 24 |
import org.eclipse.gef.commands.UnexecutableCommand; |
| 25 |
import org.eclipse.gef.requests.CreateRequest; |
| 26 |
import org.eclipse.gmf.runtime.diagram.core.commands.AddCommand; |
| 27 |
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| 28 |
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; |
| 29 |
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ResizableEditPolicyEx; |
| 30 |
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants; |
| 31 |
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; |
| 32 |
import org.eclipse.gmf.runtime.notation.View; |
| 33 |
|
| 34 |
/** |
| 35 |
* A LayoutEditPolicy that could be used in addition with a FlowLayout to |
| 36 |
* support child reordering |
| 37 |
* |
| 38 |
* Creation : 11 mar. 2008 |
| 39 |
* |
| 40 |
* @author <a href="mailto:jacques.lescot@anyware-tech.com">Jacques LESCOT</a> |
| 41 |
*/ |
| 42 |
public class ReorderingCompartmentEditPolicy extends org.eclipse.gef.editpolicies.FlowLayoutEditPolicy { |
| 43 |
|
| 44 |
// The domain model list that contains the element to reorder |
| 45 |
private EStructuralFeature feature = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* @param feature |
| 49 |
* The EStructutalFeature containing elements to reorder |
| 50 |
*/ |
| 51 |
public ReorderingCompartmentEditPolicy(EStructuralFeature feature) { |
| 52 |
super(); |
| 53 |
this.feature = feature; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @see org.eclipse.gef.editpolicies.OrderedLayoutEditPolicy#createAddCommand(org.eclipse.gef.EditPart, |
| 58 |
* org.eclipse.gef.EditPart) |
| 59 |
*/ |
| 60 |
protected Command createAddCommand(EditPart child, EditPart after) { |
| 61 |
View viewToMove = (View) child.getModel(); |
| 62 |
if (viewToMove != null) { |
| 63 |
View newParentView = (View) getHost().getModel(); |
| 64 |
if (newParentView != null) { |
| 65 |
// We check if the move/add is possible at notational level |
| 66 |
if (EcoreVisualIDRegistry.canCreateNode(newParentView, EcoreVisualIDRegistry.getVisualID(viewToMove.getType()))) { |
| 67 |
int index = getHost().getChildren().indexOf(after); |
| 68 |
TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain(); |
| 69 |
AddCommand command = new AddCommand(editingDomain, new EObjectAdapter(newParentView), new EObjectAdapter(viewToMove), index); |
| 70 |
return new ICommandProxy(command); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
return UnexecutableCommand.INSTANCE; |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @see org.eclipse.gef.editpolicies.OrderedLayoutEditPolicy#createChildEditPolicy(org.eclipse.gef.EditPart) |
| 80 |
*/ |
| 81 |
protected EditPolicy createChildEditPolicy(EditPart child) { |
| 82 |
ResizableEditPolicyEx policy = new ResizableEditPolicyEx(); |
| 83 |
policy.setResizeDirections(0); |
| 84 |
return policy; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* @see org.eclipse.gef.editpolicies.OrderedLayoutEditPolicy#createMoveChildCommand(org.eclipse.gef.EditPart, |
| 89 |
* org.eclipse.gef.EditPart) |
| 90 |
*/ |
| 91 |
protected Command createMoveChildCommand(EditPart child, EditPart after) { |
| 92 |
int newIndex; |
| 93 |
int moveOffset; |
| 94 |
|
| 95 |
int childIndex = getHost().getChildren().indexOf(child); |
| 96 |
int afterIndex = getHost().getChildren().indexOf(after); |
| 97 |
|
| 98 |
if (afterIndex == -1) { |
| 99 |
// Move the child to the last position |
| 100 |
newIndex = getHost().getChildren().size() - 1; |
| 101 |
moveOffset = newIndex - childIndex; |
| 102 |
} else { |
| 103 |
newIndex = afterIndex; |
| 104 |
moveOffset = afterIndex - childIndex; |
| 105 |
if (childIndex <= afterIndex) { |
| 106 |
newIndex--; |
| 107 |
moveOffset--; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost()).getEditingDomain(); |
| 112 |
CompartmentReorderEObjectCommand command = new CompartmentReorderEObjectCommand(editingDomain, "", (EList<EObject>) ((View) child.getParent().getModel()).getElement().eGet(feature), child, |
| 113 |
after); |
| 114 |
|
| 115 |
return new ICommandProxy(command); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getCreateCommand(org.eclipse.gef.requests.CreateRequest) |
| 120 |
*/ |
| 121 |
protected Command getCreateCommand(CreateRequest request) { |
| 122 |
return null; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getDeleteDependantCommand(org.eclipse.gef.Request) |
| 127 |
*/ |
| 128 |
protected Command getDeleteDependantCommand(Request request) { |
| 129 |
return null; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @see org.eclipse.gef.editpolicies.LayoutEditPolicy#getOrphanChildrenCommand(org.eclipse.gef.Request) |
| 134 |
*/ |
| 135 |
protected Command getOrphanChildrenCommand(Request request) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void eraseTargetFeedback(Request request) { |
| 141 |
// Need to handle case of REQ_DROP request's type to erase feedback |
| 142 |
if (RequestConstants.REQ_DROP.equals(request.getType())) |
| 143 |
eraseLayoutTargetFeedback(request); |
| 144 |
super.eraseTargetFeedback(request); |
| 145 |
} |
| 146 |
} |