Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5264 was 5239, checked in by swagner, 14 years ago

Adapted method RebuildImageList to work with null items (#1324)

File size: 19.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.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Collections;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Core.Views {
32  [View("ItemArray View")]
33  [Content(typeof(ItemArray<>), true)]
34  [Content(typeof(IItemArray<>), false)]
35  public partial class ItemArrayView<T> : ItemView where T : class, IItem {
36    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
37    protected TypeSelectorDialog typeSelectorDialog;
38
39    public new IItemArray<T> Content {
40      get { return (IItemArray<T>)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public ListView ItemsListView {
45      get { return itemsListView; }
46    }
47
48    public ItemArrayView() {
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.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
63      Content.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
64      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<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.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced);
73      Content.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved);
74      Content.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<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
88      int selectedIndex = -1;
89      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
90
91      itemsListView.Items.Clear();
92      itemListViewItemMapping.Clear();
93      RebuildImageList();
94      viewHost.Content = null;
95      if (Content != null) {
96        Caption += " (" + Content.GetType().Name + ")";
97        foreach (T item in Content)
98          AddListViewItem(CreateListViewItem(item));
99        AdjustListViewColumnSizes();
100        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
101          itemsListView.Items[selectedIndex].Selected = true;
102      }
103    }
104
105    protected override void SetEnabledStateOfControls() {
106      base.SetEnabledStateOfControls();
107      if (Content == null) {
108        addButton.Enabled = false;
109        moveUpButton.Enabled = false;
110        moveDownButton.Enabled = false;
111        removeButton.Enabled = false;
112        itemsListView.Enabled = false;
113        detailsGroupBox.Enabled = false;
114      } else {
115        addButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
116                            !Content.IsReadOnly && !ReadOnly;
117        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
118                               itemsListView.SelectedIndices[0] != 0 &&
119                               !Content.IsReadOnly && !ReadOnly;
120        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
121                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
122                                 !Content.IsReadOnly && !ReadOnly;
123        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
124                               !Content.IsReadOnly && !ReadOnly;
125        itemsListView.Enabled = true;
126        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
127      }
128    }
129
130    protected virtual T CreateItem() {
131      if (typeSelectorDialog == null) {
132        typeSelectorDialog = new TypeSelectorDialog();
133        typeSelectorDialog.Caption = "Select Item";
134        typeSelectorDialog.TypeSelector.Caption = "Available Items";
135        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
136      }
137
138      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
139        try {
140          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
141        }
142        catch (Exception ex) {
143          ErrorHandling.ShowErrorDialog(this, ex);
144        }
145      }
146      return null;
147    }
148    protected virtual ListViewItem CreateListViewItem(T item) {
149      ListViewItem listViewItem = new ListViewItem();
150      if (item == null) {
151        listViewItem.Text = "null";
152        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing);
153        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
154      } else {
155        listViewItem.Text = item.ToString();
156        listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
157        itemsListView.SmallImageList.Images.Add(item.ItemImage);
158        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
159        listViewItem.Tag = item;
160      }
161      return listViewItem;
162    }
163    protected virtual void AddListViewItem(ListViewItem listViewItem) {
164      T item = (listViewItem.Tag as T);
165      itemsListView.Items.Add(listViewItem);
166      if (item != null) {
167        if (!itemListViewItemMapping.ContainsKey(item)) {
168          RegisterItemEvents(item);
169          itemListViewItemMapping.Add(item, new List<ListViewItem>());
170        }
171        itemListViewItemMapping[item].Add(listViewItem);
172      }
173    }
174    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
175      T item = (listViewItem.Tag as T);
176      itemsListView.Items.Insert(index, listViewItem);
177      if (item != null) {
178        if (!itemListViewItemMapping.ContainsKey(item)) {
179          RegisterItemEvents(item);
180          itemListViewItemMapping.Add(item, new List<ListViewItem>());
181        }
182        itemListViewItemMapping[item].Add(listViewItem);
183      }
184    }
185    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
186      T item = (listViewItem.Tag as T);
187      if (item != null) {
188        itemListViewItemMapping[item].Remove(listViewItem);
189        if (itemListViewItemMapping[item].Count == 0) {
190          itemListViewItemMapping.Remove(item);
191          DeregisterItemEvents(item);
192        }
193      }
194      listViewItem.Remove();
195    }
196    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
197      T item = listViewItem.Tag as T;
198      int i = listViewItem.ImageIndex;
199      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : item.ItemImage;
200      listViewItem.ImageIndex = -1;
201      listViewItem.ImageIndex = i;
202    }
203    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
204      T item = listViewItem.Tag as T;
205      listViewItem.Text = item == null ? "null" : item.ToString();
206      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
207    }
208    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
209      if (item == null) {
210        List<ListViewItem> listViewItems = new List<ListViewItem>();
211        foreach (ListViewItem listViewItem in itemsListView.Items) {
212          if (listViewItem.Tag == null) listViewItems.Add(listViewItem);
213        }
214        return listViewItems;
215      } else {
216        return itemListViewItemMapping[item];
217      }
218    }
219
220    #region ListView Events
221    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
222      addButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
223      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
224                             itemsListView.SelectedIndices[0] != 0 &&
225                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
226      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
227                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
228                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
229      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
230      AdjustListViewColumnSizes();
231
232      if (showDetailsCheckBox.Checked) {
233        if (itemsListView.SelectedItems.Count == 1) {
234          T item = itemsListView.SelectedItems[0].Tag as T;
235          detailsGroupBox.Enabled = true;
236          viewHost.Content = item;
237        } else {
238          viewHost.Content = null;
239          detailsGroupBox.Enabled = false;
240        }
241      }
242    }
243
244    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
245      if (e.KeyCode == Keys.Delete) {
246        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
247          foreach (ListViewItem item in itemsListView.SelectedItems)
248            Content[item.Index] = null;
249        }
250      }
251    }
252
253    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
254      if (itemsListView.SelectedItems.Count == 1) {
255        T item = itemsListView.SelectedItems[0].Tag as T;
256        if (item != null) {
257          IContentView view = MainFormManager.MainForm.ShowContent(item);
258          if (view != null) {
259            view.ReadOnly = ReadOnly;
260            view.Locked = Locked;
261          }
262        }
263      }
264    }
265
266    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
267      if (!Locked) {
268        ListViewItem listViewItem = (ListViewItem)e.Item;
269        T item = listViewItem.Tag as T;
270        if (item != null) {
271          DataObject data = new DataObject();
272          data.SetData("Type", item.GetType());
273          data.SetData("Value", item);
274          if (Content.IsReadOnly || ReadOnly) {
275            DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
276          } else {
277            DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
278            if ((result & DragDropEffects.Move) == DragDropEffects.Move)
279              Content[listViewItem.Index] = null;
280          }
281        }
282      }
283    }
284    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
285      e.Effect = DragDropEffects.None;
286      Type type = e.Data.GetData("Type") as Type;
287      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
288        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
289        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
290        if (listViewItem != null) {
291          if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
292          else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
293          else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
294          else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
295          else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
296        }
297      }
298    }
299    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
300      if (e.Effect != DragDropEffects.None) {
301        T item = e.Data.GetData("Value") as T;
302        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
303
304        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
305        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
306        Content[listViewItem.Index] = item;
307      }
308    }
309    #endregion
310
311    #region Button Events
312    protected virtual void addButton_Click(object sender, EventArgs e) {
313      if (itemsListView.SelectedItems.Count > 0) {
314        T item = CreateItem();
315        if (item != null) {
316          foreach (ListViewItem listViewItem in itemsListView.SelectedItems)
317            Content[listViewItem.Index] = item;
318        }
319      }
320    }
321    protected virtual void moveUpButton_Click(object sender, EventArgs e) {
322      if (itemsListView.SelectedItems.Count == 1) {
323        int index = itemsListView.SelectedIndices[0];
324        T item = Content[index - 1];
325        Content[index - 1] = Content[index];
326        itemsListView.Items[index].Selected = false;
327        itemsListView.Items[index - 1].Selected = true;
328        Content[index] = item;
329      }
330    }
331    protected virtual void moveDownButton_Click(object sender, EventArgs e) {
332      if (itemsListView.SelectedItems.Count == 1) {
333        int index = itemsListView.SelectedIndices[0];
334        T item = Content[index + 1];
335        Content[index + 1] = Content[index];
336        itemsListView.Items[index].Selected = false;
337        itemsListView.Items[index + 1].Selected = true;
338        Content[index] = item;
339      }
340    }
341    protected virtual void removeButton_Click(object sender, EventArgs e) {
342      if (itemsListView.SelectedItems.Count > 0) {
343        foreach (ListViewItem item in itemsListView.SelectedItems)
344          Content[item.Index] = null;
345      }
346    }
347    #endregion
348
349    #region CheckBox Events
350    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
351      if (showDetailsCheckBox.Checked) {
352        splitContainer.Panel2Collapsed = false;
353        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
354        viewHost.Content = itemsListView.SelectedItems.Count == 1 ? (T)itemsListView.SelectedItems[0].Tag : null;
355      } else {
356        splitContainer.Panel2Collapsed = true;
357        viewHost.Content = null;
358      }
359    }
360    #endregion
361
362    #region Content Events
363    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
364      if (InvokeRequired)
365        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsReplaced), sender, e);
366      else {
367        int[] selected = new int[itemsListView.SelectedIndices.Count];
368        itemsListView.SelectedIndices.CopyTo(selected, 0);
369
370        List<ListViewItem> listViewItems = new List<ListViewItem>();
371        foreach (IndexedItem<T> item in e.OldItems)
372          listViewItems.Add(itemsListView.Items[item.Index]);
373        foreach (ListViewItem listViewItem in listViewItems)
374          RemoveListViewItem(listViewItem);
375        RebuildImageList();
376
377        foreach (IndexedItem<T> item in e.Items)
378          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
379        AdjustListViewColumnSizes();
380
381        for (int i = 0; i < selected.Length; i++)
382          itemsListView.Items[selected[i]].Selected = true;
383      }
384    }
385    protected virtual void Content_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
386      if (InvokeRequired)
387        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_ItemsMoved), sender, e);
388      else {
389        foreach (IndexedItem<T> item in e.Items) {
390          ListViewItem listViewItem = itemsListView.Items[item.Index];
391          listViewItem.Tag = item.Value;
392          UpdateListViewItemImage(listViewItem);
393          UpdateListViewItemText(listViewItem);
394        }
395      }
396    }
397    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
398      if (InvokeRequired)
399        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<T>>(Content_CollectionReset), sender, e);
400      else {
401        List<ListViewItem> listViewItems = new List<ListViewItem>();
402        foreach (IndexedItem<T> item in e.OldItems)
403          listViewItems.Add(itemsListView.Items[item.Index]);
404        foreach (ListViewItem listViewItem in listViewItems)
405          RemoveListViewItem(listViewItem);
406        RebuildImageList();
407
408        foreach (IndexedItem<T> item in e.Items)
409          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
410        AdjustListViewColumnSizes();
411      }
412    }
413    #endregion
414
415    #region Item Events
416    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
417      if (InvokeRequired)
418        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
419      else {
420        T item = (T)sender;
421        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
422          UpdateListViewItemImage(listViewItem);
423      }
424    }
425    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
426      if (InvokeRequired)
427        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
428      else {
429        T item = (T)sender;
430        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
431          UpdateListViewItemText(listViewItem);
432      }
433    }
434    #endregion
435
436    #region Helpers
437    protected virtual void AdjustListViewColumnSizes() {
438      if (itemsListView.Items.Count > 0) {
439        for (int i = 0; i < itemsListView.Columns.Count; i++)
440          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
441      }
442    }
443    protected virtual void RebuildImageList() {
444      itemsListView.SmallImageList.Images.Clear();
445      foreach (ListViewItem listViewItem in itemsListView.Items) {
446        T item = listViewItem.Tag as T;
447        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : item.ItemImage);
448        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
449      }
450    }
451    #endregion
452  }
453}
Note: See TracBrowser for help on using the repository browser.