Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 17009

Last change on this file since 17009 was 17009, checked in by abeham, 5 years ago

#3010: Added batch methods for checking multiple items at once for CheckedItemList and CheckedItemCollection

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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    private bool suppressCheckedEvents;
37
38    public new ICheckedItemCollection<T> Content {
39      get { return (ICheckedItemCollection<T>)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public CheckedItemCollectionView()
44      : base() {
45      InitializeComponent();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null) {
51        SetNumberOfCheckItems();
52      }
53    }
54
55    protected override void RegisterContentEvents() {
56      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
57      base.RegisterContentEvents();
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
62      base.DeregisterContentEvents();
63    }
64
65    private Color backupColor = Color.Empty;
66    protected override void SetEnabledStateOfControls() {
67      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
68      base.SetEnabledStateOfControls();
69      if (ReadOnly || Locked)
70        base.itemsListView.BackColor = ListView.DefaultBackColor;
71      else
72        base.itemsListView.BackColor = backupColor;
73    }
74
75    protected override ListViewItem CreateListViewItem(T item) {
76      ListViewItem listViewItem = base.CreateListViewItem(item);
77      listViewItem.Checked = Content.ItemChecked(item);
78      return listViewItem;
79    }
80
81    #region ListView Events
82    private bool doubleClick;
83    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
84      if (doubleClick) {
85        e.NewValue = e.CurrentValue;
86        doubleClick = false;
87      } else {
88        bool check = e.NewValue == CheckState.Checked;
89        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
90        if (Content.ItemChecked(checkedItem) == check) return;
91
92        suppressCheckedEvents = true;
93        try {
94          if (itemsListView.SelectedIndices.Count > 1
95            && itemsListView.SelectedIndices.Contains(e.Index)) {
96            if (!ReadOnly && !Locked) Content.SetItemCheckedState(itemsListView.SelectedItems.Cast<ListViewItem>().Select(x => (T)x.Tag), check);
97            else e.NewValue = e.CurrentValue;
98          } else {
99            if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
100            else e.NewValue = e.CurrentValue;
101          }
102        } finally { suppressCheckedEvents = false; }
103      }
104    }
105    protected void itemsListView_MouseDown(object sender, MouseEventArgs e) {
106      if (e.Clicks > 1)
107        doubleClick = true;
108    }
109    #endregion
110
111    #region Content Events
112    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<T> e) {
113      if (InvokeRequired)
114        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
115      else {
116        if (!suppressCheckedEvents) UpdateCheckedItemState(e.Items);
117        SetNumberOfCheckItems();
118      }
119    }
120    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
121      base.Content_CollectionReset(sender, e);
122      SetNumberOfCheckItems();
123    }
124    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
125      base.Content_ItemsAdded(sender, e);
126      SetNumberOfCheckItems();
127    }
128    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
129      base.Content_ItemsRemoved(sender, e);
130      SetNumberOfCheckItems();
131    }
132    #endregion
133
134    private void SetNumberOfCheckItems() {
135      if (InvokeRequired) {
136        Invoke((Action)SetNumberOfCheckItems);
137      } else {
138        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
139      }
140    }
141
142    private void UpdateCheckedItemState(IEnumerable<T> items) {
143      itemsListView.BeginUpdate();
144      try {
145        foreach (T item in items) {
146          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
147            var isChecked = Content.ItemChecked(item);
148            if (listViewItem.Checked != isChecked)
149              listViewItem.Checked = isChecked;
150          }
151        }
152      } finally { itemsListView.EndUpdate(); itemsListView.Refresh(); }
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.