Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 5287

Last change on this file since 5287 was 5287, checked in by abeham, 13 years ago

#1337

  • Renamed VS2008ImageLibrary resource to VSImageLibrary
  • Added Filter icon to the VS2010ImageLibrary folder and the resource manager
File size: 16.2 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Core.Views {
32  [View("ItemCollection View")]
33  [Content(typeof(ItemCollection<>), true)]
34  [Content(typeof(IItemCollection<>), false)]
35  public partial class ItemCollectionView<T> : ItemView where T : class, IItem {
36    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
37    protected TypeSelectorDialog typeSelectorDialog;
38
39    public new IItemCollection<T> Content {
40      get { return (IItemCollection<T>)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public ListView ItemsListView {
45      get { return itemsListView; }
46    }
47
48    public ItemCollectionView() {
49      InitializeComponent();
50      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
51    }
52
53    protected override void Dispose(bool disposing) {
54      if (disposing) {
55        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
56        if (components != null) components.Dispose();
57      }
58      base.Dispose(disposing);
59    }
60
61    protected override void DeregisterContentEvents() {
62      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
63      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
64      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
65      foreach (T item in itemListViewItemMapping.Keys) {
66        DeregisterItemEvents(item);
67      }
68      base.DeregisterContentEvents();
69    }
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
73      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
74      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
75    }
76    protected virtual void DeregisterItemEvents(T item) {
77      item.ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
78      item.ToStringChanged -= new EventHandler(Item_ToStringChanged);
79    }
80    protected virtual void RegisterItemEvents(T item) {
81      item.ItemImageChanged += new EventHandler(Item_ItemImageChanged);
82      item.ToStringChanged += new EventHandler(Item_ToStringChanged);
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      itemsListView.Items.Clear();
88      itemListViewItemMapping.Clear();
89      RebuildImageList();
90      viewHost.Content = null;
91      if (Content != null) {
92        Caption += " (" + Content.GetType().Name + ")";
93        foreach (T item in Content)
94          AddListViewItem(CreateListViewItem(item));
95        AdjustListViewColumnSizes();
96        SortItemsListView(SortOrder.Ascending);
97      }
98    }
99
100    protected override void SetEnabledStateOfControls() {
101      base.SetEnabledStateOfControls();
102      if (Content == null) {
103        addButton.Enabled = false;
104        sortAscendingButton.Enabled = false;
105        sortDescendingButton.Enabled = false;
106        removeButton.Enabled = false;
107        itemsListView.Enabled = false;
108        detailsGroupBox.Enabled = false;
109      } else {
110        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
111        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
112        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
113        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
114        itemsListView.Enabled = true;
115        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
116      }
117    }
118
119    protected virtual T CreateItem() {
120      if (typeSelectorDialog == null) {
121        typeSelectorDialog = new TypeSelectorDialog();
122        typeSelectorDialog.Caption = "Select Item";
123        typeSelectorDialog.TypeSelector.Caption = "Available Items";
124        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
125      }
126
127      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
128        try {
129          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
130        } catch (Exception ex) {
131          ErrorHandling.ShowErrorDialog(this, ex);
132        }
133      }
134      return null;
135    }
136    protected virtual ListViewItem CreateListViewItem(T item) {
137      ListViewItem listViewItem = new ListViewItem();
138      if (item == null) {
139        listViewItem.Text = "null";
140        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
141        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
142      } else {
143        listViewItem.Text = item.ToString();
144        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
145        itemsListView.SmallImageList.Images.Add(item.ItemImage);
146        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
147        listViewItem.Tag = item;
148      }
149      return listViewItem;
150    }
151    protected virtual void AddListViewItem(ListViewItem listViewItem) {
152      T item = (listViewItem.Tag as T);
153      itemsListView.Items.Add(listViewItem);
154      if (item != null) {
155        if (!itemListViewItemMapping.ContainsKey(item)) {
156          RegisterItemEvents(item);
157          itemListViewItemMapping.Add(item, new List<ListViewItem>());
158        }
159        itemListViewItemMapping[item].Add(listViewItem);
160      }
161      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
162      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
163    }
164    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
165      T item = (listViewItem.Tag as T);
166      if (item != null) {
167        itemListViewItemMapping[item].Remove(listViewItem);
168        if (itemListViewItemMapping[item].Count == 0) {
169          itemListViewItemMapping.Remove(item);
170          DeregisterItemEvents(item);
171        }
172      }
173      listViewItem.Remove();
174      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
175      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
176    }
177    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
178      T item = listViewItem.Tag as T;
179      int i = listViewItem.ImageIndex;
180      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
181      listViewItem.ImageIndex = -1;
182      listViewItem.ImageIndex = i;
183    }
184    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
185      T item = listViewItem.Tag as T;
186      listViewItem.Text = item == null ? "null" : item.ToString();
187      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
188    }
189    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
190      if (item == null) {
191        List<ListViewItem> listViewItems = new List<ListViewItem>();
192        foreach (ListViewItem listViewItem in itemsListView.Items) {
193          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
194        }
195        return listViewItems;
196      } else {
197        return itemListViewItemMapping[item];
198      }
199    }
200
201    #region ListView Events
202    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
203      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
204      AdjustListViewColumnSizes();
205      if (showDetailsCheckBox.Checked) {
206        if (itemsListView.SelectedItems.Count == 1) {
207          T item = (T)itemsListView.SelectedItems[0].Tag;
208          detailsGroupBox.Enabled = true;
209          viewHost.Content = item;
210        } else {
211          viewHost.Content = null;
212          detailsGroupBox.Enabled = false;
213        }
214      }
215    }
216    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
217      if (e.KeyCode == Keys.Delete) {
218        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
219          foreach (ListViewItem item in itemsListView.SelectedItems)
220            Content.Remove((T)item.Tag);
221        }
222      }
223    }
224    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
225      if (itemsListView.SelectedItems.Count == 1) {
226        T item = itemsListView.SelectedItems[0].Tag as T;
227        if (item != null) {
228          IContentView view = MainFormManager.MainForm.ShowContent(item);
229          if (view != null) {
230            view.ReadOnly = ReadOnly;
231            view.Locked = Locked;
232          }
233        }
234      }
235    }
236    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
237      if (!Locked) {
238        ListViewItem listViewItem = (ListViewItem)e.Item;
239        T item = listViewItem.Tag as T;
240        if (item != null) {
241          DataObject data = new DataObject();
242          data.SetData("Type", item.GetType());
243          data.SetData("Value", item);
244          if (Content.IsReadOnly || ReadOnly) {
245            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
246          } else {
247            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
248            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
249              Content.Remove(item);
250          }
251        }
252      }
253    }
254    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
255      e.Effect = DragDropEffects.None;
256      Type type = e.Data.GetData("Type") as Type;
257      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
258        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
259        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
260        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
261        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
262        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
263      }
264    }
265    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
266      if (e.Effect != DragDropEffects.None) {
267        T item = e.Data.GetData("Value") as T;
268        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
269        Content.Add(item);
270      }
271    }
272    #endregion
273
274    #region Button Events
275    protected virtual void addButton_Click(object sender, EventArgs e) {
276      T item = CreateItem();
277      if (item != null)
278        Content.Add(item);
279    }
280    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
281      SortItemsListView(SortOrder.Ascending);
282    }
283    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
284      SortItemsListView(SortOrder.Descending);
285    }
286    protected virtual void removeButton_Click(object sender, EventArgs e) {
287      if (itemsListView.SelectedItems.Count > 0) {
288        foreach (ListViewItem item in itemsListView.SelectedItems)
289          Content.Remove((T)item.Tag);
290        itemsListView.SelectedItems.Clear();
291      }
292    }
293    #endregion
294
295    #region CheckBox Events
296    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
297      if (showDetailsCheckBox.Checked) {
298        splitContainer.Panel2Collapsed = false;
299        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
300        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
301      } else {
302        splitContainer.Panel2Collapsed = true;
303        viewHost.Content = null;
304      }
305    }
306    #endregion
307
308    #region Content Events
309    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
310      if (InvokeRequired)
311        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
312      else {
313        foreach (T item in e.Items)
314          AddListViewItem(CreateListViewItem(item));
315        AdjustListViewColumnSizes();
316      }
317
318    }
319    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
320      if (InvokeRequired)
321        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
322      else {
323        foreach (T item in e.Items) {
324          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
325          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
326          if (listviewItem != null) RemoveListViewItem(listviewItem);
327        }
328        RebuildImageList();
329      }
330    }
331    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
332      if (InvokeRequired)
333        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
334      else {
335        foreach (T item in e.OldItems) {
336          //remove only the first matching ListViewItem, because the IItem could be contained multiple times in the ItemCollection
337          ListViewItem listviewItem = GetListViewItemsForItem(item).FirstOrDefault();
338          if (listviewItem != null) RemoveListViewItem(listviewItem);
339        }
340        RebuildImageList();
341        foreach (T item in e.Items)
342          AddListViewItem(CreateListViewItem(item));
343      }
344    }
345    #endregion
346
347    #region Item Events
348    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
349      if (InvokeRequired)
350        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
351      else {
352        T item = (T)sender;
353        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
354          UpdateListViewItemImage(listViewItem);
355      }
356    }
357    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
358      if (InvokeRequired)
359        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
360      else {
361        T item = (T)sender;
362        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
363          UpdateListViewItemText(listViewItem);
364      }
365    }
366    #endregion
367
368    #region Helpers
369    protected virtual void SortItemsListView(SortOrder sortOrder) {
370      itemsListView.Sorting = SortOrder.None;
371      itemsListView.Sorting = sortOrder;
372      itemsListView.Sorting = SortOrder.None;
373    }
374    protected virtual void AdjustListViewColumnSizes() {
375      if (itemsListView.Items.Count > 0) {
376        for (int i = 0; i < itemsListView.Columns.Count; i++)
377          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
378      }
379    }
380    protected virtual void RebuildImageList() {
381      itemsListView.SmallImageList.Images.Clear();
382      foreach (ListViewItem listViewItem in itemsListView.Items) {
383        T item = listViewItem.Tag as T;
384        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
385        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
386      }
387    }
388    #endregion
389  }
390}
Note: See TracBrowser for help on using the repository browser.