Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5711 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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