Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 11168

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

#1888:

  • Merged revisions from trunk
File size: 5.0 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  [View("CheckedItemCollection View")]
31  [Content(typeof(ICheckedItemCollection<>), true)]
32  [Content(typeof(CheckedItemCollection<>), true)]
33  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
34    public new ICheckedItemCollection<T> Content {
35      get { return (ICheckedItemCollection<T>)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public CheckedItemCollectionView()
40      : base() {
41      InitializeComponent();
42    }
43
44    protected override void OnContentChanged() {
45      base.OnContentChanged();
46      if (Content != null) {
47        SetNumberOfCheckItems();
48      }
49    }
50
51    protected override void RegisterContentEvents() {
52      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
53      base.RegisterContentEvents();
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
58      base.DeregisterContentEvents();
59    }
60
61    private Color backupColor = Color.Empty;
62    protected override void SetEnabledStateOfControls() {
63      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
64      base.SetEnabledStateOfControls();
65      if (ReadOnly || Locked)
66        base.itemsListView.BackColor = ListView.DefaultBackColor;
67      else
68        base.itemsListView.BackColor = backupColor;
69    }
70
71    protected override ListViewItem CreateListViewItem(T item) {
72      ListViewItem listViewItem = base.CreateListViewItem(item);
73      listViewItem.Checked = Content.ItemChecked(item);
74      return listViewItem;
75    }
76
77    #region ListView Events
78    private bool doubleClick;
79    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
80      if (doubleClick) {
81        e.NewValue = e.CurrentValue;
82        doubleClick = false;
83      } else {
84        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
85        bool check = e.NewValue == CheckState.Checked;
86        if (Content.ItemChecked(checkedItem) != check) {
87          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
88          else e.NewValue = e.CurrentValue;
89        }
90      }
91    }
92    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
93      if (e.Clicks > 1)
94        doubleClick = true;
95    }
96    #endregion
97
98    #region Content Events
99    protected virtual void Content_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) {
100      if (InvokeRequired)
101        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
102      else {
103        foreach (T item in e.Items) {
104          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
105            if (listViewItem.Checked != Content.ItemChecked(item))
106              listViewItem.Checked = Content.ItemChecked(item);
107          }
108        }
109        SetNumberOfCheckItems();
110      }
111    }
112    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
113      base.Content_CollectionReset(sender, e);
114      SetNumberOfCheckItems();
115    }
116    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
117      base.Content_ItemsAdded(sender, e);
118      SetNumberOfCheckItems();
119    }
120    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
121      base.Content_ItemsRemoved(sender, e);
122      SetNumberOfCheckItems();
123    }
124    #endregion
125
126    private void SetNumberOfCheckItems() {
127      if (InvokeRequired) {
128        Invoke((Action)SetNumberOfCheckItems);
129      } else {
130        this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.