Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3317 was 3317, checked in by swagner, 12 years ago

Implemented ReadOnlyView property for items (#969).

File size: 12.2 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      SetListViewItemImage(listViewItem);
105      return listViewItem;
106    }
107    protected virtual void AddListViewItem(ListViewItem listViewItem) {
108      itemsListView.Items.Add(listViewItem);
109      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
110      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
111      sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
112      sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
113    }
114    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
115      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
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      if (itemsListView.SmallImageList.Images[listViewItem.ImageIndex] != ((T)listViewItem.Tag).ItemImage)
125        SetListViewItemImage(listViewItem);
126    }
127    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
128      foreach (ListViewItem listViewItem in itemsListView.Items) {
129        if (((T)listViewItem.Tag) == item)
130          yield return listViewItem;
131      }
132    }
133
134    #region ListView Events
135    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
136      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
137      if (itemsListView.SelectedItems.Count == 1) {
138        T item = (T)itemsListView.SelectedItems[0].Tag;
139        detailsGroupBox.Enabled = true;
140        viewHost.ViewType = null;
141        viewHost.Content = item;
142      } else {
143        viewHost.Content = null;
144        detailsGroupBox.Enabled = false;
145      }
146    }
147    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
148      if (itemsListView.Columns.Count > 0)
149        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
150    }
151    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
152      if (e.KeyCode == Keys.Delete) {
153        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
154          foreach (ListViewItem item in itemsListView.SelectedItems)
155            Content.Remove((T)item.Tag);
156        }
157      }
158    }
159    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
160      if (itemsListView.SelectedItems.Count == 1) {
161        T item = (T)itemsListView.SelectedItems[0].Tag;
162        IView view = MainFormManager.CreateDefaultView(item);
163        if (view != null) view.Show();
164      }
165    }
166    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
167      ListViewItem listViewItem = (ListViewItem)e.Item;
168      T item = (T)listViewItem.Tag;
169      DataObject data = new DataObject();
170      data.SetData("Type", item.GetType());
171      data.SetData("Value", item);
172      if (Content.IsReadOnly) {
173        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
174      } else {
175        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
176        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
177          Content.Remove(item);
178      }
179    }
180    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
181      e.Effect = DragDropEffects.None;
182      Type type = e.Data.GetData("Type") as Type;
183      if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
184        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
185        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
186        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
187        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
188        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
189      }
190    }
191    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
192      if (e.Effect != DragDropEffects.None) {
193        T item = e.Data.GetData("Value") as T;
194        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
195        Content.Add(item);
196      }
197    }
198    #endregion
199
200    #region Button Events
201    protected virtual void addButton_Click(object sender, EventArgs e) {
202      T item = CreateItem();
203      if (item != null)
204        Content.Add(item);
205    }
206    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
207      SortItemsListView(SortOrder.Ascending);
208    }
209    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
210      SortItemsListView(SortOrder.Descending);
211    }
212    protected virtual void removeButton_Click(object sender, EventArgs e) {
213      if (itemsListView.SelectedItems.Count > 0) {
214        foreach (ListViewItem item in itemsListView.SelectedItems)
215          Content.Remove((T)item.Tag);
216        itemsListView.SelectedItems.Clear();
217      }
218    }
219    #endregion
220
221    #region Content Events
222    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
223      if (InvokeRequired)
224        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
225      else
226        foreach (T item in e.Items)
227          AddListViewItem(CreateListViewItem(item));
228    }
229    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
230      if (InvokeRequired)
231        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
232      else {
233        foreach (T item in e.Items) {
234          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
235            RemoveListViewItem(listViewItem);
236            break;
237          }
238        }
239      }
240    }
241    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
242      if (InvokeRequired)
243        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
244      else {
245        foreach (T item in e.OldItems) {
246          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
247            RemoveListViewItem(listViewItem);
248            break;
249          }
250        }
251        foreach (T item in e.Items)
252          AddListViewItem(CreateListViewItem(item));
253      }
254    }
255    #endregion
256
257    #region Item Events
258    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
259      if (InvokeRequired)
260        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
261      else {
262        T item = (T)sender;
263        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
264          UpdateListViewItem(listViewItem);
265      }
266    }
267    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
268      if (InvokeRequired)
269        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
270      else {
271        T item = (T)sender;
272        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
273          UpdateListViewItem(listViewItem);
274      }
275    }
276    #endregion
277
278    #region Helpers
279    protected virtual void SetListViewItemImage(ListViewItem listViewItem) {
280      T item = (T)listViewItem.Tag;
281      int i = 0;
282      while ((i < itemsListView.SmallImageList.Images.Count) && !item.ItemImage.Equals(itemsListView.SmallImageList.Images[i]))
283        i++;
284      if (i == itemsListView.SmallImageList.Images.Count) itemsListView.SmallImageList.Images.Add(item.ItemImage);
285      listViewItem.ImageIndex = i;
286    }
287    protected virtual void SortItemsListView(SortOrder sortOrder) {
288      itemsListView.Sorting = SortOrder.None;
289      itemsListView.Sorting = sortOrder;
290      itemsListView.Sorting = SortOrder.None;
291    }
292    #endregion
293  }
294}
Note: See TracBrowser for help on using the repository browser.