Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7195 was 7195, checked in by mkommend, 12 years ago

#1713: Implemented drag & drop operations for the checked state.

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