Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 17526

Last change on this file since 17526 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Core.Views {
31  [View("CheckedItemCollection View")]
32  [Content(typeof(ICheckedItemCollection<>), true)]
33  [Content(typeof(CheckedItemCollection<>), true)]
34  [Content(typeof(ReadOnlyCheckedItemCollection<>), true)]
35  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
36    public new ICheckedItemCollection<T> Content {
37      get { return (ICheckedItemCollection<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public CheckedItemCollectionView()
42      : base() {
43      InitializeComponent();
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content != null) {
49        SetNumberOfCheckItems();
50      }
51    }
52
53    protected override void RegisterContentEvents() {
54      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
55      base.RegisterContentEvents();
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
60      base.DeregisterContentEvents();
61    }
62
63    private Color backupColor = Color.Empty;
64    protected override void SetEnabledStateOfControls() {
65      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
66      base.SetEnabledStateOfControls();
67      if (ReadOnly || Locked)
68        base.itemsListView.BackColor = ListView.DefaultBackColor;
69      else
70        base.itemsListView.BackColor = backupColor;
71    }
72
73    protected override ListViewItem CreateListViewItem(T item) {
74      ListViewItem listViewItem = base.CreateListViewItem(item);
75      listViewItem.Checked = Content.ItemChecked(item);
76      return listViewItem;
77    }
78
79    #region ListView Events
80    private bool doubleClick;
81    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
82      if (doubleClick) {
83        e.NewValue = e.CurrentValue;
84        doubleClick = false;
85      } else {
86        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
87        bool check = e.NewValue == CheckState.Checked;
88        if (Content.ItemChecked(checkedItem) != check) {
89          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
90          else e.NewValue = e.CurrentValue;
91        }
92      }
93    }
94    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
95      if (e.Clicks > 1)
96        doubleClick = true;
97    }
98    #endregion
99
100    #region Content Events
101    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<T> e) {
102      if (InvokeRequired)
103        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
104      else {
105        UpdateCheckedItemState(e.Items);
106        SetNumberOfCheckItems();
107      }
108    }
109    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
110      base.Content_CollectionReset(sender, e);
111      SetNumberOfCheckItems();
112    }
113    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
114      base.Content_ItemsAdded(sender, e);
115      SetNumberOfCheckItems();
116    }
117    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
118      base.Content_ItemsRemoved(sender, e);
119      SetNumberOfCheckItems();
120    }
121    #endregion
122
123    private void SetNumberOfCheckItems() {
124      if (InvokeRequired) {
125        Invoke((Action)SetNumberOfCheckItems);
126      } else {
127        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
128      }
129    }
130
131    private void UpdateCheckedItemState(IEnumerable<T> items) {
132      foreach (T item in items) {
133        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
134          var isChecked = Content.ItemChecked(item);
135          if (listViewItem.Checked != isChecked)
136            listViewItem.Checked = isChecked;
137        }
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.