Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Data;
|
---|
6 | using System.Collections.ObjectModel;
|
---|
7 |
|
---|
8 | namespace 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.