Free cookie consent management tool by TermsFeed Policy Generator

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

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

Fixed bug which was introduced in r3306 (#893).

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