Free cookie consent management tool by TermsFeed Policy Generator

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

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

implemented ContentViews and propagation of view state changes (ticket #982)

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);
193        if (view != null) {
194          view.ReadOnly = this.ReadOnly;
195          view.Show();
196        }
197      }
198    }
199    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
200      ListViewItem listViewItem = (ListViewItem)e.Item;
201      T item = (T)listViewItem.Tag;
202      DataObject data = new DataObject();
203      data.SetData("Type", item.GetType());
204      data.SetData("Value", item);
205      if (Content.IsReadOnly || ReadOnly) {
206        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
207      } else {
208        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
209        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
210          Content.Remove(item);
211      }
212    }
213    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
214      e.Effect = DragDropEffects.None;
215      Type type = e.Data.GetData("Type") as Type;
216      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
217        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
218        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
219        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
220        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
221        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
222      }
223    }
224    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
225      if (e.Effect != DragDropEffects.None) {
226        T item = e.Data.GetData("Value") as T;
227        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
228        Content.Add(item);
229      }
230    }
231    #endregion
232
233    #region Button Events
234    protected virtual void addButton_Click(object sender, EventArgs e) {
235      T item = CreateItem();
236      if (item != null)
237        Content.Add(item);
238    }
239    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
240      SortItemsListView(SortOrder.Ascending);
241    }
242    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
243      SortItemsListView(SortOrder.Descending);
244    }
245    protected virtual void removeButton_Click(object sender, EventArgs e) {
246      if (itemsListView.SelectedItems.Count > 0) {
247        foreach (ListViewItem item in itemsListView.SelectedItems)
248          Content.Remove((T)item.Tag);
249        itemsListView.SelectedItems.Clear();
250      }
251    }
252    #endregion
253
254    #region Content Events
255    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
256      if (InvokeRequired)
257        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
258      else
259        foreach (T item in e.Items)
260          AddListViewItem(CreateListViewItem(item));
261    }
262    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
263      if (InvokeRequired)
264        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
265      else {
266        foreach (T item in e.Items) {
267          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
268            RemoveListViewItem(listViewItem);
269            break;
270          }
271        }
272      }
273    }
274    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
275      if (InvokeRequired)
276        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
277      else {
278        foreach (T item in e.OldItems) {
279          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
280            RemoveListViewItem(listViewItem);
281            break;
282          }
283        }
284        foreach (T item in e.Items)
285          AddListViewItem(CreateListViewItem(item));
286      }
287    }
288    #endregion
289
290    #region Item Events
291    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
292      if (InvokeRequired)
293        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
294      else {
295        T item = (T)sender;
296        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
297          UpdateListViewItemImage(listViewItem);
298      }
299    }
300    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
301      if (InvokeRequired)
302        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
303      else {
304        T item = (T)sender;
305        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
306          UpdateListViewItemText(listViewItem);
307        AdjustListViewColumnSizes();
308      }
309    }
310    #endregion
311
312    #region Helpers
313    protected virtual void SortItemsListView(SortOrder sortOrder) {
314      itemsListView.Sorting = SortOrder.None;
315      itemsListView.Sorting = sortOrder;
316      itemsListView.Sorting = SortOrder.None;
317    }
318    protected virtual void AdjustListViewColumnSizes() {
319      if (itemsListView.Items.Count > 0) {
320        for (int i = 0; i < itemsListView.Columns.Count; i++)
321          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
322      }
323    }
324    #endregion
325  }
326}
Note: See TracBrowser for help on using the repository browser.