Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18078 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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  [Content(typeof(ReadOnlyCheckedItemCollection<>), true)]
35  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
36    private bool suppressCheckedEvents;
37
38    public new ICheckedItemCollection<T> Content {
39      get { return (ICheckedItemCollection<T>)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public CheckedItemCollectionView()
44      : base() {
45      InitializeComponent();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content != null) {
51        SetNumberOfCheckItems();
52      }
53    }
54
55    protected override void RegisterContentEvents() {
56      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
57      base.RegisterContentEvents();
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
62      base.DeregisterContentEvents();
63    }
64
65    private Color backupColor = Color.Empty;
66    protected override void SetEnabledStateOfControls() {
67      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
68      base.SetEnabledStateOfControls();
69      if (ReadOnly || Locked)
70        base.itemsListView.BackColor = ListView.DefaultBackColor;
71      else
72        base.itemsListView.BackColor = backupColor;
73    }
74
75    protected override ListViewItem CreateListViewItem(T item) {
76      ListViewItem listViewItem = base.CreateListViewItem(item);
77      listViewItem.Checked = Content.ItemChecked(item);
78      return listViewItem;
79    }
80
81    #region ListView Events
82    private bool doubleClick;
83    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
84      if (doubleClick) {
85        e.NewValue = e.CurrentValue;
86      } else {
87        bool check = e.NewValue == CheckState.Checked;
88        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
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; }
102      }
103    }
104    protected void itemsListView_MouseDown(object sender, MouseEventArgs e) {
105      doubleClick = e.Clicks > 1;
106    }
107    #endregion
108
109    #region Content Events
110    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<T> e) {
111      if (InvokeRequired)
112        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
113      else {
114        if (!suppressCheckedEvents) UpdateCheckedItemState(e.Items);
115        SetNumberOfCheckItems();
116      }
117    }
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    }
130    #endregion
131
132    private void SetNumberOfCheckItems() {
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      }
138    }
139
140    private void UpdateCheckedItemState(IEnumerable<T> items) {
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          }
149        }
150      } finally { itemsListView.EndUpdate(); itemsListView.Refresh(); }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.