Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8793 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.3 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.Linq;
23using System.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Core.Views {
29  /// <summary>
30  /// The visual representation of a list of checked items.
31  /// </summary>
32  [View("CheckedItemList View")]
33  [Content(typeof(CheckedItemList<>), true)]
34  [Content(typeof(ICheckedItemList<>), true)]
35  public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
36    public new ICheckedItemList<T> Content {
37      get { return (ICheckedItemList<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public CheckedItemListView()
42      : base() {
43      InitializeComponent();
44    }
45
46    protected override void RegisterContentEvents() {
47      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
48      base.RegisterContentEvents();
49    }
50    protected override void DeregisterContentEvents() {
51      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
52      base.DeregisterContentEvents();
53    }
54
55    private Color backupColor = Color.Empty;
56    protected override void SetEnabledStateOfControls() {
57      if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
58      base.SetEnabledStateOfControls();
59      if (ReadOnly || Locked)
60        base.itemsListView.BackColor = ListView.DefaultBackColor;
61      else
62        base.itemsListView.BackColor = backupColor;
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    private bool doubleClick;
73    protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
74      if (doubleClick) {
75        e.NewValue = e.CurrentValue;
76        doubleClick = false;
77      } else {
78        var checkedItem = (T)itemsListView.Items[e.Index].Tag;
79        bool check = e.NewValue == CheckState.Checked;
80        if (Content.ItemChecked(checkedItem) != check) {
81          if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
82          else e.NewValue = e.CurrentValue;
83        }
84      }
85    }
86
87    protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
88      if (e.Clicks > 1)
89        doubleClick = true;
90    }
91
92    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
93      validDragOperation = false;
94      if (Locked || ReadOnly) return;
95
96      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
97      if (data != null)
98        validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
99      else
100        base.itemsListView_DragEnter(sender, e);
101    }
102
103    protected override void itemsListView_DragOver(object sender, DragEventArgs e) {
104      e.Effect = DragDropEffects.None;
105      if (!validDragOperation) return;
106
107      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
108      if (data != null)
109        e.Effect = DragDropEffects.Copy;
110      else
111        base.itemsListView_DragOver(sender, e);
112    }
113
114    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
115      if (e.Effect == DragDropEffects.None) return;
116      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
117      if (data != null) {
118        for (int i = 0; i < Content.Count; i++) {
119          Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
120        }
121      } else
122        base.itemsListView_DragDrop(sender, e);
123    }
124    #endregion
125
126    #region Content Events
127    protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
128      if (InvokeRequired)
129        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
130      else {
131        foreach (var item in e.Items) {
132          if (itemsListView.Items[item.Index].Checked != Content.ItemChecked(item.Value))
133            itemsListView.Items[item.Index].Checked = Content.ItemChecked(item.Value);
134        }
135      }
136    }
137    #endregion
138  }
139}
Note: See TracBrowser for help on using the repository browser.