Free cookie consent management tool by TermsFeed Policy Generator

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

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

adapted view captions (ticket #893)

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