public ConfigurationModel() { logic = new ConfigurationLogic(); AvailableProviders = new ObservableCollection<InputProvider>(); SetCurrentProvider = new DelegateCommand(OnSetCurrentProvider, OnCanSetCurrentProvider); RestartService = new DelegateCommand(OnRestartCommand); ShowConfiguration = new DelegateCommand(OnShowConfiguration, OnCanExecuteShowConfiguration); if(!DesignerProperties.GetIsInDesignMode(this)) Initialize(); }
/// <summary> /// Initializes a new instance of the <see cref="ProgramMngVM"/> class. /// </summary> public ProgramMngVM() { this.programManager = new ProgramManager(); this.programManager.Watcher.Created += this.NewModuleAdded; this.programManager.StepFinished += this.RefreshVM; this.config = new ConfigurationLogic(); this.StartButtonPath = @"\ButtonPictures\start.png"; this.StepButtonPath = @"\ButtonPictures\step.png"; this.StopButtonPath = @"\ButtonPictures\stop.png"; this.StartCommand = new Command(obj => { this.StartButtonPath = @"\ButtonPictures\start_pressed.png"; Dispatcher.CurrentDispatcher.Invoke(() => this.loopTask = new Task(() => { if (!this.programManager.RunActive) { this.programManager.SetActive(); this.programManager.Run(); this.RestoreSaveState(); } })).Start(); }); this.StepCommand = new Command(obj => { if (!this.programManager.RunActive) { this.programManager.SetActive(); this.programManager.RunLoop(0); // step this.programManager.StopActive(); } }); this.StopCommand = new Command(obj => { this.StartButtonPath = @"\ButtonPictures\start.png"; this.StopWaitForTask(); }); this.setPinCommand = new Command(obj => { var pin = obj as PinVM; this.SetSelectedPin(pin); }); this.removeCommand = new Command(obj => { this.StopWaitForTask(); var nodeInFieldVM = obj as ComponentVM; foreach (var n in this.programManager.FieldNodes) { if (nodeInFieldVM.Node == n) { this.RestoreSaveState(); this.programManager.FieldNodes.Remove(n); this.nodesVMInField.Remove(nodeInFieldVM); this.OnFieldComponentRemoved(this, new FieldComponentEventArgs(nodeInFieldVM)); this.RemoveDeletedComponentConnections(nodeInFieldVM); this.UpdateUndoHistory(); // Not sure if i have to update the program manager! this.SetSaveState(); break; } } this.FireOnComponentVMRemoved(nodeInFieldVM); this.programManager.FieldNodes.Remove(nodeInFieldVM.Node); }); this.addCommand = new Command(obj => { this.StopWaitForTask(); var representationNode = obj as ComponentRepresentationVM; this.RestoreSaveState(); var realComponent = representationNode.Node; var newGenerateComp = (IDisplayableNode)Activator.CreateInstance(realComponent.GetType()); this.programManager.FieldNodes.Add(newGenerateComp); var compVM = new ComponentVM(newGenerateComp, this.CreateUniqueName(realComponent), this.GetUniqueNumber(), this.setPinCommand, this.removeCommand, this.config); this.nodesVMInField.Add(compVM); this.FireOnFieldComponentAdded(compVM); this.UpdateUndoHistory(); this.SetSaveState(); }); this.UndoCommand = new Command(obj => { this.StopWaitForTask(); if (this.undoHistoryStack.Count > 0) { this.RestoreSaveState(); var history = this.undoHistoryStack.Pop(); this.redoHistoryStack.Push(history); var differencesComps = new List<ComponentVM>(this.NodesVMInField.Except(history.Item2)); differencesComps.AddRange(new List<ComponentVM>(history.Item2.Except(this.NodesVMInField))); var differencesConnects = new List<ConnectionVM>(this.ConnectionsVM.Except(history.Item1)); differencesConnects.AddRange(new List<ConnectionVM>(history.Item1.Except(this.ConnectionsVM))); if (differencesComps.Count == 0 && this.undoHistoryStack.Count > 0 && differencesConnects.Count == 0) { history = this.undoHistoryStack.Pop(); this.redoHistoryStack.Push(history); differencesComps = new List<ComponentVM>(this.NodesVMInField.Except(history.Item2)); differencesComps.AddRange(new List<ComponentVM>(history.Item2.Except(this.NodesVMInField))); differencesConnects = new List<ConnectionVM>(this.ConnectionsVM.Except(history.Item1)); differencesConnects.AddRange(new List<ConnectionVM>(history.Item1.Except(this.ConnectionsVM))); } foreach (var item in differencesComps) { if (!history.Item2.Contains(item)) { this.OnFieldComponentRemoved(this, new FieldComponentEventArgs(item)); } else { this.FireOnFieldComponentAdded(item); } } foreach (var item in differencesConnects) { if (!history.Item1.Contains(item)) { this.OnPinsDisconnected(this, new PinsConnectedEventArgs(item.OutputPin.Pin, item.InputPin.Pin)); } else { this.OnPinsConnected(this, new PinVMConnectionChangedEventArgs(item)); } } this.ConnectionsVM = new ObservableCollection<ConnectionVM>(history.Item1); this.NodesVMInField = new ObservableCollection<ComponentVM>(history.Item2); this.programManager.ConnectedOutputInputPairs = this.ConnectionsVM.Select(x => new Tuple<IPin, IPin>(x.InputPin.Pin, x.OutputPin.Pin)).ToList(); this.programManager.FieldNodes = this.NodesVMInField.Select(x => x.Node).ToList(); this.SetSaveState(); } }); this.RedoCommand = new Command(obj => { this.StopWaitForTask(); if (this.redoHistoryStack.Count > 0) { this.RestoreSaveState(); var futureHistory = this.redoHistoryStack.Pop(); this.undoHistoryStack.Push(futureHistory); var differencesComps = new List<ComponentVM>(this.NodesVMInField.Except(futureHistory.Item2)); differencesComps.AddRange(new List<ComponentVM>(futureHistory.Item2.Except(this.NodesVMInField))); var differencesConnects = new List<ConnectionVM>(this.ConnectionsVM.Except(futureHistory.Item1)); differencesConnects.AddRange(new List<ConnectionVM>(futureHistory.Item1.Except(this.ConnectionsVM))); if (differencesComps.Count == 0 && this.redoHistoryStack.Count > 0 && differencesConnects.Count == 0) { futureHistory = this.redoHistoryStack.Pop(); this.undoHistoryStack.Push(futureHistory); differencesComps = new List<ComponentVM>(this.NodesVMInField.Except(futureHistory.Item2)); differencesComps.AddRange(new List<ComponentVM>(futureHistory.Item2.Except(this.NodesVMInField))); differencesConnects = new List<ConnectionVM>(this.ConnectionsVM.Except(futureHistory.Item1)); differencesConnects.AddRange(new List<ConnectionVM>(futureHistory.Item1.Except(this.ConnectionsVM))); } foreach (var item in differencesComps) { if (!futureHistory.Item2.Contains(item)) { this.OnFieldComponentRemoved(this, new FieldComponentEventArgs(item)); } else { this.FireOnFieldComponentAdded(item); } } // differencesConnects.AddRange(new List<ConnectionVM>(futureHistory.Item1.Except(this.ConnectionsVM))); foreach (var item in differencesConnects) { if (!futureHistory.Item1.Contains(item)) { this.OnPinsDisconnected(this, new PinsConnectedEventArgs(item.OutputPin.Pin, item.InputPin.Pin)); } else { this.OnPinsConnected(this, new PinVMConnectionChangedEventArgs(item)); } } this.ConnectionsVM = new ObservableCollection<ConnectionVM>(futureHistory.Item1); this.NodesVMInField = new ObservableCollection<ComponentVM>(futureHistory.Item2); this.programManager.ConnectedOutputInputPairs = this.ConnectionsVM.Select(x => new Tuple<IPin, IPin>(x.OutputPin.Pin, x.InputPin.Pin)).ToList(); this.programManager.FieldNodes = this.NodesVMInField.Select(x => x.Node).ToList(); this.SetSaveState(); } }); var nodesInField = this.programManager.FieldNodes.Select(node => new ComponentVM( node, this.CreateUniqueName(node), this.GetUniqueNumber(), this.setPinCommand, this.removeCommand, this.config)); this.nodesVMInField = new ObservableCollection<ComponentVM>(nodesInField); var nodesToChoose = this.programManager.SerializationPathInfo.Select( node => new ComponentRepresentationVM(this.addCommand, node.Item1, node.Item2)); this.SelectableComponents = new ObservableCollection<ComponentRepresentationVM>(nodesToChoose); var connections = this.programManager.ConnectedOutputInputPairs.Select(conn => new ConnectionVM( new PinVM( conn.Item1, this.GetUniqueNumberFromEnumerator(), false, this.setPinCommand), new PinVM(conn.Item2, this.GetUniqueNumberFromEnumerator(), true, this.setPinCommand), this.NewUniqueConnectionId(), this.config.LinePassiveColor)); this.connectionsVM = new ObservableCollection<ConnectionVM>(connections); this.undoHistoryStack = new Stack<Tuple<ObservableCollection<ConnectionVM>, ObservableCollection<ComponentVM>>>(); this.redoHistoryStack = new Stack<Tuple<ObservableCollection<ConnectionVM>, ObservableCollection<ComponentVM>>>(); this.UpdateUndoHistory(); this.SetSaveState(); this.programManager.PinsDisconnected += this.OnPinsDisconnected; this.programManager.ConnectionUpdated += this.OnConnectionUpdated; }