Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/CheckedItemCollectionView.cs @ 3562

Last change on this file since 3562 was 3562, checked in by gkronber, 15 years ago

Implemented changes as requested by swagner. #992 (CheckedItemList and CheckedItemCollection is necessary)

File size: 14.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("CheckedItemCollection View")]
32  [Content(typeof(CheckedItemCollection<>), true)]
33  public partial class CheckedItemCollectionView<T> : ItemView where T : class, IItem {
34    protected TypeSelectorDialog typeSelectorDialog;
35
36    public new ICheckedItemCollection<T> Content {
37      get { return (ICheckedItemCollection<T>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public ListView ItemsListView {
42      get { return itemsListView; }
43    }
44
45    public CheckedItemCollectionView() {
46      InitializeComponent();
47      Caption = "Checked Item Collection";
48    }
49    public CheckedItemCollectionView(ICheckedItemCollection<T> content)
50      : this() {
51      Content = content;
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
56      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
57      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
58      Content.CheckedItemsChanged -= new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked);
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      Content.CheckedItemsChanged += new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      Caption = "Checked Item Collection";
72      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
73      viewHost.Content = null;
74      if (Content != null) {
75        Caption += " (" + Content.GetType().Name + ")";
76        foreach (T item in Content)
77          AddListViewItem(CreateListViewItem(item));
78        SortItemsListView(SortOrder.Ascending);
79      }
80      SetEnabledStateOfControls();
81    }
82
83    protected override void OnReadOnlyChanged() {
84      base.OnReadOnlyChanged();
85      SetEnabledStateOfControls();
86    }
87
88    private void SetEnabledStateOfControls() {
89      if (Content == null) {
90        addButton.Enabled = false;
91        sortAscendingButton.Enabled = false;
92        sortDescendingButton.Enabled = false;
93        removeButton.Enabled = false;
94        itemsListView.Enabled = false;
95        detailsGroupBox.Enabled = false;
96      } else {
97        addButton.Enabled = !Content.IsReadOnly && !ReadOnly;
98        sortAscendingButton.Enabled = itemsListView.Items.Count > 1;
99        sortDescendingButton.Enabled = itemsListView.Items.Count > 1;
100        removeButton.Enabled = !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
101        itemsListView.Enabled = true;
102        detailsGroupBox.Enabled = true;
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    private void itemsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
170      var checkedItem = e.Item;
171      Content.SetItemCheckedState((T)checkedItem.Tag, checkedItem.Checked);
172    }
173
174    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
175      removeButton.Enabled = (Content != null) && !Content.IsReadOnly && !ReadOnly && itemsListView.SelectedItems.Count > 0;
176      if (itemsListView.SelectedItems.Count == 1) {
177        T item = (T)itemsListView.SelectedItems[0].Tag;
178        detailsGroupBox.Enabled = true;
179        viewHost.ViewType = null;
180        viewHost.Content = item;
181      } else {
182        viewHost.Content = null;
183        detailsGroupBox.Enabled = false;
184      }
185    }
186    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
187      if (e.KeyCode == Keys.Delete) {
188        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
189          foreach (ListViewItem item in itemsListView.SelectedItems)
190            Content.Remove((T)item.Tag);
191        }
192      }
193    }
194    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
195      if (itemsListView.SelectedItems.Count == 1) {
196        T item = (T)itemsListView.SelectedItems[0].Tag;
197        IContentView view = MainFormManager.MainForm.ShowContent(item);
198        if (view != null) {
199          view.ReadOnly = ReadOnly;
200          view.Locked = Locked;
201        }
202      }
203    }
204    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
205      if (!Locked) {
206        ListViewItem listViewItem = (ListViewItem)e.Item;
207        T item = (T)listViewItem.Tag;
208        DataObject data = new DataObject();
209        data.SetData("Type", item.GetType());
210        data.SetData("Value", item);
211        if (Content.IsReadOnly || ReadOnly) {
212          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
213        } else {
214          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
215          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
216            Content.Remove(item);
217        }
218      }
219    }
220    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
221      e.Effect = DragDropEffects.None;
222      Type type = e.Data.GetData("Type") as Type;
223      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(T).IsAssignableFrom(type))) {
224        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
225        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
226        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
227        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
228        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
229      }
230    }
231    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
232      if (e.Effect != DragDropEffects.None) {
233        T item = e.Data.GetData("Value") as T;
234        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
235        Content.Add(item);
236      }
237    }
238    #endregion
239
240    #region Button Events
241    protected virtual void addButton_Click(object sender, EventArgs e) {
242      T item = CreateItem();
243      if (item != null)
244        Content.Add(item);
245    }
246    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
247      SortItemsListView(SortOrder.Ascending);
248    }
249    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
250      SortItemsListView(SortOrder.Descending);
251    }
252    protected virtual void removeButton_Click(object sender, EventArgs e) {
253      if (itemsListView.SelectedItems.Count > 0) {
254        foreach (ListViewItem item in itemsListView.SelectedItems)
255          Content.Remove((T)item.Tag);
256        itemsListView.SelectedItems.Clear();
257      }
258    }
259    #endregion
260
261    #region Content Events
262    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
263      if (InvokeRequired)
264        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
265      else
266        foreach (T item in e.Items)
267          AddListViewItem(CreateListViewItem(item));
268    }
269    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
270      if (InvokeRequired)
271        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
272      else {
273        foreach (T item in e.Items) {
274          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
275            RemoveListViewItem(listViewItem);
276            break;
277          }
278        }
279      }
280    }
281    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
282      if (InvokeRequired)
283        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
284      else {
285        foreach (T item in e.OldItems) {
286          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
287            RemoveListViewItem(listViewItem);
288            break;
289          }
290        }
291        foreach (T item in e.Items)
292          AddListViewItem(CreateListViewItem(item));
293      }
294    }
295    protected virtual void Content_ItemsChecked(object sender, CollectionItemsChangedEventArgs<T> e) {
296      if (InvokeRequired)
297        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsChecked), sender, e);
298      else {
299        foreach (T item in e.Items) {
300          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
301            if (listViewItem.Checked != Content.ItemChecked(item))
302              listViewItem.Checked = Content.ItemChecked(item);
303          }
304        }
305      }
306    }
307    #endregion
308
309    #region Item Events
310    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
311      if (InvokeRequired)
312        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
313      else {
314        T item = (T)sender;
315        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
316          UpdateListViewItemImage(listViewItem);
317      }
318    }
319    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
320      if (InvokeRequired)
321        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
322      else {
323        T item = (T)sender;
324        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
325          UpdateListViewItemText(listViewItem);
326        AdjustListViewColumnSizes();
327      }
328    }
329    #endregion
330
331    #region Helpers
332    protected virtual void SortItemsListView(SortOrder sortOrder) {
333      itemsListView.Sorting = SortOrder.None;
334      itemsListView.Sorting = sortOrder;
335      itemsListView.Sorting = SortOrder.None;
336    }
337    protected virtual void AdjustListViewColumnSizes() {
338      if (itemsListView.Items.Count > 0) {
339        for (int i = 0; i < itemsListView.Columns.Count; i++)
340          itemsListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
341      }
342    }
343    #endregion
344  }
345}
Note: See TracBrowser for help on using the repository browser.