using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Windows.Controls; using System.Windows.Data; using HeuristicLab.BackgroundProcessing; using System.ComponentModel; using System.Windows; using System.Threading; using System.Collections.ObjectModel; using System.Windows.Input; using HeuristicLab.OKB.Cockpit.Query.OKBQuery; using HeuristicLab.OKB.Client; namespace HeuristicLab.OKB.Cockpit.Query { public partial class AttributeSelectorWindow : Window { private Dictionary> AttributeSelectorTemplates; private AttributeSelector attributeSelector; public AttributeSelector AttributeSelector { get { return attributeSelector; } set { if (value == attributeSelector) return; attributeSelector = value; this.DataContext = attributeSelector; } } public AttributeSelectorWindow(IEnumerable templates) { InitializeComponent(); AttributeSelector = new AttributeSelector(); if (templates != null) InitializeTemplates(templates); } protected virtual void OnClick(object sender, EventArgs args) { if (sender == OKButton) { DialogResult = true; Close(); } else if (sender == CancelButton) { DialogResult = false; Close(); } else if (sender == QueryButton) { RefreshAttributeSelectorList(); return; } } private void InitializeTemplates(IEnumerable templates) { AttributeSelectorTemplates = new Dictionary>(); foreach (var selector in templates) { if (!AttributeSelectorTemplates.ContainsKey(selector.TableName)) AttributeSelectorTemplates.Add(selector.TableName, new Dictionary()); AttributeSelectorTemplates[selector.TableName].Add(selector.FieldName, selector); } TableName.ItemsSource = AttributeSelectorTemplates.Keys; TableName.SelectionChanged += (s, a) => { FieldName.ItemsSource = AttributeSelectorTemplates[(string)TableName.SelectedItem].Keys; FieldName.SelectedItem = null; }; FieldName.SelectionChanged += (s, a) => { if (TableName.SelectedItem == null || !AttributeSelectorTemplates.ContainsKey((string)TableName.SelectedItem) || FieldName.SelectedItem == null || !AttributeSelectorTemplates[(string)TableName.SelectedItem].ContainsKey((string)FieldName.SelectedItem)) AttributeSelector.DataTypeName = null; else AttributeSelector.DataTypeName = AttributeSelectorTemplates[(string)TableName.SelectedItem][(string)FieldName.SelectedItem].DataTypeName; }; } public void RefreshAttributeSelectorList() { ObservableBackgroundWorker loader = new ObservableBackgroundWorker("Refreshing schema"); List selectors = null; loader.DoWork += (s, a) => { try { QueryServiceClient client = ClientFactory.Create(); selectors = client.GetAllAttributeSelectors(); client.Close(); } catch { } }; loader.RunWorkerCompleted += (s, a) => { if (selectors != null) InitializeTemplates(selectors); else if (MessageBox.Show("Refreshing DB Schema failed. Retry?", Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK) { RefreshAttributeSelectorList(); } }; loader.RunWorkerAsync(); } protected void CloseOnEnterOrEscape(object sender, KeyEventArgs args) { switch (args.Key) { case Key.Enter: DialogResult = true; Close(); break; case Key.Escape: DialogResult = false; Close(); break; } } } }