Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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