Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11831 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

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