Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 6193

Last change on this file since 6193 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Windows.Forms;
23using HeuristicLab.Collections;
24using HeuristicLab.MainForm;
25
26namespace HeuristicLab.Core.Views {
27  [View("CheckedItemCollection View")]
28  [Content(typeof(ICheckedItemCollection<>), true)]
29  [Content(typeof(CheckedItemCollection<>), true)]
30  public partial class CheckedItemCollectionView<T> : ItemCollectionView<T> where T : class, IItem {
31    public new ICheckedItemCollection<T> Content {
32      get { return (ICheckedItemCollection<T>)base.Content; }
33      set { base.Content = value; }
34    }
35
36    public CheckedItemCollectionView()
37      : base() {
38      InitializeComponent();
39    }
40
41    protected override void RegisterContentEvents() {
42      Content.CheckedItemsChanged += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
43      base.RegisterContentEvents();
44    }
45
46    protected override void DeregisterContentEvents() {
47      Content.CheckedItemsChanged -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged);
48      base.DeregisterContentEvents();
49    }
50
51    protected override void SetEnabledStateOfControls() {
52      base.SetEnabledStateOfControls();
53      base.itemsListView.Enabled = !this.Locked;
54    }
55
56    protected override ListViewItem CreateListViewItem(T item) {
57      ListViewItem listViewItem = base.CreateListViewItem(item);
58      listViewItem.Checked = Content.ItemChecked(item);
59      return listViewItem;
60    }
61
62    #region ListView Events
63    private bool doubleClick;
64    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
65      if (doubleClick) {
66        e.NewValue = e.CurrentValue;
67        doubleClick = false;
68      } else {
69        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
70        bool check = e.NewValue == CheckState.Checked;
71        if (Content.ItemChecked(checkedItem) != check) {
72          Content.SetItemCheckedState(checkedItem, check);
73        }
74      }
75    }
76    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
77      if (e.Clicks > 1)
78        doubleClick = true;
79    }
80    #endregion
81
82    #region Content Events
83    protected virtual void Content_CheckedItemsChanged(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) {
84      if (InvokeRequired)
85        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CheckedItemsChanged), sender, e);
86      else {
87        foreach (T item in e.Items) {
88          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
89            if (listViewItem.Checked != Content.ItemChecked(item))
90              listViewItem.Checked = Content.ItemChecked(item);
91          }
92        }
93      }
94    }
95    #endregion
96  }
97}
Note: See TracBrowser for help on using the repository browser.