aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/ViewModels/MainViewVM.cs
blob: 3dd4e62a34311a83fca87220bcd519314995b36e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using Tango.Scripting.Editors.Utils;
using System;

namespace Tango.Scripting.Editors
{
	/// <summary>
	/// Contains weak event managers for <see cref="ITextEditorComponent"/>.
	/// </summary>
	public static class TextEditorWeakEventManager
	{
		/// <summary>
		/// Weak event manager for the <see cref="ITextEditorComponent.DocumentChanged"/> event.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
		public sealed class DocumentChanged : WeakEventManagerBase<DocumentChanged, ITextEditorComponent>
		{
			/// <inheritdoc/>
			protected override void StartListening(ITextEditorComponent source)
			{
				source.DocumentChanged += DeliverEvent;
			}
			
			/// <inheritdoc/>
			protected override void StopListening(ITextEditorComponent source)
			{
				source.DocumentChanged -= DeliverEvent;
			}
		}
		
		/// <summary>
		/// Weak event manager for the <see cref="ITextEditorComponent.OptionChanged"/> event.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
		public sealed class OptionChanged : WeakEventManagerBase<OptionChanged, ITextEditorComponent>
		{
			/// <inheritdoc/>
			protected override void StartListening(ITextEditorComponent source)
			{
				source.OptionChanged += DeliverEvent;
			}
			
			/// <inheritdoc/>
			protected override void StopListening(ITextEditorComponent source)
			{
				source.OptionChanged -= DeliverEvent;
			}
		}
	}
}
d='n404' href='#n404'>404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.BL.Entities;
using Tango.MachineStudio.Common.Notifications;
using Tango.SharedUI;
using Tango.BL;
using Tango.SharedUI.Components;
using System.Runtime.CompilerServices;

namespace Tango.MachineStudio.HardwareDesigner.ViewModels
{
    public class MainViewVM : ViewModel
    {
        private INotificationProvider _notification;
        private bool _isNew;

        private ObservablesEntitiesAdapter _adapter;
        public ObservablesEntitiesAdapter Adapter
        {
            get { return _adapter; }
            set { _adapter = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareMotorType> _motorTypes;
        public SelectedObjectCollection<HardwareMotorType> MotorTypes
        {
            get { return _motorTypes; }
            set { _motorTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareMotorType> _selectedMotorTypes;
        public ObservableCollection<HardwareMotorType> SelectedMotorTypes
        {
            get { return _selectedMotorTypes; }
            set { _selectedMotorTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareDancerType> _dancerTypes;
        public SelectedObjectCollection<HardwareDancerType> DancerTypes
        {
            get { return _dancerTypes; }
            set { _dancerTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareDancerType> _selectedDancerTypes;
        public ObservableCollection<HardwareDancerType> SelectedDancerTypes
        {
            get { return _selectedDancerTypes; }
            set { _selectedDancerTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwarePidControlType> _pidControlTypes;
        public SelectedObjectCollection<HardwarePidControlType> PidControlTypes
        {
            get { return _pidControlTypes; }
            set { _pidControlTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwarePidControlType> _selectedPidControlTypes;
        public ObservableCollection<HardwarePidControlType> SelectedPidControlTypes
        {
            get { return _selectedPidControlTypes; }
            set { _selectedPidControlTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareWinderType> _winderTypes;
        public SelectedObjectCollection<HardwareWinderType> WinderTypes
        {
            get { return _winderTypes; }
            set { _winderTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareWinderType> _selectedWinderTypes;
        public ObservableCollection<HardwareWinderType> SelectedWinderTypes
        {
            get { return _selectedWinderTypes; }
            set { _selectedWinderTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareSpeedSensorType> _speedSensorTypes;
        public SelectedObjectCollection<HardwareSpeedSensorType> SpeedSensorTypes
        {
            get { return _speedSensorTypes; }
            set { _speedSensorTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareSpeedSensorType> _selectedSpeedSensorTypes;
        public ObservableCollection<HardwareSpeedSensorType> SelectedSpeedSensorTypes
        {
            get { return _selectedSpeedSensorTypes; }
            set { _selectedSpeedSensorTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareBlowerType> _blowerTypes;
        public SelectedObjectCollection<HardwareBlowerType> BlowerTypes
        {
            get { return _blowerTypes; }
            set { _blowerTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareBlowerType> _selectedBlowerTypes;
        public ObservableCollection<HardwareBlowerType> SelectedBlowerTypes
        {
            get { return _selectedBlowerTypes; }
            set { _selectedBlowerTypes = value; RaisePropertyChangedAuto(); }
        }

        private SelectedObjectCollection<HardwareBreakSensorType> _breakSensorTypes;
        public SelectedObjectCollection<HardwareBreakSensorType> BreakSensorTypes
        {
            get { return _breakSensorTypes; }
            set { _breakSensorTypes = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<HardwareBreakSensorType> _selectedBreakSensorTypes;
        public ObservableCollection<HardwareBreakSensorType> SelectedBreakSensorTypes
        {
            get { return _selectedBreakSensorTypes; }
            set { _selectedBreakSensorTypes = value; RaisePropertyChangedAuto(); }
        }

        private HardwareVersion _selectedVersion;
        public HardwareVersion SelectedVersion
        {
            get { return _selectedVersion; }
            set { _selectedVersion = value; RaisePropertyChangedAuto(); OnSelectedVersionChanged(); }
        }

        private HardwareVersion _currentVersion;
        public HardwareVersion CurrentVersion
        {
            get { return _currentVersion; }
            set { _currentVersion = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        private IObservableEntity _selectedHardwareObject;
        public IObservableEntity SelectedHardwareObject
        {
            get { return _selectedHardwareObject; }
            set
            {
                _selectedHardwareObject = null;
                RaisePropertyChangedAuto();
                _selectedHardwareObject = value;
                RaisePropertyChangedAuto();
            }
        }

        private Object _selectedHardwareObjectType;
        public Object SelectedHardwareObjectType
        {
            get { return _selectedHardwareObjectType; }
            set
            {
                _selectedHardwareObjectType = null;
                RaisePropertyChangedAuto();
                _selectedHardwareObjectType = value;
                RaisePropertyChangedAuto();
                OnSelectedHardwareObjectTypeChanged();
            }
        }

        public RelayCommand SaveCommand { get; set; }

        public RelayCommand DeleteCommand { get; set; }

        public RelayCommand NewCommand { get; set; }

        public RelayCommand CloneCommand { get; set; }

        public RelayCommand CopyParametersCommand { get; set; }

        public MainViewVM(INotificationProvider notification)
        {
            _notification = notification;

            Adapter = ObservablesEntitiesAdapter.Instance;
            SaveCommand = new RelayCommand(Save, () => SelectedVersion != null);
            NewCommand = new RelayCommand(New);
            DeleteCommand = new RelayCommand(Delete, () => !_isNew && SelectedVersion != null);

            CurrentVersion = new HardwareVersion();
            CreateTemplate(CurrentVersion);

            CopyParametersCommand = new RelayCommand(CopyParameters, (x) => SelectedVersion != null && SelectedHardwareObjectType != null);
            CloneCommand = new RelayCommand(CloneCurrentVersion, () => SelectedVersion != null);
        }

        private void CopyParameters(object obj)
        {
            IObservableEntity source = obj.GetType().GetProperty("Data").GetValue(obj) as IObservableEntity;
            IObservableEntity target = null;

            if (source is HardwareMotorType)
            {
                target = CurrentVersion.HardwareMotors.SingleOrDefault(x => x.HardwareMotorType == source);
            }
            else if (source is HardwareDancerType)
            {
                target = CurrentVersion.HardwareDancers.SingleOrDefault(x => x.HardwareDancerType == source);
            }
            else if (source is HardwarePidControlType)
            {
                target = CurrentVersion.HardwarePidControls.SingleOrDefault(x => x.HardwarePidControlType == source);
            }
            else if (source is HardwareWinderType)
            {
                target = CurrentVersion.HardwareWinders.SingleOrDefault(x => x.HardwareWinderType == source);
            }
            else if (source is HardwareSpeedSensorType)
            {
                target = CurrentVersion.HardwareSpeedSensors.SingleOrDefault(x => x.HardwareSpeedSensorType == source);
            }
            else if (source is HardwareBlowerType)
            {
                target = CurrentVersion.HardwareBlowers.SingleOrDefault(x => x.HardwareBlowerType == source);
            }
            else if (source is HardwareBreakSensorType)
            {
                target = CurrentVersion.HardwareBreakSensors.SingleOrDefault(x => x.HardwareBreakSensorType == source);
            }

            target.MapPrimitivesTo(SelectedHardwareObject);
        }

        private void OnSelectedHardwareObjectTypeChanged()
        {
            if (SelectedHardwareObjectType != null)
            {
                if (SelectedHardwareObjectType is SelectedObject<HardwareMotorType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareMotorType>).Data;
                    var hardwareObj = CurrentVersion.HardwareMotors.SingleOrDefault(x => x.HardwareMotorType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareMotor() { HardwareMotorType = type };
                        CurrentVersion.HardwareMotors.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwareDancerType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareDancerType>).Data;
                    var hardwareObj = CurrentVersion.HardwareDancers.SingleOrDefault(x => x.HardwareDancerType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareDancer() { HardwareDancerType = type };
                        CurrentVersion.HardwareDancers.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwarePidControlType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwarePidControlType>).Data;
                    var hardwareObj = CurrentVersion.HardwarePidControls.SingleOrDefault(x => x.HardwarePidControlType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwarePidControl() { HardwarePidControlType = type };
                        CurrentVersion.HardwarePidControls.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwareWinderType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareWinderType>).Data;
                    var hardwareObj = CurrentVersion.HardwareWinders.SingleOrDefault(x => x.HardwareWinderType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareWinder() { HardwareWinderType = type };
                        CurrentVersion.HardwareWinders.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwareSpeedSensorType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareSpeedSensorType>).Data;
                    var hardwareObj = CurrentVersion.HardwareSpeedSensors.SingleOrDefault(x => x.HardwareSpeedSensorType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareSpeedSensor() { HardwareSpeedSensorType = type };
                        CurrentVersion.HardwareSpeedSensors.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwareBlowerType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareBlowerType>).Data;
                    var hardwareObj = CurrentVersion.HardwareBlowers.SingleOrDefault(x => x.HardwareBlowerType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareBlower() { HardwareBlowerType = type };
                        CurrentVersion.HardwareBlowers.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
                else if (SelectedHardwareObjectType is SelectedObject<HardwareBreakSensorType>)
                {
                    var type = (SelectedHardwareObjectType as SelectedObject<HardwareBreakSensorType>).Data;
                    var hardwareObj = CurrentVersion.HardwareBreakSensors.SingleOrDefault(x => x.HardwareBreakSensorType == type);

                    if (hardwareObj != null)
                    {
                        SelectedHardwareObject = hardwareObj;
                    }
                    else
                    {
                        hardwareObj = new HardwareBreakSensor() { HardwareBreakSensorType = type };
                        CurrentVersion.HardwareBreakSensors.Add(hardwareObj);
                        SelectedHardwareObject = hardwareObj;
                    }
                }
            }
        }

        private void CreateTemplate(HardwareVersion version)
        {
            if (version == null)
            {
                SelectedMotorTypes = new ObservableCollection<HardwareMotorType>();
                SelectedDancerTypes = new ObservableCollection<HardwareDancerType>();
                SelectedPidControlTypes = new ObservableCollection<HardwarePidControlType>();
                SelectedWinderTypes = new ObservableCollection<HardwareWinderType>();
                SelectedSpeedSensorTypes = new ObservableCollection<HardwareSpeedSensorType>();
                SelectedBlowerTypes = new ObservableCollection<HardwareBlowerType>();
                SelectedBreakSensorTypes = new ObservableCollection<HardwareBreakSensorType>();
            }
            else
            {
                SelectedMotorTypes = version.HardwareMotors.Select(x => x.HardwareMotorType).ToObservableCollection();
                SelectedDancerTypes = version.HardwareDancers.Select(x => x.HardwareDancerType).ToObservableCollection();
                SelectedPidControlTypes = version.HardwarePidControls.Select(x => x.HardwarePidControlType).ToObservableCollection();
                SelectedWinderTypes = version.HardwareWinders.Select(x => x.HardwareWinderType).ToObservableCollection();
                SelectedSpeedSensorTypes = version.HardwareSpeedSensors.Select(x => x.HardwareSpeedSensorType).ToObservableCollection();
                SelectedBlowerTypes = version.HardwareBlowers.Select(x => x.HardwareBlowerType).ToObservableCollection();
                SelectedBreakSensorTypes = version.HardwareBreakSensors.Select(x => x.HardwareBreakSensorType).ToObservableCollection();
            }

            MotorTypes = new SelectedObjectCollection<HardwareMotorType>(Adapter.HardwareMotorTypes, SelectedMotorTypes);
            DancerTypes = new SelectedObjectCollection<HardwareDancerType>(Adapter.HardwareDancerTypes, SelectedDancerTypes);
            PidControlTypes = new SelectedObjectCollection<HardwarePidControlType>(Adapter.HardwarePidControlTypes, SelectedPidControlTypes);
            WinderTypes = new SelectedObjectCollection<HardwareWinderType>(Adapter.HardwareWinderTypes, SelectedWinderTypes);
            SpeedSensorTypes = new SelectedObjectCollection<HardwareSpeedSensorType>(Adapter.HardwareSpeedSensorTypes, SelectedSpeedSensorTypes);
            BlowerTypes = new SelectedObjectCollection<HardwareBlowerType>(Adapter.HardwareBlowerTypes, SelectedBlowerTypes);
            BreakSensorTypes = new SelectedObjectCollection<HardwareBreakSensorType>(Adapter.HardwareBreakSensorTypes, SelectedBreakSensorTypes);
        }

        private void OnSelectedVersionChanged()
        {
            if (SelectedVersion != null)
            {
                _isNew = false;
                CurrentVersion = SelectedVersion.Clone();
                CreateTemplate(CurrentVersion);
            }

            InvalidateRelayCommands();
        }

        private bool CheckCurrentVersionNull()
        {
            if (CurrentVersion == null)
            {
                _notification.ShowInfo("Please select a hardware version before attempting to insert any components.");
                return true;
            }

            return false;
        }

        private void New()
        {
            String name = _notification.ShowTextInput("Enter hardware version name", "Name");

            if (!String.IsNullOrWhiteSpace(name))
            {
                SelectedVersion = null;
                CurrentVersion = new HardwareVersion();
                CurrentVersion.Version = Adapter.HardwareVersions.Max(x => x.Version) + 1;
                CurrentVersion.Name = name;
                CreateTemplate(CurrentVersion);
                _isNew = true;
                InvalidateRelayCommands();
            }
        }

        private async void Save()
        {
            if (CurrentVersion != null)
            {
                using (_notification.PushTaskItem("Saving hardware version..."))
                {
                    await Task.Factory.StartNew(() =>
                    {
                        HardwareVersion realVersion = null;

                        if (_isNew)
                        {
                            realVersion = CurrentVersion.Clone();

                            realVersion.HardwareMotors.ToList().Where(x => !SelectedMotorTypes.Contains(x.HardwareMotorType)).ToList().ForEach(x => realVersion.HardwareMotors.Remove(x));
                            realVersion.HardwareDancers.ToList().Where(x => !SelectedDancerTypes.Contains(x.HardwareDancerType)).ToList().ForEach(x => realVersion.HardwareDancers.Remove(x));
                            realVersion.HardwarePidControls.ToList().Where(x => !SelectedPidControlTypes.Contains(x.HardwarePidControlType)).ToList().ForEach(x => realVersion.HardwarePidControls.Remove(x));
                            realVersion.HardwareWinders.ToList().Where(x => !SelectedWinderTypes.Contains(x.HardwareWinderType)).ToList().ForEach(x => realVersion.HardwareWinders.Remove(x));
                            realVersion.HardwareSpeedSensors.ToList().Where(x => !SelectedSpeedSensorTypes.Contains(x.HardwareSpeedSensorType)).ToList().ForEach(x => realVersion.HardwareSpeedSensors.Remove(x));
                            realVersion.HardwareBlowers.ToList().Where(x => !SelectedBlowerTypes.Contains(x.HardwareBlowerType)).ToList().ForEach(x => realVersion.HardwareBlowers.Remove(x));
                            realVersion.HardwareBreakSensors.ToList().Where(x => !SelectedBreakSensorTypes.Contains(x.HardwareBreakSensorType)).ToList().ForEach(x => realVersion.HardwareBreakSensors.Remove(x));
                        }
                        else
                        {
                            realVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == SelectedVersion.Guid);

                            realVersion.Version = CurrentVersion.Version;
                            realVersion.Name = CurrentVersion.Name;

                            realVersion.HardwareDancers.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwareMotors.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwarePidControls.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwareWinders.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwareSpeedSensors.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwareBlowers.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));
                            realVersion.HardwareBreakSensors.ToList().ForEach(x => x.DefferedDelete(Adapter.Context));

                            realVersion.HardwareDancers.Clear();
                            realVersion.HardwareMotors.Clear();
                            realVersion.HardwarePidControls.Clear();
                            realVersion.HardwareWinders.Clear();
                            realVersion.HardwareSpeedSensors.Clear();
                            realVersion.HardwareBlowers.Clear();
                            realVersion.HardwareBreakSensors.Clear();

                            foreach (var type in SelectedDancerTypes)
                            {
                                var item = CurrentVersion.HardwareDancers.SingleOrDefault(x => x.HardwareDancerType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareDancers.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareDancers.Add(new HardwareDancer()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareDancerType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedMotorTypes)
                            {
                                var item = CurrentVersion.HardwareMotors.SingleOrDefault(x => x.HardwareMotorType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareMotors.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareMotors.Add(new HardwareMotor()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareMotorType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedPidControlTypes)
                            {
                                var item = CurrentVersion.HardwarePidControls.SingleOrDefault(x => x.HardwarePidControlType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwarePidControls.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwarePidControls.Add(new HardwarePidControl()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwarePidControlType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedWinderTypes)
                            {
                                var item = CurrentVersion.HardwareWinders.SingleOrDefault(x => x.HardwareWinderType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareWinders.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareWinders.Add(new HardwareWinder()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareWinderType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedSpeedSensorTypes)
                            {
                                var item = CurrentVersion.HardwareSpeedSensors.SingleOrDefault(x => x.HardwareSpeedSensorType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareSpeedSensors.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareSpeedSensors.Add(new HardwareSpeedSensor()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareSpeedSensorType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedBlowerTypes)
                            {
                                var item = CurrentVersion.HardwareBlowers.SingleOrDefault(x => x.HardwareBlowerType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareBlowers.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareBlowers.Add(new HardwareBlower()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareBlowerType = type
                                    });
                                }
                            }

                            foreach (var type in SelectedBreakSensorTypes)
                            {
                                var item = CurrentVersion.HardwareBreakSensors.SingleOrDefault(x => x.HardwareBreakSensorType == type);
                                if (item != null)
                                {
                                    item.HardwareVersionGuid = realVersion.Guid;
                                    realVersion.HardwareBreakSensors.Add(item);
                                }
                                else
                                {
                                    realVersion.HardwareBreakSensors.Add(new HardwareBreakSensor()
                                    {
                                        HardwareVersionGuid = realVersion.Guid,
                                        HardwareBreakSensorType = type
                                    });
                                }
                            }
                        }



                        if (_isNew)
                        {
                            Adapter.Context.HardwareVersions.Add(realVersion);
                        }

                        realVersion.Save(Adapter.Context);

                        SelectedVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == realVersion.Guid);
                    });
                }
            }
        }

        private async void CloneCurrentVersion()
        {
            if (CurrentVersion != null)
            {
                String name = _notification.ShowTextInput("Enter new hardware configuration name", "Name", CurrentVersion.Name + " - Copy");

                if (!String.IsNullOrWhiteSpace(name))
                {
                    using (_notification.PushTaskItem("Cloning hardware configuration..."))
                    {
                        await Task.Factory.StartNew(() =>
                        {
                            var realVersion = CurrentVersion.Clone();
                            realVersion.Name = name;
                            realVersion.Version = 1;

                            realVersion.HardwareMotors.ToList().Where(x => !SelectedMotorTypes.Contains(x.HardwareMotorType)).ToList().ForEach(x => realVersion.HardwareMotors.Remove(x));
                            realVersion.HardwareDancers.ToList().Where(x => !SelectedDancerTypes.Contains(x.HardwareDancerType)).ToList().ForEach(x => realVersion.HardwareDancers.Remove(x));
                            realVersion.HardwarePidControls.ToList().Where(x => !SelectedPidControlTypes.Contains(x.HardwarePidControlType)).ToList().ForEach(x => realVersion.HardwarePidControls.Remove(x));
                            realVersion.HardwareWinders.ToList().Where(x => !SelectedWinderTypes.Contains(x.HardwareWinderType)).ToList().ForEach(x => realVersion.HardwareWinders.Remove(x));
                            realVersion.HardwareSpeedSensors.ToList().Where(x => !SelectedSpeedSensorTypes.Contains(x.HardwareSpeedSensorType)).ToList().ForEach(x => realVersion.HardwareSpeedSensors.Remove(x));
                            realVersion.HardwareBlowers.ToList().Where(x => !SelectedBlowerTypes.Contains(x.HardwareBlowerType)).ToList().ForEach(x => realVersion.HardwareBlowers.Remove(x));
                            realVersion.HardwareBreakSensors.ToList().Where(x => !SelectedBreakSensorTypes.Contains(x.HardwareBreakSensorType)).ToList().ForEach(x => realVersion.HardwareBreakSensors.Remove(x));

                            realVersion.HardwareMotors.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwareDancers.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwarePidControls.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwareWinders.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwareSpeedSensors.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwareBlowers.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);
                            realVersion.HardwareBreakSensors.ToList().ForEach(x => x.HardwareVersionGuid = realVersion.Guid);

                            realVersion.HardwareMotors.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwareDancers.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwarePidControls.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwareWinders.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwareSpeedSensors.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwareBlowers.ToList().ForEach(x => x.HardwareVersion = realVersion);
                            realVersion.HardwareBreakSensors.ToList().ForEach(x => x.HardwareVersion = realVersion);

                            Adapter.Context.HardwareVersions.Add(realVersion);
                            realVersion.Save(Adapter.Context);

                            SelectedVersion = Adapter.HardwareVersions.SingleOrDefault(x => x.Guid == realVersion.Guid);
                        });
                    }
                }
            }
        }

        private void Delete()
        {
            if (_notification.ShowQuestion("Are you sure you want to delete this hardware version?"))
            {
                using (_notification.PushTaskItem("Deleting hardware version..."))
                {
                    SelectedVersion.DeleteAsync(Adapter.Context);
                    SelectedVersion = null;
                    CurrentVersion = null;
                }
            }
        }

        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);
            InvalidateRelayCommands();
        }
    }
}