[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 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;
|
---|
[2655] | 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Collections;
|
---|
[6472] | 29 | using HeuristicLab.Common;
|
---|
[2655] | 30 | using HeuristicLab.MainForm;
|
---|
[2713] | 31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[3758] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
[2655] | 33 |
|
---|
| 34 | namespace HeuristicLab.Core.Views {
|
---|
[2917] | 35 | [View("ItemList View")]
|
---|
[2727] | 36 | [Content(typeof(ItemList<>), true)]
|
---|
[3393] | 37 | [Content(typeof(IItemList<>), false)]
|
---|
[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();
|
---|
[5302] | 148 | }
|
---|
| 149 | catch (Exception ex) {
|
---|
[3758] | 150 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 151 | }
|
---|
[2655] | 152 | }
|
---|
[3407] | 153 | return null;
|
---|
[2655] | 154 | }
|
---|
| 155 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
| 156 | ListViewItem listViewItem = new ListViewItem();
|
---|
[5237] | 157 | if (item == null) {
|
---|
| 158 | listViewItem.Text = "null";
|
---|
[5287] | 159 | itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
|
---|
[5237] | 160 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 161 | } else {
|
---|
| 162 | listViewItem.Text = item.ToString();
|
---|
| 163 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
| 164 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
| 165 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
| 166 | listViewItem.Tag = item;
|
---|
| 167 | }
|
---|
[2655] | 168 | return listViewItem;
|
---|
| 169 | }
|
---|
| 170 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
[5371] | 171 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 172 | T item = (listViewItem.Tag as T);
|
---|
[2655] | 173 | itemsListView.Items.Add(listViewItem);
|
---|
[5237] | 174 | if (item != null) {
|
---|
| 175 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
| 176 | RegisterItemEvents(item);
|
---|
| 177 | itemListViewItemMapping.Add(item, new List<ListViewItem>());
|
---|
| 178 | }
|
---|
| 179 | itemListViewItemMapping[item].Add(listViewItem);
|
---|
| 180 | }
|
---|
[2655] | 181 | }
|
---|
| 182 | protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
|
---|
[5371] | 183 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 184 | T item = (listViewItem.Tag as T);
|
---|
[2655] | 185 | itemsListView.Items.Insert(index, listViewItem);
|
---|
[5237] | 186 | if (item != null) {
|
---|
| 187 | if (!itemListViewItemMapping.ContainsKey(item)) {
|
---|
| 188 | RegisterItemEvents(item);
|
---|
| 189 | itemListViewItemMapping.Add(item, new List<ListViewItem>());
|
---|
| 190 | }
|
---|
| 191 | itemListViewItemMapping[item].Add(listViewItem);
|
---|
| 192 | }
|
---|
[2655] | 193 | }
|
---|
| 194 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
[5371] | 195 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 196 | T item = (listViewItem.Tag as T);
|
---|
| 197 | if (item != null) {
|
---|
| 198 | itemListViewItemMapping[item].Remove(listViewItem);
|
---|
| 199 | if (itemListViewItemMapping[item].Count == 0) {
|
---|
| 200 | itemListViewItemMapping.Remove(item);
|
---|
| 201 | DeregisterItemEvents(item);
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
[2655] | 204 | listViewItem.Remove();
|
---|
| 205 | }
|
---|
[3327] | 206 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
[5371] | 207 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 208 | T item = listViewItem.Tag as T;
|
---|
[3327] | 209 | int i = listViewItem.ImageIndex;
|
---|
[5839] | 210 | itemsListView.SmallImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
|
---|
[3327] | 211 | listViewItem.ImageIndex = -1;
|
---|
| 212 | listViewItem.ImageIndex = i;
|
---|
[2655] | 213 | }
|
---|
[3327] | 214 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
[5371] | 215 | if (listViewItem == null) throw new ArgumentNullException();
|
---|
[5237] | 216 | T item = listViewItem.Tag as T;
|
---|
| 217 | listViewItem.Text = item == null ? "null" : item.ToString();
|
---|
| 218 | listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
|
---|
[3327] | 219 | }
|
---|
[5237] | 220 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
[5238] | 221 | if (item == null) {
|
---|
| 222 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 223 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 224 | if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
|
---|
| 225 | }
|
---|
| 226 | return listViewItems;
|
---|
| 227 | } else {
|
---|
[5302] | 228 | List<ListViewItem> listViewItems = null;
|
---|
| 229 | itemListViewItemMapping.TryGetValue(item, out listViewItems);
|
---|
| 230 | return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
|
---|
[5238] | 231 | }
|
---|
[5237] | 232 | }
|
---|
[2655] | 233 |
|
---|
[3362] | 234 | #region ListView Events
|
---|
[2655] | 235 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 236 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 237 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
[3456] | 238 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[2655] | 239 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 240 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
[3456] | 241 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[2655] | 242 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
|
---|
[3456] | 243 | (Content != null) && !Content.IsReadOnly && !ReadOnly;
|
---|
[3829] | 244 | AdjustListViewColumnSizes();
|
---|
[2655] | 245 |
|
---|
[4069] | 246 | if (showDetailsCheckBox.Checked) {
|
---|
| 247 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 248 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
| 249 | detailsGroupBox.Enabled = true;
|
---|
| 250 | viewHost.Content = item;
|
---|
| 251 | } else {
|
---|
| 252 | viewHost.Content = null;
|
---|
| 253 | detailsGroupBox.Enabled = false;
|
---|
| 254 | }
|
---|
[2655] | 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 259 | if (e.KeyCode == Keys.Delete) {
|
---|
[3435] | 260 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
[2655] | 261 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 262 | Content.RemoveAt(item.Index);
|
---|
[2655] | 263 | }
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 268 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
[5237] | 269 | T item = itemsListView.SelectedItems[0].Tag as T;
|
---|
| 270 | if (item != null) {
|
---|
| 271 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
| 272 | if (view != null) {
|
---|
| 273 | view.ReadOnly = ReadOnly;
|
---|
| 274 | view.Locked = Locked;
|
---|
| 275 | }
|
---|
[3416] | 276 | }
|
---|
[2655] | 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[3432] | 281 | if (!Locked) {
|
---|
[5744] | 282 | List<T> items = new List<T>();
|
---|
| 283 | foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
|
---|
| 284 | T item = listViewItem.Tag as T;
|
---|
| 285 | if (item != null) items.Add(item);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | if (items.Count > 0) {
|
---|
[5237] | 289 | DataObject data = new DataObject();
|
---|
[5837] | 290 | if (items.Count == 1) data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items[0]);
|
---|
| 291 | else data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, items);
|
---|
[5237] | 292 | if (Content.IsReadOnly || ReadOnly) {
|
---|
| 293 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 294 | } else {
|
---|
| 295 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
[5744] | 296 | if ((result & DragDropEffects.Move) == DragDropEffects.Move) {
|
---|
| 297 | foreach (ListViewItem listViewItem in itemsListView.SelectedItems.Cast<ListViewItem>().ToArray()) {
|
---|
| 298 | if (listViewItem.Tag != null) Content.RemoveAt(listViewItem.Index);
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
[5237] | 301 | }
|
---|
[3432] | 302 | }
|
---|
[2655] | 303 | }
|
---|
| 304 | }
|
---|
[5744] | 305 | protected virtual void itemsListView_DragEnter(object sender, DragEventArgs e) {
|
---|
| 306 | validDragOperation = false;
|
---|
[5837] | 307 | if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T)) {
|
---|
[5744] | 308 | validDragOperation = true;
|
---|
[5837] | 309 | } else if (!Content.IsReadOnly && !ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable)) {
|
---|
[5744] | 310 | validDragOperation = true;
|
---|
[5837] | 311 | IEnumerable items = (IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 312 | foreach (object item in items)
|
---|
| 313 | validDragOperation = validDragOperation && (item is T);
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | protected virtual void itemsListView_DragOver(object sender, DragEventArgs e) {
|
---|
[2655] | 317 | e.Effect = DragDropEffects.None;
|
---|
[5744] | 318 | if (validDragOperation) {
|
---|
[3694] | 319 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[2694] | 320 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
[5744] | 321 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 322 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 323 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
[2655] | 324 | }
|
---|
| 325 | }
|
---|
| 326 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 327 | if (e.Effect != DragDropEffects.None) {
|
---|
| 328 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 329 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
[5744] | 330 |
|
---|
[5837] | 331 | if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is T) {
|
---|
| 332 | T item = (T)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
|
---|
[5744] | 333 | if (listViewItem != null) Content.Insert(listViewItem.Index, e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
| 334 | else Content.Add(e.Effect.HasFlag(DragDropEffects.Copy) ? (T)item.Clone() : item);
|
---|
[5837] | 335 | } else if (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IEnumerable) {
|
---|
| 336 | IEnumerable<T> items = ((IEnumerable)e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat)).Cast<T>();
|
---|
[6472] | 337 | if (e.Effect.HasFlag(DragDropEffects.Copy)) {
|
---|
| 338 | Cloner cloner = new Cloner();
|
---|
[6527] | 339 | items = items.Select(x => cloner.Clone(x));
|
---|
[6472] | 340 | }
|
---|
[5744] | 341 | foreach (T item in items) {
|
---|
[6472] | 342 | if (listViewItem != null) Content.Insert(listViewItem.Index, item);
|
---|
| 343 | else Content.Add(item);
|
---|
[5744] | 344 | }
|
---|
| 345 | }
|
---|
[2655] | 346 | }
|
---|
| 347 | }
|
---|
| 348 | #endregion
|
---|
| 349 |
|
---|
| 350 | #region Button Events
|
---|
| 351 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
| 352 | T item = CreateItem();
|
---|
| 353 | if (item != null)
|
---|
[2713] | 354 | Content.Add(item);
|
---|
[2655] | 355 | }
|
---|
| 356 | protected virtual void moveUpButton_Click(object sender, EventArgs e) {
|
---|
| 357 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 358 | int index = itemsListView.SelectedIndices[0];
|
---|
[5928] | 359 | Content.Reverse(index - 1, 2);
|
---|
[2655] | 360 | itemsListView.Items[index].Selected = false;
|
---|
| 361 | itemsListView.Items[index - 1].Selected = true;
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 | protected virtual void moveDownButton_Click(object sender, EventArgs e) {
|
---|
| 365 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 366 | int index = itemsListView.SelectedIndices[0];
|
---|
[5928] | 367 | Content.Reverse(index, 2);
|
---|
[2655] | 368 | itemsListView.Items[index].Selected = false;
|
---|
| 369 | itemsListView.Items[index + 1].Selected = true;
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
| 373 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 374 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 375 | Content.RemoveAt(item.Index);
|
---|
[2655] | 376 | }
|
---|
| 377 | }
|
---|
| 378 | #endregion
|
---|
| 379 |
|
---|
[4069] | 380 | #region CheckBox Events
|
---|
[4096] | 381 | protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
[4069] | 382 | if (showDetailsCheckBox.Checked) {
|
---|
| 383 | splitContainer.Panel2Collapsed = false;
|
---|
| 384 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
| 385 | viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
|
---|
| 386 | } else {
|
---|
| 387 | splitContainer.Panel2Collapsed = true;
|
---|
[4096] | 388 | viewHost.Content = null;
|
---|
[4069] | 389 | }
|
---|
| 390 | }
|
---|
| 391 | #endregion
|
---|
| 392 |
|
---|
[2713] | 393 | #region Content Events
|
---|
| 394 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 395 | if (InvokeRequired)
|
---|
[2713] | 396 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
|
---|
[4240] | 397 | else {
|
---|
[2655] | 398 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 399 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 400 | AdjustListViewColumnSizes();
|
---|
| 401 | }
|
---|
[2655] | 402 | }
|
---|
[2713] | 403 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 404 | if (InvokeRequired)
|
---|
[2713] | 405 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
|
---|
[2655] | 406 | else {
|
---|
| 407 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 408 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 409 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 410 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 411 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 412 | RebuildImageList();
|
---|
[2655] | 413 | }
|
---|
| 414 | }
|
---|
[2713] | 415 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 416 | if (InvokeRequired)
|
---|
[2713] | 417 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
|
---|
[2655] | 418 | else {
|
---|
| 419 | int[] selected = new int[itemsListView.SelectedIndices.Count];
|
---|
| 420 | itemsListView.SelectedIndices.CopyTo(selected, 0);
|
---|
| 421 |
|
---|
| 422 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 423 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 424 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 425 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 426 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 427 | RebuildImageList();
|
---|
[2655] | 428 |
|
---|
| 429 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 430 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 431 | AdjustListViewColumnSizes();
|
---|
[2655] | 432 |
|
---|
| 433 | for (int i = 0; i < selected.Length; i++)
|
---|
| 434 | itemsListView.Items[selected[i]].Selected = true;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
[2713] | 437 | protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 438 | if (InvokeRequired)
|
---|
[2713] | 439 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
|
---|
[2655] | 440 | else {
|
---|
| 441 | foreach (IndexedItem<T> item in e.Items) {
|
---|
| 442 | ListViewItem listViewItem = itemsListView.Items[item.Index];
|
---|
[7161] | 443 | if (listViewItem.Tag != null)
|
---|
| 444 | itemListViewItemMapping[(T)listViewItem.Tag].Remove(listViewItem);
|
---|
[2655] | 445 | listViewItem.Tag = item.Value;
|
---|
[7161] | 446 | if (listViewItem.Tag != null)
|
---|
| 447 | itemListViewItemMapping[item.Value].Add(listViewItem);
|
---|
[3327] | 448 | UpdateListViewItemImage(listViewItem);
|
---|
| 449 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 450 | }
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
[2713] | 453 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2655] | 454 | if (InvokeRequired)
|
---|
[2713] | 455 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
|
---|
[2655] | 456 | else {
|
---|
| 457 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 458 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 459 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 460 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 461 | RemoveListViewItem(listViewItem);
|
---|
[5237] | 462 | RebuildImageList();
|
---|
[2655] | 463 |
|
---|
| 464 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 465 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
[4240] | 466 | AdjustListViewColumnSizes();
|
---|
[2655] | 467 | }
|
---|
| 468 | }
|
---|
| 469 | #endregion
|
---|
| 470 |
|
---|
| 471 | #region Item Events
|
---|
[3306] | 472 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 473 | if (InvokeRequired)
|
---|
| 474 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 475 | else {
|
---|
| 476 | T item = (T)sender;
|
---|
[5237] | 477 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 478 | UpdateListViewItemImage(listViewItem);
|
---|
[3306] | 479 | }
|
---|
| 480 | }
|
---|
[2932] | 481 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
[2655] | 482 | if (InvokeRequired)
|
---|
[2932] | 483 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
[2655] | 484 | else {
|
---|
| 485 | T item = (T)sender;
|
---|
[5237] | 486 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
| 487 | UpdateListViewItemText(listViewItem);
|
---|
[9755] | 488 | AdjustListViewColumnSizes();
|
---|
[2655] | 489 | }
|
---|
| 490 | }
|
---|
| 491 | #endregion
|
---|
[3362] | 492 |
|
---|
| 493 | #region Helpers
|
---|
| 494 | protected virtual void AdjustListViewColumnSizes() {
|
---|
| 495 | if (itemsListView.Items.Count > 0) {
|
---|
| 496 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
| 497 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
[5239] | 500 | protected virtual void RebuildImageList() {
|
---|
[5237] | 501 | itemsListView.SmallImageList.Images.Clear();
|
---|
[5239] | 502 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 503 | T item = listViewItem.Tag as T;
|
---|
[5287] | 504 | itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
|
---|
[5239] | 505 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
[5237] | 506 | }
|
---|
| 507 | }
|
---|
[3362] | 508 | #endregion
|
---|
[2655] | 509 | }
|
---|
| 510 | }
|
---|