Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit.Admin/EntityEditorSupport.cs @ 4770

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

Integrated OKB clients for HL 3.3 (#1166)

File size: 1.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Data;
6using System.Collections.ObjectModel;
7
8namespace HeuristicLab.OKB.Cockpit.Admin {
9  public static class EntityEditorSupport {
10
11    public static IEnumerable<T> GetObjects<T>(DataTable table, IEnumerable<string> propertyNames) where T : class {
12      foreach (DataRow row in table.Rows) {
13        if (row["Id"] != DBNull.Value) {
14          T o = Activator.CreateInstance<T>();
15          foreach (string name in propertyNames)
16            typeof(T).GetProperty(name).SetValue(o, row[name] == DBNull.Value ? null : (object)row[name], null);
17          yield return o;
18        }
19      }
20    }
21
22    public static void ShowSelector<T>(Func<T, bool> filter, Action<IEnumerable<T>> updater) where T : class {
23      var loader = new TableLoadWorker(typeof(T).Name, false);
24      loader.RunWorkerCompleted += (s, a) => {
25        var objects = GetObjects<T>(loader.Table, new[] { "Id", "Name", "Description", }).ToList();
26        RelationEditorWindow relationEditor = new RelationEditorWindow() {
27          Title = typeof(T).Name,
28          AvailableObjects = new ObservableCollection<object>(objects.Cast<object>()),
29          SelectedObjects = new ObservableCollection<object>(objects.Where(o => filter(o)).Cast<object>()),
30        };
31        if (relationEditor.ShowDialog() == true) {
32          updater(relationEditor.SelectedObjects.Cast<T>());
33        }
34      };
35      loader.RunWorkerAsync();
36    }
37
38  }
39}
Note: See TracBrowser for help on using the repository browser.