Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 19.7 KB
RevLine 
[2746]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2746]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;
[5302]25using System.Linq;
[2746]26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
[3758]30using HeuristicLab.PluginInfrastructure;
[2746]31
32namespace HeuristicLab.Core.Views {
[2917]33  [View("ItemArray View")]
[2746]34  [Content(typeof(ItemArray<>), true)]
[3393]35  [Content(typeof(IItemArray<>), false)]
[3483]36  public partial class ItemArrayView<T> : ItemView where T : class, IItem {
[5237]37    protected Dictionary<T, List<ListViewItem>> itemListViewItemMapping;
[3407]38    protected TypeSelectorDialog typeSelectorDialog;
39
[3393]40    public new IItemArray<T> Content {
41      get { return (IItemArray<T>)base.Content; }
[2746]42      set { base.Content = value; }
43    }
44
45    public ListView ItemsListView {
46      get { return itemsListView; }
47    }
48
49    public ItemArrayView() {
50      InitializeComponent();
[5237]51      itemListViewItemMapping = new Dictionary<T, List<ListViewItem>>();
[2746]52    }
53
[5237]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
[2746]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);
[5237]66      foreach (T item in itemListViewItemMapping.Keys) {
67        DeregisterItemEvents(item);
[5070]68      }
[2746]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    }
[5237]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    }
[2746]85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
[3775]88
89      int selectedIndex = -1;
90      if (itemsListView.SelectedItems.Count == 1) selectedIndex = itemsListView.SelectedIndices[0];
91
[5237]92      itemsListView.Items.Clear();
93      itemListViewItemMapping.Clear();
94      RebuildImageList();
[2746]95      viewHost.Content = null;
96      if (Content != null) {
97        Caption += " (" + Content.GetType().Name + ")";
98        foreach (T item in Content)
99          AddListViewItem(CreateListViewItem(item));
[4240]100        AdjustListViewColumnSizes();
[3775]101        if ((selectedIndex != -1) && (selectedIndex < itemsListView.Items.Count))
102          itemsListView.Items[selectedIndex].Selected = true;
[2746]103      }
104    }
105
[3904]106    protected override void SetEnabledStateOfControls() {
107      base.SetEnabledStateOfControls();
[3362]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 &&
[3435]117                            !Content.IsReadOnly && !ReadOnly;
[3362]118        moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
119                               itemsListView.SelectedIndices[0] != 0 &&
[3435]120                               !Content.IsReadOnly && !ReadOnly;
[3362]121        moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
122                                 itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3435]123                                 !Content.IsReadOnly && !ReadOnly;
[3362]124        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 &&
[3435]125                               !Content.IsReadOnly && !ReadOnly;
[3362]126        itemsListView.Enabled = true;
[4099]127        detailsGroupBox.Enabled = itemsListView.SelectedItems.Count == 1;
[3362]128      }
129    }
130
[2746]131    protected virtual T CreateItem() {
[3407]132      if (typeSelectorDialog == null) {
133        typeSelectorDialog = new TypeSelectorDialog();
134        typeSelectorDialog.Caption = "Select Item";
135        typeSelectorDialog.TypeSelector.Caption = "Available Items";
[3588]136        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, true);
[2746]137      }
[3407]138
139      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
140        try {
141          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
[5302]142        }
143        catch (Exception ex) {
[3758]144          ErrorHandling.ShowErrorDialog(this, ex);
[3407]145        }
[2746]146      }
[3407]147      return null;
[2746]148    }
149    protected virtual ListViewItem CreateListViewItem(T item) {
[3341]150      ListViewItem listViewItem = new ListViewItem();
[5237]151      if (item == null) {
152        listViewItem.Text = "null";
[5287]153        itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
[5237]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      }
[3341]162      return listViewItem;
[2746]163    }
164    protected virtual void AddListViewItem(ListViewItem listViewItem) {
[5371]165      if (listViewItem == null) throw new ArgumentNullException();
[5237]166      T item = (listViewItem.Tag as T);
[2746]167      itemsListView.Items.Add(listViewItem);
[5237]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);
[3341]174      }
[2746]175    }
176    protected virtual void InsertListViewItem(int index, ListViewItem listViewItem) {
[5371]177      if (listViewItem == null) throw new ArgumentNullException();
[5237]178      T item = (listViewItem.Tag as T);
[2746]179      itemsListView.Items.Insert(index, listViewItem);
[5237]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);
[3341]186      }
[2746]187    }
188    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[5371]189      if (listViewItem == null) throw new ArgumentNullException();
[5237]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        }
[3341]197      }
[2746]198      listViewItem.Remove();
199    }
[3341]200    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
[5371]201      if (listViewItem == null) throw new ArgumentNullException();
[2746]202      T item = listViewItem.Tag as T;
[3341]203      int i = listViewItem.ImageIndex;
[5287]204      listViewItem.ImageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
[3341]205      listViewItem.ImageIndex = -1;
206      listViewItem.ImageIndex = i;
207    }
208    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
[5371]209      if (listViewItem == null) throw new ArgumentNullException();
[3341]210      T item = listViewItem.Tag as T;
[2746]211      listViewItem.Text = item == null ? "null" : item.ToString();
212      listViewItem.ToolTipText = item == null ? string.Empty : item.ItemName + ": " + item.ItemDescription;
213    }
[5237]214    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
[5238]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 {
[5302]222        List<ListViewItem> listViewItems = null;
223        itemListViewItemMapping.TryGetValue(item, out listViewItems);
224        return listViewItems == null ? Enumerable.Empty<ListViewItem>() : listViewItems;
[5238]225      }
[5237]226    }
[2746]227
[3362]228    #region ListView Events
[2746]229    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]230      addButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2746]231      moveUpButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
232                             itemsListView.SelectedIndices[0] != 0 &&
[3456]233                             (Content != null) && !Content.IsReadOnly && !ReadOnly;
[2746]234      moveDownButton.Enabled = itemsListView.SelectedItems.Count == 1 &&
235                               itemsListView.SelectedIndices[0] != itemsListView.Items.Count - 1 &&
[3456]236                               (Content != null) && !Content.IsReadOnly && !ReadOnly;
237      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3829]238      AdjustListViewColumnSizes();
[2746]239
[4096]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        }
[2746]249      }
250    }
251
252    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
253      if (e.KeyCode == Keys.Delete) {
[3435]254        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[2746]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) {
[3557]265          IContentView view = MainFormManager.MainForm.ShowContent(item);
[3416]266          if (view != null) {
267            view.ReadOnly = ReadOnly;
[3423]268            view.Locked = Locked;
[3416]269          }
[2746]270        }
271      }
272    }
273
274    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]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);
[3435]282          if (Content.IsReadOnly || ReadOnly) {
[3432]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          }
[2746]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;
[3435]295      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
[2746]296        Point p = itemsListView.PointToClient(new Point(e.X, e.Y));
297        ListViewItem listViewItem = itemsListView.GetItemAt(p.X, p.Y);
298        if (listViewItem != null) {
[3694]299          if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2746]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;
[3526]303          else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[2746]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
[3341]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    }
[2746]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    }
[3341]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    }
[2746]355    #endregion
356
[4096]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
[2746]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);
[5237]383        RebuildImageList();
[2746]384
385        foreach (IndexedItem<T> item in e.Items)
386          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]387        AdjustListViewColumnSizes();
[2746]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;
[3341]400          UpdateListViewItemImage(listViewItem);
401          UpdateListViewItemText(listViewItem);
[2746]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);
[5237]414        RebuildImageList();
[2746]415
416        foreach (IndexedItem<T> item in e.Items)
417          InsertListViewItem(item.Index, CreateListViewItem(item.Value));
[4240]418        AdjustListViewColumnSizes();
[2746]419      }
420    }
421    #endregion
422
423    #region Item Events
[3341]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;
[5237]429        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
430          UpdateListViewItemImage(listViewItem);
[3341]431      }
432    }
[2932]433    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
[2746]434      if (InvokeRequired)
[2932]435        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
[2746]436      else {
437        T item = (T)sender;
[5237]438        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
439          UpdateListViewItemText(listViewItem);
[2746]440      }
441    }
442    #endregion
[3362]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    }
[5239]451    protected virtual void RebuildImageList() {
[5237]452      itemsListView.SmallImageList.Images.Clear();
[5239]453      foreach (ListViewItem listViewItem in itemsListView.Items) {
454        T item = listViewItem.Tag as T;
[5287]455        itemsListView.SmallImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
[5239]456        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[5237]457      }
458    }
[3362]459    #endregion
[2746]460  }
461}
Note: See TracBrowser for help on using the repository browser.