Free cookie consent management tool by TermsFeed Policy Generator

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

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

Restricted types of child operators in MultiOperator (#979)

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> : AsynchronousContentView 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    public ItemCollectionView(IItemCollection<T> content)
51      : this() {
52      Content = content;
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
57      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
58      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
59      base.DeregisterContentEvents();
60    }
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
64      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
65      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      Caption = "Item Collection";
71      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
72      viewHost.Content = null;
73      if (Content != null) {
74        Caption += " (" + Content.GetType().Name + ")";
75        foreach (T item in Content)
76          AddListViewItem(CreateListViewItem(item));
77        SortItemsListView(SortOrder.Ascending);
78      }
79      SetEnabledStateOfControls();
80    }
81
82    protected override void OnReadOnlyChanged() {
83      base.OnReadOnlyChanged();
84      SetEnabledStateOfControls();
85    }
86
87    private void SetEnabledStateOfControls() {
88      if (Content == null) {
89        addButton.Enabled = false;
90        sortAscendingButton.Enabled = false;
91        sortDescendingButton.Enabled = false;
92        removeButton.Enabled = false;
93        itemsListView.Enabled = false;
94        detailsGroupBox.Enabled = false;
95      } else {
96        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
97        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
98        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
99        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
100        itemsListView.Enabled = true;
101        detailsGroupBox.Enabled = true;
102        viewHost.ReadOnly = ReadOnly;
103      }
104    }
105
106    protected virtual T CreateItem() {
107      if (typeSelectorDialog == null) {
108        typeSelectorDialog = new TypeSelectorDialog();
109        typeSelectorDialog.Caption = "Select Item";
110        typeSelectorDialog.TypeSelector.Caption = "Available Items";
111        typeSelectorDialog.TypeSelector.Configure(typeof(T), false, false);
112      }
113
114      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
115        try {
116          return (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
117        }
118        catch (Exception ex) {
119          Auxiliary.ShowErrorMessageBox(ex);
120        }
121      }
122      return null;
123    }
124    protected virtual ListViewItem CreateListViewItem(T item) {
125      ListViewItem listViewItem = new ListViewItem();
126      listViewItem.Text = item.ToString();
127      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
128      listViewItem.Tag = item;
129      itemsListView.SmallImageList.Images.Add(item.ItemImage);
130      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
131      return listViewItem;
132    }
133    protected virtual void AddListViewItem(ListViewItem listViewItem) {
134      itemsListView.Items.Add(listViewItem);
135      ((T)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
136      ((T)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
137      AdjustListViewColumnSizes();
138      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
139      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
140    }
141    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
142      ((T)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
143      ((T)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
144      listViewItem.Remove();
145      foreach (ListViewItem other in itemsListView.Items)
146        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
147      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
148      sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
149      sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
150    }
151    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
152      int i = listViewItem.ImageIndex;
153      listViewItem.ImageList.Images[i] = ((T)listViewItem.Tag).ItemImage;
154      listViewItem.ImageIndex = -1;
155      listViewItem.ImageIndex = i;
156    }
157    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
158      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
159        listViewItem.Text = listViewItem.Tag.ToString();
160    }
161    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
162      foreach (ListViewItem listViewItem in itemsListView.Items) {
163        if (((T)listViewItem.Tag) == item)
164          yield return listViewItem;
165      }
166    }
167
168    #region ListView Events
169    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
170      removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
171      if (itemsListView.SelectedItems.Count == 1) {
172        T item = (T)itemsListView.SelectedItems[0].Tag;
173        detailsGroupBox.Enabled = true;
174        viewHost.ViewType = null;
175        viewHost.Content = item;
176      } else {
177        viewHost.Content = null;
178        detailsGroupBox.Enabled = false;
179      }
180    }
181    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
182      if (e.KeyCode == Keys.Delete) {
183        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
184          foreach (ListViewItem item in itemsListView.SelectedItems)
185            Content.Remove((T)item.Tag);
186        }
187      }
188    }
189    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
190      if (itemsListView.SelectedItems.Count == 1) {
191        T item = (T)itemsListView.SelectedItems[0].Tag;
192        IView view = MainFormManager.CreateDefaultView(item, ReadOnly);
193        if (view != null) view.Show();
194      }
195    }
196    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
197      ListViewItem listViewItem = (ListViewItem)e.Item;
198      T item = (T)listViewItem.Tag;
199      DataObject data = new DataObject();
200      data.SetData("Type", item.GetType());
201      data.SetData("Value", item);
202      if (Content.IsReadOnly || ReadOnly) {
203        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
204      } else {
205        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
206        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
207          Content.Remove(item);
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 & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
215        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
216        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
217        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
218        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
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.