Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemArrayView.cs @ 2879

Last change on this file since 2879 was 2870, checked in by swagner, 15 years ago

Added some of the changes suggested by abeham to improve UI reaction times (#887)

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