diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-07-03 16:36:54 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-07-03 16:36:54 +0300 |
| commit | 856773f7eafb9d04500ede0cfae9c0e75231418b (patch) | |
| tree | d1b4f9577b5a47dd10fa7243823fd1016de1418e /Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels | |
| parent | 94e4072838c235be4d48a4635e47204a39cdd78b (diff) | |
| download | Tango-856773f7eafb9d04500ede0cfae9c0e75231418b.tar.gz Tango-856773f7eafb9d04500ede0cfae9c0e75231418b.zip | |
Implemented StubsView & VM !!!
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels')
3 files changed, 11 insertions, 599 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs deleted file mode 100644 index 451e1a1f0..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/CodeTabVM.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.Core.Commands; -using Tango.SharedUI; - -namespace Tango.MachineStudio.Stubs.ViewModels -{ - /// <summary> - /// Represents a single script editor tab view model; - /// </summary> - /// <seealso cref="Tango.SharedUI.ViewModel" /> - public class CodeTabVM : ViewModel - { - private String _title; - /// <summary> - /// Gets or sets the script title/file name. - /// </summary> - public String Title - { - get - { - return File != null ? Path.GetFileName(File) : _title; - } - set - { - _title = value; - RaisePropertyChanged(nameof(Title)); - } - } - - private String _file; - /// <summary> - /// Gets or sets the full script file path. - /// </summary> - public String File - { - get { return _file; } - set - { - _file = value; - RaisePropertyChanged(nameof(File)); - RaisePropertyChanged(nameof(Title)); - } - } - - private String _code; - /// <summary> - /// Gets or sets the script code. - /// </summary> - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - private bool _isRunning; - /// <summary> - /// Gets or sets a value indicating whether this instance is running. - /// </summary> - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChangedAuto(); } - } - - private RelayCommand _insertCodeSnippetCommand; - /// <summary> - /// Gets or sets the insert snippet command. (Inserts stub snippet to editor) - /// </summary> - public RelayCommand InsertSnippetCommand - { - get { return _insertCodeSnippetCommand; } - set { _insertCodeSnippetCommand = value; RaisePropertyChanged(nameof(InsertSnippetCommand)); } - } - - /// <summary> - /// Initializes a new instance of the <see cref="CodeTabVM"/> class. - /// </summary> - public CodeTabVM() - { - Title = "untitled"; - Code = Properties.Resources.CodeTabTemplate; - } - - /// <summary> - /// Returns a <see cref="System.String" /> that represents this instance. - /// </summary> - /// <returns> - /// A <see cref="System.String" /> that represents this instance. - /// </returns> - public override string ToString() - { - return Title; - } - } -} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs index afd5e4d70..f222b7584 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/MainViewVM.cs @@ -12,10 +12,12 @@ using Tango.Core.Commands; using Tango.MachineStudio.Common; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.Common.StudioApplication; +using Tango.PMR; using Tango.Scripting; using Tango.Settings; using Tango.SharedUI; using Tango.Stubs; +using Tango.Stubs.ViewModels; using Tango.Transport; using Tango.Transport.Adapters; @@ -27,8 +29,6 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// <seealso cref="Tango.SharedUI.ViewModel" /> public class MainViewVM : StudioViewModel<StubsModule> { - private UsbTransportAdapter _adapter; //Holds the USB transport adapter. - private StubManager _stubManager; private INotificationProvider _notification; private StubsModuleSettings _settings; @@ -58,162 +58,18 @@ namespace Tango.MachineStudio.Stubs.ViewModels } } + private StubsViewVM _stubsViewVM; /// <summary> - /// Gets or sets the code tabs. + /// Gets or sets the stubs view vm. /// </summary> - public ObservableCollection<CodeTabVM> CodeTabs { get; set; } - - /// <summary> - /// Gets or sets the additional highlight C# types. - /// </summary> - public ObservableCollection<KeyValuePair<String, Type>> HighlightTypes { get; set; } - - /// <summary> - /// Gets or sets the collection of stub snippets. - /// </summary> - public ObservableCollection<StubSnippetVM> StubSnippets { get; set; } - - private StubSnippetVM _selectedStubSnippet; - /// <summary> - /// Gets or sets the selected stub snippet. - /// </summary> - public StubSnippetVM SelectedStubSnippet - { - get { return _selectedStubSnippet; } - set { _selectedStubSnippet = value; RaisePropertyChanged(nameof(SelectedStubSnippet)); } - } - - private String _log; - /// <summary> - /// Gets or sets the current response log. - /// </summary> - public String Log + public StubsViewVM StubsViewVM { - get { return _log; } - set { _log = value; RaisePropertyChanged(nameof(Log)); } - } - - private CodeTabVM _selectedCodeTab; - /// <summary> - /// Gets or sets the selected code tab. - /// </summary> - public CodeTabVM SelectedCodeTab - { - get { return _selectedCodeTab; } - set { _selectedCodeTab = value; RaisePropertyChanged(nameof(SelectedCodeTab)); InvalidateRelayCommands(); } - } - - private bool _isConnected; - /// <summary> - /// Gets or sets a value indicating whether the USB adapter is connected. - /// </summary> - public bool IsConnected - { - get { return _isConnected; } - set { _isConnected = value; RaisePropertyChanged(nameof(IsConnected)); InvalidateRelayCommands(); } - } - - private List<String> _ports; - /// <summary> - /// Gets or sets the available USB ports. - /// </summary> - public List<String> Ports - { - get { return _ports; } - set { _ports = value; RaisePropertyChanged(nameof(Ports)); } - } - - private String _selectedPort; - /// <summary> - /// Gets or sets the selected USB port. - /// </summary> - public String SelectedPort - { - get { return _selectedPort; } - set { _selectedPort = value; RaisePropertyChanged(nameof(SelectedPort)); InvalidateRelayCommands(); } - } - - private String _status; - /// <summary> - /// Gets or sets the current status bar text. - /// </summary> - public String Status - { - get { return _status; } - set { _status = value; RaisePropertyChanged(nameof(Status)); } - } - - private bool _isRunning; - /// <summary> - /// Gets or sets a value indicating whether a stub is currently running. - /// </summary> - public bool IsRunning - { - get { return _isRunning; } - set { _isRunning = value; RaisePropertyChanged(nameof(IsRunning)); InvalidateRelayCommands(); } + get { return _stubsViewVM; } + set { _stubsViewVM = value; RaisePropertyChangedAuto(); } } #endregion - #region Commands - - /// <summary> - /// Gets or sets the new command. - /// </summary> - public RelayCommand NewCommand { get; set; } - - /// <summary> - /// Gets or sets the close tab command. - /// </summary> - public RelayCommand<CodeTabVM> CloseTabCommand { get; set; } - - /// <summary> - /// Gets or sets the run command. - /// </summary> - public RelayCommand RunCommand { get; set; } - - /// <summary> - /// Gets or sets the stop command. - /// </summary> - public RelayCommand StopCommand { get; set; } - - /// <summary> - /// Gets or sets the toggle connection command. - /// </summary> - public RelayCommand ToggleConnectionCommand { get; set; } - - /// <summary> - /// Gets or sets the open command. - /// </summary> - public RelayCommand OpenCommand { get; set; } - - /// <summary> - /// Gets or sets the save command. - /// </summary> - public RelayCommand SaveCommand { get; set; } - - /// <summary> - /// Gets or sets the save as command. - /// </summary> - public RelayCommand SaveAsCommand { get; set; } - - /// <summary> - /// Gets or sets the stub snippet selected command. - /// </summary> - public RelayCommand StubSnippetSelectedCommand { get; set; } - - /// <summary> - /// Gets or sets the insert snippet command. - /// </summary> - public RelayCommand<String> InsertSnippetCommand { get; set; } - - /// <summary> - /// Gets or sets the exit command. - /// </summary> - public RelayCommand ExitCommand { get; set; } - - #endregion - #region Constructors /// <summary> @@ -221,310 +77,14 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// </summary> public MainViewVM(IStudioApplicationManager applicationManager, INotificationProvider notification) { + StubsViewVM = new StubsViewVM(ConnectionMode.External); + _settings = SettingsManager.Default.GetOrCreate<StubsModuleSettings>(); ApplicationManager = applicationManager; _notification = notification; - CodeTabs = new ObservableCollection<CodeTabVM>(); - - NewCommand = new RelayCommand(CreateNewTab); - CloseTabCommand = new RelayCommand<CodeTabVM>(OnTabClosing); - RunCommand = new RelayCommand(RunTab, (x) => (IsConnected || UseConnectedMachine) && !IsRunning && SelectedCodeTab != null); - StopCommand = new RelayCommand(StopTab, (x) => (IsConnected || UseConnectedMachine) && IsRunning && SelectedCodeTab != null); - InsertSnippetCommand = new RelayCommand<string>((x) => { }); - - HighlightTypes = new ObservableCollection<KeyValuePair<string, Type>>(); - HighlightTypes.Add(new KeyValuePair<string, Type>("stubManager", typeof(StubManager))); - - StubSnippets = new ObservableCollection<StubSnippetVM>(); - - foreach (var stubType in StubBase.GetAvailableRequestStubs()) - { - StubSnippetVM snippet = new StubSnippetVM(); - snippet.Name = stubType.Name.Replace("Stub_", ""); - snippet.Code = String.Format("stubManager.Run(\"{0}\" ,{1});", stubType.Name, String.Join(", ", stubType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Select(x => x.PropertyType.Name == "string" ? "\"string\"" : x.PropertyType.Name))); - StubSnippets.Add(snippet); - } - - ToggleConnectionCommand = new RelayCommand(ToggleConnection, (x) => !IsRunning); - OpenCommand = new RelayCommand(OpenFile); - SaveCommand = new RelayCommand(SaveFile); - SaveAsCommand = new RelayCommand(SaveAsFile); - StubSnippetSelectedCommand = new RelayCommand(OnStubSnippetSelected); - ExitCommand = new RelayCommand(() => Application.Current.Shutdown()); - - Ports = new List<string>() - { - "COM1", - "COM2", - "COM3", - "COM4", - "COM5", - "COM6", - "COM7", - "COM8", - "COM9", - }; - - SelectedPort = _settings.SelectedPort != null ? _settings.SelectedPort : Ports.First(); - - Status = "Ready"; - - if (_settings.LastTabs.Count > 0) - { - foreach (var file in _settings.LastTabs) - { - if (File.Exists(file)) - { - OpenFile(file); - } - } - } - else - { - CreateNewTab(); - } - } - - #endregion - - #region Virtual Methods - - /// <summary> - /// Called when a stub snippet is double clicked. - /// </summary> - protected virtual void OnStubSnippetSelected() - { - if (SelectedStubSnippet != null) - { - if (InsertSnippetCommand != null) - { - InsertSnippetCommand.Execute(SelectedStubSnippet.Code); - } - } - } - - /// <summary> - /// Called when user closes a script tab. - /// </summary> - /// <param name="codeTab">The code tab.</param> - protected virtual void OnTabClosing(CodeTabVM codeTab) - { - CodeTabs.Remove(codeTab); - } - - #endregion - - #region Private Methods - - /// <summary> - /// Saves the selected script file. - /// </summary> - private void SaveFile() - { - if (SelectedCodeTab != null) - { - if (SelectedCodeTab.File == null) - { - SaveAsFile(); - } - else - { - File.WriteAllText(SelectedCodeTab.File, SelectedCodeTab.Code); - } - } - } - - /// <summary> - /// Saves the selected script file. - /// </summary> - private void SaveAsFile() - { - if (SelectedCodeTab != null) - { - SaveFileDialog dlg = new SaveFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.DefaultExt = ".cs"; - if (dlg.ShowDialog().Value) - { - File.WriteAllText(dlg.FileName, SelectedCodeTab.Code); - SelectedCodeTab.File = dlg.FileName; - } - } - } - - /// <summary> - /// Opens a script from HD. - /// </summary> - private void OpenFile() - { - OpenFileDialog dlg = new OpenFileDialog(); - dlg.Filter = "C# Script Files|*.cs"; - dlg.Multiselect = true; - if (dlg.ShowDialog().Value) - { - foreach (var file in dlg.FileNames) - { - OpenFile(file); - } - } - } - - /// <summary> - /// Opens the file. - /// </summary> - /// <param name="file">The file.</param> - private void OpenFile(String file) - { - var newTab = new CodeTabVM(); - newTab.File = file; - newTab.Code = File.ReadAllText(file); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// <summary> - /// Toggles the USB adapter connection. - /// </summary> - private void ToggleConnection() - { - try - { - if (!IsConnected) - { - _adapter = new UsbTransportAdapter(SelectedPort); - _adapter.Connect().Wait(); - IsConnected = true; - } - else - { - _adapter.Disconnect().Wait(); - IsConnected = false; - } - } - catch (Exception ex) - { - MessageBox.Show(ex.ToString(), "Tango"); - } - } - - /// <summary> - /// Creates a new script tab. - /// </summary> - private void CreateNewTab() - { - var newTab = new CodeTabVM(); - CodeTabs.Add(newTab); - SelectedCodeTab = newTab; - } - - /// <summary> - /// Runs the selected script tab. - /// </summary> - private async void RunTab() - { - if (UseConnectedMachine && !ApplicationManager.IsMachineConnected) - { - _notification.ShowError("Cannot execute stub while 'Connected Machine' is set but no machine connected."); - return; - } - - IsRunning = true; - SelectedCodeTab.IsRunning = true; - Log = (DateTime.Now.ToTimeString() + ": ") + "Executing script '" + SelectedCodeTab.Title + "'..." + Environment.NewLine; - - await Task.Factory.StartNew(async () => - { - try - { - ITransportAdapter adapter = _adapter; - - if (ApplicationManager.IsMachineConnected && UseConnectedMachine) - { - adapter = ApplicationManager.ConnectedMachine.Adapter; - } - - _stubManager = new StubManager(adapter); - var thisStubManager = _stubManager; - _stubManager.Completed += Manager_Completed; - _stubManager.Failed += Manager_Failed; - _stubManager.Executed += Manager_Executed; - - ScriptEngine engine = new ScriptEngine(new StubOnExecuteParameters(_stubManager)); - - engine.ReferencedAssemblies.Add(this.GetType()); - await engine.Run(SelectedCodeTab.Code,null); - - if (!thisStubManager.Aborted) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - } - } - catch (Exception ex) - { - IsRunning = false; - SelectedCodeTab.IsRunning = false; - MessageBox.Show(ex.Message, "Tango"); - } - }); - } - - /// <summary> - /// Stops the currently current script. - /// </summary> - private void StopTab() - { - if (_stubManager != null) - { - _stubManager.Abort(); - IsRunning = false; - SelectedCodeTab.IsRunning = false; - Status = "Stopped!"; - Log += (DateTime.Now.ToTimeString() + ": ") + "Stopped!" + Environment.NewLine; - } - } - - #endregion - - #region Event Handlers - - /// <summary> - /// Handled the <see cref="StubManager"/> Executed event. - /// </summary> - /// <param name="sender">The sender.</param> - /// <param name="stubName">Name of the stub.</param> - private void Manager_Executed(object sender, string stubName) - { - Log += (DateTime.Now.ToTimeString() + ": ") + "Executing '" + stubName + "'..." + Environment.NewLine; - Status = "Executing " + stubName + "..."; - } - - /// <summary> - /// Handled the <see cref="StubManager"/> Failed event. - /// </summary> - /// <param name="sender">The sender.</param> - /// <param name="ex">The exception.</param> - private void Manager_Failed(object sender, Exception ex) - { - if (IsRunning) - { - Log += (DateTime.Now.ToTimeString() + ": ") + ex.Message + Environment.NewLine; - Status = "Failed!"; - } - } - - /// <summary> - /// Handled the <see cref="StubManager"/> Completed event. - /// </summary> - /// <param name="sender">The sender.</param> - /// <param name="response">The response.</param> - private void Manager_Completed(object sender, string response) - { - Log += (DateTime.Now.ToTimeString() + ": ") + "Response Received:" + Environment.NewLine; - Log += (DateTime.Now.ToTimeString() + ": ") + response + Environment.NewLine; - Status = "Completed"; + StubsViewVM.Bind(nameof(StubsViewVM.MachineOperator), ApplicationManager, nameof(ApplicationManager.ConnectedMachine)); } #endregion @@ -537,8 +97,7 @@ namespace Tango.MachineStudio.Stubs.ViewModels /// <returns></returns> public override void OnShuttingDown() { - _settings.SelectedPort = SelectedPort; - _settings.LastTabs = CodeTabs.Select(x => x.File).ToList(); + } #endregion diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs deleted file mode 100644 index d99906f20..000000000 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Stubs/ViewModels/StubSnippetVM.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Tango.SharedUI; - -namespace Tango.MachineStudio.Stubs.ViewModels -{ - /// <summary> - /// Represents a single stub snippet view model. - /// </summary> - /// <seealso cref="Tango.SharedUI.ViewModel" /> - public class StubSnippetVM : ViewModel - { - private String _name; - /// <summary> - /// Gets or sets the stub name. - /// </summary> - public String Name - { - get { return _name; } - set { _name = value; RaisePropertyChanged(nameof(Name)); } - } - - private String _code; - /// <summary> - /// Gets or sets the snippet code. - /// </summary> - public String Code - { - get { return _code; } - set { _code = value; RaisePropertyChanged(nameof(Code)); } - } - - /// <summary> - /// Returns a <see cref="System.String" /> that represents this instance. - /// </summary> - /// <returns> - /// A <see cref="System.String" /> that represents this instance. - /// </returns> - public override string ToString() - { - return Name; - } - } -} |
