[no bug] Add drag and drop support for elements
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/actions/MoveAction.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/actions/MoveAction.java new file mode 100644 index 0000000..810c329 --- /dev/null +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/actions/MoveAction.java
@@ -0,0 +1,246 @@ +/******************************************************************************* + * Copyright (c) 2001, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.xsd.editor.internal.actions; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.jface.action.Action; +import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; +import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; +import org.eclipse.xsd.XSDConcreteComponent; +import org.eclipse.xsd.XSDModelGroup; +import org.eclipse.xsd.XSDParticle; +import org.eclipse.xsd.XSDParticleContent; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +public class MoveAction extends Action +{ + protected List selectedNodes; + protected Node parentNode; + protected Node previousRefChild, nextRefChild; + boolean doInsertBefore; + + List selectedComponentsList; + XSDModelGroup parentModelGroup; + XSDConcreteComponent previousRefComponent, nextRefComponent; + + public MoveAction(XSDModelGroup parentComponent, List selectedComponents, XSDConcreteComponent previousRefChildComponent, XSDConcreteComponent nextRefChildComponent) + { + this.parentModelGroup = parentComponent; + this.selectedComponentsList = selectedComponents; + this.previousRefComponent = previousRefChildComponent; + this.nextRefComponent = nextRefChildComponent; + + selectedNodes = new ArrayList(selectedComponents.size()); + for (Iterator i = selectedComponents.iterator(); i.hasNext(); ) + { + XSDConcreteComponent concreteComponent = (XSDConcreteComponent)i.next(); + selectedNodes.add(concreteComponent.getElement()); + } + parentNode = parentComponent.getElement(); + nextRefChild = nextRefChildComponent != null ? nextRefChildComponent.getElement() : null; + previousRefChild = previousRefChildComponent != null ? previousRefChildComponent.getElement() : null; + + doInsertBefore = (nextRefChild != null); + if (nextRefComponent != null) + { + if (nextRefComponent.getContainer().getContainer() == parentModelGroup) + { + doInsertBefore = true; + } + } + if (previousRefComponent != null) + { + if (previousRefComponent.getContainer().getContainer() == parentModelGroup) + { + doInsertBefore = false; + } + } + } + + public boolean canMove() + { + // TODO... there are likely more restriction to consider here + boolean result = true; + for (Iterator i = selectedNodes.iterator(); i.hasNext(); ) + { + Node child = (Node)i.next(); + if (isDecendantOrSelf(child, parentNode)) + { + result = false; + break; + } + } + return result; + } + + protected boolean isDecendantOrSelf(Node potentialParent, Node node) + { + boolean result = false; + while (node != null) + { + if (node == potentialParent) + { + result = true; + break; + } + node = node.getParentNode(); + } + return result; + } + + protected IDOMModel getModel() + { + IDOMModel model = null; + if (parentNode instanceof IDOMNode) + { + model = ((IDOMNode)parentNode).getModel(); + } + return model; + } + + + + /* + * @see IAction#run() + */ + public void run() + { + try + { + for (Iterator i = selectedComponentsList.iterator(); i.hasNext(); ) + { + XSDConcreteComponent concreteComponent = (XSDConcreteComponent)i.next(); + + if (doInsertBefore) + { + if (concreteComponent == nextRefComponent) + continue; + } + else + { + if (concreteComponent == previousRefComponent) + continue; + } + + + for (Iterator particles = parentModelGroup.getContents().iterator(); particles.hasNext(); ) + { + XSDParticle particle = (XSDParticle) particles.next(); + XSDParticleContent particleContent = particle.getContent(); + if (particleContent == concreteComponent) + { + parentModelGroup.getContents().remove(particle); + break; + } + } + int index = 0; + List particles = parentModelGroup.getContents(); + for (Iterator iterator = particles.iterator(); iterator.hasNext(); ) + { + XSDParticle particle = (XSDParticle) iterator.next(); + XSDParticleContent particleContent = particle.getContent(); + if (doInsertBefore) + { + if (particleContent == nextRefComponent) + { + parentModelGroup.getContents().add(index, (XSDParticle)concreteComponent.getContainer()); + break; + } + } + else + { + if (particleContent == previousRefComponent) + { + parentModelGroup.getContents().add(index + 1, (XSDParticle)concreteComponent.getContainer()); + break; + } + } + index ++; + } + if (particles.size() == 0) + { + parentModelGroup.getContents().add((XSDParticle)concreteComponent.getContainer()); + } + + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + + public void repositionBefore(Node child) + { + // TODO... when the refChild (inserting as the last element) we need to + // special case the way we preserve indentation + Node oldParent = child.getParentNode(); + if (oldParent != null && nextRefChild != child) + { + // consider any indentation text node that preceeds the child + // + Node textNode = isWhitespaceTextNode(child.getPreviousSibling()) ? child.getPreviousSibling() : null; + + // remove the child + // + oldParent.removeChild(child); + + // Instead of inserting the child immediatlely infront of the refChild, we first check to see if there + // is an indentation text node preceeding the refChild. If we find such a node, we perform the insertion + // so that the child is inserted before the indentation text node. + Node adjustedRefChild = nextRefChild; + if (nextRefChild != null && isWhitespaceTextNode(nextRefChild.getPreviousSibling())) + { + adjustedRefChild = nextRefChild.getPreviousSibling(); + } + + // reposition the child and any indentation text node + // + if (doInsertBefore) + { + try { + parentNode.insertBefore(child, adjustedRefChild); + if (textNode != null) + { + oldParent.removeChild(textNode); + parentNode.insertBefore(textNode, child); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + + } + else + { + adjustedRefChild = previousRefChild.getNextSibling(); + parentNode.insertBefore(child, adjustedRefChild); + } + } + } + + + protected static boolean isWhitespaceTextNode(Node node) + { + boolean result = false; + if (node != null && node.getNodeType() == Node.TEXT_NODE) + { + String data = ((Text)node).getData(); + result = (data == null || data.trim().length() == 0); + } + return result; + } +}
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/design/editparts/SpaceFillerForFieldEditPart.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/design/editparts/SpaceFillerForFieldEditPart.java index 41cfa23..f57cca7 100644 --- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/design/editparts/SpaceFillerForFieldEditPart.java +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/design/editparts/SpaceFillerForFieldEditPart.java
@@ -13,10 +13,10 @@ import org.eclipse.draw2d.IFigure; import org.eclipse.draw2d.Label; import org.eclipse.draw2d.MarginBorder; -import org.eclipse.wst.xsd.adt.design.editparts.BaseEditPart; +import org.eclipse.wst.xsd.adt.design.editparts.BaseFieldEditPart; import org.eclipse.wst.xsd.editor.XSDEditorPlugin; -public class SpaceFillerForFieldEditPart extends BaseEditPart +public class SpaceFillerForFieldEditPart extends BaseFieldEditPart { Label space; public SpaceFillerForFieldEditPart() @@ -38,5 +38,6 @@ protected void createEditPolicies() { + } }
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editparts/BaseFieldEditPart.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editparts/BaseFieldEditPart.java index 7a46fce..9c78fac 100644 --- a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editparts/BaseFieldEditPart.java +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editparts/BaseFieldEditPart.java
@@ -32,8 +32,10 @@ import org.eclipse.wst.xsd.adt.design.directedit.TypeReferenceDirectEditManager; import org.eclipse.wst.xsd.adt.design.editparts.model.FocusTypeColumn; import org.eclipse.wst.xsd.adt.design.editpolicies.ADTDirectEditPolicy; -import org.eclipse.wst.xsd.adt.design.editpolicies.ADTSelectionFeedbackEditPolicy; +import org.eclipse.wst.xsd.adt.design.editpolicies.DragAndDropEditPolicy; +import org.eclipse.wst.xsd.adt.design.editpolicies.GraphNodeDragTracker; import org.eclipse.wst.xsd.adt.design.editpolicies.IADTUpdateCommand; +import org.eclipse.wst.xsd.adt.design.editpolicies.SelectionHandlesEditPolicyImpl; import org.eclipse.wst.xsd.adt.design.figures.IFieldFigure; import org.eclipse.wst.xsd.adt.facade.IField; import org.eclipse.wst.xsd.adt.facade.IType; @@ -117,13 +119,14 @@ return connectionFigure; } - + protected SelectionHandlesEditPolicyImpl selectionHandlesEditPolicy = new SelectionHandlesEditPolicyImpl(); protected void createEditPolicies() { installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, adtDirectEditPolicy); - installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, new ADTSelectionFeedbackEditPolicy()); + installEditPolicy(EditPolicy.SELECTION_FEEDBACK_ROLE, selectionHandlesEditPolicy); + installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new DragAndDropEditPolicy(getViewer(), selectionHandlesEditPolicy)); } - + public void refresh() { super.refresh(); @@ -152,7 +155,7 @@ public DragTracker getDragTracker(Request request) { - return super.getDragTracker(request); + return new GraphNodeDragTracker((EditPart)this); } /*
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropCommand.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropCommand.java new file mode 100644 index 0000000..6ab9956 --- /dev/null +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropCommand.java
@@ -0,0 +1,341 @@ +/******************************************************************************* + * Copyright (c) 2001, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.xsd.adt.design.editpolicies; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.draw2d.FigureCanvas; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PointList; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.EditPartViewer; +import org.eclipse.gef.requests.ChangeBoundsRequest; +import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; +import org.eclipse.wst.xsd.adt.design.editparts.BaseFieldEditPart; +import org.eclipse.wst.xsd.adt.design.editparts.CompartmentEditPart; +import org.eclipse.wst.xsd.adt.design.editparts.ComplexTypeEditPart; +import org.eclipse.wst.xsd.editor.internal.actions.MoveAction; +import org.eclipse.wst.xsd.editor.internal.adapters.XSDBaseAdapter; +import org.eclipse.wst.xsd.editor.internal.design.editparts.ModelGroupDefinitionReferenceEditPart; +import org.eclipse.wst.xsd.editor.internal.design.editparts.ModelGroupEditPart; +import org.eclipse.wst.xsd.editor.internal.design.editparts.TargetConnectionSpacingFigureEditPart; +import org.eclipse.wst.xsd.editor.internal.design.editparts.XSDGroupsForAnnotationEditPart; +import org.eclipse.wst.xsd.editor.internal.design.figures.GenericGroupFigure; +import org.eclipse.wst.xsd.ui.common.commands.BaseCommand; +import org.eclipse.xsd.XSDConcreteComponent; +import org.eclipse.xsd.XSDModelGroup; + + +public class DragAndDropCommand extends BaseCommand +{ + protected EditPartViewer viewer; + protected ChangeBoundsRequest request; + protected BaseFieldEditPart previousChildRefEditPart, nextChildRefEditPart; + public ModelGroupEditPart parentEditPart; + public Point location; + protected MoveAction action; + protected boolean canExecute; + EditPart target; + List modelGroupsList = new ArrayList(); + List targetSpacesList = new ArrayList(); + + public DragAndDropCommand(EditPartViewer viewer, ChangeBoundsRequest request) + { + this.viewer = viewer; + this.request = request; + + location = request.getLocation(); + target = viewer.findObjectAt(location); + if (viewer instanceof ScrollingGraphicalViewer) + { + ScrollingGraphicalViewer sgv = (ScrollingGraphicalViewer)viewer; + Point p = ((FigureCanvas)sgv.getControl()).getViewport().getViewLocation(); + location.y += p.y; + location.x += p.x; + } + + List list = request.getEditParts(); + if (list.size() > 0 && target instanceof BaseFieldEditPart) + { + BaseFieldEditPart baseFieldEditPart = (BaseFieldEditPart)target; + XSDModelGroup targetModelGroup = null; + modelGroupsList.clear(); + targetSpacesList.clear(); + calculateModelGroupList(); + + List modelGroups = new ArrayList(modelGroupsList.size()); + for (Iterator i = modelGroupsList.iterator(); i.hasNext(); ) + { + ModelGroupEditPart editPart = (ModelGroupEditPart)i.next(); + modelGroups.add(editPart.getXSDModelGroup()); + } + + EditPart compartment = baseFieldEditPart.getParent(); + parentEditPart = null; + if (compartment != null) + { + List l = compartment.getChildren(); + Rectangle rectangle = new Rectangle(0, 0, 0, 0), previousRectangle = new Rectangle(0,0,0,0); + int index = -1; + BaseFieldEditPart childGraphNodeEditPart = null; + for (Iterator i = l.iterator(); i.hasNext(); ) + { + EditPart child = (EditPart)i.next(); + if (child instanceof BaseFieldEditPart) + { + index ++; + previousChildRefEditPart = childGraphNodeEditPart; + childGraphNodeEditPart = (BaseFieldEditPart)child; + if (previousChildRefEditPart != null) + { + previousRectangle = previousChildRefEditPart.getFigure().getBounds(); + } + rectangle = childGraphNodeEditPart.getFigure().getBounds(); + + if (location.y < (rectangle.getCenter().y)) + { + nextChildRefEditPart = childGraphNodeEditPart; + TargetConnectionSpacingFigureEditPart tSpace = (TargetConnectionSpacingFigureEditPart)targetSpacesList.get(index-1); + if (previousRectangle != null && location.y > (previousRectangle.getBottom().y)) + { + tSpace = (TargetConnectionSpacingFigureEditPart)targetSpacesList.get(index); + } + parentEditPart = (ModelGroupEditPart)tSpace.getParent(); + targetModelGroup = parentEditPart.getXSDModelGroup(); + break; + } + +// if (location.y < (rectangle.getCenter().y)) +// { +// nextChildRefEditPart = childGraphNodeEditPart; +// TargetConnectionSpacingFigureEditPart previousSpace = null; +// for (Iterator s = targetSpacesList.iterator(); s.hasNext(); ) +// { +// TargetConnectionSpacingFigureEditPart tSpace = (TargetConnectionSpacingFigureEditPart) s.next(); +// Rectangle tRect = tSpace.getFigure().getBounds(); +// if (location.y < (tRect.getCenter().y + tRect.height/2)) +// { +// parentEditPart = (ModelGroupEditPart)tSpace.getParent(); +// targetModelGroup = parentEditPart.getXSDModelGroup(); +// break; +// } +// previousSpace = tSpace; +// } +// if (parentEditPart != null) +// break; +// } + } + else + { + // This is the annotation edit part + } + } + + List editPartsList = request.getEditParts(); + List concreteComponentList = new ArrayList(editPartsList.size()); + for (Iterator i = editPartsList.iterator(); i.hasNext(); ) + { + EditPart editPart = (EditPart)i.next(); + concreteComponentList.add((XSDConcreteComponent) ((XSDBaseAdapter)editPart.getModel()).getTarget()); + } + XSDConcreteComponent previousRefComponent = null, nextRefComponent = null; + if (nextChildRefEditPart != null) + { + if (nextChildRefEditPart.getModel() instanceof XSDBaseAdapter) + { + nextRefComponent = (XSDConcreteComponent)((XSDBaseAdapter)nextChildRefEditPart.getModel()).getTarget(); + } + } + if (previousChildRefEditPart != null) + { + if (previousChildRefEditPart.getModel() instanceof XSDBaseAdapter) + { + previousRefComponent = (XSDConcreteComponent)((XSDBaseAdapter)previousChildRefEditPart.getModel()).getTarget(); + } + } + System.out.println(previousRefComponent + "\n " + nextRefComponent); + action = new MoveAction(targetModelGroup, concreteComponentList, previousRefComponent, nextRefComponent); + canExecute = action.canMove(); + } + } + } + + protected void calculateModelGroupList() + { + EditPart editPart = target; + while (editPart != null) + { + if (editPart instanceof ModelGroupEditPart) + { + ModelGroupEditPart modelGroupEditPart = (ModelGroupEditPart)editPart; + modelGroupsList.addAll(getModelGroupEditParts(modelGroupEditPart)); + } + else if (editPart instanceof ComplexTypeEditPart || + editPart instanceof ModelGroupDefinitionReferenceEditPart) + { + List list = editPart.getChildren(); + for (Iterator i = list.iterator(); i.hasNext(); ) + { + Object child = i.next(); + if (child instanceof CompartmentEditPart) + { + List compartmentList = ((CompartmentEditPart)child).getChildren(); + for (Iterator it = compartmentList.iterator(); it.hasNext(); ) + { + Object obj = it.next(); + if (obj instanceof XSDGroupsForAnnotationEditPart) + { + XSDGroupsForAnnotationEditPart groups = (XSDGroupsForAnnotationEditPart)obj; + List groupList = groups.getChildren(); + for (Iterator iter = groupList.iterator(); iter.hasNext(); ) + { + Object groupChild = iter.next(); + if (groupChild instanceof ModelGroupEditPart) + { + ModelGroupEditPart modelGroupEditPart = (ModelGroupEditPart)groupChild; + modelGroupsList.add(modelGroupEditPart); + modelGroupsList.addAll(getModelGroupEditParts(modelGroupEditPart)); + } + } + } + } + } + } + } + editPart = editPart.getParent(); + } + } + + protected List getModelGroupEditParts(ModelGroupEditPart modelGroupEditPart) + { + List modelGroupList = new ArrayList(); + List list = modelGroupEditPart.getChildren(); + for (Iterator i = list.iterator(); i.hasNext(); ) + { + Object object = i.next(); + if (object instanceof TargetConnectionSpacingFigureEditPart) + { + targetSpacesList.add(object); + } + else if (object instanceof ModelGroupDefinitionReferenceEditPart) + { + ModelGroupDefinitionReferenceEditPart groupRef = (ModelGroupDefinitionReferenceEditPart)object; + List groupRefChildren = groupRef.getChildren(); + for (Iterator it = groupRefChildren.iterator(); it.hasNext(); ) + { + Object o = it.next(); + if (o instanceof ModelGroupEditPart) + { + ModelGroupEditPart aGroup = (ModelGroupEditPart)o; + modelGroupList.add(aGroup); + modelGroupList.addAll(getModelGroupEditParts(aGroup)); + } + } + } + else if (object instanceof ModelGroupEditPart) + { + ModelGroupEditPart aGroup = (ModelGroupEditPart)object; + modelGroupList.add(aGroup); + modelGroupList.addAll(getModelGroupEditParts(aGroup)); + } + } + return modelGroupList; + } + + + public void execute() + { + if (canExecute) + { + action.run(); + } + } + + public void redo() + { + + } + + public void undo() + { + } + + public boolean canExecute() + { + return canExecute; + } + + public PointList getConnectionPoints(Rectangle draggedFigureBounds) + { + PointList pointList = null; + if (parentEditPart != null && nextChildRefEditPart != null) + { + pointList = getConnectionPoints(parentEditPart, nextChildRefEditPart, draggedFigureBounds); + } + return pointList != null ? pointList : new PointList(); + } + + // This method supports the preview connection line function related to drag and drop + // + public PointList getConnectionPoints(ModelGroupEditPart parentEditPart, BaseFieldEditPart childRefEditPart, Rectangle draggedFigureBounds) + { + PointList pointList = new PointList(); + int[] data = new int[1]; + Point a = getConnectionPoint(parentEditPart, childRefEditPart, data); + if (a != null) + { + int draggedFigureBoundsY = draggedFigureBounds.y + draggedFigureBounds.height/2; + + pointList.addPoint(a); + + if (data[0] == 0) // insert between 2 items + { + int x = a.x + (draggedFigureBounds.x - a.x)/2; + pointList.addPoint(new Point(x, a.y)); + pointList.addPoint(new Point(x, draggedFigureBoundsY)); + pointList.addPoint(new Point(draggedFigureBounds.x, draggedFigureBoundsY)); + } + else // insert at first or last position + { + pointList.addPoint(new Point(a.x, draggedFigureBoundsY)); + pointList.addPoint(new Point(draggedFigureBounds.x, draggedFigureBoundsY)); + } + } + return pointList; + } + + // This method supports the preview connection line function related to drag and drop + // + protected Point getConnectionPoint(ModelGroupEditPart parentEditPart, BaseFieldEditPart childRefEditPart, int[] data) + { + Point point = null; + List childList = parentEditPart.getChildren(); + + if (parentEditPart.getFigure() instanceof GenericGroupFigure && childList.size() > 0) + { + point = new Point(); + + Rectangle r = getConnectedEditPartConnectionBounds(parentEditPart); + point.x = r.x + r.width; + point.y = r.y + r.height/2; + } + return point; + } + + protected Rectangle getConnectedEditPartConnectionBounds(ModelGroupEditPart editPart) + { + return ((GenericGroupFigure)editPart.getFigure()).getIconFigure().getBounds(); + } + +}
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropEditPolicy.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropEditPolicy.java new file mode 100644 index 0000000..58d6463 --- /dev/null +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/DragAndDropEditPolicy.java
@@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2001, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.xsd.adt.design.editpolicies; + +import org.eclipse.gef.EditPartViewer; +import org.eclipse.gef.Request; +import org.eclipse.gef.requests.ChangeBoundsRequest; + +public class DragAndDropEditPolicy extends org.eclipse.gef.editpolicies.GraphicalEditPolicy +{ + protected EditPartViewer viewer; + protected SelectionHandlesEditPolicyImpl selectionHandlesEditPolicy; + + public DragAndDropEditPolicy(EditPartViewer viewer, SelectionHandlesEditPolicyImpl selectionHandlesEditPolicy) + { + this.viewer = viewer; + this.selectionHandlesEditPolicy = selectionHandlesEditPolicy; + } + + public boolean understandsRequest(Request req) + { + return true; + } + + + public org.eclipse.gef.commands.Command getCommand(Request request) + { + DragAndDropCommand command = null; + if (request instanceof ChangeBoundsRequest) + { + command = new DragAndDropCommand(viewer, (ChangeBoundsRequest)request); + selectionHandlesEditPolicy.setDragAndDropCommand(command); + } + return command; + } +}
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/GraphNodeDragTracker.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/GraphNodeDragTracker.java new file mode 100644 index 0000000..5fa0988 --- /dev/null +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/GraphNodeDragTracker.java
@@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2001, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.xsd.adt.design.editpolicies; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.Request; +import org.eclipse.gef.commands.Command; +import org.eclipse.gef.tools.DragEditPartsTracker; + + +public class GraphNodeDragTracker extends DragEditPartsTracker +{ + protected EditPart editPart; + + public GraphNodeDragTracker(EditPart editPart) + { + super(editPart); + this.editPart = editPart; + } + + protected Command getCommand() + { + Request request = getTargetRequest(); + return editPart.getCommand(request); + } +}
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/SelectionHandlesEditPolicyImpl.java b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/SelectionHandlesEditPolicyImpl.java new file mode 100644 index 0000000..27932a2 --- /dev/null +++ b/bundles/org.eclipse.wst.xsd.ui/src-adt/org/eclipse/wst/xsd/adt/design/editpolicies/SelectionHandlesEditPolicyImpl.java
@@ -0,0 +1,235 @@ +/******************************************************************************* + * Copyright (c) 2001, 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.wst.xsd.adt.design.editpolicies; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.draw2d.ColorConstants; +import org.eclipse.draw2d.Figure; +import org.eclipse.draw2d.FigureUtilities; +import org.eclipse.draw2d.FreeformLayout; +import org.eclipse.draw2d.Graphics; +import org.eclipse.draw2d.IFigure; +import org.eclipse.draw2d.Polyline; +import org.eclipse.draw2d.RectangleFigure; +import org.eclipse.draw2d.geometry.Dimension; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.draw2d.geometry.PointList; +import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.gef.EditPart; +import org.eclipse.gef.GraphicalEditPart; +import org.eclipse.gef.Request; +import org.eclipse.gef.handles.MoveHandle; +import org.eclipse.gef.handles.MoveHandleLocator; +import org.eclipse.gef.requests.ChangeBoundsRequest; +import org.eclipse.swt.graphics.Cursor; +import org.eclipse.wst.xsd.adt.design.editparts.BaseEditPart; + + +public class SelectionHandlesEditPolicyImpl extends ADTSelectionFeedbackEditPolicy +{ + protected IFigure feedback; + protected Rectangle originalLocation; + protected DragAndDropCommand dragAndDropCommand; + protected Polyline polyLine; + protected RectangleFigure ghostShape; + + protected List createSelectionHandles() + { + List list = new ArrayList(); + EditPart editPart = getHost(); + + if (editPart instanceof GraphicalEditPart) + { + GraphicalEditPart graphicalEditPart = (GraphicalEditPart)editPart; + IFigure figure = (graphicalEditPart instanceof BaseEditPart) ? + ((BaseEditPart)graphicalEditPart).getFigure() : + graphicalEditPart.getFigure(); + + Cursor cursorFigure = figure.getCursor(); + MoveHandleLocator loc = new MoveHandleLocator(figure); + MoveHandle moveHandle = new MoveHandle(graphicalEditPart, loc); + moveHandle.setCursor(cursorFigure); + list.add(moveHandle); + } + + return list; + } + + + public boolean understandsRequest(Request request) + { + boolean result = false; + + if (REQ_MOVE.equals(request.getType())) + { + result = false; // return false to disable move for now + } + else + { + result = super.understandsRequest(request); + } + return result; + } + + + public org.eclipse.gef.commands.Command getCommand(Request request) + { + return null; + } + + public void setDragAndDropCommand(DragAndDropCommand dragAndDropCommand) + { + this.dragAndDropCommand = dragAndDropCommand; + } + + protected org.eclipse.gef.commands.Command getMoveCommand(ChangeBoundsRequest request) + { + ChangeBoundsRequest req = new ChangeBoundsRequest(REQ_MOVE_CHILDREN); + req.setEditParts(getHost()); + + req.setMoveDelta(request.getMoveDelta()); + req.setSizeDelta(request.getSizeDelta()); + req.setLocation(request.getLocation()); + + return getHost().getParent().getCommand(req); + } + + public void showSourceFeedback(Request request) + { + if (REQ_MOVE.equals(request.getType()) || + REQ_ADD.equals(request.getType())) + showChangeBoundsFeedback((ChangeBoundsRequest)request); + } + + protected void showChangeBoundsFeedback(ChangeBoundsRequest request) + { + IFigure p = getDragSourceFeedbackFigure(); + Rectangle r = originalLocation.getTranslated(request.getMoveDelta()); + Dimension resize = request.getSizeDelta(); + r.width += resize.width; + r.height+= resize.height; + + ((GraphicalEditPart)getHost()).getFigure().translateToAbsolute(r); + + p.translateToRelative(r); + Rectangle pBounds = r.getCopy(); + + if (dragAndDropCommand != null && dragAndDropCommand.canExecute()) + { + int size = request.getEditParts().size(); + if (size > 0 && request.getEditParts().get(size - 1) == getHost()) + { + PointList pointList = dragAndDropCommand.getConnectionPoints(r); + if (pointList != null && pointList.size() > 0) + { + polyLine.setPoints(pointList); + + Point firstPoint = pointList.getFirstPoint(); + if (firstPoint != null) + { + pBounds = pBounds.getUnion(new Rectangle(firstPoint.x, firstPoint.y, 1, 1)); + } + } + } + } + p.setBounds(pBounds); + ghostShape.setBounds(r); + p.validate(); + } + + + + + protected IFigure getDragSourceFeedbackFigure() + { + EditPart editPart = getHost(); + if (feedback == null && editPart instanceof BaseEditPart) + { + BaseEditPart baseEditPart = (BaseEditPart)editPart; + originalLocation = new Rectangle(baseEditPart.getFigure().getBounds()); + feedback = createDragSourceFeedbackFigure(baseEditPart.getFigure()); + } + return feedback; + } + + protected IFigure createDragSourceFeedbackFigure(IFigure draggedFigure) + { + // Use a ghost rectangle for feedback + Figure panel = new Figure(); + panel.setLayoutManager(new FreeformLayout()); + + ghostShape = new RectangleFigure(); + FigureUtilities.makeGhostShape(ghostShape); + ghostShape.setLineStyle(Graphics.LINE_DASHDOT); + ghostShape.setForegroundColor(ColorConstants.white); + ghostShape.setOpaque(false); + + Rectangle r = draggedFigure.getBounds(); + panel.setOpaque(false); + panel.add(ghostShape); + + polyLine = new Polyline(); + //polyLine.setLineStyle(Graphics.LINE_DASHDOT); + polyLine.setLineWidth(1); + panel.add(polyLine); + + panel.setBounds(r); + ghostShape.setBounds(r); + + addFeedback(panel); + + return panel; + } + + public void deactivate() + { + if (feedback != null) + { + removeFeedback(feedback); + feedback = null; + } + hideFocus(); + super.deactivate(); + } + + /** + * Erase feedback indicating that the receiver object is + * being dragged. This method is called when a drag is + * completed or cancelled on the receiver object. + * @param dragTracker org.eclipse.gef.tools.DragTracker The drag tracker of the tool performing the drag. + */ + protected void eraseChangeBoundsFeedback(ChangeBoundsRequest request) + { + if (feedback != null) + { + removeFeedback(feedback); + } + feedback = null; + originalLocation = null; + } + + /** + * Erase feedback indicating that the receiver object is + * being dragged. This method is called when a drag is + * completed or cancelled on the receiver object. + * @param dragTracker org.eclipse.gef.tools.DragTracker The drag tracker of the tool performing the drag. + */ + public void eraseSourceFeedback(Request request) + { + if (REQ_MOVE.equals(request.getType()) || REQ_ADD.equals(request.getType())) + { + eraseChangeBoundsFeedback((ChangeBoundsRequest)request); + } + } + +}