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