Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemListView.cs @ 3306

Last change on this file since 3306 was 3306, checked in by swagner, 12 years ago

Implemented reviewers' comments (#893).

File size: 16.0 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  [View("ItemList View")]
35  [Content(typeof(ItemList<>), true)]
36  [Content(typeof(IObservableList<>), false)]
37  public partial class ItemListView<T> : AsynchronousContentView 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 IObservableList<T> Content {
44      get { return (IObservableList<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 ItemListView() {
56      InitializeComponent();
57      Caption = "Item List";
58    }
59    public ItemListView(IObservableList<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.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
70      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
71      Content.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
72      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
73      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
74      base.DeregisterContentEvents();
75    }
76
77    /// <summary>
78    /// Adds eventhandlers to the underlying <see cref="IScope"/>.
79    /// </summary>
80    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
81    protected override void RegisterContentEvents() {
82      base.RegisterContentEvents();
83      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded);
84      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved);
85      Content.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
86      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
87      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset);
88    }
89
90    protected override void OnContentChanged() {
91      base.OnContentChanged();
92      Caption = "Item List";
93      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
94      itemsListView.Enabled = false;
95      detailsGroupBox.Enabled = false;
96      viewHost.Content = null;
97      addButton.Enabled = false;
98      moveUpButton.Enabled = false;
99      moveDownButton.Enabled = false;
100      removeButton.Enabled = false;
101
102      if (Content != null) {
103        Caption += " (" + Content.GetType().Name + ")";
104        itemsListView.Enabled = true;
105        addButton.Enabled = !Content.IsReadOnly;
106        foreach (T item in Content)
107          AddListViewItem(CreateListViewItem(item));
108      }
109    }
110
111    protected virtual T CreateItem() {
112      try {
113        return (T)Activator.CreateInstance(typeof(T));
114      }
115      catch (Exception ex) {
116        Auxiliary.ShowErrorMessageBox(ex);
117        return null;
118      }
119    }
120    protected virtual ListViewItem CreateListViewItem(T item) {
121      ListViewItem listViewItem = new ListViewItem();
122      listViewItem.Text = item.ToString();
123      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
124      listViewItem.Tag = item;
125      SetListViewItemImage(listViewItem);
126      return listViewItem;
127    }
128    protected virtual void AddListViewItem(ListViewItem listViewItem) {
129      itemsListView.Items.Add(listViewItem);
130      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
131      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
132    }
133    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
134      itemsListView.Items.Insert(index, listViewItem);
135      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
136      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
137    }
138    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
139      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
140      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
141      listViewItem.Remove();
142    }
143    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
144      T item = (T)listViewItem.Tag;
145      listViewItem.Text = item.ToString();
146      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
147      if (itemsListView.SmallImageList.Images[listViewItem.ImageIndex] != item.ItemImage)
148        SetListViewItemImage(listViewItem);
149    }
150
151    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
152      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
153                             itemsListView.SelectedIndices[0] != 0 &&
154                             !Content.IsReadOnly;
155      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
156                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
157                               !Content.IsReadOnly;
158      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
159                             !Content.IsReadOnly;
160
161      if (itemsListView.SelectedItems.Count == 1) {
162        T item = (T)itemsListView.SelectedItems[0].Tag;
163        detailsGroupBox.Enabled = true;
164        viewHost.ViewType = null;
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.RemoveAt(item.Index);
183        }
184      }
185    }
186
187    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
188      if (itemsListView.SelectedItems.Count == 1) {
189        T item = (T)itemsListView.SelectedItems[0].Tag;
190        IView view = MainFormManager.CreateDefaultView(item);
191        if (view != null) view.Show();
192      }
193    }
194
195    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
196      ListViewItem listViewItem = (ListViewItem)e.Item;
197      T item = (T)listViewItem.Tag;
198      DataObject data = new DataObject();
199      data.SetData("Type", item.GetType());
200      data.SetData("Value", item);
201      if (Content.IsReadOnly) {
202        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
203      } else {
204        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
205        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
206          Content.RemoveAt(listViewItem.Index);
207      }
208    }
209    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
210      e.Effect = DragDropEffects.None;
211      Type type = e.Data.GetData("Type") as Type;
212      if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
213        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
214        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
215        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
216        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
217        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
218      }
219    }
220    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
221      if (e.Effect != DragDropEffects.None) {
222        T item = e.Data.GetData("Value") as T;
223        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
224
225        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
226        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
227        if (listViewItem != null) Content.Insert(listViewItem.Index, item);
228        else Content.Add(item);
229      }
230    }
231    #endregion
232
233    #region Button Events
234    protected virtual void addButton_Click(object sender, EventArgs e) {
235      T item = CreateItem();
236      if (item != null)
237        Content.Add(item);
238    }
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    protected virtual void removeButton_Click(object sender, EventArgs e) {
260      if (itemsListView.SelectedItems.Count > 0) {
261        foreach (ListViewItem item in itemsListView.SelectedItems)
262          Content.RemoveAt(item.Index);
263      }
264    }
265    #endregion
266
267    #region Content Events
268    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
269      if (InvokeRequired)
270        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
271      else
272        foreach (IndexedItem<T> item in e.Items)
273          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
274    }
275    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
276      if (InvokeRequired)
277        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
278      else {
279        List<ListViewItem> listViewItems = new List<ListViewItem>();
280        foreach (IndexedItem<T> item in e.Items)
281          listViewItems.Add(itemsListView.Items[item.Index]);
282        foreach (ListViewItem listViewItem in listViewItems)
283          RemoveListViewItem(listViewItem);
284      }
285    }
286    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
287      if (InvokeRequired)
288        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
289      else {
290        int[] selected = new int[itemsListView.SelectedIndices.Count];
291        itemsListView.SelectedIndices.CopyTo(selected, 0);
292
293        List<ListViewItem> listViewItems = new List<ListViewItem>();
294        foreach (IndexedItem<T> item in e.OldItems)
295          listViewItems.Add(itemsListView.Items[item.Index]);
296        foreach (ListViewItem listViewItem in listViewItems)
297          RemoveListViewItem(listViewItem);
298
299        foreach (IndexedItem<T> item in e.Items)
300          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
301
302        for (int i = 0; i < selected.Length; i++)
303          itemsListView.Items[selected[i]].Selected = true;
304      }
305    }
306    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
307      if (InvokeRequired)
308        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
309      else {
310        foreach (IndexedItem<T> item in e.Items) {
311          ListViewItem listViewItem = itemsListView.Items[item.Index];
312          listViewItem.Tag = item.Value;
313          UpdateListViewItem(listViewItem);
314        }
315      }
316    }
317    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
318      if (InvokeRequired)
319        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
320      else {
321        List<ListViewItem> listViewItems = new List<ListViewItem>();
322        foreach (IndexedItem<T> item in e.OldItems)
323          listViewItems.Add(itemsListView.Items[item.Index]);
324        foreach (ListViewItem listViewItem in listViewItems)
325          RemoveListViewItem(listViewItem);
326
327        foreach (IndexedItem<T> item in e.Items)
328          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
329      }
330    }
331    #endregion
332
333    #region Item Events
334    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
335      if (InvokeRequired)
336        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
337      else {
338        T item = (T)sender;
339        foreach (ListViewItem listViewItem in itemsListView.Items) {
340          if (((T)listViewItem.Tag) == item)
341            UpdateListViewItem(listViewItem);
342        }
343      }
344    }
345    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
346      if (InvokeRequired)
347        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
348      else {
349        T item = (T)sender;
350        foreach (ListViewItem listViewItem in itemsListView.Items) {
351          if (((T)listViewItem.Tag) == item)
352            UpdateListViewItem(listViewItem);
353        }
354      }
355    }
356    #endregion
357
358    #region Helpers
359    protected virtual void SetListViewItemImage(ListViewItem listViewItem) {
360      T item = (T)listViewItem.Tag;
361      int i = 0;
362      while ((i < itemsListView.SmallImageList.Images.Count) && (item.ItemImage != itemsListView.SmallImageList.Images[i]))
363        i++;
364      if (i == itemsListView.SmallImageList.Images.Count) itemsListView.SmallImageList.Images.Add(item.ItemImage);
365      listViewItem.ImageIndex = i;
366    }
367    #endregion
368  }
369}
Note: See TracBrowser for help on using the repository browser.