Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12338 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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