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