[251008] [Graphical Editor] Provide better control over the component's insertion point
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDAttributeDeclarationAdapter.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDAttributeDeclarationAdapter.java
index 59fc8e1..0e666c3 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDAttributeDeclarationAdapter.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDAttributeDeclarationAdapter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2007 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -10,13 +10,20 @@
  *******************************************************************************/
 package org.eclipse.wst.xsd.ui.internal.adapters;
 
-import org.eclipse.xsd.XSDAttributeDeclaration;
-import org.eclipse.xsd.XSDTypeDefinition;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.wst.xsd.ui.internal.adt.actions.BaseSelectionAction;
 import org.eclipse.wst.xsd.ui.internal.adt.design.editparts.model.IActionProvider;
 import org.eclipse.wst.xsd.ui.internal.adt.facade.IADTObject;
 import org.eclipse.wst.xsd.ui.internal.adt.facade.IModel;
+import org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction;
+import org.eclipse.wst.xsd.ui.internal.editor.Messages;
+import org.eclipse.xsd.XSDAttributeDeclaration;
 import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.XSDTypeDefinition;
 
 public class XSDAttributeDeclarationAdapter extends XSDBaseAttributeAdapter implements IActionProvider
 {
@@ -46,6 +53,38 @@
     return (IModel)adapter;
   }
 
+  // https://bugs.eclipse.org/bugs/show_bug.cgi?id=251008
+  public String[] getActions(Object object)
+  {    
+    if(!isGlobal())
+    {    	    	
+    	List defaultActions = new ArrayList();
+    	defaultActions = Arrays.asList(super.getActions(object));
+    	
+    	ArrayList list = new ArrayList();
+    	list.add(BaseSelectionAction.SUBMENU_START_ID + Messages._UI_ACTION_ADD_ATTRIBUTE);
+    	list.add(AddXSDAttributeDeclarationAction.BEFORE_SELECTED_ID);
+    	list.add(AddXSDAttributeDeclarationAction.AFTER_SELECTED_ID);
+    	list.add(BaseSelectionAction.SUBMENU_END_ID);
+    	
+    	int len = defaultActions.size();
+    	for(int i = 0; i < len; i++)
+    	{
+    		if (defaultActions.get(i).equals(AddXSDAttributeDeclarationAction.ID))
+    		{
+    		  continue;
+    		}
+    		list.add(defaultActions.get(i));
+    	}
+      return (String [])list.toArray(new String[0]);
+    }
+    else
+    {
+    	return super.getActions(object);
+    }    
+  }  
+  
+  
   public boolean isFocusAllowed()
   {
     return isGlobal();
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDElementDeclarationAdapter.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDElementDeclarationAdapter.java
index 643f97d..e9d92fa 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDElementDeclarationAdapter.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/adapters/XSDElementDeclarationAdapter.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2007 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -271,8 +271,14 @@
   public String[] getActions(Object object)
   {
     List list = new ArrayList();
+
     if (!isGlobal())
-      list.add(AddXSDElementAction.ID);
+    {
+      list.add(BaseSelectionAction.SUBMENU_START_ID + Messages._UI_ACTION_ADD_ELEMENT);
+      list.add(AddXSDElementAction.BEFORE_SELECTED_ID);
+      list.add(AddXSDElementAction.AFTER_SELECTED_ID);
+      list.add(BaseSelectionAction.SUBMENU_END_ID);
+    }
 
     list.add(BaseSelectionAction.SUBMENU_START_ID + Messages._UI_ACTION_SET_TYPE);
     list.add(SetTypeAction.SET_NEW_TYPE_ID);
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
index d5037a3..1a9210a 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/InternalXSDMultiPageEditor.java
@@ -628,6 +628,16 @@
     action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDElement.gif"));
     registry.registerAction(action);
 
+    action = new AddXSDElementAction(this, AddXSDElementAction.BEFORE_SELECTED_ID, Messages._UI_ACTION_ADD_ELEMENT_BEFORE, false);
+    action.setSelectionProvider(getSelectionManager());
+    action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDElement.gif"));
+    registry.registerAction(action);       
+    
+    action = new AddXSDElementAction(this, AddXSDElementAction.AFTER_SELECTED_ID, Messages._UI_ACTION_ADD_ELEMENT_AFTER, false);
+    action.setSelectionProvider(getSelectionManager());
+    action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDElement.gif"));
+    registry.registerAction(action);      
+    
     action = new AddXSDElementAction(this, AddXSDElementAction.REF_ID, Messages._UI_ACTION_ADD_ELEMENT_REF, true);
     action.setSelectionProvider(getSelectionManager());
     action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDElementRef.gif"));
@@ -675,6 +685,16 @@
     action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDAttribute.gif"));
     registry.registerAction(action);
 
+    action = new AddXSDAttributeDeclarationAction(this, AddXSDAttributeDeclarationAction.BEFORE_SELECTED_ID, Messages._UI_ACTION_ADD_ATTRIBUTE_BEFORE, false);
+    action.setSelectionProvider(getSelectionManager());
+    action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDAttribute.gif"));
+    registry.registerAction(action);
+    
+    action = new AddXSDAttributeDeclarationAction(this, AddXSDAttributeDeclarationAction.AFTER_SELECTED_ID, Messages._UI_ACTION_ADD_ATTRIBUTE_AFTER, false);
+    action.setSelectionProvider(getSelectionManager());
+    action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDAttribute.gif"));
+    registry.registerAction(action);
+    
     action = new AddXSDAttributeDeclarationAction(this, AddXSDAttributeDeclarationAction.REF_ID, Messages._UI_ACTION_ADD_ATTRIBUTE_REF, true);
     action.setSelectionProvider(getSelectionManager());
     action.setImageDescriptor(ImageDescriptor.createFromFile(XSDEditorPlugin.class, "icons/XSDAttributeRef.gif"));
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/Messages.java b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/Messages.java
index fce2d29..dc09cea 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/Messages.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/Messages.java
@@ -80,6 +80,8 @@
   public static String _UI_MENU_XSD_EDITOR;
   public static String _UI_LABEL_SOURCE;
   public static String _UI_ACTION_ADD_ELEMENT;
+  public static String _UI_ACTION_ADD_ELEMENT_BEFORE;
+  public static String _UI_ACTION_ADD_ELEMENT_AFTER;
   public static String _UI_ACTION_ADD_ELEMENT_REF;
   public static String _UI_ACTION_NEW;
   public static String _UI_ACTION_BROWSE;
@@ -112,6 +114,8 @@
   public static String _UI_ACTION_ADD_IMPORT;
   public static String _UI_ACTION_ADD_REDEFINE;
   public static String _UI_ACTION_ADD_ATTRIBUTE;
+  public static String _UI_ACTION_ADD_ATTRIBUTE_BEFORE;
+  public static String _UI_ACTION_ADD_ATTRIBUTE_AFTER;
   public static String _UI_ACTION_ADD_ATTRIBUTE_REF;
 
   public static String _UI_LABEL_ELEMENTFORMDEFAULT;
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/messages.properties b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/messages.properties
index de4d1e5..e28dd1d 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/messages.properties
+++ b/bundles/org.eclipse.wst.xsd.ui/src-adt-xsd/org/eclipse/wst/xsd/ui/internal/editor/messages.properties
@@ -78,6 +78,8 @@
 _UI_ACTION_UPDATE_ATTRIBUTE_REFERENCE=Update Attribute reference
 _UI_ACTION_ADD_FIELD=Add Field
 _UI_ACTION_ADD_ELEMENT=Add Element
+_UI_ACTION_ADD_ELEMENT_BEFORE=Add Element Before
+_UI_ACTION_ADD_ELEMENT_AFTER=Add Element After
 _UI_ACTION_ADD_INCLUDE=Add Include
 _UI_ACTION_ADD_IMPORT=Add Import
 _UI_ACTION_ADD_REDEFINE=Add Redefine
@@ -86,6 +88,8 @@
 _UI_ACTION_ADD_SIMPLE_TYPE=Add Simple Type
 _UI_ACTION_SET_TYPE=Set Type
 _UI_ACTION_ADD_ATTRIBUTE=Add Attribute
+_UI_ACTION_ADD_ATTRIBUTE_BEFORE=Add Attribute Before
+_UI_ACTION_ADD_ATTRIBUTE_AFTER=Add Attribute After
 _UI_ACTION_ADD_ATTRIBUTE_REF=Add Attribute Ref
 _UI_ACTION_NEW=New...
 _UI_ACTION_BROWSE=Browse...
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDAttributeDeclarationAction.java b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDAttributeDeclarationAction.java
index c55b4b5..7c113fe 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDAttributeDeclarationAction.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDAttributeDeclarationAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2007 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -26,7 +26,9 @@
 
 public class AddXSDAttributeDeclarationAction extends XSDBaseAction
 {
-  public static String ID = "AddXSDAttributeAction"; //$NON-NLS-1$
+  public static String ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction.AddXSDAttributeAction"; //$NON-NLS-1$
+  public static String BEFORE_SELECTED_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction.BEFORE_SELECTED_ID"; //$NON-NLS-1$  
+  public static String AFTER_SELECTED_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction.AFTER_SELECTED_ID"; //$NON-NLS-1$  
   public static String REF_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeReferenceAction"; //$NON-NLS-1$
   boolean isReference = false;
   
@@ -71,6 +73,7 @@
       XSDAttributeUse xsdAttributeUse = (XSDAttributeUse) selection;
       XSDConcreteComponent parent = null;
       XSDComplexTypeDefinition ct = null;
+      XSDAttributeGroupDefinition group = null;
       for (parent = xsdAttributeUse.getContainer(); parent != null;)
       {
         if (parent instanceof XSDComplexTypeDefinition)
@@ -78,11 +81,26 @@
           ct = (XSDComplexTypeDefinition) parent;
           break;
         }
+        else if (parent instanceof XSDAttributeGroupDefinition)
+        {
+          group = (XSDAttributeGroupDefinition)parent;
+          break;
+        }
         parent = parent.getContainer();
       }
       if (ct != null)
       {
-        command = new AddXSDAttributeDeclarationCommand(Messages._UI_ACTION_ADD_ATTRIBUTE, ct);
+        XSDAttributeUse sel = (XSDAttributeUse) selection;
+        int index = ct.getAttributeContents().indexOf(sel);
+        command = new AddXSDAttributeDeclarationCommand(Messages._UI_ACTION_ADD_ATTRIBUTE, ct, getId(), index);
+        command.setReference(isReference);
+        getCommandStack().execute(command);
+      }
+      else if (group != null)
+      {
+        XSDAttributeUse sel = (XSDAttributeUse) selection;
+        int index = group.eContents().indexOf(sel);
+        command = new AddXSDAttributeDeclarationCommand(Messages._UI_ACTION_ADD_ATTRIBUTE, group, getId(), index);
         command.setReference(isReference);
         getCommandStack().execute(command);
       }
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDElementAction.java b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDElementAction.java
index edc35b0..68bd64a 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDElementAction.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/actions/AddXSDElementAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2007 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -23,12 +23,15 @@
 import org.eclipse.xsd.XSDModelGroup;
 import org.eclipse.xsd.XSDModelGroupDefinition;
 import org.eclipse.xsd.XSDSchema;
+import org.eclipse.xsd.impl.XSDModelGroupImpl;
 
 //revisit this and see if we can reuse AddFieldAction??
 
 public class AddXSDElementAction extends XSDBaseAction
 {
   public static String ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementAction"; //$NON-NLS-1$
+  public static String BEFORE_SELECTED_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementAction.BEFORE_SELECTED_ID"; //$NON-NLS-1$
+  public static String AFTER_SELECTED_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementAction.AFTER_SELECTED_ID"; //$NON-NLS-1$
   public static String REF_ID = "org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementReferenceAction"; //$NON-NLS-1$
   boolean isReference;
   
@@ -99,6 +102,8 @@
       XSDConcreteComponent parent = null;
       XSDComplexTypeDefinition ct = null;
       XSDModelGroupDefinition group = null;
+      XSDModelGroupImpl ctGroup = null;
+
       for (parent = xsdConcreteComponent.getContainer(); parent != null; )
       {
         if (parent instanceof XSDComplexTypeDefinition)
@@ -111,6 +116,11 @@
           group = (XSDModelGroupDefinition)parent;
           break;
         }
+        else if (parent instanceof XSDModelGroupImpl)
+        {
+          ctGroup = (XSDModelGroupImpl) parent;
+          break;
+        }
         parent = parent.getContainer();
       }
       if (ct != null)
@@ -119,7 +129,15 @@
         command.setReference(isReference);
         getCommandStack().execute(command);
       }
-      if (group != null)
+      else if (ctGroup != null)
+      {
+        XSDElementDeclaration sel = (XSDElementDeclaration) selection;
+        int index = ctGroup.getContents().indexOf(sel.eContainer());
+        command = new AddXSDElementCommand(getText(), ctGroup, getId(), index);
+        command.setReference(isReference);
+        getCommandStack().execute(command);
+      }
+      else if (group != null)
       {
         command = new AddXSDElementCommand(getText(), group);
         command.setReference(isReference);
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDAttributeDeclarationCommand.java b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDAttributeDeclarationCommand.java
index cf6a7c6..00df7e0 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDAttributeDeclarationCommand.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDAttributeDeclarationCommand.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2007 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -33,7 +33,12 @@
   XSDConcreteComponent parent;
   boolean isReference;
   private String nameToAdd;
-
+  // The index of the currently selected item.  If no item is selected, index will be less than 0
+  private int index = -1;
+  // Determines where the attribute should be inserted based on the currently selected attribute.  If no
+  // attribute is selected, use the default behaviour of appending the attribute to the end
+  private String addAttributeLocation;  
+  
   public AddXSDAttributeDeclarationCommand(String label, XSDComplexTypeDefinition xsdComplexTypeDefinition)
   {
     super(label);
@@ -45,7 +50,42 @@
     super(label);
     this.parent = parent;
   }
+  
+  public AddXSDAttributeDeclarationCommand(String label, XSDConcreteComponent parent, String addAttributeLocation, int index)
+  {
+    super(label);
+    if (parent instanceof XSDComplexTypeDefinition)
+    {
+      this.xsdComplexTypeDefinition = (XSDComplexTypeDefinition) parent;
+    }
+    else
+    {
+      this.parent = parent;
+    }
+    this.addAttributeLocation = addAttributeLocation;
+    this.index = index;
+  }
+  
+  protected int getInsertionIndex()
+  {
+    if (index < 0)
+      return -1;
 
+    if (addAttributeLocation.equals(org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction.BEFORE_SELECTED_ID))
+    {
+      return index;
+    }
+    else if (addAttributeLocation.equals(org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDAttributeDeclarationAction.AFTER_SELECTED_ID))
+    {
+      index++;
+      return index;
+    }
+    else
+    {
+      return -1;
+    }
+  }
+  
   public void execute()
   {
     XSDAttributeDeclaration attribute = XSDFactory.eINSTANCE.createXSDAttributeDeclaration();
@@ -69,7 +109,15 @@
 
         if (xsdComplexTypeDefinition.getAttributeContents() != null)
         {
-          xsdComplexTypeDefinition.getAttributeContents().add(attributeUse);
+          index = getInsertionIndex();
+          if (index >= 0 && index < xsdComplexTypeDefinition.getAttributeContents().size())
+          {
+            xsdComplexTypeDefinition.getAttributeContents().add(index, attributeUse);
+          }
+          else
+          {
+            xsdComplexTypeDefinition.getAttributeContents().add(attributeUse);
+          }
           formatChild(xsdComplexTypeDefinition.getElement());
         }
       }
@@ -105,7 +153,15 @@
           attributeUse.setAttributeDeclaration(attribute);
           attributeUse.setContent(attribute);
 
-          ((XSDAttributeGroupDefinition) parent).getResolvedAttributeGroupDefinition().getContents().add(attributeUse);
+          index = getInsertionIndex();
+          if (index >= 0 && index < (((XSDAttributeGroupDefinition) parent).getResolvedAttributeGroupDefinition().getContents().size()))
+          {
+            ((XSDAttributeGroupDefinition) parent).getResolvedAttributeGroupDefinition().getContents().add(index, attributeUse);
+          }
+          else
+          {
+            ((XSDAttributeGroupDefinition) parent).getResolvedAttributeGroupDefinition().getContents().add(attributeUse);
+          }
           formatChild(parent.getElement());
         }
       }
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDElementCommand.java b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDElementCommand.java
index 448f738..2e5ef81 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDElementCommand.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/AddXSDElementCommand.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2006 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 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
@@ -36,7 +36,12 @@
   XSDSchema xsdSchema;
   boolean isReference;
   private String nameToAdd;
-
+  // The index of the currently selected item.  If no item is selected, index will be less than 0
+  private int index = -1;  
+  // Determines where the element should be inserted based on the currently selected element.  If no
+  // element is selected, use the default behaviour of appending the element to the end
+  private String addElementLocation; 
+  
   public AddXSDElementCommand()
   {
     super();
@@ -68,6 +73,14 @@
     super(label);
     this.xsdModelGroup = xsdModelGroup;
   }
+  
+  public AddXSDElementCommand(String label, XSDModelGroup xsdModelGroup, String ID, int index)
+  {
+    super(label);
+    this.xsdModelGroup = xsdModelGroup;
+    this.index = index;
+    this.addElementLocation = ID;
+  }
 
   public AddXSDElementCommand(String label, XSDSchema xsdSchema)
   {
@@ -80,8 +93,34 @@
     this.isReference = isReference;
   }
   
-  public void setNameToAdd(String name){
-	  nameToAdd = name;
+  public void setNameToAdd(String name)
+  {
+    nameToAdd = name;
+  }
+  
+  public void setAddElementLocation(String LocationType)
+  {
+    addElementLocation = LocationType;
+  }
+  
+  protected int getInsertionIndex()
+  {
+    if (index < 0)
+      return -1;
+
+    if (addElementLocation.equals(org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementAction.BEFORE_SELECTED_ID))
+    {
+      return index;
+    }
+    else if (addElementLocation.equals(org.eclipse.wst.xsd.ui.internal.common.actions.AddXSDElementAction.AFTER_SELECTED_ID))
+    {
+      index++;
+      return index;
+    }
+    else
+    {
+      return -1;
+    }
   }
   
   /*
@@ -131,7 +170,15 @@
         beginRecording(xsdSchema.getElement());
         if (!isReference)
         {
-          xsdModelGroup.getContents().add(createXSDElementDeclaration());
+        	index = getInsertionIndex();
+        	if(index >= 0 && index < xsdModelGroup.getContents().size())
+        	{        		
+        		xsdModelGroup.getContents().add(index,createXSDElementDeclaration());
+        	}
+        	else
+        	{
+        		xsdModelGroup.getContents().add(createXSDElementDeclaration());
+        	}
         }
         else
         {