Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17126 was 17126, checked in by abeham, 3 years ago

#3010: improved double click handling in checked collection views

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      } else {
89        bool check = e.NewValue == CheckState.Checked;
90        if (Content.ItemChecked(e.Index) == check) return;
91
92        suppressCheckedEvents = true;
93        try {
94          if (itemsListView.SelectedIndices.Count > 1
95            && itemsListView.SelectedIndices.Contains(e.Index)) {
96            if (!ReadOnly && !Locked) Content.SetItemCheckedState(itemsListView.SelectedIndices.Cast<int>(), check);
97            else e.NewValue = e.CurrentValue;
98          } else {
99            var checkedItem = (T)itemsListView.Items[e.Index].Tag;
100            if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
101            else e.NewValue = e.CurrentValue;
102          }
103        } finally { suppressCheckedEvents = false; }
104      }
105    }
106
107    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
108      doubleClick = e.Clicks > 1;
109    }
110
111    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
112      validDragOperation = false;
113      if (Locked || ReadOnly) return;
114
115      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
116      if (data != null)
117        validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
118      else
119        base.itemsListView_DragEnter(sender, e);
120    }
121
122    protected override void itemsListView_DragOver(object sender, DragEventArgs e) {
123      e.Effect = DragDropEffects.None;
124      if (!validDragOperation) return;
125
126      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
127      if (data != null)
128        e.Effect = DragDropEffects.Copy;
129      else
130        base.itemsListView_DragOver(sender, e);
131    }
132
133    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
134      if (e.Effect == DragDropEffects.None) return;
135      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
136      if (data != null) {
137        for (int i = 0; i < Content.Count; i++) {
138          Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
139        }
140      } else
141        base.itemsListView_DragDrop(sender, e);
142    }
143    #endregion
144
145    #region Content Events
146    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
147      if (InvokeRequired)
148        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
149      else {
150        if (!suppressCheckedEvents) UpdateCheckedItemState(e.Items);
151        SetNumberOfCheckItems();
152      }
153    }
154    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
155      base.Content_CollectionReset(sender, e);
156      SetNumberOfCheckItems();
157    }
158    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
159      base.Content_ItemsAdded(sender, e);
160      SetNumberOfCheckItems();
161    }
162    protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
163      base.Content_ItemsMoved(sender, e);
164      UpdateCheckedItemState(e.Items);
165      SetNumberOfCheckItems();
166    }
167    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
168      base.Content_ItemsRemoved(sender, e);
169      SetNumberOfCheckItems();
170    }
171    protected override void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
172      base.Content_ItemsReplaced(sender, e);
173      SetNumberOfCheckItems();
174    }
175    #endregion
176
177    private void SetNumberOfCheckItems() {
178      if (InvokeRequired) {
179        Invoke((Action)SetNumberOfCheckItems);
180      } else {
181        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
182      }
183    }
184
185    private void UpdateCheckedItemState(IEnumerable<IndexedItem<T>> items) {
186      itemsListView.BeginUpdate();
187      try {
188        foreach (var item in items) {
189          var isChecked = Content.ItemChecked(item.Value);
190          if (itemsListView.Items[item.Index].Checked != isChecked)
191            itemsListView.Items[item.Index].Checked = isChecked;
192        }
193      } finally { itemsListView.EndUpdate(); itemsListView.Refresh(); }
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.