[2655] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 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;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[3306] | 24 | using System.Linq;
|
---|
[2655] | 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
[2713] | 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
[2655] | 29 |
|
---|
| 30 | namespace HeuristicLab.Core.Views {
|
---|
[2917] | 31 | [View("ItemCollection View")]
|
---|
[2727] | 32 | [Content(typeof(ItemCollection<>), true)]
|
---|
| 33 | [Content(typeof(IObservableCollection<>), false)]
|
---|
[3228] | 34 | public partial class ItemCollectionView<T> : AsynchronousContentView where T : class, IItem {
|
---|
[2713] | 35 | public new IObservableCollection<T> Content {
|
---|
| 36 | get { return (IObservableCollection<T>)base.Content; }
|
---|
| 37 | set { base.Content = value; }
|
---|
[2655] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public ListView ItemsListView {
|
---|
| 41 | get { return itemsListView; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public ItemCollectionView() {
|
---|
| 45 | InitializeComponent();
|
---|
| 46 | Caption = "Item Collection";
|
---|
| 47 | }
|
---|
[2875] | 48 |
|
---|
[2727] | 49 | public ItemCollectionView(IObservableCollection<T> content)
|
---|
| 50 | : this() {
|
---|
| 51 | Content = content;
|
---|
| 52 | }
|
---|
[2655] | 53 |
|
---|
[2713] | 54 | protected override void DeregisterContentEvents() {
|
---|
| 55 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
| 56 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
| 57 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
| 58 | base.DeregisterContentEvents();
|
---|
[2655] | 59 | }
|
---|
[2713] | 60 | protected override void RegisterContentEvents() {
|
---|
| 61 | base.RegisterContentEvents();
|
---|
| 62 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
| 63 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
| 64 | Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
[2655] | 65 | }
|
---|
| 66 |
|
---|
[2713] | 67 | protected override void OnContentChanged() {
|
---|
| 68 | base.OnContentChanged();
|
---|
[2655] | 69 | Caption = "Item Collection";
|
---|
| 70 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
| 71 | itemsListView.Enabled = false;
|
---|
| 72 | detailsGroupBox.Enabled = false;
|
---|
[2713] | 73 | viewHost.Content = null;
|
---|
[2655] | 74 | addButton.Enabled = false;
|
---|
| 75 | sortAscendingButton.Enabled = false;
|
---|
| 76 | sortDescendingButton.Enabled = false;
|
---|
| 77 | removeButton.Enabled = false;
|
---|
| 78 |
|
---|
[2713] | 79 | if (Content != null) {
|
---|
| 80 | Caption += " (" + Content.GetType().Name + ")";
|
---|
[2655] | 81 | itemsListView.Enabled = true;
|
---|
[2713] | 82 | addButton.Enabled = !Content.IsReadOnly;
|
---|
| 83 | foreach (T item in Content)
|
---|
[2655] | 84 | AddListViewItem(CreateListViewItem(item));
|
---|
| 85 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 86 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 87 | SortItemsListView(SortOrder.Ascending);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | protected virtual T CreateItem() {
|
---|
| 92 | try {
|
---|
| 93 | return (T)Activator.CreateInstance(typeof(T));
|
---|
| 94 | } catch(Exception ex) {
|
---|
| 95 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 96 | return null;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
| 100 | ListViewItem listViewItem = new ListViewItem();
|
---|
| 101 | listViewItem.Text = item.ToString();
|
---|
| 102 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
| 103 | listViewItem.Tag = item;
|
---|
[3327] | 104 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
| 105 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
[2655] | 106 | return listViewItem;
|
---|
| 107 | }
|
---|
| 108 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
| 109 | itemsListView.Items.Add(listViewItem);
|
---|
[3306] | 110 | ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
[2932] | 111 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
[2655] | 112 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 113 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 114 | }
|
---|
| 115 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
[3306] | 116 | ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
|
---|
[2932] | 117 | ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
[2655] | 118 | listViewItem.Remove();
|
---|
| 119 | sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 120 | sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
|
---|
| 121 | }
|
---|
[3327] | 122 | protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
|
---|
| 123 | int i = listViewItem.ImageIndex;
|
---|
| 124 | listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
|
---|
| 125 | listViewItem.ImageIndex = -1;
|
---|
| 126 | listViewItem.ImageIndex = i;
|
---|
| 127 | }
|
---|
| 128 | protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
|
---|
[3306] | 129 | if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
|
---|
[2655] | 130 | listViewItem.Text = listViewItem.Tag.ToString();
|
---|
| 131 | }
|
---|
| 132 | protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
|
---|
| 133 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 134 | if (((T)listViewItem.Tag) == item)
|
---|
| 135 | yield return listViewItem;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | #region ListView Events
|
---|
| 140 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
[2713] | 141 | removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
|
---|
[2655] | 142 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 143 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
[2870] | 144 | detailsGroupBox.Enabled = true;
|
---|
[2949] | 145 | viewHost.ViewType = null;
|
---|
[2713] | 146 | viewHost.Content = item;
|
---|
[2655] | 147 | } else {
|
---|
[2713] | 148 | viewHost.Content = null;
|
---|
[2655] | 149 | detailsGroupBox.Enabled = false;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
[2676] | 152 | protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
|
---|
[2655] | 153 | if (itemsListView.Columns.Count > 0)
|
---|
| 154 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
| 155 | }
|
---|
| 156 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 157 | if (e.KeyCode == Keys.Delete) {
|
---|
[2713] | 158 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
|
---|
[2655] | 159 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 160 | Content.Remove((T)item.Tag);
|
---|
[2655] | 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 165 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 166 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
| 167 | IView view = MainFormManager.CreateDefaultView(item);
|
---|
[2708] | 168 | if (view != null) view.Show();
|
---|
[2655] | 169 | }
|
---|
| 170 | }
|
---|
| 171 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 172 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
| 173 | T item = (T)listViewItem.Tag;
|
---|
| 174 | DataObject data = new DataObject();
|
---|
| 175 | data.SetData("Type", item.GetType());
|
---|
| 176 | data.SetData("Value", item);
|
---|
[2713] | 177 | if (Content.IsReadOnly) {
|
---|
[2655] | 178 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 179 | } else {
|
---|
| 180 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 181 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
[2713] | 182 | Content.Remove(item);
|
---|
[2655] | 183 | }
|
---|
| 184 | }
|
---|
| 185 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 186 | e.Effect = DragDropEffects.None;
|
---|
| 187 | Type type = e.Data.GetData("Type") as Type;
|
---|
[2713] | 188 | if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
[2655] | 189 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
[2694] | 190 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 191 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
| 192 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 193 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
[2655] | 194 | }
|
---|
| 195 | }
|
---|
| 196 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 197 | if (e.Effect != DragDropEffects.None) {
|
---|
| 198 | T item = e.Data.GetData("Value") as T;
|
---|
| 199 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
[2713] | 200 | Content.Add(item);
|
---|
[2655] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | #endregion
|
---|
| 204 |
|
---|
| 205 | #region Button Events
|
---|
| 206 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
| 207 | T item = CreateItem();
|
---|
| 208 | if (item != null)
|
---|
[2713] | 209 | Content.Add(item);
|
---|
[2655] | 210 | }
|
---|
[2676] | 211 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 212 | SortItemsListView(SortOrder.Ascending);
|
---|
| 213 | }
|
---|
[2676] | 214 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 215 | SortItemsListView(SortOrder.Descending);
|
---|
| 216 | }
|
---|
| 217 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
| 218 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 219 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 220 | Content.Remove((T)item.Tag);
|
---|
[2655] | 221 | itemsListView.SelectedItems.Clear();
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | #endregion
|
---|
| 225 |
|
---|
[2713] | 226 | #region Content Events
|
---|
| 227 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 228 | if (InvokeRequired)
|
---|
[2713] | 229 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
|
---|
[2655] | 230 | else
|
---|
| 231 | foreach (T item in e.Items)
|
---|
| 232 | AddListViewItem(CreateListViewItem(item));
|
---|
| 233 | }
|
---|
[2713] | 234 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 235 | if (InvokeRequired)
|
---|
[2713] | 236 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
|
---|
[2655] | 237 | else {
|
---|
| 238 | foreach (T item in e.Items) {
|
---|
| 239 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
| 240 | RemoveListViewItem(listViewItem);
|
---|
| 241 | break;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
[2713] | 246 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 247 | if (InvokeRequired)
|
---|
[2713] | 248 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
|
---|
[2655] | 249 | else {
|
---|
| 250 | foreach (T item in e.OldItems) {
|
---|
| 251 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
|
---|
| 252 | RemoveListViewItem(listViewItem);
|
---|
| 253 | break;
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | foreach (T item in e.Items)
|
---|
| 257 | AddListViewItem(CreateListViewItem(item));
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | #endregion
|
---|
| 261 |
|
---|
| 262 | #region Item Events
|
---|
[3306] | 263 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 264 | if (InvokeRequired)
|
---|
| 265 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 266 | else {
|
---|
| 267 | T item = (T)sender;
|
---|
| 268 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 269 | UpdateListViewItemImage(listViewItem);
|
---|
[3306] | 270 | }
|
---|
| 271 | }
|
---|
[2932] | 272 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
[2655] | 273 | if (InvokeRequired)
|
---|
[2932] | 274 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
[2655] | 275 | else {
|
---|
| 276 | T item = (T)sender;
|
---|
| 277 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 278 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 279 | }
|
---|
| 280 | }
|
---|
| 281 | #endregion
|
---|
| 282 |
|
---|
| 283 | #region Helpers
|
---|
[2676] | 284 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
[3298] | 285 | itemsListView.Sorting = SortOrder.None;
|
---|
[2655] | 286 | itemsListView.Sorting = sortOrder;
|
---|
| 287 | itemsListView.Sorting = SortOrder.None;
|
---|
| 288 | }
|
---|
| 289 | #endregion
|
---|
| 290 | }
|
---|
| 291 | }
|
---|