Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9201 was 9134, checked in by sforsten, 12 years ago

#1992: number of checked items also gets now updated when items are added, removed, moved, replaced or when the content is reset

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