Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • finished implemented ideas which came up during yesterday's presentation of HeuristicLab.Core and related plugins
File size: 11.3 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    protected virtual 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 & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
181        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
182        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
183        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
184      }
185    }
186    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
187      if (e.Effect != DragDropEffects.None) {
188        T item = e.Data.GetData("Value") as T;
189        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
190        ItemCollection.Add(item);
191      }
192    }
193    #endregion
194
195    #region Button Events
196    protected virtual void addButton_Click(object sender, EventArgs e) {
197      T item = CreateItem();
198      if (item != null)
199        ItemCollection.Add(item);
200    }
201    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
202      SortItemsListView(SortOrder.Ascending);
203    }
204    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
205      SortItemsListView(SortOrder.Descending);
206    }
207    protected virtual void removeButton_Click(object sender, EventArgs e) {
208      if (itemsListView.SelectedItems.Count > 0) {
209        foreach (ListViewItem item in itemsListView.SelectedItems)
210          ItemCollection.Remove((T)item.Tag);
211        itemsListView.SelectedItems.Clear();
212      }
213    }
214    #endregion
215
216    #region ItemCollection Events
217    protected virtual void ItemCollection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
218      if (InvokeRequired)
219        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsAdded), sender, e);
220      else
221        foreach (T item in e.Items)
222          AddListViewItem(CreateListViewItem(item));
223    }
224    protected virtual void ItemCollection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
225      if (InvokeRequired)
226        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_ItemsRemoved), sender, e);
227      else {
228        foreach (T item in e.Items) {
229          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
230            RemoveListViewItem(listViewItem);
231            break;
232          }
233        }
234      }
235    }
236    protected virtual void ItemCollection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
237      if (InvokeRequired)
238        Invoke(new CollectionItemsChangedEventHandler<T>(ItemCollection_CollectionReset), sender, e);
239      else {
240        foreach (T item in e.OldItems) {
241          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
242            RemoveListViewItem(listViewItem);
243            break;
244          }
245        }
246        foreach (T item in e.Items)
247          AddListViewItem(CreateListViewItem(item));
248      }
249    }
250    #endregion
251
252    #region Item Events
253    protected virtual void Item_Changed(object sender, ChangedEventArgs e) {
254      if (InvokeRequired)
255        Invoke(new ChangedEventHandler(Item_Changed), sender, e);
256      else {
257        T item = (T)sender;
258        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
259          UpdateListViewItem(listViewItem);
260      }
261    }
262    #endregion
263
264    #region Helpers
265    protected virtual void SortItemsListView(SortOrder sortOrder) {
266      itemsListView.Sorting = sortOrder;
267      itemsListView.Sort();
268      itemsListView.Sorting = SortOrder.None;
269    }
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.