Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core.Views/3.3/ItemCollectionView.cs @ 5264

Last change on this file since 5264 was 4300, checked in by mkommend, 14 years ago

Removed calls to the ClearCache method of the ViewHost (ticket #1133).

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