private void radioReplicateDirectionByRow_Click(object sender, RoutedEventArgs e)
 {
     this.previewReplicateDirection = RepDirection.ByRow;
     PreviewFillFirstAndLast();
 }
        // Returns the new group number
        public int FillFirstAndLast(int pos1, int pos2, int typeId, int group, Direction fillDirection, int replicates, bool rectangleMode, RepDirection replicateDirection, FillSettingsModel fillSettings)
        {
            CurrentState.CheckPositionInRangeOneBased(pos1);
            CurrentState.CheckPositionInRangeOneBased(pos2);

            // Create a copy for the new state (this is necessary as changes to the layout are made in this way to avoid unnecessary updates)
            SingleLayoutEditor newState = CurrentState.Clone();
            if (pos1 == pos2)
            {
                FillPosition(newState.LayoutPositions[pos1 - 1], typeId, group, fillSettings);
                var numPositionsOfFilledGroupType = newState.GetPositionsOfGroupType(group, typeId).Count();
                if (numPositionsOfFilledGroupType == fillSettings.Replicates)
                {
                    if (group < fillSettings.MaxGroupNum)
                        group++;
                }
            }
            else
            {
                if (rectangleMode == true)
                {
                    //group = FillRectangleMode(pos1, pos2, typeId, group, fillDirection, replicates, replicateDirection, fillSettings, newState);
                    IEnumerable<LayoutPosEditor> fillEnumerable;
                    if (fillDirection == Direction.Across)
                        fillEnumerable = newState.GetEnumerableRectangleAcross(pos1, pos2);
                    else
                        fillEnumerable = newState.GetEnumerableRectangleDown(pos1, pos2);

                    int startCol = newState.GetColFromPos(pos1);
                    int endCol = newState.GetColFromPos(pos2);

                    int startRow = newState.GetRowFromPos(pos1);
                    int endRow = newState.GetRowFromPos(pos2);

                    int colWidth = Math.Abs(endCol - startCol) + 1;
                    int rowWidth = Math.Abs(endRow - startRow) + 1;

                    int replicateCount = 1;
                    int i = 1, j = 1;
                    foreach (LayoutPosEditor layoutPosEditor in fillEnumerable)
                    {
                        FillPosition(layoutPosEditor, typeId, group, fillSettings);
                        if (replicateDirection == RepDirection.ByCol && fillDirection == Direction.Down)
                        {
                            if (replicateCount == replicates || i % rowWidth == 0)
                            {
                                replicateCount = 1;
                                group++;
                            }
                            else
                                replicateCount++;
                            if (i % rowWidth == 0)
                                i = 1;
                            else
                                i++;
                        }
                        else if (replicateDirection == RepDirection.ByCol && fillDirection == Direction.Across)
                        {
                            if (i == colWidth)
                            {
                                i = 1;
                                if (j % replicates == 0)
                                {
                                    j = 1;
                                    group++;
                                }
                                else
                                {
                                    j++;
                                    group -= colWidth - 1;
                                }
                            }
                            else
                            {
                                i++;
                                group++;
                            }
                        }
                        else if (replicateDirection == RepDirection.ByRow && fillDirection == Direction.Across)
                        {
                            if (replicateCount == replicates || i % colWidth == 0)
                            {
                                replicateCount = 1;
                                group++;
                            }
                            else
                                replicateCount++;
                            if (i % colWidth == 0)
                                i = 1;
                            else
                                i++;
                        }
                        else if (replicateDirection == RepDirection.ByRow && fillDirection == Direction.Down)
                        {
                            if (i == rowWidth)
                            {
                                i = 1;
                                if (j % replicates == 0)
                                {
                                    j = 1;
                                    group++;
                                }
                                else
                                {
                                    j++;
                                    group -= rowWidth - 1;
                                }
                            }
                            else
                            {
                                i++;
                                group++;
                            }
                        }
                    }
                }
                else
                {
                    // Get the enumerable which is the fill ordering
                    IEnumerable<LayoutPosEditor> fillEnumerable;
                    if (fillDirection == Direction.Across)
                    {
                        fillEnumerable = newState.GetEnumerableAcross(pos1, pos2);
                    }
                    else
                    {
                        fillEnumerable = newState.GetEnumerableDown(pos1, pos2);
                    }

                    // Fill with the enumerable using the specified number of replicates
                    int replicateCount = 1;
                    foreach (LayoutPosEditor layoutPosEditor in fillEnumerable)
                    {
                        FillPosition(layoutPosEditor, typeId, group, fillSettings);

                        if (replicateCount == replicates)
                        {
                            replicateCount = 1;
                            group++;
                        }
                        else
                            replicateCount++;
                    }
                }
            }
            // Store the new state - this is necessary and applies only the changes between the previous state and the new state
            CurrentState = newState;
            return group;
        }
        private void LocationMouseUp(int? data)
        {
            if (data.HasValue)
            {
                switch (ControlSettings.SelectionCommand)
                {
                    case SelectionCommand.Fill:
                        //keep the last position when mouse is up
                        this.clickLastPosition = data.Value;
                        if (this.clickFirstPosition != -1)
                        {
                            if (FillSettings.ShowNextTime)
                            {
                                //save the current direction and replicates
                                this.previewDirection = FillSettings.FillDirection;
                                this.previewReplicates = FillSettings.Replicates;
                                this.previewRectangleMode = FillSettings.RectangleMode;
                                this.previewReplicateDirection = FillSettings.ReplicateDirection;
                                this.previewGroupNum = FillSettings.GroupNum;
                                this.previewMode = true;

                                PreviewFillFirstAndLast();

                                var childWindowFillSettings = new FillSettingsPopup();
                                childWindowFillSettings.DataContext = FillSettings;
                                childWindowFillSettings.Closed += new EventHandler(FillSettingsDialog_Closed);
                                childWindowFillSettings.radioButtonAcross.Click += new RoutedEventHandler(radioButtonAcross_Click);
                                childWindowFillSettings.radioButtonDown.Click += new RoutedEventHandler(radioButtonDown_Click);
                                childWindowFillSettings.radioReplicateDirectionByCol.Click += new RoutedEventHandler(radioReplicateDirectionByCol_Click);
                                childWindowFillSettings.radioReplicateDirectionByRow.Click += new RoutedEventHandler(radioReplicateDirectionByRow_Click);
                                childWindowFillSettings.checkboxRectangleMode.Click += new RoutedEventHandler(checkboxRectangleMode_Click);
                                childWindowFillSettings.textBoxReplicates.TextChanged += new TextChangedEventHandler(textBoxReplicates_TextChanged);
                                childWindowFillSettings.textBoxGroupNum.TextChanged += new TextChangedEventHandler(textBoxGroupNum_TextChanged);
                                childWindowFillSettings.ShowInteractive();

                                return;
                            }
                            else
                            {
                                FillFirstAndLast(this.clickFirstPosition, data.Value);
                            }
                        }
                        break;
                    case SelectionCommand.Flag:
                        // This checks that mouse up is on the same position as mouse down (so user can cancel the flag by moving to outside the well on mouse up)
                        if (this.clickFirstPosition == data.Value)
                        {
                            if (!_editorStateHelper.IsPositionUsed(data.Value))
                                MessageBox.Show("Only positions with samples can be flagged.");
                            else
                                FlagPosition(data.Value);
                        }
                        break;
                }
                this.clickFirstPosition = -1;
            }
            this.mouseDown = false;
        }