/// <summary>
        /// Copy alle Components of the copyModel to the actual model
        /// </summary>
        private void PasteCopiedModel(model modelToPaste, bool namesAreValid, bool addToUndoStack)
        {
            CommandObject co = new CommandObject("Delete");
            if (modelToPaste == null)
                return;
            // loading the components
            /*
             *  Create a list of lists which contain all elements which should be within a group
             *  after the paste method finishes
             */
            ArrayList groupComps = new ArrayList();
            ArrayList groups = new ArrayList();
            ArrayList groupNames = new ArrayList();
            foreach (componentType ct in modelToPaste.components) {
                if (ct.ComponentType != ACS2.componentTypeDataTypes.group)
                    continue;
                groups.Add(ct);
                groupComponent gc = groupsList[ct.id];
                ArrayList groupElems = new ArrayList();
                foreach (componentType child in gc.AddedComponentList) {
                    foreach (componentType child1 in modelToPaste.components) {
                        if (child1.id.Equals(child.id)) {
                            groupElems.Add(child1);
                        }
                    }
                }
                if (groupElems.Count > 0) {
                    groupComps.Add(groupElems);
                    bool namevalid = namesAreValid;
                    int i = 1;
                    while (namevalid == false) {
                        string modelID = ct.id + "." + i;
                        if (!deploymentComponentList.ContainsKey(modelID)) {
                            namevalid = true;
                            groupNames.Add(modelID);
                        }
                        else
                            i++;
                    }
                }
            }

            // Rename components and update all channels
            Dictionary<string, string> changedComponents = new Dictionary<string, string>();
            foreach (object o in modelToPaste.components) {
                componentType modelComp = (componentType)o;
                if (modelComp.ComponentType == ACS2.componentTypeDataTypes.group)
                    continue;
                // change id
                bool namevalid = namesAreValid;
                int i = 1;
                while (namevalid == false) {
                    string modelID = modelComp.id + "." + i;
                    if (!deploymentComponentList.ContainsKey(modelID)) {
                        if (modelToPaste.channels != null) {
                            foreach (channel c in modelToPaste.channels) {
                                if (c.source.component.id == modelComp.id)
                                    c.source.component.id = modelID;
                                if (c.target.component.id == modelComp.id)
                                    c.target.component.id = modelID;
                            }
                        }
                        if (modelToPaste.eventChannels != null) {
                            foreach (eventChannel ec in modelToPaste.eventChannels) {
                                if (ec.sources.source.component.id == modelComp.id)
                                    ec.sources.source.component.id = modelID;
                                if (ec.targets.target.component.id == modelComp.id)
                                    ec.targets.target.component.id = modelID;
                            }
                        }
                        changedComponents.Add(modelComp.id,modelID);
                        modelComp.id = modelID;
                        namevalid = true;
                    }
                    else
                        i++;
                }
                // check, if bundle is available
                if (componentList.ContainsKey(modelComp.type_id)) {

                    // init the ArrayLists containing the ports. Used for easier and faster access
                    modelComp.InitArrayLists();

                    foreach (propertyType p in modelComp.properties) {
                        modelComp.PropertyArrayList.Add(p);
                    }

                    // copy the property datatype and description from bundle_description
                    // also copy the port datatypes form the bundle_description
                    // works only, if bundle and deployment fits to each other

                    try {
                        // search for bundle component
                        Asterics.ACS2.componentTypesComponentType bundleComponent = (Asterics.ACS2.componentTypesComponentType)componentList[modelComp.type_id];

                        // copy the ComponentType of the component
                        modelComp.ComponentType = bundleComponent.type.Value;

                        if (modelComp.properties != null) {
                            foreach (propertyType deploymentProperty in modelComp.properties) {
                                int index = 0;
                                // searching for the right component property
                                while (deploymentProperty.name != bundleComponent.properties[index].name) {
                                    index++;
                                }
                                // copy the properties of the component
                                deploymentProperty.DataType = bundleComponent.properties[index].type;

                                // check, if the property is a dynamic property
                                if (bundleComponent.properties[index].getStringList) {
                                    deploymentProperty.GetStringList = true;
                                } else {
                                    deploymentProperty.GetStringList = false;
                                }

                                // check the value fitting to the datatype
                                if (!CheckPropertyDatatype(deploymentProperty.value, deploymentProperty.DataType)) {
                                    throw new LoadPropertiesException();
                                }

                                deploymentProperty.Description = bundleComponent.properties[index].description;
                                if (bundleComponent.properties[index].combobox != null) {
                                    deploymentProperty.ComboBoxStrings = bundleComponent.properties[index].combobox.Split(new String[] { "//" }, StringSplitOptions.None);
                                }
                                deploymentProperty.PropertyChanged += ComponentPropertyChanged;
                                deploymentProperty.PropertyChangeError += ComponentPropertyChangeError;
                            }
                            // check the amount of properties. Cause an exception, if the bundle has more properties then the deployment
                            if ((bundleComponent.properties == null) && (modelComp.properties.Length != 0)) {
                                throw new LoadPropertiesException();
                            }
                            else if ((bundleComponent.properties != null) && (modelComp.properties.Length != bundleComponent.properties.Length)) {
                                throw new LoadPropertiesException();
                            }
                        }

                        foreach (object portObj in modelComp.PortsList.Values) {
                            // searching for the inPorts
                            if (portObj is inputPortType) {
                                int index = 0;
                                inputPortType deploymentInPort = (inputPortType)portObj;
                                ArrayList helperListInPort = new ArrayList(); // a list with all InPorts of a component for the bundel_description
                                foreach (object bundleInPort in bundleComponent.ports) {
                                    if (bundleInPort is ACS2.inputPortType) {
                                        helperListInPort.Add(bundleInPort);
                                    }
                                }
                                while (deploymentInPort.portTypeID != ((ACS2.inputPortType)helperListInPort[index]).id) {
                                    index++; // searching for the right input port
                                }
                                // copy the dataType of the port
                                deploymentInPort.PortDataType = ((Asterics.ACS2.inputPortType)helperListInPort[index]).dataType;
                                deploymentInPort.MustBeConnected = ((Asterics.ACS2.inputPortType)helperListInPort[index]).mustBeConnected;
                                deploymentInPort.Description = ((Asterics.ACS2.inputPortType)helperListInPort[index]).description;
                                deploymentInPort.ComponentId = ((Asterics.ACS2.inputPortType)helperListInPort[index]).ComponentId;
                                deploymentInPort.ComponentTypeId = ((Asterics.ACS2.inputPortType)helperListInPort[index]).id;

                                // update the alias for group ports via property changed listener
                                deploymentInPort.PropertyChanged += InputPortIntPropertyChanged;

                                ACS2.propertyType[] sourceProperties = ((Asterics.ACS2.inputPortType)helperListInPort[index]).properties;
                                if ((sourceProperties != null) && (sourceProperties.Length > 0)) {
                                    //if (deploymentInPort.PropertyArrayList.Count > 0) {
                                    foreach (propertyType deploymentProperty in deploymentInPort.properties) {
                                        int inPortIndex = 0;
                                        while (deploymentProperty.name != sourceProperties[inPortIndex].name) {
                                            inPortIndex++;
                                        }
                                        // copy the properties of the inPort
                                        deploymentProperty.DataType = sourceProperties[inPortIndex].type;
                                        deploymentProperty.Description = sourceProperties[inPortIndex].description;
                                        if (sourceProperties[inPortIndex].combobox != null) {
                                            deploymentProperty.ComboBoxStrings = sourceProperties[inPortIndex].combobox.Split(new String[] { "//" }, StringSplitOptions.None);
                                        }
                                        deploymentProperty.PropertyChanged += InPortPropertyChanged;
                                        deploymentProperty.PropertyChangeError += ComponentPropertyChangeError;
                                    }
                                    // check the amount of properties. Cause an exception, if the bundle has more properties then the deployment
                                    if (deploymentInPort.properties.Length != sourceProperties.Length) {
                                        throw new Exception();
                                    }
                                }
                                deploymentInPort.properties = (propertyType[])deploymentInPort.PropertyArrayList.ToArray(typeof(propertyType));
                            }
                            else {
                                // comparing all outPports
                                int index = 0;
                                outputPortType outPort = (outputPortType)portObj;
                                ArrayList helperListOutPort = new ArrayList();
                                foreach (object origOutPort in bundleComponent.ports) {
                                    if (origOutPort is ACS2.outputPortType) {
                                        helperListOutPort.Add(origOutPort);
                                    }
                                }
                                while (outPort.portTypeID != ((Asterics.ACS2.outputPortType)helperListOutPort[index]).id) {
                                    index++;
                                }
                                // copy the dataType of the port
                                outPort.PortDataType = ((Asterics.ACS2.outputPortType)helperListOutPort[index]).dataType;
                                outPort.Description = ((Asterics.ACS2.outputPortType)helperListOutPort[index]).description;
                                outPort.ComponentId = ((Asterics.ACS2.outputPortType)helperListOutPort[index]).ComponentId;
                                outPort.ComponentTypeId = ((Asterics.ACS2.outputPortType)helperListOutPort[index]).id;

                                // update the alias for group ports via property changed listener
                                outPort.PropertyChanged += OutputPortIntPropertyChanged;

                                ACS2.propertyType[] sourceProperties = ((Asterics.ACS2.outputPortType)helperListOutPort[index]).properties;
                                if ((sourceProperties != null) && (sourceProperties.Length > 0)) {
                                    //if (outPort.PropertyArrayList.Count > 0) {
                                    foreach (propertyType compProperty in outPort.properties) {
                                        int outPortIndex = 0;
                                        while (compProperty.name != sourceProperties[outPortIndex].name) {
                                            outPortIndex++;
                                        }
                                        // copy the properties of the outPort
                                        compProperty.DataType = sourceProperties[outPortIndex].type;
                                        compProperty.Description = sourceProperties[outPortIndex].description;
                                        if (sourceProperties[outPortIndex].combobox != null) {
                                            compProperty.ComboBoxStrings = sourceProperties[outPortIndex].combobox.Split(new String[] { "//" }, StringSplitOptions.None);
                                        }
                                        compProperty.PropertyChanged += OutPortPropertyChanged;
                                        compProperty.PropertyChangeError += ComponentPropertyChangeError;
                                    }
                                    // check the amount of properties. Cause an exception, if the bundle has more properties then the deployment
                                    if (outPort.properties.Length != sourceProperties.Length) {
                                        throw new Exception();
                                    }
                                }
                                outPort.properties = (propertyType[])outPort.PropertyArrayList.ToArray(typeof(propertyType));
                            }
                        }
                    }
                    catch (Exception ex) {
                        MessageBox.Show(Properties.Resources.CopyPropertiesErrorTextFormat(modelComp.id), Properties.Resources.CopyPropertiesErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                        traceSource.TraceEvent(TraceEventType.Error, 3, ex.Message);

                        //versionconflict
                        int posX;
                        int posY;
                        // set coordinates for the component in case there are not already set
                        if ((modelComp.layout.posX == null) || (modelComp.layout.posX == "") || (modelComp.layout.posY == null) || (modelComp.layout.posY == "")) {
                            posX = 40;
                            posY = 40;
                        }
                        else {
                            posX = Int32.Parse(modelComp.layout.posX);
                            posY = Int32.Parse(modelComp.layout.posY);
                        }

                        // backup component to load properties
                        componentType backupComp = modelComp;

                        modelComp = componentType.CopyFromBundleModel((Asterics.ACS2.componentTypesComponentType)componentList[modelComp.type_id], modelComp.id);
                        modelComp.layout.posX = Convert.ToString(posX);
                        modelComp.layout.posY = Convert.ToString(posY);
                        // HasVersionConflict indicates a version conflict between the component in a stored model and
                        // the component in the bundle descriptor
                        modelComp.HasVersionConflict = true;
                        modelComp.ComponentCanvas.Background = new SolidColorBrush(Colors.Orange);
                        //break;

                        // new code, copy property values from invalid (version conflict) component
                        foreach (propertyType deploymentProperty in modelComp.properties) {
                            foreach (propertyType backupProperty in backupComp.properties) {
                                if (deploymentProperty.name == backupProperty.name) {
                                    if (CheckPropertyDatatype(backupProperty.value, deploymentProperty.DataType)) {
                                        deploymentProperty.value = backupProperty.value; // try-parse is missing
                                    }
                                    break;
                                }
                            }
                        }

                    } // end of exception

                    // set coordinates for the component in case there are not already set
                    if ((modelComp.layout.posX == null) || (modelComp.layout.posX == "") || (modelComp.layout.posY == null) || (modelComp.layout.posY == "")) {
                        int[] pos = ProperComponentCoordinates(40, 40);
                        modelComp.layout.posX = Convert.ToString(pos[0]);
                        modelComp.layout.posY = Convert.ToString(pos[1]);
                    }

                    // Searching for the event triggers and event listeners of a component
                    Asterics.ACS2.componentTypesComponentType bundleComponentEvents = (Asterics.ACS2.componentTypesComponentType)componentList[modelComp.type_id];
                    // If component has version conflict, the events are already set by 'CopyFromBundleModel'
                    if ((bundleComponentEvents.events != null) && (bundleComponentEvents.events != null) && !modelComp.HasVersionConflict) {
                        foreach (object eventO in bundleComponentEvents.events) {
                            if (eventO is ACS2.eventsTypeEventListenerPortType) {
                                ACS2.eventsTypeEventListenerPortType compEl = (ACS2.eventsTypeEventListenerPortType)eventO;
                                EventListenerPort el = new EventListenerPort();
                                el.EventListenerId = compEl.id;
                                el.ComponentId = modelComp.id;
                                el.EventDescription = compEl.description;
                                modelComp.EventListenerList.Add(el);
                            }
                            else if (eventO is ACS2.eventsTypeEventTriggererPortType) {
                                ACS2.eventsTypeEventTriggererPortType compEl = (ACS2.eventsTypeEventTriggererPortType)eventO;
                                EventTriggerPort el = new EventTriggerPort();
                                el.EventTriggerId = compEl.id;
                                el.ComponentId = modelComp.id;
                                el.EventDescription = compEl.description;
                                modelComp.EventTriggerList.Add(el);
                            }
                        }
                    }

                    // if the component has no version conflict, it will be pasted on the layout, otherwise, it is already on the
                    // canvas (done by 'CopyFromBundleModel')
                    if (!modelComp.HasVersionConflict) {
                        modelComp.InitGraphLayout(modelComp.id);
                    }
                    else {
                        //deploymentModel.components = deploymentComponentList.Values.ToArray();
                    }
                    canvas.Children.Add(modelComp.ComponentCanvas);
                    KeyboardNavigation.SetTabIndex(modelComp.ComponentCanvas, canvas.Children.Count + 1);

                    modelComp.Label.Text = modelComp.id;
                    double positionX = (Int32.Parse(modelComp.layout.posX) + copyXOffset * copyOffsetMulti);
                    if (positionX + modelComp.ComponentCanvas.Width > canvas.RenderSize.Width)
                        positionX = canvas.RenderSize.Width - modelComp.ComponentCanvas.Width;
                    double positionY = (Int32.Parse(modelComp.layout.posY) + copyYOffset * copyOffsetMulti);
                    if (positionY + modelComp.ComponentCanvas.Height > canvas.RenderSize.Height)
                        positionY = canvas.RenderSize.Height - modelComp.ComponentCanvas.Height;
                    modelComp.layout.posX = positionX.ToString();
                    modelComp.layout.posY = positionY.ToString();
                    deploymentComponentList.Add(modelComp.id, modelComp);
                    //componentList.Add(modelComp.id, modelComp);
                    // adding context menu
                    modelComp.MainRectangle.ContextMenu = componentContextMenu;
                    // adding keyboard focus listener
                    modelComp.ComponentCanvas.KeyDown += Component_KeyDown;
                    modelComp.ComponentCanvas.KeyUp += Component_KeyUp;
                    modelComp.ComponentCanvas.Focusable = true;
                    modelComp.ComponentCanvas.GotKeyboardFocus += ComponentCanvas_GotKeyboardFocus;
                    modelComp.ComponentCanvas.LostKeyboardFocus += ComponentCanvas_LostKeyboardFocus;

                    // adding property changed listener
                    modelComp.PropertyChanged += ComponentIntPropertyChanged;
                    modelComp.TopGrid.ContextMenu = componentContextMenu;
                    modelComp.TopRectangle.ContextMenu = componentContextMenu;
                    // set position of component on the canvas
                    Canvas.SetLeft(modelComp.ComponentCanvas, Int32.Parse(modelComp.layout.posX));
                    Canvas.SetTop(modelComp.ComponentCanvas, Int32.Parse(modelComp.layout.posY));

                    // Adapt the size of MainRectangle, if more then MAINRECTANGLENUMBEROFPORTS are in a component
                    int numInPorts = 0;
                    int numOutPorts = 0;
                    foreach (object objPort in modelComp.PortsList.Values) {
                        if (objPort is inputPortType) {
                            numInPorts++;
                        }
                        else {
                            numOutPorts++;
                        }
                    }
                    if (numOutPorts > ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) {
                        modelComp.MainRectangle.Height += (numOutPorts - ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) * (ACS.LayoutConstants.OUTPORTDISTANCE);
                        modelComp.ComponentCanvas.Height += (numOutPorts - ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) * (ACS.LayoutConstants.OUTPORTDISTANCE);
                    }
                    else if (numInPorts > ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) {
                        modelComp.MainRectangle.Height += (numInPorts - ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) * (ACS.LayoutConstants.INPORTDISTANCE);
                        modelComp.ComponentCanvas.Height += (numInPorts - ACS.LayoutConstants.MAINRECTANGLENUMBEROFPORTS) * (ACS.LayoutConstants.INPORTDISTANCE);
                    }
                    // Adapt the position of event trigger and event listener port, if the component has more input/output ports
                    if (modelComp.EventListenerList.Count > 0) {
                        Canvas.SetTop(modelComp.EventListenerPolygon.InputEventPortCanvas, modelComp.MainRectangle.Height + ACS.LayoutConstants.MAINRECTANGLEOFFSETY - 10);
                    }
                    if (modelComp.EventTriggerList.Count > 0) {
                        Canvas.SetTop(modelComp.EventTriggerPolygon.OutputEventPortCanvas, modelComp.MainRectangle.Height + ACS.LayoutConstants.MAINRECTANGLEOFFSETY - 10);
                    }

                }
                else {
                    MessageBox.Show(Properties.Resources.LoadComponentNotFoundFormat(modelComp.type_id), Properties.Resources.CopyPropertiesErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                }

                // check, if component has a gui component, and load the gui component
                if (modelComp.gui != null) {
                    AddGUIComponent(modelComp);
                }
            }
            deploymentModel.components = deploymentComponentList.Values.ToArray();

            // loading the channels
            if (modelToPaste.channels != null) {
                foreach (object o in modelToPaste.channels) {
                    channel modChannel = (channel)o;
                    // check versionconflict: add only channels to components without a conflict
                    if ((deploymentComponentList.ContainsKey(modChannel.source.component.id)) && (deploymentComponentList.ContainsKey(modChannel.target.component.id))) {

                        // one of the channels component has a conflict. Check, if port is available and datatype fits together
                        componentType tempCompOut = (componentType)deploymentComponentList[modChannel.source.component.id];
                        componentType tempCompIn = (componentType)deploymentComponentList[modChannel.target.component.id];
                        // try, if the ports are still available

                        if ((tempCompOut.PortsList.Contains(modChannel.source.port.id)) && (tempCompIn.PortsList.Contains(modChannel.target.port.id))) {
                            // check the datatypes of the ports, for the case, that they have been changed
                            //if (CheckInteroperabilityOfPorts(((outputPortType)tempCompOut.PortsList[modChannel.source.port.id]).PortDataType, ((inputPortType)tempCompIn.PortsList[modChannel.target.port.id]).PortDataType)) {
                                modChannel.id = NewIdForChannel();
                                AddChannel(modChannel);
                                modChannel.Line.Y1 = Canvas.GetTop(((outputPortType)tempCompOut.PortsList[modChannel.source.port.id]).PortRectangle) + Canvas.GetTop(tempCompOut.ComponentCanvas) + 5;
                                modChannel.Line.X1 = Canvas.GetLeft(((outputPortType)tempCompOut.PortsList[modChannel.source.port.id]).PortRectangle) + Canvas.GetLeft(tempCompOut.ComponentCanvas) + 20;

                                modChannel.Line.Y2 = Canvas.GetTop(((inputPortType)tempCompIn.PortsList[modChannel.target.port.id]).PortRectangle) + Canvas.GetTop(tempCompIn.ComponentCanvas) + 5;
                                modChannel.Line.X2 = Canvas.GetLeft(((inputPortType)tempCompIn.PortsList[modChannel.target.port.id]).PortRectangle) + Canvas.GetLeft(tempCompIn.ComponentCanvas);
                                Canvas.SetZIndex(modChannel.Line, Canvas.GetZIndex(modChannel.Line) + 1000);
                            //}
                            //else {
                            //    // if no event listener Port can be found, the component has a version conflict
                            //    MessageBox.Show(Properties.Resources.CopyChannelsErrorTextFormat(tempCompOut.id, tempCompIn.id), Properties.Resources.CopyChannelsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                            //    tempCompIn.ComponentCanvas.Background = new SolidColorBrush(Colors.Red);
                            //    tempCompIn.HasVersionConflict = true;
                            //    tempCompOut.ComponentCanvas.Background = new SolidColorBrush(Colors.Red);
                            //    tempCompOut.HasVersionConflict = true;
                            //}
                        }
                        else {
                            if (!tempCompOut.PortsList.Contains(modChannel.source.port.id)) {
                                MessageBox.Show(Properties.Resources.CopyChannelErrorNotFoundFormat(tempCompOut.id, tempCompIn.id, modChannel.source.port.id), Properties.Resources.CopyChannelsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                                tempCompOut.ComponentCanvas.Background = new SolidColorBrush(Colors.Orange);
                                tempCompOut.HasVersionConflict = true;
                            }
                            else {
                                MessageBox.Show(Properties.Resources.CopyChannelErrorNotFoundFormat(tempCompOut.id, tempCompIn.id, modChannel.source.port.id), Properties.Resources.CopyChannelsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                                tempCompIn.ComponentCanvas.Background = new SolidColorBrush(Colors.Orange);
                                tempCompIn.HasVersionConflict = true;
                            }
                        }

                    }
                }
            }
            // Loading the events and drawing the lines between the event ports
            if (modelToPaste.eventChannels != null) {
                bool foundLine = false;
                foreach (object o in modelToPaste.eventChannels) {
                    eventChannel evChannel = (eventChannel)o;
                    bool foundTriggerPort = false;
                    bool foundListenerPort = false;
                    try {
                        foreach (EventTriggerPort checkEvent in ((componentType)deploymentComponentList[evChannel.sources.source.component.id]).EventTriggerList) {
                            if (checkEvent.ComponentId == evChannel.sources.source.component.id && checkEvent.EventTriggerId == evChannel.sources.source.eventPort.id) {
                                foundTriggerPort = true;
                                break;
                            }
                        }
                        if (foundTriggerPort) {
                            foreach (EventListenerPort checkEvent in ((componentType)deploymentComponentList[evChannel.targets.target.component.id]).EventListenerList) {
                                if (checkEvent.ComponentId == evChannel.targets.target.component.id && checkEvent.EventListenerId == evChannel.targets.target.eventPort.id) {
                                    foundListenerPort = true;
                                    break;
                                }
                            }
                            if (foundListenerPort) {
                                foreach (eventChannelLine channelLine in eventChannelLinesList) {
                                    if ((evChannel.sources.source.component.id == channelLine.TriggerComponentId) && (evChannel.targets.target.component.id == channelLine.ListenerComponentId)) {
                                        foundLine = true;
                                        break;
                                    }
                                }
                                if (!foundLine) {
                                    eventChannelLine eCL = new eventChannelLine();

                                    eCL.Line.X1 = Canvas.GetLeft(((componentType)deploymentComponentList[evChannel.sources.source.component.id]).ComponentCanvas) + LayoutConstants.EVENTOUTPORTCANVASOFFSETX + LayoutConstants.EVENTPORTWIDTH / 2 + 5;
                                    //eCL.Line.Y1 = Canvas.GetTop(((modelComponent)deploymentComponentList[evChannel.sources.source.component.id]).ComponentCanvas) + LayoutConstants.EVENTOUTPORTCANVASOFFSETY + LayoutConstants.EVENTPORTHEIGHT + 3;
                                    eCL.Line.Y1 = Canvas.GetTop(((componentType)deploymentComponentList[evChannel.sources.source.component.id]).ComponentCanvas) +
                                        ((componentType)deploymentComponentList[evChannel.sources.source.component.id]).MainRectangle.Height + LayoutConstants.EVENTPORTHEIGHT + LayoutConstants.MAINRECTANGLEOFFSETY - 7;
                                    eCL.Line.X2 = Canvas.GetLeft(((componentType)deploymentComponentList[evChannel.targets.target.component.id]).ComponentCanvas) + LayoutConstants.EVENTINPORTCANVASOFFSETX + LayoutConstants.EVENTPORTWIDTH / 2 + 5;
                                    //eCL.Line.Y2 = Canvas.GetTop(((modelComponent)deploymentComponentList[evChannel.targets.target.component.id]).ComponentCanvas) + LayoutConstants.EVENTINPORTCANVASOFFSETY + LayoutConstants.EVENTPORTHEIGHT + 3;
                                    eCL.Line.Y2 = Canvas.GetTop(((componentType)deploymentComponentList[evChannel.targets.target.component.id]).ComponentCanvas) +
                                        ((componentType)deploymentComponentList[evChannel.targets.target.component.id]).MainRectangle.Height + LayoutConstants.EVENTPORTHEIGHT + LayoutConstants.MAINRECTANGLEOFFSETY - 7;

                                    eCL.Line.Focusable = true;
                                    eCL.ListenerComponentId = evChannel.targets.target.component.id;
                                    eCL.TriggerComponentId = evChannel.sources.source.component.id;
                                    eCL.Line.GotKeyboardFocus += EventChannel_GotKeyboardFocus;
                                    eCL.Line.LostKeyboardFocus += EventChannel_LostKeyboardFocus;
                                    eCL.Line.KeyDown += EventChannel_KeyDown;
                                    eCL.Line.ContextMenu = eventChannelContextMenu;
                                    eventChannelLinesList.Add(eCL);
                                    canvas.Children.Add(eCL.Line);
                                    KeyboardNavigation.SetTabIndex(eCL.Line, canvas.Children.Count + 1);
                                    Canvas.SetZIndex(eCL.Line, Canvas.GetZIndex(eCL.Line) + 2000);
                                }
                                eventChannelList.Add(o);
                                foundLine = false;
                            }
                            else {
                                // if no event listener Port can be found, the component has a version conflict
                                MessageBox.Show(Properties.Resources.CopyEventsErrorTextFormat(evChannel.targets.target.component.id), Properties.Resources.CopyEventsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                                ((componentType)deploymentComponentList[evChannel.targets.target.component.id]).ComponentCanvas.Background = new SolidColorBrush(Colors.Orange);
                                ((componentType)deploymentComponentList[evChannel.targets.target.component.id]).HasVersionConflict = true;
                            }
                        }
                        else {
                            // if no event trigger Port can be found, the component has a version conflict
                            MessageBox.Show(Properties.Resources.CopyEventsErrorTextFormat(evChannel.sources.source.component.id), Properties.Resources.CopyEventsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                            ((componentType)deploymentComponentList[evChannel.sources.source.component.id]).ComponentCanvas.Background = new SolidColorBrush(Colors.Orange);
                            ((componentType)deploymentComponentList[evChannel.sources.source.component.id]).HasVersionConflict = true;
                        }
                    }
                    catch (Exception) {
                        MessageBox.Show(Properties.Resources.CopyEventsExceptionTextFormat(evChannel.sources.source.component.id, evChannel.sources.source.eventPort.id,
                            evChannel.targets.target.component.id, evChannel.targets.target.component.id), Properties.Resources.CopyEventsErrorHeader, MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                deploymentModel.eventChannels = (eventChannel[])eventChannelList.ToArray(typeof(eventChannel));
            }

            // focus the first element
            if (canvas.Children.Count > 0) {
                Keyboard.Focus(canvas.Children[0]);
            }
            else {
                Keyboard.Focus(canvas);
            }
            copyOffsetMulti++;
            ClearSelectedChannelList();
            if (modelToPaste.channels != null) {
                foreach (channel c in modelToPaste.channels) {
                    selectedChannelList.AddLast(c);
                    co.InvolvedObjects.Add(c);
                }
            }
            UpdateSelectedChannels();
            ClearSelectedEventChannelList();
            LinkedList<eventChannelLine> selEventChannels = new LinkedList<eventChannelLine>();
            foreach (eventChannelLine ecl in eventChannelLinesList) {
                if (modelToPaste.eventChannels != null) {
                    foreach (eventChannel ech in modelToPaste.eventChannels) {
                        if (ecl.ListenerComponentId == ech.targets.target.component.id ||
                            ecl.TriggerComponentId == ech.sources.source.component.id) {
                            selEventChannels.AddLast(ecl);
                            break;
                        }
                    }
                }
            }
            foreach (eventChannelLine ecl in selEventChannels) {
                selectedEventChannelList.AddLast(ecl);
                co.InvolvedObjects.Add(ecl);
            }
            UpdateSelectedEventChannels();
            // select all inserted components
            ClearSelectedComponentList();
            foreach (componentType mc in modelToPaste.components) {
                selectedComponentList.AddLast(mc);
                co.InvolvedObjects.Add(mc);
            }
            UpdateSelectedComponents();
            if (addToUndoStack) {
                undoStack.Push(co);
                redoStack.Clear();
            }
            SetKeyboardFocus();
            int groupIndex = 0;
            AddDummyToModel(copyDummyName);
            foreach (ArrayList group in groupComps) {
                ClearSelectedChannelList();
                ClearSelectedComponentList();
                ClearSelectedEventChannelList();
                foreach (componentType ct in group) {
                    AddSelectedComponent(ct);
                }
                componentType groupComponent = (componentType) groups[groupIndex];
                // add dummy channels to retain all ports of the copied group element
                foreach (object o in groupComponent.ports) {
                    if (o is inputPortType) {
                        inputPortType inPort = (inputPortType) o;

                        string targetComponent = inPort.refs.componentID;
                        componentType newTarget = null;
                        foreach (componentType ct in group) {
                            if (ct.id.StartsWith(targetComponent)) {
                                if (newTarget == null)
                                    newTarget = ct;
                                else {
                                    if (newTarget.id.Length < ct.id.Length)
                                        newTarget = ct;
                                }
                            }
                        }
                        if (newTarget == null)
                            continue;

                        // check if an channel already exists for this inputporttype
                        bool duplicate = false;
                        foreach (channel c in deploymentChannelList.Values) {
                            if (c.target.component.id.Equals(newTarget.id) &&
                                c.target.port.id.Equals(inPort.portTypeID.Substring(targetComponent.Length + 1))) {
                                    duplicate = true;
                            }
                        }
                        if (duplicate)
                            continue;

                        // add dummy channel
                        channel groupChannel = new channel();

                        groupChannel.id = NewIdForChannel();
                        groupChannel.target.component.id = newTarget.id;
                        groupChannel.target.port.id = inPort.refs.portID;
                        groupChannel.source.component.id = copyDummyName;
                        groupChannel.source.port.id = "out";
                        if (!ChannelExists(groupChannel))
                            AddChannel(groupChannel);
                    } else if (o is outputPortType) {
                        outputPortType outPort = (outputPortType) o;
                        string sourceComponent = outPort.refs.componentID;
                        componentType newSource = null;
                        foreach (componentType ct in group) {
                            if (ct.id.StartsWith(sourceComponent)) {
                                if (newSource == null)
                                    newSource = ct;
                                else {
                                    if (newSource.id.Length < ct.id.Length)
                                        newSource = ct;
                                }
                            }
                        }
                        if (newSource == null)
                            continue;

                        // add dummy channel
                        channel groupChannel = new channel();

                        groupChannel.id = NewIdForChannel();
                        groupChannel.source.component.id = newSource.id;
                        groupChannel.source.port.id = outPort.refs.portID;
                        groupChannel.target.component.id = copyDummyName;
                        groupChannel.target.port.id = "in";
                        if (!ChannelExists(groupChannel))
                            AddChannel(groupChannel);
                    }

                }
                // add dummy eventchannels to retain all eventchannel ports of the copied group

                foreach (ArrayList echList in copyGroupEventChannels) {
                    foreach (eventChannel ech in echList) {
                        componentType source = null;
                        componentType target = null;
                        foreach (componentType ct in group) {
                            if (ct.id.StartsWith(ech.sources.source.component.id)) {
                                if (source == null)
                                    source = ct;
                                else if (ct.id.Length > source.id.Length)
                                    source = ct;
                            }
                            if (ct.id.StartsWith(ech.targets.target.component.id)) {
                                if (target == null)
                                    target = ct;
                                else if (ct.id.Length > target.id.Length)
                                    target = ct;
                            }
                        }
                        if (source != null)
                            ech.sources.source.component.id = source.id;
                        if (target != null) {
                            ech.targets.target.component.id = target.id;
                            ech.id = target.id + "_" + ech.sources.source.eventPort.id + "_" + ech.targets.target.eventPort.id;
                        }
                        if (source != null || target != null)
                            eventChannelList.Add(ech);
                    }
                }

                LinkedList<portAlias> newPortAlias = new LinkedList<portAlias>();
                group originalGroup = null;
                foreach (object o in groups) {
                   // groups.Add(ct);
                    groupComponent gc = groupsList[((componentType)o).id];
                    // adapting the alias to the new group port names

                    foreach (group gr in deploymentModel.groups) {
                        if (gr.id == ((componentType)o).id) {
                            originalGroup = gr;
                            break;
                        }
                    }

                    if (originalGroup != null && originalGroup.portAlias != null) {
                        foreach (portAlias alias in originalGroup.portAlias) {
                            foreach (string oldCompName in changedComponents.Keys) {
                                if (alias.portId.Contains(oldCompName)) {
                                    portAlias pAlias = new portAlias();
                                    pAlias.portId = alias.portId.Replace(oldCompName, changedComponents[oldCompName]);
                                    pAlias.portAlias1 = alias.portAlias1;
                                    newPortAlias.AddLast(pAlias);
                                    break;
                                }
                            }
                        }
                    }
                }

                DoGrouping((string) groupNames[groupIndex], false, true);
                group groupToUpdate = null;
                foreach (group gr in deploymentModel.groups) {
                    if (gr.id == (string)groupNames[groupIndex]) {
                        gr.portAlias = newPortAlias.ToArray();
                        groupToUpdate = gr;
                        break;
                    }
                }
                if (groupToUpdate.portAlias != null) {
                    foreach (portAlias alias in groupToUpdate.portAlias) {
                        componentType groupComponentToUpdate = deploymentComponentList[groupToUpdate.id];
                        groupComponentToUpdate.description = originalGroup.description;
                        foreach (object port in groupComponentToUpdate.ports) {
                            if ((port is inputPortType) && (((inputPortType)port).portTypeID == alias.portId)) {
                                ((inputPortType)port).PortAliasForGroups = alias.portAlias1;
                                ((inputPortType)port).PortLabel.Text = alias.portAlias1;
                                break;
                            } else if ((port is outputPortType) && (((outputPortType)port).portTypeID == alias.portId)) {
                                ((outputPortType)port).PortAliasForGroups = alias.portAlias1;
                                ((outputPortType)port).PortLabel.Text = alias.portAlias1;
                                break;
                            }
                        }
                    }
                }
                groupToUpdate.description = originalGroup.description;
                groupIndex++;
            }
            RemoveDummyFromModel(copyDummyName);
            DeleteDanglingChannels();
            DeleteDanglingEventChannels();

            // select the copied elements after paste
            selectedComponentList.Clear();
            foreach (componentType mc in modelToPaste.components) {
                selectedComponentList.AddLast(mc);
            }
            UpdateSelectedComponents();
        }
        private void OutputPortIntPropertyChanged(Object sender, PropertyChangedEventArgs e)
        {
            outputPortType  groupPort = (outputPortType)sender;
            if ((deploymentModel.groups != null)  && (deploymentComponentList[groupPort.ComponentId].ComponentType == ACS2.componentTypeDataTypes.group)) {
                foreach (group groupToUpdate in deploymentModel.groups) {
                    if (groupToUpdate.id == groupPort.ComponentId) {
                        List<portAlias> aliasList = new List<portAlias>();
                        if (groupToUpdate.portAlias != null) {
                            aliasList = groupToUpdate.portAlias.ToList();
                        }
                        if (groupPort.PortAliasForGroups == "" || groupPort.PortAliasForGroups == " ") {
                            groupPort.PortLabel.Text = groupPort.portTypeID;
                        } else {
                            groupPort.PortLabel.Text = groupPort.PortAliasForGroups;
                        }
                        // check, if an alias for this port already exists
                        portAlias foundAlias = null;
                        foreach (portAlias alias in aliasList) {
                            if (alias.portId == groupPort.portTypeID) {
                                alias.portAlias1 = groupPort.PortAliasForGroups;
                                foundAlias = alias;
                                break;
                            }
                        }
                        if ((foundAlias != null) && ((groupPort.PortAliasForGroups == "") || (groupPort.PortAliasForGroups == " "))) {
                            aliasList.Remove(foundAlias);
                        } else if (foundAlias == null) {
                            portAlias newAlias = new portAlias();
                            newAlias.portAlias1 = groupPort.PortAliasForGroups;
                            newAlias.portId = groupPort.portTypeID;
                            aliasList.Add(newAlias);
                        }
                        groupToUpdate.portAlias = aliasList.ToArray();

                    }
                }
            } else {
                MessageBox.Show("When the model will be saved, only alias for ports of groups will be stored", Properties.Resources.ReadXmlErrorHeader, MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }