[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;
|
---|
[4203] | 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));
|
---|
[4240] | 72 | AdjustListViewColumnSizes();
|
---|
[3350] | 73 | SortItemsListView(SortOrder.Ascending);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3904] | 77 | protected override void SetEnabledStateOfControls() {
|
---|
| 78 | base.SetEnabledStateOfControls();
|
---|
[3350] | 79 | if (Content == null) {
|
---|
[3362] | 80 | addButton.Enabled = false;
|
---|
| 81 | sortAscendingButton.Enabled = false;
|
---|
| 82 | sortDescendingButton.Enabled = false;
|
---|
| 83 | removeButton.Enabled = false;
|
---|
[3350] | 84 | itemsListView.Enabled = false;
|
---|
| 85 | detailsGroupBox.Enabled = false;
|
---|
| 86 | } else {
|
---|
[3435] | 87 | addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
|
---|
[3362] | 88 | sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
| 89 | sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
|
---|
[3435] | 90 | removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
|
---|
[3350] | 91 | itemsListView.Enabled = true;
|
---|
[4099] | 92 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
[2655] | 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | protected virtual T CreateItem() {
|
---|
[3407] | 97 | if (typeSelectorDialog == null) {
|
---|
| 98 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
| 99 | typeSelectorDialog.Caption = "Select Item";
|
---|
| 100 | typeSelectorDialog.TypeSelector.Caption = "Available Items";
|
---|
[3588] | 101 | typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
|
---|
[2655] | 102 | }
|
---|
[3407] | 103 |
|
---|
| 104 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
| 105 | try {
|
---|
| 106 | return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 107 | }
|
---|
| 108 | catch (Exception ex) {
|
---|
[3758] | 109 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | return null;
|
---|
[2655] | 113 | }
|
---|
| 114 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
| 115 | ListViewItem listViewItem = new ListViewItem();
|
---|
| 116 | listViewItem.Text = item.ToString();
|
---|
| 117 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
| 118 | listViewItem.Tag = item;
|
---|
[3327] | 119 | itemsListView.SmallImageList.Images.Add(item.ItemImage);
|
---|
| 120 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
|
---|
[2655] | 121 | return listViewItem;
|
---|
| 122 | }
|
---|
| 123 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
| 124 | itemsListView.Items.Add(listViewItem);
|
---|
[3306] | 125 | ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
|
---|
[2932] | 126 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
[3362] | 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();
|
---|
[4096] | 161 | if (showDetailsCheckBox.Checked) {
|
---|
| 162 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 163 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
| 164 | detailsGroupBox.Enabled = true;
|
---|
| 165 | viewHost.Content = item;
|
---|
| 166 | } else {
|
---|
| 167 | viewHost.Content = null;
|
---|
| 168 | detailsGroupBox.Enabled = false;
|
---|
| 169 | }
|
---|
[2655] | 170 | }
|
---|
| 171 | }
|
---|
| 172 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 173 | if (e.KeyCode == Keys.Delete) {
|
---|
[3435] | 174 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
|
---|
[2655] | 175 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 176 | Content.Remove((T)item.Tag);
|
---|
[2655] | 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 181 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 182 | T item = (T)itemsListView.SelectedItems[0].Tag;
|
---|
[3557] | 183 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
[3416] | 184 | if (view != null) {
|
---|
[3423] | 185 | view.ReadOnly = ReadOnly;
|
---|
| 186 | view.Locked = Locked;
|
---|
[3416] | 187 | }
|
---|
[2655] | 188 | }
|
---|
| 189 | }
|
---|
| 190 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[3432] | 191 | if (!Locked) {
|
---|
| 192 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
| 193 | T item = (T)listViewItem.Tag;
|
---|
| 194 | DataObject data = new DataObject();
|
---|
| 195 | data.SetData("Type", item.GetType());
|
---|
| 196 | data.SetData("Value", item);
|
---|
[3435] | 197 | if (Content.IsReadOnly || ReadOnly) {
|
---|
[3432] | 198 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 199 | } else {
|
---|
| 200 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 201 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
| 202 | Content.Remove(item);
|
---|
| 203 | }
|
---|
[2655] | 204 | }
|
---|
| 205 | }
|
---|
| 206 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 207 | e.Effect = DragDropEffects.None;
|
---|
| 208 | Type type = e.Data.GetData("Type") as Type;
|
---|
[3435] | 209 | if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
[3694] | 210 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[2694] | 211 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 212 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 213 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
[3526] | 214 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
[2655] | 215 | }
|
---|
| 216 | }
|
---|
| 217 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 218 | if (e.Effect != DragDropEffects.None) {
|
---|
| 219 | T item = e.Data.GetData("Value") as T;
|
---|
| 220 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
[2713] | 221 | Content.Add(item);
|
---|
[2655] | 222 | }
|
---|
| 223 | }
|
---|
| 224 | #endregion
|
---|
| 225 |
|
---|
| 226 | #region Button Events
|
---|
| 227 | protected virtual void addButton_Click(object sender, EventArgs e) {
|
---|
| 228 | T item = CreateItem();
|
---|
| 229 | if (item != null)
|
---|
[2713] | 230 | Content.Add(item);
|
---|
[2655] | 231 | }
|
---|
[2676] | 232 | protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 233 | SortItemsListView(SortOrder.Ascending);
|
---|
| 234 | }
|
---|
[2676] | 235 | protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 236 | SortItemsListView(SortOrder.Descending);
|
---|
| 237 | }
|
---|
| 238 | protected virtual void removeButton_Click(object sender, EventArgs e) {
|
---|
| 239 | if (itemsListView.SelectedItems.Count > 0) {
|
---|
| 240 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
[2713] | 241 | Content.Remove((T)item.Tag);
|
---|
[2655] | 242 | itemsListView.SelectedItems.Clear();
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | #endregion
|
---|
| 246 |
|
---|
[4096] | 247 | #region CheckBox Events
|
---|
| 248 | protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
| 249 | if (showDetailsCheckBox.Checked) {
|
---|
| 250 | splitContainer.Panel2Collapsed = false;
|
---|
| 251 | detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
|
---|
| 252 | viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
|
---|
| 253 | } else {
|
---|
| 254 | splitContainer.Panel2Collapsed = true;
|
---|
| 255 | viewHost.Content = null;
|
---|
| 256 | }
|
---|
| 257 | }
|
---|
| 258 | #endregion
|
---|
| 259 |
|
---|
[2713] | 260 | #region Content Events
|
---|
| 261 | protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 262 | if (InvokeRequired)
|
---|
[2713] | 263 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
|
---|
[4240] | 264 | else {
|
---|
[2655] | 265 | foreach (T item in e.Items)
|
---|
| 266 | AddListViewItem(CreateListViewItem(item));
|
---|
[4240] | 267 | AdjustListViewColumnSizes();
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[2655] | 270 | }
|
---|
[2713] | 271 | protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 272 | if (InvokeRequired)
|
---|
[2713] | 273 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
|
---|
[2655] | 274 | else {
|
---|
| 275 | foreach (T item in e.Items) {
|
---|
[4203] | 276 | //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
|
---|
| 277 | ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
[4240] | 278 | if (listviewItem != null) RemoveListViewItem(listviewItem);
|
---|
[2655] | 279 | }
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
[2713] | 282 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
[2655] | 283 | if (InvokeRequired)
|
---|
[2713] | 284 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
|
---|
[2655] | 285 | else {
|
---|
| 286 | foreach (T item in e.OldItems) {
|
---|
[4203] | 287 | //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
|
---|
| 288 | ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
|
---|
[4240] | 289 | if (listviewItem != null) RemoveListViewItem(listviewItem);
|
---|
[2655] | 290 | }
|
---|
| 291 | foreach (T item in e.Items)
|
---|
| 292 | AddListViewItem(CreateListViewItem(item));
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | #endregion
|
---|
| 296 |
|
---|
| 297 | #region Item Events
|
---|
[3306] | 298 | protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 299 | if (InvokeRequired)
|
---|
| 300 | Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
|
---|
| 301 | else {
|
---|
| 302 | T item = (T)sender;
|
---|
| 303 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 304 | UpdateListViewItemImage(listViewItem);
|
---|
[3306] | 305 | }
|
---|
| 306 | }
|
---|
[2932] | 307 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
[2655] | 308 | if (InvokeRequired)
|
---|
[2932] | 309 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
[2655] | 310 | else {
|
---|
| 311 | T item = (T)sender;
|
---|
| 312 | foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
|
---|
[3327] | 313 | UpdateListViewItemText(listViewItem);
|
---|
[2655] | 314 | }
|
---|
| 315 | }
|
---|
| 316 | #endregion
|
---|
| 317 |
|
---|
| 318 | #region Helpers
|
---|
[2676] | 319 | protected virtual void SortItemsListView(SortOrder sortOrder) {
|
---|
[3298] | 320 | itemsListView.Sorting = SortOrder.None;
|
---|
[2655] | 321 | itemsListView.Sorting = sortOrder;
|
---|
| 322 | itemsListView.Sorting = SortOrder.None;
|
---|
| 323 | }
|
---|
[3362] | 324 | protected virtual void AdjustListViewColumnSizes() {
|
---|
| 325 | if (itemsListView.Items.Count > 0) {
|
---|
| 326 | for (int i = 0; i < itemsListView.Columns.Count; i++)
|
---|
| 327 | itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
[2655] | 330 | #endregion
|
---|
| 331 | }
|
---|
| 332 | }
|
---|