Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14597 was 14597, checked in by pfleck, 7 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
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    public EnumValueView() {
46      InitializeComponent();
47      this.Name = typeof(T).Name + "EnumView";
48
49      valueComboBox.DataSource = Enum.GetValues(typeof(T));
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;
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();
76      if (Content == null) {
77        valueComboBox.SelectedIndex = -1;
78        foreach (ListViewItem item in flagsListView.Items)
79          item.Checked = false;
80      } else {
81        valueComboBox.SelectedItem = Content.Value;
82        foreach (ListViewItem item in flagsListView.Items) {
83          var flag = (Enum)item.Tag;
84          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
85        }
86      }
87    }
88
89    protected override void SetEnabledStateOfControls() {
90      base.SetEnabledStateOfControls();
91      if (Content == null) {
92        valueComboBox.Enabled = false;
93        flagsListView.Enabled = false;
94      } else {
95        valueComboBox.Enabled = !ReadOnly;
96        flagsListView.Enabled = !ReadOnly;
97      }
98    }
99
100    private void Content_ValueChanged(object sender, EventArgs e) {
101      if (InvokeRequired)
102        Invoke(new EventHandler(Content_ValueChanged), sender, e);
103      else {
104        valueComboBox.SelectedItem = Content.Value;
105        foreach (ListViewItem item in flagsListView.Items) {
106          var flag = (Enum)item.Tag;
107          item.Checked = ((Enum)(object)Content.Value).HasFlag(flag);
108        }
109      }
110    }
111
112    private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
113      if ((Content != null) && !Content.ReadOnly)
114        Content.Value = (T)valueComboBox.SelectedItem;
115    }
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    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.