[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;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
| 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Core.Views {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// The visual representation of all variables in a specified scope.
|
---|
| 33 | /// </summary>
|
---|
[2917] | 34 | [View("ItemArray View")]
|
---|
[2746] | 35 | [Content(typeof(ItemArray<>), true)]
|
---|
| 36 | [Content(typeof(IObservableArray<>), false)]
|
---|
| 37 | public partial class ItemArrayView<T> : ContentView where T : class, IItem {
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Gets or sets the scope whose variables to represent visually.
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
| 42 | /// No won data storage present.</remarks>
|
---|
| 43 | public new IObservableArray<T> Content {
|
---|
| 44 | get { return (IObservableArray<T>)base.Content; }
|
---|
| 45 | set { base.Content = value; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public ListView ItemsListView {
|
---|
| 49 | get { return itemsListView; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | /// <summary>
|
---|
| 53 | /// Initializes a new instance of <see cref="VariablesScopeView"/> with caption "Variables Scope View".
|
---|
| 54 | /// </summary>
|
---|
| 55 | public ItemArrayView() {
|
---|
| 56 | InitializeComponent();
|
---|
| 57 | Caption = "Item Array";
|
---|
| 58 | }
|
---|
| 59 | public ItemArrayView(IObservableArray<T> content)
|
---|
| 60 | : this() {
|
---|
| 61 | Content = content;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// Removes the eventhandlers from the underlying <see cref="IScope"/>.
|
---|
| 66 | /// </summary>
|
---|
| 67 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
| 68 | protected override void DeregisterContentEvents() {
|
---|
| 69 | Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
| 70 | Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
| 71 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
| 72 | base.DeregisterContentEvents();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /// <summary>
|
---|
| 76 | /// Adds eventhandlers to the underlying <see cref="IScope"/>.
|
---|
| 77 | /// </summary>
|
---|
| 78 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
| 79 | protected override void RegisterContentEvents() {
|
---|
| 80 | base.RegisterContentEvents();
|
---|
| 81 | Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
|
---|
| 82 | Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
|
---|
| 83 | Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | protected override void OnContentChanged() {
|
---|
| 87 | base.OnContentChanged();
|
---|
| 88 | Caption = "Item Array";
|
---|
| 89 | while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
|
---|
| 90 | itemsListView.Enabled = false;
|
---|
| 91 | detailsGroupBox.Enabled = false;
|
---|
| 92 | viewHost.Content = null;
|
---|
| 93 | moveUpButton.Enabled = false;
|
---|
| 94 | moveDownButton.Enabled = false;
|
---|
| 95 |
|
---|
| 96 | if (Content != null) {
|
---|
| 97 | Caption += " (" + Content.GetType().Name + ")";
|
---|
| 98 | itemsListView.Enabled = true;
|
---|
| 99 | foreach (T item in Content)
|
---|
| 100 | AddListViewItem(CreateListViewItem(item));
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | protected virtual T CreateItem() {
|
---|
| 105 | try {
|
---|
| 106 | return (T)Activator.CreateInstance(typeof(T));
|
---|
| 107 | }
|
---|
| 108 | catch (Exception ex) {
|
---|
| 109 | Auxiliary.ShowErrorMessageBox(ex);
|
---|
| 110 | return null;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | protected virtual ListViewItem CreateListViewItem(T item) {
|
---|
| 114 | if (item == null) {
|
---|
| 115 | return new ListViewItem("null");
|
---|
| 116 | } else {
|
---|
| 117 | if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
|
---|
| 118 | itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
|
---|
| 119 |
|
---|
| 120 | ListViewItem listViewItem = new ListViewItem();
|
---|
| 121 | listViewItem.Text = item.ToString();
|
---|
| 122 | listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
|
---|
| 123 | listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
|
---|
| 124 | listViewItem.Tag = item;
|
---|
| 125 | return listViewItem;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | protected virtual void AddListViewItem(ListViewItem listViewItem) {
|
---|
| 129 | itemsListView.Items.Add(listViewItem);
|
---|
| 130 | if (listViewItem.Tag != null)
|
---|
[2932] | 131 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
[2746] | 132 | }
|
---|
| 133 | protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
|
---|
| 134 | itemsListView.Items.Insert(index, listViewItem);
|
---|
| 135 | if (listViewItem.Tag != null)
|
---|
[2932] | 136 | ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
|
---|
[2746] | 137 | }
|
---|
| 138 | protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
|
---|
| 139 | if (listViewItem.Tag != null)
|
---|
[2932] | 140 | ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
|
---|
[2746] | 141 | listViewItem.Remove();
|
---|
| 142 | }
|
---|
| 143 | protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
|
---|
| 144 | T item = listViewItem.Tag as T;
|
---|
| 145 | if ((item != null) && (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName)))
|
---|
| 146 | itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
|
---|
| 147 |
|
---|
| 148 | listViewItem.Text = item == null ? "null" : item.ToString();
|
---|
| 149 | listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
|
---|
| 150 | listViewItem.ImageIndex = item == null ? -1 : itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 154 | moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 155 | itemsListView.SelectedIndices[0] != 0 &&
|
---|
| 156 | !Content.IsReadOnly;
|
---|
| 157 | moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
|
---|
| 158 | itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
|
---|
| 159 | !Content.IsReadOnly;
|
---|
| 160 |
|
---|
| 161 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 162 | T item = itemsListView.SelectedItems[0].Tag as T;
|
---|
[2870] | 163 | detailsGroupBox.Enabled = true;
|
---|
[2949] | 164 | viewHost.ViewType = null;
|
---|
[2746] | 165 | viewHost.Content = item;
|
---|
| 166 | } else {
|
---|
| 167 | viewHost.Content = null;
|
---|
| 168 | detailsGroupBox.Enabled = false;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | #region ListView Events
|
---|
| 173 | protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
|
---|
| 174 | if (itemsListView.Columns.Count > 0)
|
---|
| 175 | itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 179 | if (e.KeyCode == Keys.Delete) {
|
---|
| 180 | if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
|
---|
| 181 | foreach (ListViewItem item in itemsListView.SelectedItems)
|
---|
| 182 | Content[item.Index] = null;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
|
---|
| 188 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 189 | T item = itemsListView.SelectedItems[0].Tag as T;
|
---|
| 190 | if (item != null) {
|
---|
| 191 | IView view = MainFormManager.CreateDefaultView(item);
|
---|
| 192 | if (view != null) view.Show();
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 198 | ListViewItem listViewItem = (ListViewItem)e.Item;
|
---|
| 199 | T item = listViewItem.Tag as T;
|
---|
| 200 | if (item != null) {
|
---|
| 201 | DataObject data = new DataObject();
|
---|
| 202 | data.SetData("Type", item.GetType());
|
---|
| 203 | data.SetData("Value", item);
|
---|
| 204 | if (Content.IsReadOnly) {
|
---|
| 205 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 206 | } else {
|
---|
| 207 | DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
|
---|
| 208 | if ((result & DragDropEffects.Move) == DragDropEffects.Move)
|
---|
| 209 | Content[listViewItem.Index] = null;
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
| 214 | e.Effect = DragDropEffects.None;
|
---|
| 215 | Type type = e.Data.GetData("Type") as Type;
|
---|
| 216 | if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
|
---|
| 217 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 218 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
| 219 | if (listViewItem != null) {
|
---|
| 220 | if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy; // CTRL key
|
---|
| 221 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 222 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
| 223 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 224 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
|
---|
| 229 | if (e.Effect != DragDropEffects.None) {
|
---|
| 230 | T item = e.Data.GetData("Value") as T;
|
---|
| 231 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
|
---|
| 232 |
|
---|
| 233 | Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
|
---|
| 234 | ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
|
---|
| 235 | Content[listViewItem.Index] = item;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | #endregion
|
---|
| 239 |
|
---|
| 240 | #region Button Events
|
---|
| 241 | protected virtual void moveUpButton_Click(object sender, EventArgs e) {
|
---|
| 242 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 243 | int index = itemsListView.SelectedIndices[0];
|
---|
| 244 | T item = Content[index - 1];
|
---|
| 245 | Content[index - 1] = Content[index];
|
---|
| 246 | itemsListView.Items[index].Selected = false;
|
---|
| 247 | itemsListView.Items[index - 1].Selected = true;
|
---|
| 248 | Content[index] = item;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | protected virtual void moveDownButton_Click(object sender, EventArgs e) {
|
---|
| 252 | if (itemsListView.SelectedItems.Count == 1) {
|
---|
| 253 | int index = itemsListView.SelectedIndices[0];
|
---|
| 254 | T item = Content[index + 1];
|
---|
| 255 | Content[index + 1] = Content[index];
|
---|
| 256 | itemsListView.Items[index].Selected = false;
|
---|
| 257 | itemsListView.Items[index + 1].Selected = true;
|
---|
| 258 | Content[index] = item;
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | #endregion
|
---|
| 262 |
|
---|
| 263 | #region Content Events
|
---|
| 264 | protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 265 | if (InvokeRequired)
|
---|
| 266 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
|
---|
| 267 | else {
|
---|
| 268 | int[] selected = new int[itemsListView.SelectedIndices.Count];
|
---|
| 269 | itemsListView.SelectedIndices.CopyTo(selected, 0);
|
---|
| 270 |
|
---|
| 271 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 272 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 273 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 274 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 275 | RemoveListViewItem(listViewItem);
|
---|
| 276 |
|
---|
| 277 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 278 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
| 279 |
|
---|
| 280 | for (int i = 0; i < selected.Length; i++)
|
---|
| 281 | itemsListView.Items[selected[i]].Selected = true;
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 285 | if (InvokeRequired)
|
---|
| 286 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
|
---|
| 287 | else {
|
---|
| 288 | foreach (IndexedItem<T> item in e.Items) {
|
---|
| 289 | ListViewItem listViewItem = itemsListView.Items[item.Index];
|
---|
| 290 | listViewItem.Tag = item.Value;
|
---|
| 291 | UpdateListViewItem(listViewItem);
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 | protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 296 | if (InvokeRequired)
|
---|
| 297 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
|
---|
| 298 | else {
|
---|
| 299 | List<ListViewItem> listViewItems = new List<ListViewItem>();
|
---|
| 300 | foreach (IndexedItem<T> item in e.OldItems)
|
---|
| 301 | listViewItems.Add(itemsListView.Items[item.Index]);
|
---|
| 302 | foreach (ListViewItem listViewItem in listViewItems)
|
---|
| 303 | RemoveListViewItem(listViewItem);
|
---|
| 304 |
|
---|
| 305 | foreach (IndexedItem<T> item in e.Items)
|
---|
| 306 | InsertListViewItem(item.Index, CreateListViewItem(item.Value));
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | #endregion
|
---|
| 310 |
|
---|
| 311 | #region Item Events
|
---|
[2932] | 312 | protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
|
---|
[2746] | 313 | if (InvokeRequired)
|
---|
[2932] | 314 | Invoke(new EventHandler(Item_ToStringChanged), sender, e);
|
---|
[2746] | 315 | else {
|
---|
| 316 | T item = (T)sender;
|
---|
| 317 | foreach (ListViewItem listViewItem in itemsListView.Items) {
|
---|
| 318 | if (((T)listViewItem.Tag) == item)
|
---|
| 319 | UpdateListViewItem(listViewItem);
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | #endregion
|
---|
| 324 | }
|
---|
| 325 | }
|
---|