Advertisement
fak123

MainWindowViewModel

Jan 31st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.31 KB | None | 0 0
  1. using GalaSoft.MvvmLight.Command;
  2. using GalaSoft.MvvmLight.Messaging;
  3. using MVVMFirma.Helpers;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Collections.Specialized;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Data;
  15. using System.Windows.Input;
  16.  
  17. namespace MVVMFirma.ViewModel
  18. {
  19. public class MainWindowViewModel : BaseViewModel
  20. {
  21. #region Fields
  22. // to jest kolekcja przechowujaca listę komend, które pojawiaja sie z lewej strony
  23. ReadOnlyCollection<CommandViewModel> _Commands;
  24. // to jest kolekcja przechowujaca liste zakładek z prawej strony
  25. ObservableCollection<WorkspaceViewModel> _Workspaces;
  26. // przekazuje se komendoo z mejnuindo
  27. public RelayCommand<string> SendByCommand { get; private set; }
  28. #endregion Fields
  29.  
  30. #region Constructor
  31.  
  32. public MainWindowViewModel()
  33. {
  34. Messenger.Default.Register<string>(this, createObject);
  35. SendByCommand = new RelayCommand<string>(param => createObject(param));
  36. }
  37.  
  38. #endregion Constructor
  39.  
  40. #region Commands
  41.  
  42. //to jest properties do kolekcji linkow
  43. public ReadOnlyCollection<CommandViewModel> Commands
  44. {
  45. get
  46. {
  47. if (_Commands == null)
  48. {
  49. //Jezeli zbior komend jest pusty
  50. //tworze chwilowa liste, tylko po to by ja wypelnic
  51. List<CommandViewModel> cmds = this.CreateCommands();
  52. //i zrobic z niej kolekcje tylko do odczytu
  53. _Commands = new ReadOnlyCollection<CommandViewModel>(cmds);
  54. }
  55. return _Commands;
  56. }
  57. }
  58.  
  59. // to jest funkcja, która tworzy komendy z lewej strony
  60. public List<CommandViewModel> CreateCommands()
  61. {
  62.  
  63. return new List<CommandViewModel>
  64. {
  65. new CommandViewModel("Wszystkie Samochody",
  66. new BaseCommand(()=>this.showAllObjects<WszystkieSamochodyViewModel>())),
  67. new CommandViewModel("Nowy Samochod",
  68. new BaseCommand(()=>this.createObject<NowySamochodViewModel>())),
  69. new CommandViewModel("Wszyscy Klienci",
  70. new BaseCommand(()=>this.showAllObjects<WszyscyKlienciViewModel>())),
  71. new CommandViewModel("Kolory",
  72. new BaseCommand(()=>this.showAllObjects<WszystkieKoloryViewModel>())),
  73. new CommandViewModel("Nowy Kolor",
  74. new BaseCommand(()=>this.createObject<NowyKolorViewModel>())),
  75. new CommandViewModel("Rodzaje Napędów",
  76. new BaseCommand(()=>this.showAllObjects<WszystkieRodzajeNapedowViewModel>())),
  77. new CommandViewModel ("Nowy Rodzaj Napedow",
  78. new BaseCommand (()=>this.createObject<NowyRodzajeNapedowViewModel>())),
  79. new CommandViewModel("Nowy status",
  80. new BaseCommand (()=>this.createObject<NowyStatusViewModel>())),
  81. new CommandViewModel("Statusy",
  82. new BaseCommand(()=>this.showAllObjects<WszystkieStatusyViewModel>())),
  83. new CommandViewModel("Nowy rodzaj skrzynii biegow",
  84. new BaseCommand(()=>this.createObject<NowyRodzajSkrzyniiBiegowViewModel>())),
  85. new CommandViewModel("Rodzaje skrzyni biegów",
  86. new BaseCommand(()=>this.showAllObjects<WszystkieRodzajeSkrzyniiBiegowViewModel>())),
  87. new CommandViewModel("Typy Transakcji",
  88. new BaseCommand(()=>this.showAllObjects<WszystkieTypyTransakcjiViewModel>())),
  89. new CommandViewModel("Nowy Typ Transakcji",
  90. new BaseCommand(()=>this.createObject<NowyTypTransakcjiViewModel>())),
  91. new CommandViewModel("Rodzaje Płatności",
  92. new BaseCommand(()=>this.showAllObjects<WszystkieRodzajePlatnosciViewModel>())),
  93. new CommandViewModel("Nowy Rodzaj Płatności",
  94. new BaseCommand(()=>this.createObject<NowyRodzajPlatnosciViewModel>())),
  95. new CommandViewModel("Faktury",
  96. new BaseCommand(()=>this.showAllObjects<WszystkieFakturyViewModel>())),
  97. new CommandViewModel("Nowa Faktura",
  98. new BaseCommand(()=>this.createObject<NowaFakturaViewModel>())),
  99. new CommandViewModel("Umowy",
  100. new BaseCommand(()=>this.showAllObjects<WszystkieUmowyViewModel>())),
  101. new CommandViewModel("Nowa Umowa",
  102. new BaseCommand(()=>this.createObject<NowaUmowaViewModel>())),
  103. new CommandViewModel("Nowy typ nadwozia",
  104. new BaseCommand(()=>this.createObject<NowyTypNadwoziaViewModel>())),
  105. new CommandViewModel("Wszystkie typy nadwozia",
  106. new BaseCommand(()=>this.showAllObjects<WszystkieTypyNadwoziaViewModel>())),
  107. new CommandViewModel("Nowy Rodzaj Wyposażenia",
  108. new BaseCommand(()=>this.createObject<NoweWyposazenieViewModel>())),
  109. new CommandViewModel("Wszystkie typy wyposażenia",
  110. new BaseCommand(()=>this.showAllObjects<WszystkieWyposazeniaViewModel>())),
  111. new CommandViewModel("Nowy klient",
  112. new BaseCommand(()=>this.createObject<NowyKlientViewModel>())),
  113. new CommandViewModel("Nowa Transakcja",
  114. new BaseCommand(()=>this.createObject<NowaTransakcjaViewModel>())),
  115. new CommandViewModel("Nowa Marka",
  116. new BaseCommand(()=>this.createObject<NowaMarkaViewModel>())),
  117. new CommandViewModel("Wszystkie marki",
  118. new BaseCommand(()=>this.createObject<WszystkieMarkiViewModel>())),
  119. new CommandViewModel("Nowy Stan",
  120. new BaseCommand(()=>this.createObject<NowyStanViewModel>())),
  121. new CommandViewModel("Wszystkie stany",
  122. new BaseCommand(()=>this.createObject<WszystkieStanyViewModel>())) ,
  123. new CommandViewModel("Nowy model",
  124. new BaseCommand(()=>this.createObject<NowyModelViewModel>())),
  125. new CommandViewModel("Wszystkie modele",
  126. new BaseCommand(()=>this.ShowAllModele())),
  127. new CommandViewModel("Nowe Zdjecia",
  128. new BaseCommand(()=>this.createObject<NoweZdjeciaViewModel>())),
  129. new CommandViewModel("Wszystkie zdjecia",
  130. new BaseCommand(()=>this.createObject<WszystkieZdjeciaViewModel>())),
  131. new CommandViewModel("Nowy Rodzaj Koloru",
  132. new BaseCommand(()=>this.createObject<NowyRodzajKoloruViewModel>())),
  133. new CommandViewModel("Wszystkie Adresy",
  134. new BaseCommand(()=>this.showAllObjects<WszystkieAdresyViewModel>())),
  135. new CommandViewModel("Wszystkie Kraje",
  136. new BaseCommand(()=>this.showAllObjects<WszystkieKrajeViewModel>())),
  137. new CommandViewModel("Wszystkie Wersje",
  138. new BaseCommand(()=>this.showAllObjects<WszystkieWersjeViewModel>())),
  139. new CommandViewModel("Nowa Wersja",
  140. new BaseCommand(()=>this.createObject<NowaWersjaViewModel>())),
  141. new CommandViewModel("Wersje Dla Samochodu",
  142. new BaseCommand(()=>this.createObject<WersjeDlaSamochoduViewModel>())),
  143. new CommandViewModel("Klienci w miastach",
  144. new BaseCommand(()=>this.createObject<KlienciWMiastachViewModel>())),
  145. new CommandViewModel("Wszystkie Rodzaje Koloru",
  146. new BaseCommand(()=>this.createObject<WszystkieRodzajeKolorowViewModel>())),
  147.  
  148. new CommandViewModel("Wszyscy pracownicy",
  149. new BaseCommand(()=>this.createObject<WszyscyPracownicyViewModel>())),
  150.  
  151. //Raporty
  152. new CommandViewModel("Raport wydatków klienta",
  153. new BaseCommand(()=>this.showAllObjects<RaportWydatkowKlientaViewModel>())),
  154. new CommandViewModel("Raport ilości kolorów wybranego modelu",
  155. new BaseCommand(()=>this.showAllObjects<RaportIloscKolorowDanegoModeluViewModel>())),
  156. new CommandViewModel("Raport ilości transakcji wybranego pracownika",
  157. new BaseCommand(()=>this.showAllObjects<RaportIlosciTransakcjiPracownikaViewModel>())),
  158. new CommandViewModel("Raport sumy transakcji wybranego pracownika wg daty",
  159. new BaseCommand(()=>this.showAllObjects<RaportSumyTransakcjiPracownikaViewModel>())),
  160. new CommandViewModel("Raport umów wybranego pracownika",
  161. new BaseCommand(()=>this.showAllObjects<RaportUmowPracownikaViewModel>())),
  162. new CommandViewModel("Raport wartości transakcji w wybranym okresie dla danego modelu",
  163. new BaseCommand(()=>this.showAllObjects<RaportSamochodyViewModel>())),
  164. new CommandViewModel("Raport wartości sprzedazy wg marki",
  165. new BaseCommand(()=>this.showAllObjects<RaportSprzedazyWgMarkiViewModel>())),
  166. new CommandViewModel("Raport klienci wg miejscowosci",
  167. new BaseCommand(()=>this.showAllObjects<RaportKlienciMiejscowoscViewModel>())),
  168. new CommandViewModel("Raport VAT",
  169. new BaseCommand(()=>this.showAllObjects<RaportFakturViewModel>())),
  170. new CommandViewModel("Raport Umow",
  171. new BaseCommand(()=>this.showAllObjects<RaportUmowViewModel>())),
  172. new CommandViewModel("Raport zdjec",
  173. new BaseCommand(()=>this.showAllObjects<RaportIlosciZdjecViewModel>())),
  174. new CommandViewModel("Sprzedaz Procentowa po modelach",
  175. new BaseCommand(()=>this.showAllObjects<RaportSprzedazyProcentowejPoModelachViewModel>())),
  176. new CommandViewModel("Wartosc Sprzedazy po typach nadwozia",
  177. new BaseCommand(()=>this.showAllObjects<RaportSprzedazyPoTypachNadwoziaViewModel>())),
  178. new CommandViewModel("Raport wyposazen",
  179. new BaseCommand(()=>this.showAllObjects<RaportIlosciWyposazenSamochodu>()))
  180.  
  181. };
  182. }
  183. #endregion Commands
  184.  
  185. #region Workspaces
  186. public ObservableCollection<WorkspaceViewModel> Workspaces
  187. {
  188. get
  189. {
  190. if (_Workspaces == null)
  191. {
  192. _Workspaces = new ObservableCollection<WorkspaceViewModel>();
  193. _Workspaces.CollectionChanged += this.OnWorkspacesChanged;
  194. }
  195. return _Workspaces;
  196. }
  197. }
  198. void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
  199. {
  200. if (e.NewItems != null && e.NewItems.Count != 0)
  201. {
  202. foreach (WorkspaceViewModel workspace in e.NewItems)
  203. {
  204. workspace.RequestClose += this.OnWorkspaceRequestClose;
  205. }
  206. }
  207. if (e.OldItems != null && e.OldItems.Count != 0)
  208. {
  209. foreach (WorkspaceViewModel workspace in e.OldItems)
  210. {
  211. workspace.RequestClose -= this.OnWorkspaceRequestClose;
  212. }
  213. }
  214. }
  215. void OnWorkspaceRequestClose(object sender, EventArgs e)
  216. {
  217. WorkspaceViewModel workspace = sender as WorkspaceViewModel;
  218. this.Workspaces.Remove(workspace);
  219. }
  220. #endregion Workspaces
  221.  
  222. #region PrivateHelpers
  223. //
  224. // tutaj modyfikacja kodu
  225. //
  226. //
  227. private void ShowAllModele()
  228. {
  229. WszystkieModeleViewModel workspace =
  230. this.Workspaces.FirstOrDefault(vm => vm is WszystkieModeleViewModel)
  231. as WszystkieModeleViewModel;
  232. if (workspace == null)
  233. {
  234. workspace = new WszystkieModeleViewModel();
  235. this.Workspaces.Add(workspace);
  236. }
  237. this.setActiveWorkspace(workspace);
  238. }
  239. private void createObject<T>() where T : WorkspaceViewModel, new()
  240. {
  241. T workspace = new T();
  242. Workspaces.Add(workspace);
  243. setActiveWorkspace(workspace);
  244. }
  245. private void showAllObjects<T>() where T : WorkspaceViewModel, new()
  246. {
  247. T workspace =
  248. Workspaces.FirstOrDefault
  249. (vm => vm is T)
  250. as T;
  251. if (workspace == null)
  252. {
  253. workspace = new T();
  254. Workspaces.Add(workspace);
  255. }
  256. setActiveWorkspace(workspace);
  257. }
  258. private void createObject(string typeName)
  259. {
  260. if (typeName == "NowaFaktura") createObject<NowaFakturaViewModel>();
  261. if (typeName == "NowaUmowa") createObject<NowaUmowaViewModel>();
  262. if (typeName == "NoweWyposazenie") createObject<NoweWyposazenieViewModel>();
  263. if (typeName == "NowyKolor") createObject<NowyKolorViewModel>();
  264. if (typeName == "NowyRodzajNapedu") createObject<NowyRodzajeNapedowViewModel>();
  265. if (typeName == "NowyRodzajPlatnosci") createObject<NowyRodzajPlatnosciViewModel>();
  266. if (typeName == "NowyRodzajSkrzynii") createObject<NowyRodzajSkrzyniiBiegowViewModel>();
  267. if (typeName == "NowyStatus") createObject<NowyStatusViewModel>();
  268. if (typeName == "NowyTypNadwozia") createObject<NowyTypNadwoziaViewModel>();
  269. if (typeName == "NowyTypTransakcji") createObject<NowyTypTransakcjiViewModel>();
  270. if (typeName == "NowyKlient") createObject<NowyKlientViewModel>();
  271. if (typeName == "NowaTransakcja") createObject<NowaTransakcjaViewModel>();
  272. if (typeName == "NowaMarka") createObject<NowaMarkaViewModel>();
  273. if (typeName == "NowyStan") createObject<NowyStanViewModel>();
  274. if (typeName == "NowySamochod") createObject<NowySamochodViewModel>();
  275. //stworzone na potrzeby menu!
  276. if (typeName == "NowyModel") createObject<NowyModelViewModel>();
  277. if (typeName == "NowaWersja") createObject<NowaWersjaViewModel>();
  278. if (typeName == "NowyRodzajKoloru") createObject<NowyRodzajKoloruViewModel>();
  279. if (typeName == "NoweZdjecia") createObject<NoweZdjeciaViewModel>();
  280. //if (typeName == "NowyKraj") createObject<NowyKrajViewModel>();
  281. //if (typeName == "NowyAdres") createObject<NowyAdresViewModel>();
  282. //wszystkie
  283. if (typeName == "KlientShow") showAllObjects<WszyscyKlienciViewModel>();
  284. if (typeName == "StatusyShow") createObject<WszystkieStatusyViewModel>();
  285. if (typeName == "TypyTransakcjiShow") createObject<WszystkieTypyTransakcjiViewModel>();
  286. if (typeName == "RodzajePlatnosciShow") createObject<WszystkieRodzajePlatnosciViewModel>();
  287. if (typeName == "FakturyShow") createObject<WszystkieFakturyViewModel>();
  288. if (typeName == "UmowyShow") createObject<WszystkieUmowyViewModel>();
  289. if (typeName == "SamochodyShow") createObject<WszystkieSamochodyViewModel>();
  290. if (typeName == "MarkiShow") createObject<WszystkieMarkiViewModel>();
  291. if (typeName == "KoloryShow") createObject<WszystkieKoloryViewModel>();
  292. if (typeName == "RodzajeKolorowShow") createObject<WszystkieRodzajeKolorowViewModel>();
  293. if (typeName == "RodzajeNapedowShow") createObject<WszystkieRodzajeNapedowViewModel>();
  294. if (typeName == "RodzajeSkrzyniiShow") createObject<WszystkieRodzajeSkrzyniiBiegowViewModel>();
  295. if (typeName == "TypyNadwoziaShow") createObject<WszystkieTypyNadwoziaViewModel>();
  296. if (typeName == "WyposazeniaShow") createObject<WszystkieWyposazeniaViewModel>();
  297. if (typeName == "StanyShow") createObject<WszystkieStanyViewModel>();
  298. if (typeName == "ZjeciaShow") createObject<WszystkieZdjeciaViewModel>();
  299. if (typeName == "WersjeShow") createObject<WszystkieWersjeViewModel>();
  300. if (typeName == "ModeleShow") createObject<WszystkieModeleViewModel>();
  301. if (typeName == "KrajeShow") createObject<WszystkieKrajeViewModel>();
  302. if (typeName == "AdresyShow") createObject<WszystkieAdresyViewModel>();
  303. //zamykanie apki
  304. if (typeName == "ZamknijProgram") System.Environment.Exit(1);
  305. //pomoc
  306. if (typeName == "PomocProgramu") MessageBox.Show("Autorzy: WSB-NLU Informatyka niestacjonarne 2013 - 2017." + Environment.NewLine + "Kontakt: pz.wsb-nlu.gr1@outlook.com", "Pomoc: Komis samochodowy 2017");
  307. //ustawienia
  308. if (typeName == "UstawieniaProgramu") MessageBox.Show("Ustawienia programu.", "Ustawienia");
  309. if (typeName == "NowaWersja") createObject<NowaWersjaViewModel>();
  310. }
  311.  
  312. private void setActiveWorkspace(WorkspaceViewModel workspace)
  313. {
  314. Debug.Assert(this.Workspaces.Contains(workspace));
  315. ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
  316. if (collectionView != null)
  317. {
  318. collectionView.MoveCurrentTo(workspace);
  319. }
  320. }
  321. #endregion PrivateHelpers
  322. }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement