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