/***********************************************************************************************************************
 * Copyright (c) 2010 Communication & Systems.
 * 
 * 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: Sebastien GABEL (CS) - initial API and implementation
 * 
 **********************************************************************************************************************/
package org.topcased.requirement.core.commands;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.topcased.requirement.HierarchicalElement;
import org.topcased.requirement.RequirementPackage;
import org.topcased.requirement.SpecialChapter;
import org.topcased.requirement.core.internal.Messages;
import org.topcased.requirement.core.utils.RequirementUtils;

/**
 * When a model element is deleted, the requirement model must be updated.<br>
 * This command handles expected modifications :
 * <ul>
 * <li>Put the Hierarchical Element and all its involved requirements to the Trash Chapter (DeleteCommand then
 * AddCommand)</li>
 * <li>From the requirement model, delete if necessary the corresponding hierarchical elements.</li>
 * </ul>
 * 
 * @author <a href="mailto:sebastien.gabel@c-s.fr">Sebastien GABEL</a>
 * @since Topcased 2.3.0
 * 
 */
public class RemoveRequirementCommand extends CompoundCommand
{
    /** List of selected objects to remove */
    private Collection<EObject> selected;

    /** The editing domain to use */
    private EditingDomain editingDomain;

    /**
     * Constructor
     * 
     * @param deleted Represents the model object which is going to be deleted
     */
    public RemoveRequirementCommand(EditingDomain domain, EObject deleted)
    {
        super(Messages.getString("RemoveRequirementCommand.0")); //$NON-NLS-1$
        editingDomain = domain;
        selected = new ArrayList<EObject>();
        if (deleted != null)
        {
            selected.add(deleted);
        }
        initializeCommands();
    }

    /**
     * Constructor
     * 
     * @param objects A collection of model objects for which the matching {@link HierarchicalElement} must be removed.
     */
    public RemoveRequirementCommand(EditingDomain domain, Collection<EObject> deleted)
    {
        super(Messages.getString("RemoveHierarchicalElementCommand.0")); //$NON-NLS-1$
        editingDomain = domain;
        selected = deleted;
        initializeCommands();
    }

    /**
     * Initializes the commands contained in this compound command
     */
    protected void initializeCommands()
    {
        List<EObject> toRemove = new ArrayList<EObject>();
        for (Iterator<EObject> it = selected.iterator(); it.hasNext();)
        {
            HierarchicalElement elt = RequirementUtils.getHierarchicalElementFor(it.next());
            if (elt != null)
            {
                toRemove.add(elt);
            }
        }

        // 1) The HierarchicalElement(s) is/are removed from the model
        appendIfCanExecute(RemoveCommand.create(editingDomain, toRemove));

        // 2) Then, hierarchical element and its requirements are added to the trash chapter.
        SpecialChapter trash = RequirementUtils.getTrashChapter(editingDomain);
        appendIfCanExecute(AddCommand.create(editingDomain, trash, RequirementPackage.eINSTANCE.getSpecialChapter_HierarchicalElement(), toRemove));
    }

    /**
     * @see org.eclipse.emf.common.command.AbstractCommand#canExecute()
     */
    @Override
    public boolean canExecute()
    {
        return super.canExecute() && !selected.isEmpty() && editingDomain != null;
    }
}

@@ -487,16 +487,192 @@
 ion%0A * %0A
+ * Anass RADOUANI (Atos) anass.radouani@atos.net - add constructor to specify the hierarchical element to delete %0A *      when we are in the semantic element post deletion%0A * %0A
  *******
@@ -2175,16 +2175,101 @@
 omain;%0A%0A
+    /** Hierarchical Element to remove */%0A    private HierarchicalElement hElement;%0A%0A
     /**%0A
@@ -2732,32 +2732,739 @@
 mands();%0A    %7D%0A%0A
+    /**%0A     * Constructor%0A     * %0A     * @param domain The editing domain to use%0A     * @param deleted Represents the model object which is going to be deleted%0A     * @param hElement Represents the Hierarchical Element that will be added to the liste of element to delete%0A     */%0A    public RemoveRequirementCommand(EditingDomain domain, EObject deleted, HierarchicalElement hElement)%0A    %7B%0A        super(Messages.getString(%22RemoveRequirementCommand.0%22)); //$NON-NLS-1$%0A        editingDomain = domain;%0A        selected = new ArrayList%3CEObject%3E();%0A        if (deleted != null)%0A        %7B%0A            selected.add(deleted);%0A        %7D%0A        this.hElement = hElement;%0A        initializeCommands();%0A    %7D%0A    %0A
     /**%0A     * C
@@ -4067,32 +4067,41 @@
 ist%3CEObject%3E();%0A
+        %0A
         for (Ite
@@ -4278,32 +4278,78 @@
  if (elt != null
+ && !RequirementUtils.istrashChapterChild(elt)
 )%0A            %7B%0A
@@ -4404,24 +4404,232 @@
 %0A        %7D%0A%0A
+        if (hElement != null && !toRemove.contains(hElement))%0A        %7B%0A            toRemove.add(hElement);%0A        %7D%0A        %0A        if (toRemove.isEmpty())%0A        %7B%0A            return;%0A        %7D%0A        %0A
         // 1

Add constructor to specify the Hierarchical Element to delete when we are in semantic element post deletion