Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3628 was 3628, checked in by gkronber, 14 years ago

Added ReadOnlyCheckedItemCollection and ReadOnlyCheckedItemList #992 (CheckedItemList and CheckedItemCollection is necessary)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Collections;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  /// <summary>
35  /// The visual representation of a list of checked items.
36  /// </summary>
37  [View("CheckedItemList View")]
38  [Content(typeof(CheckedItemList<>), true)]
39  [Content(typeof(ICheckedItemList<>), true)]
40  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
41    public new ICheckedItemList<T> Content {
42      get { return (ICheckedItemList<T>)base.Content; }
43      set { base.Content = value; }
44    }
45
46    public CheckedItemListView()
47      : base() {
48      InitializeComponent();
49    }
50
51    protected override void RegisterContentEvents() {
52      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
53      base.RegisterContentEvents();
54    }
55    protected override void DeregisterContentEvents() {
56      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
57      base.DeregisterContentEvents();
58    }
59
60    protected override void OnContentChanged() {
61      Caption = "Checked Item List";
62      base.OnContentChanged();
63    }
64
65    protected override ListViewItem CreateListViewItem(T item) {
66      ListViewItem listViewItem = base.CreateListViewItem(item);
67      listViewItem.Checked = Content.ItemChecked(item);
68      return listViewItem;
69    }
70
71    #region ListView Events
72    protected virtual void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
73      var checkedItem = (T)e.Item.Tag;
74      if (Content.ItemChecked(checkedItem) != e.Item.Checked) {
75        Content.SetItemCheckedState(checkedItem, e.Item.Checked);
76      }
77    }
78    #endregion
79
80    #region Content Events
81    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
82      if (InvokeRequired)
83        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
84      else {
85        foreach (var item in e.Items) {
86          if (itemsListView.Items[item.Index].Checked != Content.ItemChecked(item.Value))
87            itemsListView.Items[item.Index].Checked = Content.ItemChecked(item.Value);
88        }
89      }
90    }
91    #endregion
92  }
93}
Note: See TracBrowser for help on using the repository browser.