Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adapted ReadOnly property in item collection views (#973)

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