[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2655] | 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 |
|
---|
| 22 | using System;
|
---|
[5744] | 23 | using System.Collections;
|
---|
[2655] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Drawing;
|
---|
[5302] | 26 | using System.Linq;
|
---|
[12078] | 27 | using System.Text;
|
---|
[2655] | 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Collections;
|
---|
[6472] | 30 | using HeuristicLab.Common;
|
---|
[2655] | 31 | using HeuristicLab.MainForm;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Core.Views {
|
---|
[2917] | 34 | [View("ItemList View")]
|
---|
[2727] | 35 | [Content(typeof(ItemList<>), true)]
|
---|
[3393] | 36 | [Content(typeof(IItemList<>), false)]
|
---|
[12618] | 37 | [Content(typeof(ReadOnlyItemList<>), true)]
|
---|
[3483] | 38 | public partial class ItemListView<T> : ItemView where T : class, IItem {
|
---|
[5237] | 39 | protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
|
---|
[3407] | 40 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
[5744] | 41 | protected bool validDragOperation;
|
---|
[3407] | 42 |
|
---|
[3393] | 43 | public new IItemList<T> Content {
|
---|
| 44 | get { return (IItemList<T>)base.Content; }
|
---|
[2713] | 45 | set { base.Content = value; }
|
---|
[2655] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public ListView ItemsListView {
|
---|
| 49 | get { return itemsListView; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public ItemListView() {
|
---|
| 53 | InitializeComponent();
|
---|
[5237] | 54 | itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
|
---|
[2655] | 55 | }
|
---|
| 56 |
|
---|
[5237] | 57 | protected override void Dispose(bool disposing) {
|
---|
| 58 | if (disposing) {
|
---|
| 59 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
| 60 | if (components != null) components.Dispose();
|
---|
| 61 | }
|
---|
| 62 | base.Dispose(disposing);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[2713] | 65 | protected override void DeregisterContentEvents() {
|
---|
| 66 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
|
---|
| 67 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
|
---|
| 68 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
| 69 | Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
| 70 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
[5237] | 71 | foreach (T item in itemListViewItemMapping.Keys) {
|
---|
| 72 | DeregisterItemEvents(item);
|
---|
[5070] | 73 | }
|
---|
[2713] | 74 | base.DeregisterContentEvents();
|
---|
[2655] | 75 | }
|
---|
[2713] | 76 | protected override void RegisterContentEvents() {
|
---|
| 77 | base.RegisterContentEvents();
|
---|
| 78 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
|
---|
| 79 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
|
---|
| 80 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
| 81 | Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
| 82 | Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
[2655] | 83 | }
|
---|
[5237] | 84 | protected virtual void DeregisterItemEvents(T item) {
|
---|
| 85 | item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
| 86 | item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
| 87 | }
|
---|
| 88 | protected virtual void RegisterItemEvents(T item) {
|
---|
| 89 | item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
| 90 | item.ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
| 91 | }
|
---|
[2655] | 92 |
|
---|
[2713] | 93 | protected override void OnContentChanged() {
|
---|
| 94 | base.OnContentChanged();
|
---|
[3775] | 95 |
|
---|
| 96 | int selectedIndex = -1;
|
---|
| 97 | if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
|
---|
| 98 |
|
---|
[5237] | 99 | itemsListView.Items.Clear();
|
---|
| 100 | itemListViewItemMapping.Clear();
|
---|
| 101 | RebuildImageList();
|
---|
[2713] | 102 | viewHost.Content = null;
|
---|
| 103 | if (Content != null) {
|
---|
| 104 | foreach (T item in Content)
|
---|
[2655] | 105 | AddListViewItem(CreateListViewItem(item));
|
---|
[4240] | 106 | AdjustListViewColumnSizes();
|
---|
[3775] | 107 | if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
|
---|
| 108 | itemsListView.Items[selectedIndex].Selected = true;
|
---|
[2655] | 109 | }
|
---|
[3362] | 110 | SetEnabledStateOfControls();
|
---|
[2655] | 111 | }
|
---|
| 112 |
|
---|
[3904] | 113 | protected override void SetEnabledStateOfControls() {
|
---|
| 114 | base.SetEnabledStateOfControls();
|
---|
[3362] | 115 | if (Content == null) {
|
---|
| 116 | addButton.Enabled = false;
|
---|
| 117 | moveUpButton.Enabled = false;
|
---|
| 118 | moveDownButton.Enabled = false;
|
---|
| 119 | removeButton.Enabled = false;
|
---|
| 120 | itemsListView.Enabled = false;
|
---|
| 121 | detailsGroupBox.Enabled = false;
|
---|
| 122 | } else {
|
---|
[3435] | 123 | addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
|
---|
[3362] | 124 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 125 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
[3435] | 126 | !Content.IsReadOnly && !ReadOnly;
|
---|
[3362] | 127 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 128 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
[3435] | 129 | !Content.IsReadOnly && !ReadOnly;
|
---|
[3362] | 130 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
[3435] | 131 | !Content.IsReadOnly && !ReadOnly;
|
---|
[3362] | 132 | itemsListView.Enabled = true;
|
---|
[4099] | 133 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
[3362] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[2655] | 137 | protected virtual T CreateItem() {
|
---|
[3407] | 138 | if (typeSelectorDialog == null) {
|
---|
| 139 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
| 140 | typeSelectorDialog.Caption = "Select Item";
|
---|
| 141 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
[3588] | 142 | typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
|
---|
[2655] | 143 | }
|
---|
[3407] | 144 |
|
---|
| 145 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 146 | try {
|
---|
| 147 | return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
[12078] | 148 | } catch (Exception ex) {
|
---|
[13338] | 149 | MainFormManager.MainForm.ShowError(ex.Message, ex);
|
---|
[3407] | 150 | }
|
---|
[2655] | 151 | }
|
---|
[3407] | 152 | return null;
|
---|
[2655] | 153 | }
|
---|
| 154 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
| 155 | ListViewItem listViewItem = new ListViewItem();
|
---|
[5237] | 156 | if (item == null) {
|
---|
| 157 | listViewItem.Text = "null";
|
---|
[5287] | 158 | itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
|
---|
[5237] | 159 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 160 | } else {
|
---|
| 161 | listViewItem.Text = item.ToString();
|
---|
| 162 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
| 163 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
| 164 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 165 | listViewItem.Tag = item;
|
---|
| 166 | }
|
---|
[2655] | 167 | return listViewItem;
|
---|
| 168 | }
|
---|
| 169 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
[5371] | 170 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 171 | T item = (listViewItem.Tag as T);
|
---|
[2655] | 172 | itemsListView.Items.Add(listViewItem);
|
---|
[5237] | 173 | if (item != null) {
|
---|
| 174 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
| 175 | RegisterItemEvents(item);
|
---|
| 176 | itemListViewItemMapping.Add(item, new List<ListViewItem>());
|
---|
| 177 | }
|
---|
| 178 | itemListViewItemMapping[item].Add(listViewItem);
|
---|
| 179 | }
|
---|
[2655] | 180 | }
|
---|
| 181 | protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
|
---|
[5371] | 182 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 183 | T item = (listViewItem.Tag as T);
|
---|
[2655] | 184 | itemsListView.Items.Insert(index, listViewItem);
|
---|
[5237] | 185 | if (item != null) {
|
---|
| 186 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
| 187 | RegisterItemEvents(item);
|
---|
| 188 | itemListViewItemMapping.Add(item, new List<ListViewItem>());
|
---|
| 189 | }
|
---|
| 190 | itemListViewItemMapping[item].Add(listViewItem);
|
---|
| 191 | }
|
---|
[2655] | 192 | }
|
---|
| 193 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
[5371] | 194 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 195 | T item = (listViewItem.Tag as T);
|
---|
| 196 | if (item != null) {
|
---|
| 197 | itemListViewItemMapping[item].Remove(listViewItem);
|
---|
| 198 | if (itemListViewItemMapping[item].Count == 0) {
|
---|
| 199 | itemListViewItemMapping.Remove(item);
|
---|
| 200 | DeregisterItemEvents(item);
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
[2655] | 203 | listViewItem.Remove();
|
---|
| 204 | }
|
---|
[3327] | 205 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
[5371] | 206 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 207 | T item = listViewItem.Tag as T;
|
---|
[3327] | 208 | int i = listViewItem.ImageIndex;
|
---|
[5839] | 209 | itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
|
---|
[3327] | 210 | listViewItem.ImageIndex = -1;
|
---|
| 211 | listViewItem.ImageIndex = i;
|
---|
[2655] | 212 | }
|
---|
[3327] | 213 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
[5371] | 214 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 215 | T item = listViewItem.Tag as T;
|
---|
| 216 | listViewItem.Text = item == null ? "null" : item.ToString();
|
---|
| 217 | listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
|
---|
[3327] | 218 | }
|
---|
[5237] | 219 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
[5238] | 220 | if (item == null) {
|
---|
| 221 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 222 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 223 | if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
|
---|
| 224 | }
|
---|
| 225 | return listViewItems;
|
---|
| 226 | } else {
|
---|
[5302] | 227 | List<ListViewItem> listViewItems = null;
|
---|
| 228 | itemListViewItemMapping.TryGetValue(item, out listViewItems);
|
---|
| 229 | return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
|
---|
[5238] | 230 | }
|
---|
[5237] | 231 | }
|
---|
[2655] | 232 |
|
---|
[3362] | 233 | #region ListView Events
|
---|
[2655] | 234 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 235 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 236 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
[3456] | 237 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[2655] | 238 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 239 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
[3456] | 240 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[2655] | 241 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
[3456] | 242 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[3829] | 243 | AdjustListViewColumnSizes();
|
---|
[2655] | 244 |
|
---|
[4069] | 245 | if (showDetailsCheckBox.Checked) {
|
---|
| 246 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 247 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
| 248 | detailsGroupBox.Enabled = true;
|
---|
| 249 | viewHost.Content = item;
|
---|
| 250 | } else {
|
---|
| 251 | viewHost.Content = null;
|
---|
| 252 | detailsGroupBox.Enabled = false;
|
---|
| 253 | }
|
---|
[2655] | 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 258 | if (e.KeyCode == Keys.Delete) {
|
---|
[3435] | 259 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
[2655] | 260 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 261 | Content.RemoveAt(item.Index);
|
---|
[2655] | 262 | }
|
---|
[12125] | 263 | } else if (e.KeyData == (Keys.Control | Keys.C)) {
|
---|
[12078] | 264 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 265 | var builder = new StringBuilder();
|
---|
| 266 | foreach (ListViewItem selected in itemsListView.SelectedItems) {
|
---|
| 267 | builder.AppendLine(selected.Text);
|
---|
| 268 | }
|
---|
| 269 | Clipboard.SetText(builder.ToString());
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[2655] | 274 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 275 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
[5237] | 276 | T item = itemsListView.SelectedItems[0].Tag as T;
|
---|
| 277 | if (item != null) {
|
---|
[13244] | 278 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
| 279 | if (view != null) {
|
---|
| 280 | view.ReadOnly = ReadOnly;
|
---|
| 281 | view.Locked = Locked;
|
---|
[5237] | 282 | }
|
---|
[3416] | 283 | }
|
---|
[2655] | 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[3432] | 288 | if (!Locked) {
|
---|
[5744] | 289 | List<T> items = new List<T>();
|
---|
| 290 | foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
|
---|
| 291 | T item = listViewItem.Tag as T;
|
---|
| 292 | if (item != null) items.Add(item);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | if (items.Count > 0) {
|
---|
[5237] | 296 | DataObject data = new DataObject();
|
---|
[5837] | 297 | if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
|
---|
| 298 | else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
|
---|
[5237] | 299 | if (Content.IsReadOnly || ReadOnly) {
|
---|
| 300 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 301 | } else {
|
---|
| 302 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
[5744] | 303 | if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
|
---|
| 304 | foreach (ListViewItem listViewItem in itemsListView.SelectedItems.Cast<ListViewItem>().ToArray()) {
|
---|
| 305 | if (listViewItem.Tag != null) Content.RemoveAt(listViewItem.Index);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
[5237] | 308 | }
|
---|
[3432] | 309 | }
|
---|
[2655] | 310 | }
|
---|
| 311 | }
|
---|
[5744] | 312 | protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 313 | validDragOperation = false;
|
---|
[5837] | 314 | if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
|
---|
[5744] | 315 | validDragOperation = true;
|
---|
[5837] | 316 | } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
|
---|
[5744] | 317 | validDragOperation = true;
|
---|
[5837] | 318 | IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 319 | foreach (object item in items)
|
---|
| 320 | validDragOperation = validDragOperation && (item is T);
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
|
---|
[2655] | 324 | e.Effect = DragDropEffects.None;
|
---|
[5744] | 325 | if (validDragOperation) {
|
---|
[3694] | 326 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[2694] | 327 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
[5744] | 328 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 329 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 330 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
[2655] | 331 | }
|
---|
| 332 | }
|
---|
| 333 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 334 | if (e.Effect != DragDropEffects.None) {
|
---|
| 335 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 336 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
[5744] | 337 |
|
---|
[5837] | 338 | if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
|
---|
| 339 | T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 340 | if (listViewItem != null) Content.Insert(listViewItem.Index, e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
| 341 | else Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
[5837] | 342 | } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
|
---|
| 343 | IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
|
---|
[6472] | 344 | if (e.Effect.HasFlag(DragDropEffects.Copy)) {
|
---|
| 345 | Cloner cloner = new Cloner();
|
---|
[6527] | 346 | items = items.Select(x => cloner.Clone(x));
|
---|
[6472] | 347 | }
|
---|
[5744] | 348 | foreach (T item in items) {
|
---|
[6472] | 349 | if (listViewItem != null) Content.Insert(listViewItem.Index, item);
|
---|
| 350 | else Content.Add(item);
|
---|
[5744] | 351 | }
|
---|
| 352 | }
|
---|
[2655] | 353 | }
|
---|
| 354 | }
|
---|
| 355 | #endregion
|
---|
| 356 |
|
---|
| 357 | #region Button Events
|
---|
| 358 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
| 359 | T item = CreateItem();
|
---|
| 360 | if (item != null)
|
---|
[2713] | 361 | Content.Add(item);
|
---|
[2655] | 362 | }
|
---|
| 363 | protected virtual void moveUpButton_Click(object sender, EventArgs e) {
|
---|
| 364 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 365 | int index = itemsListView.SelectedIndices[0];
|
---|
[5928] | 366 | Content.Reverse(index - 1, 2);
|
---|
[2655] | 367 | itemsListView.Items[index].Selected = false;
|
---|
| 368 | itemsListView.Items[index - 1].Selected = true;
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | protected virtual void moveDownButton_Click(object sender, EventArgs e) {
|
---|
| 372 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 373 | int index = itemsListView.SelectedIndices[0];
|
---|
[5928] | 374 | Content.Reverse(index, 2);
|
---|
[2655] | 375 | itemsListView.Items[index].Selected = false;
|
---|
| 376 | itemsListView.Items[index + 1].Selected = true;
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
| 380 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 381 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 382 | Content.RemoveAt(item.Index);
|
---|
[2655] | 383 | }
|
---|
| 384 | }
|
---|
| 385 | #endregion
|
---|
| 386 |
|
---|
[4069] | 387 | #region CheckBox Events
|
---|
[4096] | 388 | protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
[4069] | 389 | if (showDetailsCheckBox.Checked) {
|
---|
| 390 | splitContainer.Panel2Collapsed = false;
|
---|
| 391 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
| 392 | viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
|
---|
| 393 | } else {
|
---|
| 394 | splitContainer.Panel2Collapsed = true;
|
---|
[4096] | 395 | viewHost.Content = null;
|
---|
[4069] | 396 | }
|
---|
| 397 | }
|
---|
| 398 | #endregion
|
---|
| 399 |
|
---|
[2713] | 400 | #region Content Events
|
---|
| 401 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 402 | if (InvokeRequired)
|
---|
[2713] | 403 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
|
---|
[4240] | 404 | else {
|
---|
[2655] | 405 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 406 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 407 | AdjustListViewColumnSizes();
|
---|
| 408 | }
|
---|
[2655] | 409 | }
|
---|
[2713] | 410 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 411 | if (InvokeRequired)
|
---|
[2713] | 412 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
|
---|
[2655] | 413 | else {
|
---|
| 414 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 415 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 416 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 417 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 418 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 419 | RebuildImageList();
|
---|
[2655] | 420 | }
|
---|
| 421 | }
|
---|
[2713] | 422 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 423 | if (InvokeRequired)
|
---|
[2713] | 424 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
|
---|
[2655] | 425 | else {
|
---|
| 426 | int[] selected = new int[itemsListView.SelectedIndices.Count];
|
---|
| 427 | itemsListView.SelectedIndices.CopyTo(selected, 0);
|
---|
| 428 |
|
---|
| 429 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 430 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 431 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 432 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 433 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 434 | RebuildImageList();
|
---|
[2655] | 435 |
|
---|
| 436 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 437 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 438 | AdjustListViewColumnSizes();
|
---|
[2655] | 439 |
|
---|
| 440 | for (int i = 0; i < selected.Length; i++)
|
---|
| 441 | itemsListView.Items[selected[i]].Selected = true;
|
---|
| 442 | }
|
---|
| 443 | }
|
---|
[2713] | 444 | protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 445 | if (InvokeRequired)
|
---|
[2713] | 446 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
|
---|
[2655] | 447 | else {
|
---|
| 448 | foreach (IndexedItem<T> item in e.Items) {
|
---|
| 449 | ListViewItem listViewItem = itemsListView.Items[item.Index];
|
---|
[7161] | 450 | if (listViewItem.Tag != null)
|
---|
| 451 | itemListViewItemMapping[(T)listViewItem.Tag].Remove(listViewItem);
|
---|
[2655] | 452 | listViewItem.Tag = item.Value;
|
---|
[7161] | 453 | if (listViewItem.Tag != null)
|
---|
| 454 | itemListViewItemMapping[item.Value].Add(listViewItem);
|
---|
[3327] | 455 | UpdateListViewItemImage(listViewItem);
|
---|
| 456 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 457 | }
|
---|
| 458 | }
|
---|
| 459 | }
|
---|
[2713] | 460 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 461 | if (InvokeRequired)
|
---|
[2713] | 462 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
|
---|
[2655] | 463 | else {
|
---|
| 464 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 465 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 466 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 467 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 468 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 469 | RebuildImageList();
|
---|
[2655] | 470 |
|
---|
| 471 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 472 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 473 | AdjustListViewColumnSizes();
|
---|
[2655] | 474 | }
|
---|
| 475 | }
|
---|
| 476 | #endregion
|
---|
| 477 |
|
---|
| 478 | #region Item Events
|
---|
[3306] | 479 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 480 | if (InvokeRequired)
|
---|
| 481 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 482 | else {
|
---|
| 483 | T item = (T)sender;
|
---|
[5237] | 484 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 485 | UpdateListViewItemImage(listViewItem);
|
---|
[3306] | 486 | }
|
---|
| 487 | }
|
---|
[2932] | 488 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
[2655] | 489 | if (InvokeRequired)
|
---|
[2932] | 490 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
[2655] | 491 | else {
|
---|
| 492 | T item = (T)sender;
|
---|
[5237] | 493 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 494 | UpdateListViewItemText(listViewItem);
|
---|
[9755] | 495 | AdjustListViewColumnSizes();
|
---|
[2655] | 496 | }
|
---|
| 497 | }
|
---|
| 498 | #endregion
|
---|
[3362] | 499 |
|
---|
| 500 | #region Helpers
|
---|
| 501 | protected virtual void AdjustListViewColumnSizes() {
|
---|
| 502 | if (itemsListView.Items.Count > 0) {
|
---|
| 503 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
| 504 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 505 | }
|
---|
| 506 | }
|
---|
[5239] | 507 | protected virtual void RebuildImageList() {
|
---|
[5237] | 508 | itemsListView.SmallImageList.Images.Clear();
|
---|
[5239] | 509 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 510 | T item = listViewItem.Tag as T;
|
---|
[5287] | 511 | itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
|
---|
[5239] | 512 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
[5237] | 513 | }
|
---|
| 514 | }
|
---|
[3362] | 515 | #endregion
|
---|
[2655] | 516 | }
|
---|
| 517 | }
|
---|