/src/EntityConnector/ShowPool.xaml.cs
C# | 204 lines | 171 code | 30 blank | 3 comment | 22 complexity | f502938bb8326506d58de1f25f855d97 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using BurnSystems.EntityConnector;
- using System.ComponentModel;
-
- namespace EntityConnector
- {
- /// <summary>
- /// Interaktionslogik für ShowPool.xaml
- /// </summary>
- public partial class ShowPool : Window
- {
- public string PoolName
- {
- get;
- set;
- }
-
- private EntityPool pool;
-
- private BindingList<Entity> currentEntities;
-
- public ShowPool()
- {
- InitializeComponent();
- }
-
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- this.pool = new EntityPool("TestPool", "Server=.\\SQLEXPRESS;Database=mb_entity;Trusted_Connection=True;");
- this.currentEntities = new BindingList<Entity>(this.pool.GetAll());
- this.ListEntities.ItemsSource = this.currentEntities;
- }
-
- private void listItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var entity = this.ListEntities.SelectedItem as Entity;
- if (entity == null)
- {
- this.ListProperties.ItemsSource = null;
- return;
- }
-
- this.ListProperties.ItemsSource = entity.Data;
- if (entity.Data.Count > 0)
- {
- this.ListProperties.SelectedIndex = 0;
- }
- else
- {
- this.PropertyName.Text = string.Empty;
- this.PropertyValue.Text = string.Empty;
- }
- }
-
- private void ItemProperties_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var selected = this.ListProperties.SelectedItem;
-
- if (selected != null && selected is KeyValuePair<string, string>)
- {
- var pair = (KeyValuePair<string, string>)selected;
- this.PropertyName.Text = pair.Key;
- this.PropertyValue.Text = pair.Value;
- }
- }
-
- private void PropertyValue_LostFocus(object sender, RoutedEventArgs e)
- {
- this.SaveChangedText();
- }
-
- private void PropertyName_LostFocus(object sender, RoutedEventArgs e)
- {
- this.SaveChangedText();
- }
-
- private void SaveChangedText()
- {
- var entity = this.ListEntities.SelectedItem as Entity;
- if (entity == null)
- {
- return;
- }
-
- if (string.IsNullOrEmpty(this.PropertyName.Text))
- {
- return;
- }
-
- entity[this.PropertyName.Text] = this.PropertyValue.Text;
- this.ListEntities.Items.Refresh();
- this.ListProperties.Items.Refresh();
-
- this.pool.Update(entity);
- }
-
- private void ButtonAddProperty_Click(object sender, RoutedEventArgs e)
- {
- this.PropertyName.Text = string.Empty;
- this.PropertyValue.Text = string.Empty;
- this.PropertyName.Focus();
- }
-
- private void ButtonNextProperty_Click(object sender, RoutedEventArgs e)
- {
- var entity = this.ListEntities.SelectedItem as Entity;
- if (entity == null)
- {
- return;
- }
-
- var currentKey = this.PropertyName.Text;
- var hasMatched = false;
- foreach (var pair in entity.Data)
- {
- if (hasMatched)
- {
- this.PropertyName.Text = pair.Key;
- this.PropertyValue.Text = pair.Value;
- this.PropertyName.Focus();
- break;
- }
-
- if (pair.Key == currentKey)
- {
- hasMatched = true;
- }
- }
- }
-
- private void ButtonDeleteProperty_Click(object sender, RoutedEventArgs e)
- {
- var entity = this.ListEntities.SelectedItem as Entity;
- if (entity == null)
- {
- return;
- }
-
- var oldIndex = this.ListProperties.SelectedIndex;
-
- entity.Data.Remove(this.PropertyName.Text);
- this.ListProperties.Items.Refresh();
- this.ListEntities.Items.Refresh();
-
- this.pool.Update(entity);
-
- if (entity.Data.Count == 0)
- {
- this.PropertyName.Text = string.Empty;
- this.PropertyValue.Text = string.Empty;
- }
- else
- {
- this.ListProperties.SelectedIndex = oldIndex;
- }
- }
-
- private void ButtonAddEntity_Click(object sender, RoutedEventArgs e)
- {
- var entity = new Entity();
- this.pool.Add(entity);
- this.currentEntities.Add(entity);
-
- this.ListEntities.SelectedItem = entity;
- this.ListEntities.ScrollIntoView(entity);
- this.PropertyName.Focus();
- }
-
- private void ButtonDeleteEntity_Click(object sender, RoutedEventArgs e)
- {
- var entity = this.ListEntities.SelectedItem as Entity;
- if (entity == null)
- {
- return;
- }
-
- var oldIndex = this.ListEntities.SelectedIndex;
-
- this.pool.Delete(entity);
- this.currentEntities.Remove(entity);
-
- if (this.currentEntities.Count == 0)
- {
- this.PropertyName.Text = string.Empty;
- this.PropertyValue.Text = string.Empty;
- }
- else
- {
- this.ListEntities.SelectedIndex = oldIndex;
- }
- }
- }
- }