Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs @ 15325

Last change on this file since 15325 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Core.Views {
30  /// <summary>
31  /// The visual representation of a list of checked items.
32  /// </summary>
33  [View("CheckedItemList View")]
34  [Content(typeof(CheckedItemList<>), true)]
35  [Content(typeof(ICheckedItemList<>), true)]
36  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
37    public new ICheckedItemList<T> Content {
38      get { return (ICheckedItemList<T>)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public CheckedItemListView()
43      : base() {
44      InitializeComponent();
45    }
46
47    protected override void RegisterContentEvents() {
48      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
49      base.RegisterContentEvents();
50    }
51    protected override void DeregisterContentEvents() {
52      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
53      base.DeregisterContentEvents();
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content != null) {
59        SetNumberOfCheckItems();
60      }
61    }
62
63    private Color backupColor = Color.Empty;
64    protected override void SetEnabledStateOfControls() {
65      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
66      base.SetEnabledStateOfControls();
67      if (ReadOnly || Locked)
68        base.itemsListView.BackColor = ListView.DefaultBackColor;
69      else
70        base.itemsListView.BackColor = backupColor;
71    }
72
73    protected override ListViewItem CreateListViewItem(T item) {
74      ListViewItem listViewItem = base.CreateListViewItem(item);
75      listViewItem.Checked = Content.ItemChecked(item);
76      return listViewItem;
77    }
78
79    #region ListView Events
80    private bool doubleClick;
81    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
82      if (doubleClick) {
83        e.NewValue = e.CurrentValue;
84        doubleClick = false;
85      } else {
86        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
87        bool check = e.NewValue == CheckState.Checked;
88        if (Content.ItemChecked(checkedItem) != check) {
89          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
90          else e.NewValue = e.CurrentValue;
91        }
92      }
93    }
94
95    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
96      if (e.Clicks > 1)
97        doubleClick = true;
98    }
99
100    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
101      validDragOperation = false;
102      if (Locked || ReadOnly) return;
103
104      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
105      if (data != null)
106        validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
107      else
108        base.itemsListView_DragEnter(sender, e);
109    }
110
111    protected override void itemsListView_DragOver(object sender, DragEventArgs e) {
112      e.Effect = DragDropEffects.None;
113      if (!validDragOperation) return;
114
115      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
116      if (data != null)
117        e.Effect = DragDropEffects.Copy;
118      else
119        base.itemsListView_DragOver(sender, e);
120    }
121
122    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
123      if (e.Effect == DragDropEffects.None) return;
124      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
125      if (data != null) {
126        for (int i = 0; i < Content.Count; i++) {
127          Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
128        }
129      } else
130        base.itemsListView_DragDrop(sender, e);
131    }
132    #endregion
133
134    #region Content Events
135    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
136      if (InvokeRequired)
137        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
138      else {
139        foreach (var item in e.Items) {
140          if (itemsListView.Items[item.Index].Checked != Content.ItemChecked(item.Value))
141            itemsListView.Items[item.Index].Checked = Content.ItemChecked(item.Value);
142        }
143        SetNumberOfCheckItems();
144      }
145    }
146    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
147      base.Content_CollectionReset(sender, e);
148      SetNumberOfCheckItems();
149    }
150    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
151      base.Content_ItemsAdded(sender, e);
152      SetNumberOfCheckItems();
153    }
154    protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
155      base.Content_ItemsMoved(sender, e);
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}
Note: See TracBrowser for help on using the repository browser.