Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5915 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  /// <summary>
28  /// The visual representation of a list of checked items.
29  /// </summary>
30  [View("CheckedItemList View")]
31  [Content(typeof(CheckedItemList<>), true)]
32  [Content(typeof(ICheckedItemList<>), true)]
33  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
34    public new ICheckedItemList<T> Content {
35      get { return (ICheckedItemList<T>)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public CheckedItemListView()
40      : base() {
41      InitializeComponent();
42    }
43
44    protected override void RegisterContentEvents() {
45      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
46      base.RegisterContentEvents();
47    }
48    protected override void DeregisterContentEvents() {
49      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
50      base.DeregisterContentEvents();
51    }
52
53    protected override void SetEnabledStateOfControls() {
54      base.SetEnabledStateOfControls();
55      base.itemsListView.Enabled = !this.Locked;
56    }
57
58    protected override ListViewItem CreateListViewItem(T item) {
59      ListViewItem listViewItem = base.CreateListViewItem(item);
60      listViewItem.Checked = Content.ItemChecked(item);
61      return listViewItem;
62    }
63
64    #region ListView Events
65    private bool doubleClick;
66    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
67      if (doubleClick) {
68        e.NewValue = e.CurrentValue;
69        doubleClick = false;
70      } else {
71        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
72        bool check = e.NewValue == CheckState.Checked;
73        if (Content.ItemChecked(checkedItem) != check) {
74          Content.SetItemCheckedState(checkedItem, check);
75        }
76      }
77    }
78
79    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
80      if (e.Clicks > 1)
81        doubleClick = true;
82    }
83    #endregion
84
85    #region Content Events
86    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
87      if (InvokeRequired)
88        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
89      else {
90        foreach (var item in e.Items) {
91          if (itemsListView.Items[item.Index].Checked != Content.ItemChecked(item.Value))
92            itemsListView.Items[item.Index].Checked = Content.ItemChecked(item.Value);
93        }
94      }
95    }
96    #endregion
97  }
98}
Note: See TracBrowser for help on using the repository browser.