Free cookie consent management tool by TermsFeed Policy Generator

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

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

Enabled hiding details in all collection views (#1095)

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