PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ViewModel/MainWindowViewModel.cs

https://github.com/shader/QuickArch
C# | 301 lines | 251 code | 37 blank | 13 comment | 38 complexity | 9ac114f37fc88d88b7ddfb3b15790a51 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using System.Collections.Specialized;
  7. using System.Diagnostics;
  8. using System.Windows.Data;
  9. using System.Windows.Input;
  10. using QuickArch.Properties;
  11. using QuickArch.Model;
  12. using QuickArch.Utilities;
  13. using System.Drawing;
  14. namespace QuickArch.ViewModel
  15. {
  16. public class MainWindowViewModel : ViewModelBase
  17. {
  18. #region Fields
  19. //Collection of commands to be displayed in UI
  20. Collection<CommandViewModel> _fileCommands, _editCommands, _viewCommands, _toolCommands, _systemCommands, _treeviewCommands ;
  21. RelayCommand _textBoxEnterCommand, _linkButtonCommand;
  22. //ObservableCollection of components
  23. #endregion
  24. #region Properties
  25. public ObservableCollection<ComponentViewModel> TreeVMs { get; private set; }
  26. public ObservableCollection<ComponentViewModel> TabVMs { get; private set; }
  27. public ComponentViewModel SelectedComponentVM { get; set; }
  28. ComponentViewModel DisplayedComponent
  29. {
  30. get
  31. {
  32. System.ComponentModel.ICollectionView collectionView = CollectionViewSource.GetDefaultView(TabVMs);
  33. if (collectionView != null)
  34. {
  35. return collectionView.CurrentItem as ComponentViewModel;
  36. }
  37. return null;
  38. }
  39. set
  40. {
  41. if (!TabVMs.Contains(value))
  42. TabVMs.Add(value);
  43. System.ComponentModel.ICollectionView collectionView = CollectionViewSource.GetDefaultView(TabVMs);
  44. if (collectionView != null)
  45. collectionView.MoveCurrentTo(value);
  46. }
  47. }
  48. #endregion
  49. #region Constructor
  50. public MainWindowViewModel()
  51. {
  52. DisplayName = Resources.MainWindowViewModel_DisplayName;
  53. TreeVMs = new ObservableCollection<ComponentViewModel>();
  54. TreeVMs.CollectionChanged += OnComponentVMsChanged;
  55. TabVMs = new ObservableCollection<ComponentViewModel>();
  56. TabVMs.CollectionChanged += OnComponentVMsChanged;
  57. #region Build Command Collections
  58. _fileCommands = new Collection<CommandViewModel>
  59. (new CommandViewModel[] {
  60. NewCommand("Save", param => SelectedComponentVM.Save(), Resources.save),
  61. NewCommand("Save As...", param => SelectedComponentVM.SaveAs(), Resources.save),
  62. NewCommand("Save All", param => SaveAll(),Resources.save),
  63. NewCommand("Open", param => OpenSystem(), Resources.folder),
  64. NewCommand("New Document", param => CreateNewDocument(Resources.DefaultDocumentName), Resources.document),
  65. NewCommand("New Subsystem", param => CreateNewSystem(), Resources.gear)
  66. });
  67. _editCommands = new Collection<CommandViewModel>
  68. (new CommandViewModel[] {});
  69. _viewCommands = new Collection<CommandViewModel>
  70. (new CommandViewModel[] {});
  71. _toolCommands = new Collection<CommandViewModel>();
  72. //(new CommandViewModel[] {
  73. // NewCommand("Export to PNG", param => GetActiveSystem().ExportPng())
  74. //});
  75. //_systemCommands = new Collection<CommandViewModel>
  76. // (new CommandViewModel[] {
  77. // NewCommand("New Subsystem", param => (param as SystemViewModel).AddSubsystem(), Resources.gear),
  78. // NewCommand("Create New Link", param => this.CreateNewConnector(), Resources.line),
  79. // NewCommand("Delete", param => this.DeleteSystem(param as SystemViewModel), Resources.delete)
  80. // });
  81. _treeviewCommands = new Collection<CommandViewModel>
  82. (new CommandViewModel[] {
  83. NewCommand("New Document", param => CreateNewDocument(Resources.DefaultDocumentName), Resources.document)
  84. });
  85. #endregion
  86. }
  87. #endregion
  88. CommandViewModel NewCommand(string displayName, Action<object> execute, Icon icon, bool isEnabled=true)
  89. {
  90. return new CommandViewModel(displayName, new RelayCommand(execute), isEnabled, icon);
  91. }
  92. #region Components
  93. void OnComponentVMsChanged(object sender, NotifyCollectionChangedEventArgs e)
  94. {
  95. if (e.NewItems != null && e.NewItems.Count != 0)
  96. foreach (ComponentViewModel cvm in e.NewItems)
  97. {
  98. cvm.RequestClose += OnComponentRequestClose;
  99. cvm.Selected += OnComponentSelected;
  100. if (cvm is SystemViewModel)
  101. {
  102. ((SystemViewModel)cvm).ComponentVMs.CollectionChanged += OnComponentVMsChanged;
  103. }
  104. }
  105. if (e.OldItems != null && e.OldItems.Count != 0)
  106. foreach (ComponentViewModel cvm in e.OldItems)
  107. {
  108. cvm.RequestClose -= OnComponentRequestClose;
  109. cvm.Selected -= OnComponentSelected;
  110. if (cvm is SystemViewModel)
  111. {
  112. ((SystemViewModel)cvm).ComponentVMs.CollectionChanged += OnComponentVMsChanged;
  113. }
  114. }
  115. }
  116. void OnComponentRequestClose(object sender, EventArgs e)
  117. {
  118. TabVMs.Remove((ComponentViewModel)sender);
  119. }
  120. void OnComponentSelected(ComponentViewModel cvm, EventArgs e)
  121. {
  122. SelectedComponentVM = cvm;
  123. }
  124. void OnDocumentComponentVMsChanged(object sender, NotifyCollectionChangedEventArgs e)
  125. {
  126. if (e.NewItems != null && e.NewItems.Count != 0)
  127. foreach (ComponentViewModel cvm in e.NewItems)
  128. {
  129. TabVMs.Add(cvm);
  130. }
  131. if (e.OldItems != null && e.OldItems.Count != 0)
  132. foreach (ComponentViewModel cvm in e.OldItems)
  133. {
  134. TabVMs.Remove(cvm);
  135. }
  136. }
  137. #endregion
  138. #region Private Helpers
  139. void OpenSystem()
  140. {
  141. string filename = FileManager.OpenFile(Resources.DefaultFilename, Resources.Extension, Resources.Filter);
  142. if (filename != null)
  143. {
  144. SystemViewModel sys = new SystemViewModel(new QuickArch.Model.System(filename));
  145. TreeVMs.Add(sys);
  146. TabVMs.Add(sys);
  147. }
  148. }
  149. void CreateNewSystem()
  150. {
  151. CreateNewSystem(Resources.DefaultComponentName);
  152. }
  153. //overloaded method
  154. void CreateNewSystem(String title)
  155. {
  156. if (SelectedComponentVM != null && SelectedComponentVM is SystemViewModel)
  157. {
  158. ((SystemViewModel)SelectedComponentVM).AddSubsystem(title);
  159. }
  160. //for top level system
  161. else
  162. {
  163. SystemViewModel sys = new SystemViewModel(new QuickArch.Model.System(title, null));
  164. TreeVMs.Add(sys);
  165. }
  166. }
  167. void CreateNewDocument(String title)
  168. {
  169. DocumentViewModel doc = new DocumentViewModel(new Document(title));
  170. TreeVMs.Add(doc);
  171. doc.ComponentVMs.CollectionChanged += OnDocumentComponentVMsChanged;
  172. }
  173. void CreateNewSystemDiagram(String title)
  174. {
  175. (SelectedComponentVM as DocumentViewModel).AddSystemDiagram(title);
  176. }
  177. void CreateNewConnector()
  178. {
  179. if (SelectedComponentVM != null && SelectedComponentVM is SystemViewModel)
  180. ((SystemViewModel)SelectedComponentVM).AddConnector();
  181. else
  182. {
  183. ConnectorViewModel conn = new ConnectorViewModel(new QuickArch.Model.Connector());
  184. TreeVMs.Add(conn);
  185. }
  186. }
  187. void DeleteSystem(SystemViewModel sys)
  188. {
  189. if (TreeVMs.Contains(sys))
  190. {
  191. TreeVMs.Remove(sys);
  192. TabVMs.Remove(sys);
  193. sys.Dispose();
  194. sys = null;
  195. }
  196. else
  197. {
  198. sys.Delete();
  199. }
  200. }
  201. void SaveAll()
  202. {
  203. foreach (ComponentViewModel cvm in TreeVMs)
  204. {
  205. cvm.Save();
  206. }
  207. }
  208. #endregion
  209. #region Presentation Properties
  210. #region Commands
  211. public Collection<CommandViewModel> FileCommands
  212. {
  213. get { return _fileCommands; }
  214. }
  215. public Collection<CommandViewModel> EditCommands
  216. {
  217. get { return _editCommands; }
  218. }
  219. public Collection<CommandViewModel> ViewCommands
  220. {
  221. get { return _viewCommands; }
  222. }
  223. public Collection<CommandViewModel> ToolCommands
  224. {
  225. get { return _toolCommands; }
  226. }
  227. public Collection<CommandViewModel> SystemCommands
  228. {
  229. get { return _systemCommands; }
  230. }
  231. public Collection<CommandViewModel> TreeViewCommands
  232. {
  233. get { return _treeviewCommands; }
  234. }
  235. #endregion
  236. public ICommand TextBoxEnterCommand
  237. {
  238. get
  239. {
  240. if(_textBoxEnterCommand == null)
  241. _textBoxEnterCommand = new RelayCommand(param => this.CreateNewSystem((String)param));
  242. return _textBoxEnterCommand;
  243. }
  244. }
  245. public ICommand LinkButtonCommand
  246. {
  247. get
  248. {
  249. if (_linkButtonCommand == null)
  250. _linkButtonCommand = new RelayCommand(param =>
  251. {
  252. if (DisplayedComponent != null)
  253. {
  254. (DisplayedComponent as SystemViewModel).ComponentSelected +=
  255. (DisplayedComponent as SystemViewModel).StartTempConnector;
  256. }
  257. });
  258. Debug.WriteLine("test");
  259. return _linkButtonCommand;
  260. }
  261. }
  262. #endregion
  263. }
  264. }