Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 18242

Last change on this file since 18242 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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