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

Last change on this file since 15706 was 15706, checked in by mkommend, 23 months ago

#2887: Ignored TestResults in trunk folder.

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