Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit.Query/RunView/AttributeSelectorWindow.xaml.cs @ 4481

Last change on this file since 4481 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Data;
6using System.Windows.Controls;
7using System.Windows.Data;
8using HeuristicLab.BackgroundProcessing;
9using System.ComponentModel;
10using System.Windows;
11using System.Threading;
12using System.Collections.ObjectModel;
13using System.Windows.Input;
14using HeuristicLab.OKB.Cockpit.Query.OKBQuery;
15using HeuristicLab.OKB.Client;
16
17namespace HeuristicLab.OKB.Cockpit.Query {
18
19  public partial class AttributeSelectorWindow : Window {
20
21    private Dictionary<string, Dictionary<string, AttributeSelector>> AttributeSelectorTemplates;
22
23    private AttributeSelector attributeSelector;
24    public AttributeSelector AttributeSelector {
25      get {
26        return attributeSelector;
27      }
28      set {
29        if (value == attributeSelector)
30          return;
31        attributeSelector = value;
32        this.DataContext = attributeSelector;
33      }
34    }
35
36    public AttributeSelectorWindow(IEnumerable<AttributeSelector> templates) {
37      InitializeComponent();
38      AttributeSelector = new AttributeSelector();
39      if (templates != null)
40        InitializeTemplates(templates);
41    }
42
43    protected virtual void OnClick(object sender, EventArgs args) {
44      if (sender == OKButton) {
45        DialogResult = true;
46        Close();
47      } else if (sender == CancelButton) {
48        DialogResult = false;
49        Close();
50      } else if (sender == QueryButton) {
51        RefreshAttributeSelectorList();
52        return;
53      }
54    }
55
56    private void InitializeTemplates(IEnumerable<AttributeSelector> templates) {
57      AttributeSelectorTemplates = new Dictionary<string, Dictionary<string, AttributeSelector>>();
58      foreach (var selector in templates) {
59        if (!AttributeSelectorTemplates.ContainsKey(selector.TableName))
60          AttributeSelectorTemplates.Add(selector.TableName, new Dictionary<string, AttributeSelector>());
61        AttributeSelectorTemplates[selector.TableName].Add(selector.FieldName, selector);
62      }
63      TableName.ItemsSource = AttributeSelectorTemplates.Keys;
64      TableName.SelectionChanged += (s, a) => {
65        FieldName.ItemsSource = AttributeSelectorTemplates[(string)TableName.SelectedItem].Keys;
66        FieldName.SelectedItem = null;
67      };
68      FieldName.SelectionChanged += (s, a) => {
69        if (TableName.SelectedItem == null || !AttributeSelectorTemplates.ContainsKey((string)TableName.SelectedItem) ||
70          FieldName.SelectedItem == null || !AttributeSelectorTemplates[(string)TableName.SelectedItem].ContainsKey((string)FieldName.SelectedItem))
71          AttributeSelector.DataTypeName = null;
72        else
73          AttributeSelector.DataTypeName = AttributeSelectorTemplates[(string)TableName.SelectedItem][(string)FieldName.SelectedItem].DataTypeName;
74      };
75    }
76
77    public void RefreshAttributeSelectorList() {
78      ObservableBackgroundWorker loader = new ObservableBackgroundWorker("Refreshing schema");
79      List<AttributeSelector> selectors = null;
80      loader.DoWork += (s, a) => {
81        try {
82          QueryServiceClient client = ClientFactory.Create<QueryServiceClient, IQueryService>();
83          selectors = client.GetAllAttributeSelectors();
84          client.Close();
85        } catch {
86        }
87      };
88      loader.RunWorkerCompleted += (s, a) => {
89        if (selectors != null)
90          InitializeTemplates(selectors);
91        else if (MessageBox.Show("Refreshing DB Schema failed. Retry?", Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK) {
92          RefreshAttributeSelectorList();
93        }
94      };
95      loader.RunWorkerAsync();
96    }
97
98    protected void CloseOnEnterOrEscape(object sender, KeyEventArgs args) {
99      switch (args.Key) {
100        case Key.Enter:
101          DialogResult = true;
102          Close();
103          break;
104        case Key.Escape:
105          DialogResult = false;
106          Close();
107          break;
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.