AddDataSourceCommand.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using Aitex.UI.Charting.Model;
  7. using Aitex.UI.Charting.ViewModel;
  8. using System.Windows;
  9. using DataAnalysisControl.Core;
  10. namespace Aitex.UI.Charting.Command
  11. {
  12. public class AddDataSourceCommand : ICommand
  13. {
  14. public AddDataSourceCommand(IDataSource dataSource)
  15. {
  16. _dataSource = dataSource;
  17. }
  18. IDataSource _dataSource;
  19. public bool CanExecute(object parameter)
  20. {
  21. return true;
  22. }
  23. #pragma warning disable 0067
  24. public event EventHandler CanExecuteChanged;
  25. #pragma warning restore 0067
  26. private void AsyncShowWarningMessageBox(string message, string title)
  27. {
  28. Application.Current.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(message, title, MessageBoxButton.OK, MessageBoxImage.Warning)));
  29. }
  30. public void Execute(object parameter)
  31. {
  32. string reason = string.Empty;
  33. try
  34. {
  35. var commonViewModel = (CommonViewModel)parameter;
  36. if (commonViewModel.DataSources.ContainsKey(_dataSource.Title))
  37. {
  38. if (_dataSource.BeginTime == commonViewModel.DataSources[_dataSource.Title].BeginTime &&
  39. _dataSource.EndTime == commonViewModel.DataSources[_dataSource.Title].EndTime)
  40. {
  41. reason = string.Format("数据名 \"{0}\" 已存在,未添加到数据源列表中...\n", _dataSource.Title);
  42. CONTEXT.WriteLog(reason);
  43. AsyncShowWarningMessageBox(reason, "警告");
  44. return;
  45. }
  46. else
  47. {
  48. //Recipe name相同,炉次不同,默认在数据源后补上1,2,3,4后缀以示区别
  49. for (int postNum = 1; postNum < 100; postNum++)
  50. {
  51. string newDsName = string.Format("{0}({1})", _dataSource.Title, postNum);
  52. if (!commonViewModel.DataSources.ContainsKey(newDsName))
  53. {
  54. _dataSource.Title = newDsName;
  55. break;
  56. }
  57. }
  58. }
  59. }
  60. commonViewModel.DataSources.Add(_dataSource.Title, _dataSource);
  61. commonViewModel.CurrentSelectedDataSource = _dataSource;
  62. commonViewModel.InvokePropertyChanged();
  63. }
  64. catch (Exception ex)
  65. {
  66. CONTEXT.WriteLog(ex);
  67. AsyncShowWarningMessageBox(string.Format("添加数据源{0}发生错误:\n{1}", _dataSource.Title, ex.Message), "警告");
  68. }
  69. }
  70. }
  71. }