Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15720 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 5.2 KB
RevLine 
[3595]1#region License Information
[3558]2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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 {
[3558]36    public new ICheckedItemCollection<T> Content {
37      get { return (ICheckedItemCollection<T>)base.Content; }
38      set { base.Content = value; }
39    }
[4068]40
[3595]41    public CheckedItemCollectionView()
42      : base() {
43      InitializeComponent();
[3558]44    }
45
[9026]46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48      if (Content != null) {
49        SetNumberOfCheckItems();
50      }
51    }
52
[3595]53    protected override void RegisterContentEvents() {
54      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
55      base.RegisterContentEvents();
[3558]56    }
57
58    protected override void DeregisterContentEvents() {
[3595]59      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
[3558]60      base.DeregisterContentEvents();
61    }
62
[6233]63    private Color backupColor = Color.Empty;
[3904]64    protected override void SetEnabledStateOfControls() {
[6233]65      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
[3904]66      base.SetEnabledStateOfControls();
[6233]67      if (ReadOnly || Locked)
68        base.itemsListView.BackColor = ListView.DefaultBackColor;
69      else
70        base.itemsListView.BackColor = backupColor;
[3558]71    }
72
[3595]73    protected override ListViewItem CreateListViewItem(T item) {
74      ListViewItem listViewItem = base.CreateListViewItem(item);
[3564]75      listViewItem.Checked = Content.ItemChecked(item);
[3558]76      return listViewItem;
77    }
78
79    #region ListView Events
[3788]80    private bool doubleClick;
[3738]81    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
[3788]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) {
[6233]89          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
90          else e.NewValue = e.CurrentValue;
[3788]91        }
[3564]92      }
[3558]93    }
[3788]94    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
95      if (e.Clicks > 1)
96        doubleClick = true;
97    }
[3558]98    #endregion
99
100    #region Content Events
[9857]101    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<T> e) {
[3558]102      if (InvokeRequired)
[3595]103        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
[3558]104      else {
[9857]105        UpdateCheckedItemState(e.Items);
[9026]106        SetNumberOfCheckItems();
[3558]107      }
[4068]108    }
[9134]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    }
[3558]121    #endregion
[9026]122
123    private void SetNumberOfCheckItems() {
[9134]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      }
[9026]129    }
[9857]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    }
[3558]140  }
141}
Note: See TracBrowser for help on using the repository browser.