PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/EntityConnector/ShowPool.xaml.cs

https://bitbucket.org/mbrenn/entityconnector
C# | 204 lines | 171 code | 30 blank | 3 comment | 22 complexity | f502938bb8326506d58de1f25f855d97 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using BurnSystems.EntityConnector;
  14. using System.ComponentModel;
  15. namespace EntityConnector
  16. {
  17. /// <summary>
  18. /// Interaktionslogik für ShowPool.xaml
  19. /// </summary>
  20. public partial class ShowPool : Window
  21. {
  22. public string PoolName
  23. {
  24. get;
  25. set;
  26. }
  27. private EntityPool pool;
  28. private BindingList<Entity> currentEntities;
  29. public ShowPool()
  30. {
  31. InitializeComponent();
  32. }
  33. private void Window_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. this.pool = new EntityPool("TestPool", "Server=.\\SQLEXPRESS;Database=mb_entity;Trusted_Connection=True;");
  36. this.currentEntities = new BindingList<Entity>(this.pool.GetAll());
  37. this.ListEntities.ItemsSource = this.currentEntities;
  38. }
  39. private void listItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
  40. {
  41. var entity = this.ListEntities.SelectedItem as Entity;
  42. if (entity == null)
  43. {
  44. this.ListProperties.ItemsSource = null;
  45. return;
  46. }
  47. this.ListProperties.ItemsSource = entity.Data;
  48. if (entity.Data.Count > 0)
  49. {
  50. this.ListProperties.SelectedIndex = 0;
  51. }
  52. else
  53. {
  54. this.PropertyName.Text = string.Empty;
  55. this.PropertyValue.Text = string.Empty;
  56. }
  57. }
  58. private void ItemProperties_SelectionChanged(object sender, SelectionChangedEventArgs e)
  59. {
  60. var selected = this.ListProperties.SelectedItem;
  61. if (selected != null && selected is KeyValuePair<string, string>)
  62. {
  63. var pair = (KeyValuePair<string, string>)selected;
  64. this.PropertyName.Text = pair.Key;
  65. this.PropertyValue.Text = pair.Value;
  66. }
  67. }
  68. private void PropertyValue_LostFocus(object sender, RoutedEventArgs e)
  69. {
  70. this.SaveChangedText();
  71. }
  72. private void PropertyName_LostFocus(object sender, RoutedEventArgs e)
  73. {
  74. this.SaveChangedText();
  75. }
  76. private void SaveChangedText()
  77. {
  78. var entity = this.ListEntities.SelectedItem as Entity;
  79. if (entity == null)
  80. {
  81. return;
  82. }
  83. if (string.IsNullOrEmpty(this.PropertyName.Text))
  84. {
  85. return;
  86. }
  87. entity[this.PropertyName.Text] = this.PropertyValue.Text;
  88. this.ListEntities.Items.Refresh();
  89. this.ListProperties.Items.Refresh();
  90. this.pool.Update(entity);
  91. }
  92. private void ButtonAddProperty_Click(object sender, RoutedEventArgs e)
  93. {
  94. this.PropertyName.Text = string.Empty;
  95. this.PropertyValue.Text = string.Empty;
  96. this.PropertyName.Focus();
  97. }
  98. private void ButtonNextProperty_Click(object sender, RoutedEventArgs e)
  99. {
  100. var entity = this.ListEntities.SelectedItem as Entity;
  101. if (entity == null)
  102. {
  103. return;
  104. }
  105. var currentKey = this.PropertyName.Text;
  106. var hasMatched = false;
  107. foreach (var pair in entity.Data)
  108. {
  109. if (hasMatched)
  110. {
  111. this.PropertyName.Text = pair.Key;
  112. this.PropertyValue.Text = pair.Value;
  113. this.PropertyName.Focus();
  114. break;
  115. }
  116. if (pair.Key == currentKey)
  117. {
  118. hasMatched = true;
  119. }
  120. }
  121. }
  122. private void ButtonDeleteProperty_Click(object sender, RoutedEventArgs e)
  123. {
  124. var entity = this.ListEntities.SelectedItem as Entity;
  125. if (entity == null)
  126. {
  127. return;
  128. }
  129. var oldIndex = this.ListProperties.SelectedIndex;
  130. entity.Data.Remove(this.PropertyName.Text);
  131. this.ListProperties.Items.Refresh();
  132. this.ListEntities.Items.Refresh();
  133. this.pool.Update(entity);
  134. if (entity.Data.Count == 0)
  135. {
  136. this.PropertyName.Text = string.Empty;
  137. this.PropertyValue.Text = string.Empty;
  138. }
  139. else
  140. {
  141. this.ListProperties.SelectedIndex = oldIndex;
  142. }
  143. }
  144. private void ButtonAddEntity_Click(object sender, RoutedEventArgs e)
  145. {
  146. var entity = new Entity();
  147. this.pool.Add(entity);
  148. this.currentEntities.Add(entity);
  149. this.ListEntities.SelectedItem = entity;
  150. this.ListEntities.ScrollIntoView(entity);
  151. this.PropertyName.Focus();
  152. }
  153. private void ButtonDeleteEntity_Click(object sender, RoutedEventArgs e)
  154. {
  155. var entity = this.ListEntities.SelectedItem as Entity;
  156. if (entity == null)
  157. {
  158. return;
  159. }
  160. var oldIndex = this.ListEntities.SelectedIndex;
  161. this.pool.Delete(entity);
  162. this.currentEntities.Remove(entity);
  163. if (this.currentEntities.Count == 0)
  164. {
  165. this.PropertyName.Text = string.Empty;
  166. this.PropertyValue.Text = string.Empty;
  167. }
  168. else
  169. {
  170. this.ListEntities.SelectedIndex = oldIndex;
  171. }
  172. }
  173. }
  174. }