Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 13.1 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 (itemsListView.SelectedItems.Count == 1) {
161        T item = (T)itemsListView.SelectedItems[0].Tag;
162        detailsGroupBox.Enabled = true;
163        viewHost.Content = item;
164      } else {
165        viewHost.Content = null;
166        detailsGroupBox.Enabled = false;
167      }
168    }
169    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
170      if (e.KeyCode == Keys.Delete) {
171        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
172          foreach (ListViewItem item in itemsListView.SelectedItems)
173            Content.Remove((T)item.Tag);
174        }
175      }
176    }
177    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
178      if (itemsListView.SelectedItems.Count == 1) {
179        T item = (T)itemsListView.SelectedItems[0].Tag;
180        IContentView view = MainFormManager.MainForm.ShowContent(item);
181        if (view != null) {
182          view.ReadOnly = ReadOnly;
183          view.Locked = Locked;
184        }
185      }
186    }
187    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
188      if (!Locked) {
189        ListViewItem listViewItem = (ListViewItem)e.Item;
190        T item = (T)listViewItem.Tag;
191        DataObject data = new DataObject();
192        data.SetData("Type", item.GetType());
193        data.SetData("Value", item);
194        if (Content.IsReadOnly || ReadOnly) {
195          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
196        } else {
197          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
198          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
199            Content.Remove(item);
200        }
201      }
202    }
203    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
204      e.Effect = DragDropEffects.None;
205      Type type = e.Data.GetData("Type") as Type;
206      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
207        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
208        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
209        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
210        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
211        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
212      }
213    }
214    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
215      if (e.Effect != DragDropEffects.None) {
216        T item = e.Data.GetData("Value") as T;
217        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
218        Content.Add(item);
219      }
220    }
221    #endregion
222
223    #region Button Events
224    protected virtual void addButton_Click(object sender, EventArgs e) {
225      T item = CreateItem();
226      if (item != null)
227        Content.Add(item);
228    }
229    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
230      SortItemsListView(SortOrder.Ascending);
231    }
232    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
233      SortItemsListView(SortOrder.Descending);
234    }
235    protected virtual void removeButton_Click(object sender, EventArgs e) {
236      if (itemsListView.SelectedItems.Count > 0) {
237        foreach (ListViewItem item in itemsListView.SelectedItems)
238          Content.Remove((T)item.Tag);
239        itemsListView.SelectedItems.Clear();
240      }
241    }
242    #endregion
243
244    #region Content Events
245    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
246      if (InvokeRequired)
247        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
248      else
249        foreach (T item in e.Items)
250          AddListViewItem(CreateListViewItem(item));
251    }
252    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
253      if (InvokeRequired)
254        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
255      else {
256        foreach (T item in e.Items) {
257          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
258            RemoveListViewItem(listViewItem);
259            break;
260          }
261        }
262      }
263    }
264    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
265      if (InvokeRequired)
266        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
267      else {
268        foreach (T item in e.OldItems) {
269          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
270            RemoveListViewItem(listViewItem);
271            break;
272          }
273        }
274        foreach (T item in e.Items)
275          AddListViewItem(CreateListViewItem(item));
276      }
277    }
278    #endregion
279
280    #region Item Events
281    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
282      if (InvokeRequired)
283        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
284      else {
285        T item = (T)sender;
286        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
287          UpdateListViewItemImage(listViewItem);
288      }
289    }
290    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
291      if (InvokeRequired)
292        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
293      else {
294        T item = (T)sender;
295        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
296          UpdateListViewItemText(listViewItem);
297      }
298    }
299    #endregion
300
301    #region Helpers
302    protected virtual void SortItemsListView(SortOrder sortOrder) {
303      itemsListView.Sorting = SortOrder.None;
304      itemsListView.Sorting = sortOrder;
305      itemsListView.Sorting = SortOrder.None;
306    }
307    protected virtual void AdjustListViewColumnSizes() {
308      if (itemsListView.Items.Count > 0) {
309        for (int i = 0; i < itemsListView.Columns.Count; i++)
310          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
311      }
312    }
313    #endregion
314  }
315}
Note: See TracBrowser for help on using the repository browser.