Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2845_EnhancedProgress/HeuristicLab.Data.Views/3.3/EnumValueView.cs @ 16300

Last change on this file since 16300 was 15010, checked in by mkommend, 8 years ago

#2724: Fixed caption of EnumValueView.

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.Data.Views {
28  [View("EnumValue View")]
29  [Content(typeof(EnumValue<>), true)]
30  public partial class EnumValueView<T> : ItemView where T : struct, IComparable {
31
32    public new EnumValue<T> Content {
33      get { return (EnumValue<T>)base.Content; }
34      set { base.Content = value; }
35    }
36
37    public override bool ReadOnly {
38      get {
39        if ((Content != null) && Content.ReadOnly) return true;
40        return base.ReadOnly;
41      }
42      set { base.ReadOnly = value; }
43    }
44
45    static EnumValueView() {
46      if (!typeof(T).IsEnum)
47        throw new InvalidOperationException("Generic type " + typeof(T).Name + " is not an enum.");
48    }
49
50    public EnumValueView() {
51      InitializeComponent();
52      this.Caption = typeof(T).Name + " View";
53
54      valueComboBox.DataSource = Enum.GetValues(typeof(T));
55      foreach (T flag in Enum.GetValues(typeof(T)))
56        flagsListView.Items.Add(new ListViewItem(flag.ToString()) { Tag = flag });
57      columnHeader.Width = -1;
58
59      bool isFlags = Attribute.IsDefined(typeof(T), typeof(FlagsAttribute));
60      if (isFlags) valueLabel.Text = "Flags:";
61      valueComboBox.Visible = !isFlags;
62      flagsListView.Visible = isFlags;
63    }
64    public EnumValueView(EnumValue<T> content)
65      : this() {
66      Content = content;
67    }
68
69    protected override void DeregisterContentEvents() {
70      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
71      base.DeregisterContentEvents();
72    }
73
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.ValueChanged += new EventHandler(Content_ValueChanged);
77    }
78
79    protected override void OnContentChanged() {
80      base.OnContentChanged();
81      if (Content == null) {
82        valueComboBox.SelectedIndex = -1;
83        foreach (ListViewItem item in flagsListView.Items)
84          item.Checked = false;
85      } else {
86        valueComboBox.SelectedItem = Content.Value;
87        foreach (ListViewItem item in flagsListView.Items) {
88          var flag = (Enum)item.Tag;
89          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
90        }
91      }
92    }
93
94    protected override void SetEnabledStateOfControls() {
95      base.SetEnabledStateOfControls();
96      if (Content == null) {
97        valueComboBox.Enabled = false;
98        flagsListView.Enabled = false;
99      } else {
100        valueComboBox.Enabled = !ReadOnly;
101        flagsListView.Enabled = !ReadOnly;
102      }
103    }
104
105    private void Content_ValueChanged(object sender, EventArgs e) {
106      if (InvokeRequired)
107        Invoke(new EventHandler(Content_ValueChanged), sender, e);
108      else {
109        valueComboBox.SelectedItem = Content.Value;
110        foreach (ListViewItem item in flagsListView.Items) {
111          var flag = (Enum)item.Tag;
112          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
113        }
114      }
115    }
116
117    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
118      if ((Content != null) && !Content.ReadOnly)
119        Content.Value = (T)valueComboBox.SelectedItem;
120    }
121
122    private void flagsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
123      var flag = (T)e.Item.Tag;
124      if ((Content != null) && !Content.ReadOnly)
125        Content.Value = ((Enum)(object)Content.Value).SetFlag(flag, e.Item.Checked);
126    }
127  }
128
129  internal static class EnumHelper {
130    //https://stackoverflow.com/a/21581418
131    public static T SetFlag<T>(this Enum value, T flag, bool set) {
132      var baseType = Enum.GetUnderlyingType(value.GetType());
133      dynamic valueAsBase = Convert.ChangeType(value, baseType);
134      dynamic flagAsBase = Convert.ChangeType(flag, baseType);
135      if (set)
136        valueAsBase |= flagAsBase;
137      else
138        valueAsBase &= ~flagAsBase;
139      return (T)valueAsBase;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.