[3595] | 1 | #region License Information
|
---|
[3565] | 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3565] | 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 |
|
---|
[9026] | 22 | using System;
|
---|
[9857] | 23 | using System.Collections.Generic;
|
---|
[9026] | 24 | using System.Drawing;
|
---|
[7195] | 25 | using System.Linq;
|
---|
[3565] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.Collections;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Core.Views {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// The visual representation of a list of checked items.
|
---|
| 33 | /// </summary>
|
---|
| 34 | [View("CheckedItemList View")]
|
---|
| 35 | [Content(typeof(CheckedItemList<>), true)]
|
---|
[3628] | 36 | [Content(typeof(ICheckedItemList<>), true)]
|
---|
[3595] | 37 | public partial class CheckedItemListView<T> : ItemListView<T> where T : class, IItem {
|
---|
[3565] | 38 | public new ICheckedItemList<T> Content {
|
---|
| 39 | get { return (ICheckedItemList<T>)base.Content; }
|
---|
| 40 | set { base.Content = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[3595] | 43 | public CheckedItemListView()
|
---|
| 44 | : base() {
|
---|
| 45 | InitializeComponent();
|
---|
[3565] | 46 | }
|
---|
| 47 |
|
---|
[3595] | 48 | protected override void RegisterContentEvents() {
|
---|
| 49 | Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
|
---|
| 50 | base.RegisterContentEvents();
|
---|
[3565] | 51 | }
|
---|
| 52 | protected override void DeregisterContentEvents() {
|
---|
| 53 | Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged);
|
---|
| 54 | base.DeregisterContentEvents();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[9026] | 57 | protected override void OnContentChanged() {
|
---|
| 58 | base.OnContentChanged();
|
---|
| 59 | if (Content != null) {
|
---|
| 60 | SetNumberOfCheckItems();
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[6233] | 64 | private Color backupColor = Color.Empty;
|
---|
[3904] | 65 | protected override void SetEnabledStateOfControls() {
|
---|
[7195] | 66 | if (backupColor == Color.Empty) backupColor = base.itemsListView.BackColor;
|
---|
[3904] | 67 | base.SetEnabledStateOfControls();
|
---|
[6233] | 68 | if (ReadOnly || Locked)
|
---|
| 69 | base.itemsListView.BackColor = ListView.DefaultBackColor;
|
---|
| 70 | else
|
---|
| 71 | base.itemsListView.BackColor = backupColor;
|
---|
[3565] | 72 | }
|
---|
| 73 |
|
---|
[3595] | 74 | protected override ListViewItem CreateListViewItem(T item) {
|
---|
| 75 | ListViewItem listViewItem = base.CreateListViewItem(item);
|
---|
[3565] | 76 | listViewItem.Checked = Content.ItemChecked(item);
|
---|
| 77 | return listViewItem;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | #region ListView Events
|
---|
[3788] | 81 | private bool doubleClick;
|
---|
[3738] | 82 | protected virtual void itemsListView_ItemCheck(object sender, ItemCheckEventArgs e) {
|
---|
[3788] | 83 | if (doubleClick) {
|
---|
| 84 | e.NewValue = e.CurrentValue;
|
---|
| 85 | doubleClick = false;
|
---|
| 86 | } else {
|
---|
| 87 | var checkedItem = (T)itemsListView.Items[e.Index].Tag;
|
---|
| 88 | bool check = e.NewValue == CheckState.Checked;
|
---|
| 89 | if (Content.ItemChecked(checkedItem) != check) {
|
---|
[6233] | 90 | if (!ReadOnly && !Locked) Content.SetItemCheckedState(checkedItem, check);
|
---|
| 91 | else e.NewValue = e.CurrentValue;
|
---|
[3788] | 92 | }
|
---|
[3565] | 93 | }
|
---|
| 94 | }
|
---|
[3788] | 95 |
|
---|
| 96 | protected void itemsListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
|
---|
| 97 | if (e.Clicks > 1)
|
---|
| 98 | doubleClick = true;
|
---|
| 99 | }
|
---|
[7195] | 100 |
|
---|
| 101 | protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 102 | validDragOperation = false;
|
---|
| 103 | if (Locked || ReadOnly) return;
|
---|
| 104 |
|
---|
| 105 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
|
---|
| 106 | if (data != null)
|
---|
| 107 | validDragOperation = Content.Select(x => x.ToString()).SequenceEqual(data.Select(x => x.ToString()));
|
---|
| 108 | else
|
---|
| 109 | base.itemsListView_DragEnter(sender, e);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | protected override void itemsListView_DragOver(object sender, DragEventArgs e) {
|
---|
| 113 | e.Effect = DragDropEffects.None;
|
---|
| 114 | if (!validDragOperation) return;
|
---|
| 115 |
|
---|
| 116 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
|
---|
| 117 | if (data != null)
|
---|
| 118 | e.Effect = DragDropEffects.Copy;
|
---|
| 119 | else
|
---|
| 120 | base.itemsListView_DragOver(sender, e);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 124 | if (e.Effect == DragDropEffects.None) return;
|
---|
| 125 | var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as ICheckedItemList<T>;
|
---|
| 126 | if (data != null) {
|
---|
| 127 | for (int i = 0; i < Content.Count; i++) {
|
---|
| 128 | Content.SetItemCheckedState(Content[i], data.ItemChecked(data[i]));
|
---|
| 129 | }
|
---|
| 130 | } else
|
---|
| 131 | base.itemsListView_DragDrop(sender, e);
|
---|
| 132 | }
|
---|
[3565] | 133 | #endregion
|
---|
| 134 |
|
---|
| 135 | #region Content Events
|
---|
| 136 | protected virtual void Content_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 137 | if (InvokeRequired)
|
---|
| 138 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CheckedItemsChanged), sender, e);
|
---|
| 139 | else {
|
---|
[9857] | 140 | UpdateCheckedItemState(e.Items);
|
---|
[9026] | 141 | SetNumberOfCheckItems();
|
---|
[3565] | 142 | }
|
---|
| 143 | }
|
---|
[9134] | 144 | protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 145 | base.Content_CollectionReset(sender, e);
|
---|
| 146 | SetNumberOfCheckItems();
|
---|
| 147 | }
|
---|
| 148 | protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 149 | base.Content_ItemsAdded(sender, e);
|
---|
| 150 | SetNumberOfCheckItems();
|
---|
| 151 | }
|
---|
| 152 | protected override void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 153 | base.Content_ItemsMoved(sender, e);
|
---|
[9857] | 154 | UpdateCheckedItemState(e.Items);
|
---|
[9134] | 155 | SetNumberOfCheckItems();
|
---|
| 156 | }
|
---|
| 157 | protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 158 | base.Content_ItemsRemoved(sender, e);
|
---|
| 159 | SetNumberOfCheckItems();
|
---|
| 160 | }
|
---|
| 161 | protected override void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 162 | base.Content_ItemsReplaced(sender, e);
|
---|
| 163 | SetNumberOfCheckItems();
|
---|
| 164 | }
|
---|
[3565] | 165 | #endregion
|
---|
[9026] | 166 |
|
---|
| 167 | private void SetNumberOfCheckItems() {
|
---|
[9134] | 168 | if (InvokeRequired) {
|
---|
| 169 | Invoke((Action)SetNumberOfCheckItems);
|
---|
| 170 | } else {
|
---|
| 171 | this.itemsGroupBox.Text = String.Format("Items (Checked: {0}/{1})", Content.CheckedItems.Count(), Content.Count);
|
---|
| 172 | }
|
---|
[9026] | 173 | }
|
---|
[9857] | 174 |
|
---|
| 175 | private void UpdateCheckedItemState(IEnumerable<IndexedItem<T>> items) {
|
---|
| 176 | foreach (var item in items) {
|
---|
| 177 | var isChecked = Content.ItemChecked(item.Value);
|
---|
| 178 | if (itemsListView.Items[item.Index].Checked != isChecked)
|
---|
| 179 | itemsListView.Items[item.Index].Checked = isChecked;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[3565] | 182 | }
|
---|
| 183 | }
|
---|