Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs @ 16003

Last change on this file since 16003 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    public new ICheckedItemList<T> Content {
40      get { return (ICheckedItemList<T>)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public CheckedItemListView()
45      : base() {
46      InitializeComponent();
47    }
48
49    protected override void RegisterContentEvents() {
50      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
51      base.RegisterContentEvents();
52    }
53    protected override void DeregisterContentEvents() {
54      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
55      base.DeregisterContentEvents();
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content != null) {
61        SetNumberOfCheckItems();
62      }
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        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) {
91          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
92          else e.NewValue = e.CurrentValue;
93        }
94      }
95    }
96
97    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
98      if (e.Clicks > 1)
99        doubleClick = true;
100    }
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)
108        validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
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) {
128        for (int i = 0; i < Content.Count; i++) {
129          Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
130        }
131      } else
132        base.itemsListView_DragDrop(sender, e);
133    }
134    #endregion
135
136    #region Content Events
137    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
138      if (InvokeRequired)
139        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
140      else {
141        UpdateCheckedItemState(e.Items);
142        SetNumberOfCheckItems();
143      }
144    }
145    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
146      base.Content_CollectionReset(sender, e);
147      SetNumberOfCheckItems();
148    }
149    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
150      base.Content_ItemsAdded(sender, e);
151      SetNumberOfCheckItems();
152    }
153    protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
154      base.Content_ItemsMoved(sender, e);
155      UpdateCheckedItemState(e.Items);
156      SetNumberOfCheckItems();
157    }
158    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
159      base.Content_ItemsRemoved(sender, e);
160      SetNumberOfCheckItems();
161    }
162    protected override void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
163      base.Content_ItemsReplaced(sender, e);
164      SetNumberOfCheckItems();
165    }
166    #endregion
167
168    private void SetNumberOfCheckItems() {
169      if (InvokeRequired) {
170        Invoke((Action)SetNumberOfCheckItems);
171      } else {
172        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
173      }
174    }
175
176    private void UpdateCheckedItemState(IEnumerable<IndexedItem<T>> items) {
177      foreach (var item in items) {
178        var isChecked = Content.ItemChecked(item.Value);
179        if (itemsListView.Items[item.Index].Checked != isChecked)
180          itemsListView.Items[item.Index].Checked = isChecked;
181      }
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.