Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bug which was introduced in r3306 (#893).

File size: 15.8 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      itemsListView.SmallImageList.Images.Add(item.ItemImage);
126      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
127      return listViewItem;
128    }
129    protected virtual void AddListViewItem(ListViewItem listViewItem) {
130      itemsListView.Items.Add(listViewItem);
131      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
132      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
133    }
134    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
135      itemsListView.Items.Insert(index, listViewItem);
136      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
137      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
138    }
139    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
140      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
141      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
142      listViewItem.Remove();
143    }
144    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
145      int i = listViewItem.ImageIndex;
146      listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
147      listViewItem.ImageIndex = -1;
148      listViewItem.ImageIndex = i;
149    }
150    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
151      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
152        listViewItem.Text = listViewItem.Tag.ToString();
153    }
154
155    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
156      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
157                             itemsListView.SelectedIndices[0] != 0 &&
158                             !Content.IsReadOnly;
159      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
160                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
161                               !Content.IsReadOnly;
162      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
163                             !Content.IsReadOnly;
164
165      if (itemsListView.SelectedItems.Count == 1) {
166        T item = (T)itemsListView.SelectedItems[0].Tag;
167        detailsGroupBox.Enabled = true;
168        viewHost.ViewType = null;
169        viewHost.Content = item;
170      } else {
171        viewHost.Content = null;
172        detailsGroupBox.Enabled = false;
173      }
174    }
175
176    #region ListView Events
177    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
178      if (itemsListView.Columns.Count > 0)
179        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
180    }
181
182    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
183      if (e.KeyCode == Keys.Delete) {
184        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
185          foreach (ListViewItem item in itemsListView.SelectedItems)
186            Content.RemoveAt(item.Index);
187        }
188      }
189    }
190
191    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
192      if (itemsListView.SelectedItems.Count == 1) {
193        T item = (T)itemsListView.SelectedItems[0].Tag;
194        IView view = MainFormManager.CreateDefaultView(item);
195        if (view != null) view.Show();
196      }
197    }
198
199    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
200      ListViewItem listViewItem = (ListViewItem)e.Item;
201      T item = (T)listViewItem.Tag;
202      DataObject data = new DataObject();
203      data.SetData("Type", item.GetType());
204      data.SetData("Value", item);
205      if (Content.IsReadOnly) {
206        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
207      } else {
208        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
209        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
210          Content.RemoveAt(listViewItem.Index);
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        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
218        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
219        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
220        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
221        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
222      }
223    }
224    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
225      if (e.Effect != DragDropEffects.None) {
226        T item = e.Data.GetData("Value") as T;
227        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
228
229        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
230        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
231        if (listViewItem != null) Content.Insert(listViewItem.Index, item);
232        else Content.Add(item);
233      }
234    }
235    #endregion
236
237    #region Button Events
238    protected virtual void addButton_Click(object sender, EventArgs e) {
239      T item = CreateItem();
240      if (item != null)
241        Content.Add(item);
242    }
243    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
244      if (itemsListView.SelectedItems.Count == 1) {
245        int index = itemsListView.SelectedIndices[0];
246        T item = Content[index - 1];
247        Content[index - 1] = Content[index];
248        itemsListView.Items[index].Selected = false;
249        itemsListView.Items[index - 1].Selected = true;
250        Content[index] = item;
251      }
252    }
253    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
254      if (itemsListView.SelectedItems.Count == 1) {
255        int index = itemsListView.SelectedIndices[0];
256        T item = Content[index + 1];
257        Content[index + 1] = Content[index];
258        itemsListView.Items[index].Selected = false;
259        itemsListView.Items[index + 1].Selected = true;
260        Content[index] = item;
261      }
262    }
263    protected virtual void removeButton_Click(object sender, EventArgs e) {
264      if (itemsListView.SelectedItems.Count > 0) {
265        foreach (ListViewItem item in itemsListView.SelectedItems)
266          Content.RemoveAt(item.Index);
267      }
268    }
269    #endregion
270
271    #region Content Events
272    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
273      if (InvokeRequired)
274        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsAdded), sender, e);
275      else
276        foreach (IndexedItem<T> item in e.Items)
277          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
278    }
279    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
280      if (InvokeRequired)
281        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsRemoved), sender, e);
282      else {
283        List<ListViewItem> listViewItems = new List<ListViewItem>();
284        foreach (IndexedItem<T> item in e.Items)
285          listViewItems.Add(itemsListView.Items[item.Index]);
286        foreach (ListViewItem listViewItem in listViewItems)
287          RemoveListViewItem(listViewItem);
288      }
289    }
290    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
291      if (InvokeRequired)
292        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
293      else {
294        int[] selected = new int[itemsListView.SelectedIndices.Count];
295        itemsListView.SelectedIndices.CopyTo(selected, 0);
296
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        for (int i = 0; i < selected.Length; i++)
307          itemsListView.Items[selected[i]].Selected = true;
308      }
309    }
310    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
311      if (InvokeRequired)
312        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
313      else {
314        foreach (IndexedItem<T> item in e.Items) {
315          ListViewItem listViewItem = itemsListView.Items[item.Index];
316          listViewItem.Tag = item.Value;
317          UpdateListViewItemImage(listViewItem);
318          UpdateListViewItemText(listViewItem);
319        }
320      }
321    }
322    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
323      if (InvokeRequired)
324        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
325      else {
326        List<ListViewItem> listViewItems = new List<ListViewItem>();
327        foreach (IndexedItem<T> item in e.OldItems)
328          listViewItems.Add(itemsListView.Items[item.Index]);
329        foreach (ListViewItem listViewItem in listViewItems)
330          RemoveListViewItem(listViewItem);
331
332        foreach (IndexedItem<T> item in e.Items)
333          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
334      }
335    }
336    #endregion
337
338    #region Item Events
339    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
340      if (InvokeRequired)
341        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
342      else {
343        T item = (T)sender;
344        foreach (ListViewItem listViewItem in itemsListView.Items) {
345          if (((T)listViewItem.Tag) == item)
346            UpdateListViewItemImage(listViewItem);
347        }
348      }
349    }
350    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
351      if (InvokeRequired)
352        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
353      else {
354        T item = (T)sender;
355        foreach (ListViewItem listViewItem in itemsListView.Items) {
356          if (((T)listViewItem.Tag) == item)
357            UpdateListViewItemText(listViewItem);
358        }
359      }
360    }
361    #endregion
362  }
363}
Note: See TracBrowser for help on using the repository browser.