source: trunk/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs @ 17009

Last change on this file since 17009 was 17009, checked in by abeham, 2 years ago

#3010: Added batch methods for checking multiple items at once for CheckedItemList and CheckedItemCollection

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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  /// <summary>
32  /// The visual representation of a list of checked items.
33  /// </summary>
34  [View("CheckedItemList View")]
35  [Content(typeof(CheckedItemList<>), true)]
36  [Content(typeof(ICheckedItemList<>), true)]
37  [Content(typeof(ReadOnlyCheckedItemList<>), true)]
38  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
39    private bool suppressCheckedEvents;
40
41    public new ICheckedItemList<T> Content {
42      get { return (ICheckedItemList<T>)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public CheckedItemListView()
47      : base() {
48      InitializeComponent();
49    }
50
51    protected override void RegisterContentEvents() {
52      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
53      base.RegisterContentEvents();
54    }
55    protected override void DeregisterContentEvents() {
56      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
57      base.DeregisterContentEvents();
58    }
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
62      if (Content != null) {
63        SetNumberOfCheckItems();
64      }
65    }
66
67    private Color backupColor = Color.Empty;
68    protected override void SetEnabledStateOfControls() {
69      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
70      base.SetEnabledStateOfControls();
71      if (ReadOnly || Locked)
72        base.itemsListView.BackColor = ListView.DefaultBackColor;
73      else
74        base.itemsListView.BackColor = backupColor;
75    }
76
77    protected override ListViewItem CreateListViewItem(T item) {
78      ListViewItem listViewItem = base.CreateListViewItem(item);
79      listViewItem.Checked = Content.ItemChecked(item);
80      return listViewItem;
81    }
82
83    #region ListView Events
84    private bool doubleClick;
85    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
86      if (doubleClick) {
87        e.NewValue = e.CurrentValue;
88        doubleClick = false;
89      } else {
90        bool check = e.NewValue == CheckState.Checked;
91        if (Content.ItemChecked(e.Index) == check) return;
92
93        suppressCheckedEvents = true;
94        try {
95          if (itemsListView.SelectedIndices.Count > 1
96            && itemsListView.SelectedIndices.Contains(e.Index)) {
97            if (!ReadOnly && !Locked) Content.SetItemCheckedState(itemsListView.SelectedIndices.Cast<int>(), check);
98            else e.NewValue = e.CurrentValue;
99          } else {
100            var checkedItem = (T)itemsListView.Items[e.Index].Tag;
101            if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
102            else e.NewValue = e.CurrentValue;
103          }
104        } finally { suppressCheckedEvents = false; }
105      }
106    }
107
108    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
109      if (e.Clicks > 1)
110        doubleClick = true;
111    }
112
113    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
114      validDragOperation = false;
115      if (Locked || ReadOnly) return;
116
117      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
118      if (data != null)
119        validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
120      else
121        base.itemsListView_DragEnter(sender, e);
122    }
123
124    protected override void itemsListView_DragOver(object sender, DragEventArgs e) {
125      e.Effect = DragDropEffects.None;
126      if (!validDragOperation) return;
127
128      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
129      if (data != null)
130        e.Effect = DragDropEffects.Copy;
131      else
132        base.itemsListView_DragOver(sender, e);
133    }
134
135    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
136      if (e.Effect == DragDropEffects.None) return;
137      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
138      if (data != null) {
139        for (int i = 0; i < Content.Count; i++) {
140          Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
141        }
142      } else
143        base.itemsListView_DragDrop(sender, e);
144    }
145    #endregion
146
147    #region Content Events
148    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
149      if (InvokeRequired)
150        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
151      else {
152        if (!suppressCheckedEvents) UpdateCheckedItemState(e.Items);
153        SetNumberOfCheckItems();
154      }
155    }
156    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
157      base.Content_CollectionReset(sender, e);
158      SetNumberOfCheckItems();
159    }
160    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
161      base.Content_ItemsAdded(sender, e);
162      SetNumberOfCheckItems();
163    }
164    protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
165      base.Content_ItemsMoved(sender, e);
166      UpdateCheckedItemState(e.Items);
167      SetNumberOfCheckItems();
168    }
169    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
170      base.Content_ItemsRemoved(sender, e);
171      SetNumberOfCheckItems();
172    }
173    protected override void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
174      base.Content_ItemsReplaced(sender, e);
175      SetNumberOfCheckItems();
176    }
177    #endregion
178
179    private void SetNumberOfCheckItems() {
180      if (InvokeRequired) {
181        Invoke((Action)SetNumberOfCheckItems);
182      } else {
183        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
184      }
185    }
186
187    private void UpdateCheckedItemState(IEnumerable<IndexedItem<T>> items) {
188      itemsListView.BeginUpdate();
189      try {
190        foreach (var item in items) {
191          var isChecked = Content.ItemChecked(item.Value);
192          if (itemsListView.Items[item.Index].Checked != isChecked)
193            itemsListView.Items[item.Index].Checked = isChecked;
194        }
195      } finally { itemsListView.EndUpdate(); itemsListView.Refresh(); }
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.