[no bug] Add drag and drop support for attributes
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
index 810c329..4c0d467 100644
--- 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
@@ -9,19 +9,18 @@
  *     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.XSDAttributeDeclaration;
 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
 {
@@ -29,99 +28,66 @@
   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;
-	  
+    this.parentModelGroup = parentComponent;
+    this.selectedComponentsList = selectedComponents;
+    this.previousRefComponent = previousRefChildComponent;
+    this.nextRefComponent = nextRefChildComponent;
+
     selectedNodes = new ArrayList(selectedComponents.size());
-    for (Iterator i = selectedComponents.iterator(); i.hasNext(); )
+    for (Iterator i = selectedComponents.iterator(); i.hasNext();)
     {
-      XSDConcreteComponent concreteComponent = (XSDConcreteComponent)i.next();
+      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;
+        doInsertBefore = true;
       }
     }
     if (previousRefComponent != null)
     {
       if (previousRefComponent.getContainer().getContainer() == parentModelGroup)
       {
-    	doInsertBefore = false;
+        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;
-  }
-  
+    boolean result = true;
 
-     
+    if (nextRefComponent instanceof XSDAttributeDeclaration || previousRefComponent instanceof XSDAttributeDeclaration)
+      return false;
+
+    return result;
+  }
+
   /*
    * @see IAction#run()
    */
   public void run()
-  {                            
+  {
     try
     {
-      for (Iterator i = selectedComponentsList.iterator(); i.hasNext(); )
+      for (Iterator i = selectedComponentsList.iterator(); i.hasNext();)
       {
- 	    XSDConcreteComponent concreteComponent = (XSDConcreteComponent)i.next();
- 	    
+        XSDConcreteComponent concreteComponent = (XSDConcreteComponent) i.next();
+
         if (doInsertBefore)
         {
           if (concreteComponent == nextRefComponent)
@@ -132,115 +98,51 @@
           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());
- 	    }
+
+        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/actions/MoveAttributeAction.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/actions/MoveAttributeAction.java
new file mode 100644
index 0000000..9742f75
--- /dev/null
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/actions/MoveAttributeAction.java
@@ -0,0 +1,245 @@
+/*******************************************************************************
+ * 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.xsd.XSDAttributeDeclaration;
+import org.eclipse.xsd.XSDAttributeGroupContent;
+import org.eclipse.xsd.XSDAttributeGroupDefinition;
+import org.eclipse.xsd.XSDAttributeUse;
+import org.eclipse.xsd.XSDComplexTypeDefinition;
+import org.eclipse.xsd.XSDConcreteComponent;
+import org.eclipse.xsd.XSDElementDeclaration;
+import org.w3c.dom.Node;
+
+// TODO COMMON AND CLEAN UP THIS CODE
+public class MoveAttributeAction extends Action
+{
+
+  protected List selectedNodes;
+  protected Node parentNode;
+  protected Node previousRefChild, nextRefChild;
+  boolean doInsertBefore;
+
+  List selectedComponentsList;
+  XSDConcreteComponent parentComponent;
+  XSDConcreteComponent previousRefComponent, nextRefComponent;
+
+  public MoveAttributeAction(XSDConcreteComponent parentComponent, List selectedComponents, XSDConcreteComponent previousRefChildComponent, XSDConcreteComponent nextRefChildComponent)
+  {
+    this.parentComponent = 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() == parentComponent)
+      {
+        doInsertBefore = true;
+      }
+    }
+    if (previousRefComponent != null)
+    {
+      if (previousRefComponent.getContainer().getContainer() == parentComponent)
+      {
+        doInsertBefore = false;
+      }
+    }
+
+  }
+
+  public boolean canMove()
+  {
+    boolean result = true;
+
+    if (nextRefComponent instanceof XSDElementDeclaration || previousRefComponent instanceof XSDElementDeclaration)
+      return false;
+
+    return result;
+  }
+
+  /*
+   * @see IAction#run()
+   */
+  public void run()
+  {
+    if (parentComponent instanceof XSDAttributeGroupDefinition)
+    {
+      moveUnderXSDAttributeGroupDefinition((XSDAttributeGroupDefinition) parentComponent);
+    }
+    else if (parentComponent instanceof XSDComplexTypeDefinition)
+    {
+      moveUnderXSDComplexTypeDefinition((XSDComplexTypeDefinition) parentComponent);
+    }
+  }
+
+  protected void moveUnderXSDAttributeGroupDefinition(XSDAttributeGroupDefinition parentModelGroup)
+  {
+    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 iterator = parentModelGroup.getContents().iterator(); iterator.hasNext();)
+        {
+          XSDAttributeGroupContent attributeGroupContent = (XSDAttributeGroupContent) iterator.next();
+          if (attributeGroupContent instanceof XSDAttributeUse)
+          {
+            XSDAttributeDeclaration attribute = ((XSDAttributeUse) attributeGroupContent).getContent();
+            if (attribute == concreteComponent)
+            {
+              parentModelGroup.getContents().remove(attribute.getContainer());
+              break;
+            }
+          }
+        }
+        int index = 0;
+        List attributeGroupContents = parentModelGroup.getContents();
+        for (Iterator iterator = attributeGroupContents.iterator(); iterator.hasNext();)
+        {
+          XSDAttributeGroupContent attributeGroupContent = (XSDAttributeGroupContent) iterator.next();
+          if (attributeGroupContent instanceof XSDAttributeUse)
+          {
+            XSDAttributeDeclaration attribute = ((XSDAttributeUse) attributeGroupContent).getContent();
+            if (doInsertBefore)
+            {
+              if (attribute == nextRefComponent)
+              {
+                parentModelGroup.getContents().add(index, (XSDAttributeUse) concreteComponent.getContainer());
+                break;
+              }
+            }
+            else
+            {
+              if (attribute == previousRefComponent)
+              {
+                parentModelGroup.getContents().add(index + 1, (XSDAttributeUse) concreteComponent.getContainer());
+                break;
+              }
+            }
+          }
+          index++;
+        }
+        if (attributeGroupContents.size() == 0)
+        {
+          parentModelGroup.getContents().add((XSDAttributeUse) concreteComponent.getContainer());
+        }
+
+      }
+    }
+    catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+  }
+
+  protected void moveUnderXSDComplexTypeDefinition(XSDComplexTypeDefinition complexType)
+  {
+    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 iterator = complexType.getAttributeContents().iterator(); iterator.hasNext();)
+        {
+          XSDAttributeGroupContent attributeGroupContent = (XSDAttributeGroupContent) iterator.next();
+          if (attributeGroupContent instanceof XSDAttributeUse)
+          {
+            XSDAttributeDeclaration attribute = ((XSDAttributeUse) attributeGroupContent).getContent();
+            if (attribute == concreteComponent)
+            {
+              complexType.getAttributeContents().remove(attribute.getContainer());
+              break;
+            }
+          }
+        }
+        int index = 0;
+        List attributeGroupContents = complexType.getAttributeContents();
+        for (Iterator iterator = attributeGroupContents.iterator(); iterator.hasNext();)
+        {
+          XSDAttributeGroupContent attributeGroupContent = (XSDAttributeGroupContent) iterator.next();
+          if (attributeGroupContent instanceof XSDAttributeUse)
+          {
+            XSDAttributeDeclaration attribute = ((XSDAttributeUse) attributeGroupContent).getContent();
+            if (doInsertBefore)
+            {
+              if (attribute == nextRefComponent)
+              {
+                complexType.getAttributeContents().add(index, (XSDAttributeUse) concreteComponent.getContainer());
+                break;
+              }
+            }
+            else
+            {
+              if (attribute == previousRefComponent)
+              {
+                complexType.getAttributeContents().add(index + 1, (XSDAttributeUse) concreteComponent.getContainer());
+                break;
+              }
+            }
+          }
+          index++;
+        }
+        if (attributeGroupContents.size() == 0)
+        {
+          complexType.getAttributeContents().add((XSDAttributeUse) concreteComponent.getContainer());
+        }
+
+      }
+    }
+    catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+
+  }
+}
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/commands/DragAndDropCommand.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/commands/DragAndDropCommand.java
index 9ae66fc..c7cb86b 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/commands/DragAndDropCommand.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/editor/internal/commands/DragAndDropCommand.java
@@ -25,38 +25,59 @@
 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.adt.design.editparts.model.Compartment;
 import org.eclipse.wst.xsd.editor.internal.actions.MoveAction;
+import org.eclipse.wst.xsd.editor.internal.actions.MoveAttributeAction;
 import org.eclipse.wst.xsd.editor.internal.adapters.XSDBaseAdapter;
+import org.eclipse.wst.xsd.editor.internal.adapters.XSDBaseAttributeAdapter;
+import org.eclipse.wst.xsd.editor.internal.adapters.XSDElementDeclarationAdapter;
+import org.eclipse.wst.xsd.editor.internal.design.editparts.AttributeGroupDefinitionEditPart;
+import org.eclipse.wst.xsd.editor.internal.design.editparts.ConnectableEditPart;
 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.XSDAttributesForAnnotationEditPart;
+import org.eclipse.wst.xsd.editor.internal.design.editparts.XSDBaseFieldEditPart;
 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.XSDAttributeGroupDefinition;
 import org.eclipse.xsd.XSDConcreteComponent;
 import org.eclipse.xsd.XSDModelGroup;
 
-
+// TODO : clean and common up code
 public class DragAndDropCommand extends BaseCommand
 { 
   protected EditPartViewer viewer;    
   protected ChangeBoundsRequest request;
   protected BaseFieldEditPart previousChildRefEditPart, nextChildRefEditPart;    
-  public ModelGroupEditPart parentEditPart; 
+  public ModelGroupEditPart parentEditPart;
+  protected AttributeGroupDefinitionEditPart parentAttributeGroupEditPart;
+  protected XSDConcreteComponent parentComponent;
   public Point location;
-  protected MoveAction action;    
-  protected boolean canExecute;
+  protected MoveAction action;
+  protected MoveAttributeAction moveAttributeAction;
+  protected boolean canExecute, isElementToDrag;
   EditPart target;
+  XSDBaseFieldEditPart selected;
   List modelGroupsList = new ArrayList();
   List targetSpacesList = new ArrayList();
+  List attributeGroupsList = new ArrayList();
+  XSDConcreteComponent previousRefComponent = null, nextRefComponent = null;
+  ComplexTypeEditPart complexTypeEditPart;
 
   public DragAndDropCommand(EditPartViewer viewer, ChangeBoundsRequest request)
   {
     this.viewer = viewer;                    
     this.request = request;
-
+    setup();
+  }
+  
+  protected void setup()
+  {
     location = request.getLocation();
-    target = viewer.findObjectAt(location); 
+    target = viewer.findObjectAt(location);
+    
     if (viewer instanceof ScrollingGraphicalViewer)
     {  
       ScrollingGraphicalViewer sgv = (ScrollingGraphicalViewer)viewer;
@@ -64,112 +85,224 @@
       location.y += p.y;
       location.x += p.x;
     }
-     
     List list = request.getEditParts();
-    if (list.size() > 0 && target instanceof BaseFieldEditPart)
+    canExecute = false;
+    // allow drag and drop of only one selected object
+    if (list.size() == 1 && 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());
+      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());
       }
-      
-      EditPart compartment = baseFieldEditPart.getParent();
-      parentEditPart = null;
-      if (compartment != null)
+
+      Object itemToDrag = list.get(0);
+      if (itemToDrag instanceof XSDBaseFieldEditPart)
       {
-        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)
+    	  selected = (XSDBaseFieldEditPart) itemToDrag;
+    	  if (selected.getModel() instanceof XSDElementDeclarationAdapter)
+    	  {
+    		  isElementToDrag = true;
+    	  }
+    	  else if (selected.getModel() instanceof XSDBaseAttributeAdapter)
+    	  {
+    		  isElementToDrag = false;
+    	  }
+    	  else
+    	  {
+    		  return;
+    	  }
+
+    	  if (!isElementToDrag)
+    	  {
+    		  XSDAttributeGroupDefinition attributeGroup = null;
+    		  attributeGroupsList.clear();
+    		  targetSpacesList.clear();
+          parentAttributeGroupEditPart = null;
+          calculateAttributeGroupList();
+          EditPart compartment = target.getParent();
+          System.out.println(((Compartment)compartment.getModel()).getKind());
+          parentEditPart = null;
+          if (compartment != null)
           {
-            index ++;
-            previousChildRefEditPart = childGraphNodeEditPart;
-            childGraphNodeEditPart = (BaseFieldEditPart)child;
-            if (previousChildRefEditPart != 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(); )
             {
-              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))
+              EditPart child = (EditPart)i.next();
+              if (child instanceof BaseFieldEditPart)
               {
-            	  tSpace = (TargetConnectionSpacingFigureEditPart)targetSpacesList.get(index);
+                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);
+                  }
+                  if (tSpace.getParent() instanceof AttributeGroupDefinitionEditPart)
+                  {
+                    parentAttributeGroupEditPart = (AttributeGroupDefinitionEditPart)tSpace.getParent();
+                    attributeGroup = parentAttributeGroupEditPart.getXSDAttributeGroupDefinition().getResolvedAttributeGroupDefinition();
+                    parentComponent = attributeGroup;
+                  }
+                  else if (tSpace.getParent() instanceof XSDAttributesForAnnotationEditPart)
+                  {
+                    parentComponent = (XSDConcreteComponent)((XSDBaseAdapter)complexTypeEditPart.getModel()).getTarget(); 
+                  }
+                  break;
+                }            
               }
-              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
+              }
+            }  
           }
-          else
+          calculatePreviousAndNextEditParts();
+          moveAttributeAction = new MoveAttributeAction(parentComponent, concreteComponentList, previousRefComponent, nextRefComponent);
+          canExecute = moveAttributeAction.canMove();
+    	  }
+    	  else if (isElementToDrag)
+    	  {
+    		  XSDModelGroup targetModelGroup = null;
+    		  modelGroupsList.clear();
+    		  targetSpacesList.clear();
+    		  calculateModelGroupList();
+
+          List modelGroups = new ArrayList(modelGroupsList.size());
+          for (Iterator i = modelGroupsList.iterator(); i.hasNext(); )
           {
-        	  // This is the annotation edit part
+            ModelGroupEditPart editPart = (ModelGroupEditPart)i.next();
+            modelGroups.add(editPart.getXSDModelGroup());
           }
-        } 
-           
-        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)
+        
+          EditPart compartment = target.getParent();
+          parentEditPart = null;
+          if (compartment != null)
           {
-            nextRefComponent = (XSDConcreteComponent)((XSDBaseAdapter)nextChildRefEditPart.getModel()).getTarget();
+            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;
+                }            
+              }
+              else
+              {
+           	  // This is the annotation edit part
+              }
+            }  
           }
+          calculatePreviousAndNextEditParts();
+          action = new MoveAction(targetModelGroup, concreteComponentList, previousRefComponent, nextRefComponent);
+          canExecute = action.canMove();
         }
-        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 calculatePreviousAndNextEditParts()
+  {
+    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();
+      }
+    }
+
+  }
+  
+  protected void calculateAttributeGroupList()
+  {
+    EditPart editPart = target;
+    while (editPart != null)
+    {                     
+      if (editPart instanceof ComplexTypeEditPart)
+      {
+        complexTypeEditPart = (ComplexTypeEditPart)editPart;
+        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 XSDAttributesForAnnotationEditPart)
+              {
+                XSDAttributesForAnnotationEditPart groups = (XSDAttributesForAnnotationEditPart)obj;
+                List groupList = groups.getChildren();
+                for (Iterator iter = groupList.iterator(); iter.hasNext(); )
+                {
+                  Object groupChild = iter.next();
+                  if (groupChild instanceof TargetConnectionSpacingFigureEditPart)
+                  {
+                    targetSpacesList.add(groupChild);
+                  }
+                  else if (groupChild instanceof AttributeGroupDefinitionEditPart)
+                  {
+                    AttributeGroupDefinitionEditPart attributeGroupEditPart = (AttributeGroupDefinitionEditPart)groupChild;
+                    attributeGroupsList.add(attributeGroupEditPart);
+                    attributeGroupsList.addAll(getAttributeGroupEditParts(attributeGroupEditPart));
+                  }
+                }
+              }
+            }
+          }
+        }   
+      }
+      editPart = editPart.getParent();
+    }
+   
+  }
              
   protected void calculateModelGroupList()
   {
@@ -217,9 +350,44 @@
     }
   }
   
+  protected List getAttributeGroupEditParts(AttributeGroupDefinitionEditPart attributeGroupEditPart)
+  {
+    List groupList = new ArrayList();
+    List list = attributeGroupEditPart.getChildren();
+    for (Iterator i = list.iterator(); i.hasNext(); )
+    {
+      Object object = i.next();
+      if (object instanceof TargetConnectionSpacingFigureEditPart)
+      {
+        targetSpacesList.add(object);
+      }
+      else if (object instanceof AttributeGroupDefinitionEditPart)
+      {
+        AttributeGroupDefinitionEditPart groupRef = (AttributeGroupDefinitionEditPart)object;
+        List groupRefChildren = groupRef.getChildren();
+        for (Iterator it = groupRefChildren.iterator(); it.hasNext(); )
+        {
+         Object o = it.next();
+         if (o instanceof TargetConnectionSpacingFigureEditPart)
+         {
+           targetSpacesList.add(o);
+         }
+         else if (o instanceof AttributeGroupDefinitionEditPart)
+         {
+           AttributeGroupDefinitionEditPart aGroup = (AttributeGroupDefinitionEditPart)o;
+           groupList.add(aGroup);
+           groupList.addAll(getAttributeGroupEditParts(aGroup));
+         }
+        }
+      }
+    }   
+    return groupList;   
+  }
+  
+  
   protected List getModelGroupEditParts(ModelGroupEditPart modelGroupEditPart)
   {
-	List modelGroupList = new ArrayList();
+	  List modelGroupList = new ArrayList();
     List list = modelGroupEditPart.getChildren();
     for (Iterator i = list.iterator(); i.hasNext(); )
     {
@@ -255,10 +423,13 @@
   
 
   public void execute()
-  {           
+  {
     if (canExecute)
     { 
-      action.run(); 
+      if (isElementToDrag)
+        action.run();
+      else
+        moveAttributeAction.run();
     }
   }     
   
@@ -278,17 +449,27 @@
 
   public PointList getConnectionPoints(Rectangle draggedFigureBounds)
   {             
-    PointList pointList = null;      
-    if (parentEditPart != null && nextChildRefEditPart != null)
-    {                                
-      pointList = getConnectionPoints(parentEditPart, nextChildRefEditPart, draggedFigureBounds);      
-    }               
+    PointList pointList = null;
+    if (isElementToDrag)
+    {
+      if (parentEditPart != null && nextChildRefEditPart != null)
+      {                                
+        pointList = getConnectionPoints(parentEditPart, nextChildRefEditPart, draggedFigureBounds);      
+      }
+    }
+    else
+    {
+      if (parentAttributeGroupEditPart!= null && nextChildRefEditPart != null)
+      {
+        pointList = getConnectionPoints(parentAttributeGroupEditPart, 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)
+  public PointList getConnectionPoints(ConnectableEditPart parentEditPart, BaseFieldEditPart childRefEditPart, Rectangle draggedFigureBounds)
   {           
     PointList pointList = new PointList();                         
     int[] data = new int[1];
@@ -317,7 +498,7 @@
 
   // This method supports the preview connection line function related to drag and drop
   //     
-  protected Point getConnectionPoint(ModelGroupEditPart parentEditPart, BaseFieldEditPart childRefEditPart, int[] data)
+  protected Point getConnectionPoint(ConnectableEditPart parentEditPart, BaseFieldEditPart childRefEditPart, int[] data)
   {                      
     Point point = null;     
     List childList = parentEditPart.getChildren();         
@@ -333,7 +514,7 @@
     return point;
   }
 
-  protected Rectangle getConnectedEditPartConnectionBounds(ModelGroupEditPart editPart)
+  protected Rectangle getConnectedEditPartConnectionBounds(ConnectableEditPart editPart)
   {
 	return ((GenericGroupFigure)editPart.getFigure()).getIconFigure().getBounds();
   }