Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2664 was 2664, checked in by swagner, 15 years ago

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 10.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Collections;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Core.Views {
34  public partial class ItemCollectionView<T> : ObjectView where T : class, IItem {
35    public IObservableCollection<T> ItemCollection {
36      get { return (IObservableCollection<T>)Object; }
37      set { base.Object = value; }
38    }
39
40    public ListView ItemsListView {
41      get { return itemsListView; }
42    }
43
44    public ItemCollectionView() {
45      InitializeComponent();
46      Caption = "Item Collection";
47    }
48
49    protected override void DeregisterObjectEvents() {
50      ItemCollection.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsAdded);
51      ItemCollection.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsRemoved);
52      ItemCollection.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ItemCollection_CollectionReset);
53      base.DeregisterObjectEvents();
54    }
55    protected override void RegisterObjectEvents() {
56      base.RegisterObjectEvents();
57      ItemCollection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsAdded);
58      ItemCollection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsRemoved);
59      ItemCollection.CollectionReset += new CollectionItemsChangedEventHandler<T>(ItemCollection_CollectionReset);
60    }
61
62    protected override void OnObjectChanged() {
63      base.OnObjectChanged();
64      Caption = "Item Collection";
65      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
66      itemsListView.Enabled = false;
67      detailsGroupBox.Enabled = false;
68      viewHost.Object = null;
69      addButton.Enabled = false;
70      sortAscendingButton.Enabled = false;
71      sortDescendingButton.Enabled = false;
72      removeButton.Enabled = false;
73
74      if (ItemCollection != null) {
75        Caption += " (" + ItemCollection.GetType().Name + ")";
76        itemsListView.Enabled = true;
77        addButton.Enabled = !ItemCollection.IsReadOnly;
78        foreach (T item in ItemCollection)
79          AddListViewItem(CreateListViewItem(item));
80        sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
81        sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
82        SortItemsListView(SortOrder.Ascending);
83      }
84    }
85
86    protected virtual T CreateItem() {
87      try {
88        return (T)Activator.CreateInstance(typeof(T));
89      } catch(Exception ex) {
90        Auxiliary.ShowErrorMessageBox(ex);
91        return null;
92      }
93    }
94    protected virtual ListViewItem CreateListViewItem(T item) {
95      if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
96        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
97
98      ListViewItem listViewItem = new ListViewItem();
99      listViewItem.Text = item.ToString();
100      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
101      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
102      listViewItem.Tag = item;
103      return listViewItem;
104    }
105    protected virtual void AddListViewItem(ListViewItem listViewItem) {
106      itemsListView.Items.Add(listViewItem);
107      ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
108      sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
109      sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
110    }
111    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
112      ((T)listViewItem.Tag).Changed -= new ChangedEventHandler(Item_Changed);
113      listViewItem.Remove();
114      sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
115      sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
116    }
117    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
118      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString())) {
119        listViewItem.Text = listViewItem.Tag.ToString();
120        itemsListView.Sort();
121      }
122    }
123    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
124      foreach (ListViewItem listViewItem in itemsListView.Items) {
125        if (((T)listViewItem.Tag) == item)
126          yield return listViewItem;
127      }
128    }
129
130    #region ListView Events
131    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
132      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !ItemCollection.IsReadOnly;
133      if (itemsListView.SelectedItems.Count == 1) {
134        T item = (T)itemsListView.SelectedItems[0].Tag;
135        viewHost.Object = item;
136        detailsGroupBox.Enabled = true;
137      } else {
138        viewHost.Object = null;
139        detailsGroupBox.Enabled = false;
140      }
141    }
142    private void itemsListView_SizeChanged(object sender, EventArgs e) {
143      if (itemsListView.Columns.Count > 0)
144        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
145    }
146    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
147      if (e.KeyCode == Keys.Delete) {
148        if ((itemsListView.SelectedItems.Count > 0) && !ItemCollection.IsReadOnly) {
149          foreach (ListViewItem item in itemsListView.SelectedItems)
150            ItemCollection.Remove((T)item.Tag);
151        }
152      }
153    }
154    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
155      if (itemsListView.SelectedItems.Count == 1) {
156        T item = (T)itemsListView.SelectedItems[0].Tag;
157        IView view = MainFormManager.CreateDefaultView(item);
158        if (view != null) MainFormManager.MainForm.ShowView(view);
159      }
160    }
161    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
162      ListViewItem listViewItem = (ListViewItem)e.Item;
163      T item = (T)listViewItem.Tag;
164      DataObject data = new DataObject();
165      data.SetData("Type", item.GetType());
166      data.SetData("Value", item);
167      if (ItemCollection.IsReadOnly) {
168        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
169      } else {
170        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
171        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
172          ItemCollection.Remove(item);
173      }
174    }
175    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
176      e.Effect = DragDropEffects.None;
177      Type type = e.Data.GetData("Type") as Type;
178      if ((!ItemCollection.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
179        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
180        else if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Move;  // ALT key
181        else e.Effect = DragDropEffects.Link;
182      }
183    }
184    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
185      if (e.Effect != DragDropEffects.None) {
186        T item = e.Data.GetData("Value") as T;
187        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
188        ItemCollection.Add(item);
189      }
190    }
191    #endregion
192
193    #region Button Events
194    protected virtual void addButton_Click(object sender, EventArgs e) {
195      T item = CreateItem();
196      if (item != null)
197        ItemCollection.Add(item);
198    }
199    protected void sortAscendingButton_Click(object sender, EventArgs e) {
200      SortItemsListView(SortOrder.Ascending);
201    }
202    protected void sortDescendingButton_Click(object sender, EventArgs e) {
203      SortItemsListView(SortOrder.Descending);
204    }
205    protected virtual void removeButton_Click(object sender, EventArgs e) {
206      if (itemsListView.SelectedItems.Count > 0) {
207        foreach (ListViewItem item in itemsListView.SelectedItems)
208          ItemCollection.Remove((T)item.Tag);
209        itemsListView.SelectedItems.Clear();
210      }
211    }
212    #endregion
213
214    #region ItemCollection Events
215    private void ItemCollection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
216      if (InvokeRequired)
217        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsAdded), sender, e);
218      else
219        foreach (T item in e.Items)
220          AddListViewItem(CreateListViewItem(item));
221    }
222    private void ItemCollection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
223      if (InvokeRequired)
224        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsRemoved), sender, e);
225      else {
226        foreach (T item in e.Items) {
227          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
228            RemoveListViewItem(listViewItem);
229            break;
230          }
231        }
232      }
233    }
234    private void ItemCollection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
235      if (InvokeRequired)
236        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_CollectionReset), sender, e);
237      else {
238        foreach (T item in e.OldItems) {
239          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
240            RemoveListViewItem(listViewItem);
241            break;
242          }
243        }
244        foreach (T item in e.Items)
245          AddListViewItem(CreateListViewItem(item));
246      }
247    }
248    #endregion
249
250    #region Item Events
251    private void Item_Changed(object sender, ChangedEventArgs e) {
252      if (InvokeRequired)
253        Invoke(new ChangedEventHandler(Item_Changed), sender, e);
254      else {
255        T item = (T)sender;
256        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
257          UpdateListViewItem(listViewItem);
258      }
259    }
260    #endregion
261
262    #region Helpers
263    private void SortItemsListView(SortOrder sortOrder) {
264      itemsListView.Sorting = sortOrder;
265      itemsListView.Sort();
266      itemsListView.Sorting = SortOrder.None;
267    }
268    #endregion
269  }
270}
Note: See TracBrowser for help on using the repository browser.