Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data.Views/3.3/EnumValueView.cs @ 14778

Last change on this file since 14778 was 14597, checked in by pfleck, 8 years ago

#2724

  • Added a ListView for Flag-attributed enums in the EnumValueView.
  • Simplified EnumValue cloning (already done in base-constructor).
  • Added a SetFlag helper for enums.
File size: 4.1 KB
RevLine 
[12037]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12037]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;
[14597]23using System.Windows.Forms;
[12037]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    public EnumValueView() {
46      InitializeComponent();
[14597]47      this.Name = typeof(T).Name + "EnumView";
48
[12037]49      valueComboBox.DataSource = Enum.GetValues(typeof(T));
[14597]50      foreach (T flag in Enum.GetValues(typeof(T)))
51        flagsListView.Items.Add(new ListViewItem(flag.ToString()) { Tag = flag });
52      columnHeader.Width = -1;
53
54      bool isFlags = Attribute.IsDefined(typeof(T), typeof(FlagsAttribute));
55      if (isFlags) valueLabel.Text = "Flags:";
56      valueComboBox.Visible = !isFlags;
57      flagsListView.Visible = isFlags;
[12037]58    }
59    public EnumValueView(EnumValue<T> content)
60      : this() {
61      Content = content;
62    }
63
64    protected override void DeregisterContentEvents() {
65      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
66      base.DeregisterContentEvents();
67    }
68
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      Content.ValueChanged += new EventHandler(Content_ValueChanged);
72    }
73
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
[14597]76      if (Content == null) {
77        valueComboBox.SelectedIndex = -1;
78        foreach (ListViewItem item in flagsListView.Items)
79          item.Checked = false;
80      } else {
[12037]81        valueComboBox.SelectedItem = Content.Value;
[14597]82        foreach (ListViewItem item in flagsListView.Items) {
83          var flag = (Enum)item.Tag;
84          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
85        }
86      }
[12037]87    }
88
89    protected override void SetEnabledStateOfControls() {
90      base.SetEnabledStateOfControls();
[14597]91      if (Content == null) {
92        valueComboBox.Enabled = false;
93        flagsListView.Enabled = false;
94      } else {
95        valueComboBox.Enabled = !ReadOnly;
96        flagsListView.Enabled = !ReadOnly;
97      }
[12037]98    }
99
100    private void Content_ValueChanged(object sender, EventArgs e) {
101      if (InvokeRequired)
102        Invoke(new EventHandler(Content_ValueChanged), sender, e);
[14597]103      else {
[12037]104        valueComboBox.SelectedItem = Content.Value;
[14597]105        foreach (ListViewItem item in flagsListView.Items) {
106          var flag = (Enum)item.Tag;
107          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
108        }
109      }
[12037]110    }
111
112    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
113      if ((Content != null) && !Content.ReadOnly)
114        Content.Value = (T)valueComboBox.SelectedItem;
115    }
[14597]116
117    private void flagsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
118      var flag = (T)e.Item.Tag;
119      if ((Content != null) && !Content.ReadOnly)
120        Content.Value = ((Enum)(object)Content.Value).SetFlag(flag, e.Item.Checked);
121    }
[12037]122  }
123}
Note: See TracBrowser for help on using the repository browser.