Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Core.Views/3.3/CheckedItemListView.cs @ 15906

Last change on this file since 15906 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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