Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 12771

Last change on this file since 12771 was 9839, checked in by abeham, 11 years ago

#2044:

  • Updated checked state of listview items in CheckedItemListView when elements were moved in the CheckedItemList
  • Cosmetic change in CheckedItemCollectionView
File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
35    public new ICheckedItemCollection<T> Content {
36      get { return (ICheckedItemCollection<T>)base.Content; }
37      set { base.Content = value; }
38    }
39
40    public CheckedItemCollectionView()
41      : base() {
42      InitializeComponent();
43    }
44
45    protected override void OnContentChanged() {
46      base.OnContentChanged();
47      if (Content != null) {
48        SetNumberOfCheckItems();
49      }
50    }
51
52    protected override void RegisterContentEvents() {
53      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
54      base.RegisterContentEvents();
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
59      base.DeregisterContentEvents();
60    }
61
62    private Color backupColor = Color.Empty;
63    protected override void SetEnabledStateOfControls() {
64      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
65      base.SetEnabledStateOfControls();
66      if (ReadOnly || Locked)
67        base.itemsListView.BackColor = ListView.DefaultBackColor;
68      else
69        base.itemsListView.BackColor = backupColor;
70    }
71
72    protected override ListViewItem CreateListViewItem(T item) {
73      ListViewItem listViewItem = base.CreateListViewItem(item);
74      listViewItem.Checked = Content.ItemChecked(item);
75      return listViewItem;
76    }
77
78    #region ListView Events
79    private bool doubleClick;
80    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
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) {
88          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
89          else e.NewValue = e.CurrentValue;
90        }
91      }
92    }
93    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
94      if (e.Clicks > 1)
95        doubleClick = true;
96    }
97    #endregion
98
99    #region Content Events
100    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<T> e) {
101      if (InvokeRequired)
102        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
103      else {
104        UpdateCheckedItemState(e.Items);
105        SetNumberOfCheckItems();
106      }
107    }
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    }
120    #endregion
121
122    private void SetNumberOfCheckItems() {
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      }
128    }
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    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.