Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3694 was 3694, checked in by swagner, 14 years ago

Implemented reviewers' comments (#893)

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