Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit.Admin/ItemSelector.xaml.cs @ 4898

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

Integrated OKB clients for HL 3.3 (#1166)

File size: 5.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Controls;
6using HeuristicLab.MainForm.WPF;
7using System.Windows;
8using System.Collections.ObjectModel;
9using System.Collections.Specialized;
10using System.Windows.Controls.Primitives;
11using System.Collections;
12using System.Diagnostics;
13using System.ComponentModel;
14
15namespace HeuristicLab.OKB.Cockpit.Admin {
16
17  public partial class ItemSelector : UserControl, IOKBCockpitItem {
18
19    class SelectionWrapper {
20
21      public event EventHandler IsCheckedChanged;
22      private bool isChecked;
23      public bool IsChecked {
24        get { return isChecked; }
25        set {
26          if (value == isChecked) return;
27          isChecked = value;
28          OnIsCheckedChanged();
29        }
30      }
31
32      public object Obj { get; set; }
33
34      public SelectionWrapper() {
35      }
36
37      public SelectionWrapper(object o) {
38        Obj = o;
39      }
40
41      protected void OnIsCheckedChanged() {
42        if (IsCheckedChanged != null)
43          IsCheckedChanged(this, new EventArgs());
44      }
45
46      public override bool Equals(object obj) {
47        return Obj.Equals(obj);
48      }
49
50      public override int GetHashCode() {
51        if (Obj == null)
52          return 0;
53        return Obj.GetHashCode();
54      }
55    }
56
57    public static DependencyProperty AvailableObjectsProperty = DependencyProperty.Register(
58      "AvailableObjects", typeof(ObservableCollection<object>), typeof(ItemSelector),
59      new PropertyMetadata(new ObservableCollection<object>()));
60
61    public static DependencyProperty SelectedObjectsProperty = DependencyProperty.Register(
62      "SelectedObjects", typeof(ObservableCollection<object>), typeof(ItemSelector),
63      new PropertyMetadata(new ObservableCollection<object>()));
64
65    public ObservableCollection<object> AvailableObjects {
66      get { return (ObservableCollection<object>)GetValue(AvailableObjectsProperty); }
67      set { SetValue(AvailableObjectsProperty, value); }
68    }
69
70    public ObservableCollection<object> SelectedObjects {
71      get { return (ObservableCollection<object>)GetValue(SelectedObjectsProperty); }
72      set { SetValue(SelectedObjectsProperty, value); }
73    }
74
75    private ObservableCollection<SelectionWrapper> DisplayedAvailableObjects;
76
77    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
78      base.OnPropertyChanged(e);
79      if (e.Property == AvailableObjectsProperty) {
80        ((ObservableCollection<object>)e.OldValue).CollectionChanged -= PropageAvailableObjectsChange;
81        AvailableObjects.CollectionChanged += PropageAvailableObjectsChange;
82        PropageAvailableObjectsChange(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
83        ValidateSelection();
84      } else if (e.Property == SelectedObjectsProperty) {
85        ValidateSelection();
86      }
87    }
88
89    private void ValidateSelection() {
90      if (SelectedObjects == null)
91        return;
92      var selectedObjects = new HashSet<object>(SelectedObjects);
93      SelectedObjects.Clear();
94      if (AvailableObjects != null) {
95        selectedObjects.IntersectWith(AvailableObjects);
96        foreach (var wrapper in DisplayedAvailableObjects)
97          wrapper.IsChecked = selectedObjects.Contains(wrapper.Obj);
98        foreach (var o in selectedObjects)
99          SelectedObjects.Add(o);
100      }
101    }
102
103    private void PropageAvailableObjectsChange(object sender, NotifyCollectionChangedEventArgs args) {
104      switch (args.Action) {
105        case NotifyCollectionChangedAction.Add:
106          foreach (var o in args.NewItems) DisplayedAvailableObjects.Add(new SelectionWrapper(o)); break;
107        case NotifyCollectionChangedAction.Move:
108          DisplayedAvailableObjects.Move(args.OldStartingIndex, args.NewStartingIndex); break;
109        case NotifyCollectionChangedAction.Remove:
110          foreach (var o in args.OldItems) DisplayedAvailableObjects.Remove(new SelectionWrapper(o)); break;
111        case NotifyCollectionChangedAction.Replace:
112          foreach (var o in args.OldItems) DisplayedAvailableObjects.Remove(new SelectionWrapper(o));
113          foreach (var o in args.NewItems) DisplayedAvailableObjects.Insert(args.OldStartingIndex, new SelectionWrapper(o));
114          break;
115        case NotifyCollectionChangedAction.Reset:
116          DisplayedAvailableObjects.Clear();
117          foreach (var o in AvailableObjects) DisplayedAvailableObjects.Add(new SelectionWrapper(o));
118          break;
119      }
120    }
121
122    public ItemSelector() {
123      InitializeComponent();
124      DisplayedAvailableObjects = new ObservableCollection<SelectionWrapper>();
125      AvailableObjectsList.ItemsSource = DisplayedAvailableObjects;
126      SelectedObjectsList.DataContext = this;
127      SelectedObjectsList.SetBinding(ListBox.ItemsSourceProperty, "SelectedObjects");
128      AvailableObjects.CollectionChanged += PropageAvailableObjectsChange;
129    }
130
131
132
133    protected void OnAvailableClicked(object sender, RoutedEventArgs args) {
134      ToggleButton button = args.OriginalSource as ToggleButton;
135      if (button == null) return;
136      SelectionWrapper wrapper = button.Tag as SelectionWrapper;
137      if (wrapper == null) return;
138      if (!SelectedObjects.Contains(wrapper.Obj)) {
139        SelectedObjects.Add(wrapper.Obj);
140        wrapper.IsChecked = true;
141      } else {
142        SelectedObjects.Remove(wrapper.Obj);
143        wrapper.IsChecked = false;
144      }
145    }
146
147    protected void OnSelectedClicked(object sender, RoutedEventArgs args) {
148      Button button = args.OriginalSource as Button;
149      if (button == null) return;
150      object o = button.Tag;
151      Debug.Assert(SelectedObjects.Contains(o));
152      SelectedObjects.Remove(o);
153      var wrappers = DisplayedAvailableObjects.Where(w => w.Obj == o);
154      foreach (var w in wrappers)
155        w.IsChecked = false;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.