Free cookie consent management tool by TermsFeed Policy Generator

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

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

added setting of locked property after the creation of contentviews (ticket #982)

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