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