Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2879 was 2875, checked in by mkommend, 15 years ago

corrected layouting issues in netron (ticket #867)

File size: 12.6 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.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.MainForm;
27using HeuristicLab.MainForm.WindowsForms;
28
29namespace HeuristicLab.Core.Views {
30  [Content(typeof(ItemCollection<>), true)]
31  [Content(typeof(IObservableCollection<>), false)]
32  public partial class ItemCollectionView<T> : ContentView where T : class, IItem {
33    public new IObservableCollection<T> Content {
34      get { return (IObservableCollection<T>)base.Content; }
35      set { base.Content = value; }
36    }
37
38    public ListView ItemsListView {
39      get { return itemsListView; }
40    }
41
42    public ItemCollectionView() {
43      InitializeComponent();
44      Caption = "Item Collection";
45      this.Resize += new EventHandler(ItemCollectionView_Resize);
46      this.splitContainer.Resize += new EventHandler(splitContainer_Resize);
47      this.splitContainer.Panel1.Resize += new EventHandler(Panel1_Resize);
48      this.splitContainer.Panel2.Resize += new EventHandler(Panel2_Resize);
49
50      this.detailsGroupBox.Resize += new EventHandler(detailsGroupBox_Resize);
51      this.itemsGroupBox.Resize += new EventHandler(itemsGroupBox_Resize);
52      this.itemsListView.Resize += new EventHandler(itemsListView_Resize);
53    }
54
55    void Panel1_Resize(object sender, EventArgs e) {
56     // MessageBox.Show("split.panel1 resized");
57    }
58
59    void Panel2_Resize(object sender, EventArgs e) {
60     // MessageBox.Show("split.panel2 resized");
61    }
62
63    void itemsListView_Resize(object sender, EventArgs e) {
64      //MessageBox.Show("itemslistview resized");
65    }
66
67    void itemsGroupBox_Resize(object sender, EventArgs e) {
68     // MessageBox.Show("items groupbox resized");
69    }
70
71    void detailsGroupBox_Resize(object sender, EventArgs e) {
72   //   MessageBox.Show("details groupbox resized");
73    }
74
75    void splitContainer_Resize(object sender, EventArgs e) {
76      //MessageBox.Show("split container resized");
77     
78    }
79
80    void ItemCollectionView_Resize(object sender, EventArgs e) {
81   //   MessageBox.Show("control resized");
82      this.splitContainer.SplitterDistance = this.splitContainer.SplitterDistance + 1;
83    }
84    public ItemCollectionView(IObservableCollection<T> content)
85      : this() {
86      Content = content;
87    }
88
89    protected override void DeregisterContentEvents() {
90      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
91      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
92      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
93      base.DeregisterContentEvents();
94    }
95    protected override void RegisterContentEvents() {
96      base.RegisterContentEvents();
97      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
98      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
99      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
100    }
101
102    protected override void OnContentChanged() {
103      base.OnContentChanged();
104      Caption = "Item Collection";
105      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
106      itemsListView.Enabled = false;
107      detailsGroupBox.Enabled = false;
108      viewHost.Content = null;
109      addButton.Enabled = false;
110      sortAscendingButton.Enabled = false;
111      sortDescendingButton.Enabled = false;
112      removeButton.Enabled = false;
113
114      if (Content != null) {
115        Caption += " (" + Content.GetType().Name + ")";
116        itemsListView.Enabled = true;
117        addButton.Enabled = !Content.IsReadOnly;
118        foreach (T item in Content)
119          AddListViewItem(CreateListViewItem(item));
120        sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
121        sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
122        SortItemsListView(SortOrder.Ascending);
123      }
124    }
125
126    protected virtual T CreateItem() {
127      try {
128        return (T)Activator.CreateInstance(typeof(T));
129      } catch(Exception ex) {
130        Auxiliary.ShowErrorMessageBox(ex);
131        return null;
132      }
133    }
134    protected virtual ListViewItem CreateListViewItem(T item) {
135      if (!itemsListView.SmallImageList.Images.ContainsKey(item.GetType().FullName))
136        itemsListView.SmallImageList.Images.Add(item.GetType().FullName, item.ItemImage);
137
138      ListViewItem listViewItem = new ListViewItem();
139      listViewItem.Text = item.ToString();
140      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
141      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.IndexOfKey(item.GetType().FullName);
142      listViewItem.Tag = item;
143      return listViewItem;
144    }
145    protected virtual void AddListViewItem(ListViewItem listViewItem) {
146      itemsListView.Items.Add(listViewItem);
147      ((T)listViewItem.Tag).Changed += new ChangedEventHandler(Item_Changed);
148      sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
149      sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
150    }
151    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
152      ((T)listViewItem.Tag).Changed -= new ChangedEventHandler(Item_Changed);
153      listViewItem.Remove();
154      sortAscendingButton.Enabled = itemsListView.Items.Count > 0;
155      sortDescendingButton.Enabled = itemsListView.Items.Count > 0;
156    }
157    protected virtual void UpdateListViewItem(ListViewItem listViewItem) {
158      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString())) {
159        listViewItem.Text = listViewItem.Tag.ToString();
160        itemsListView.Sort();
161      }
162    }
163    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(T item) {
164      foreach (ListViewItem listViewItem in itemsListView.Items) {
165        if (((T)listViewItem.Tag) == item)
166          yield return listViewItem;
167      }
168    }
169
170    #region ListView Events
171    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
172      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
173      if (itemsListView.SelectedItems.Count == 1) {
174        T item = (T)itemsListView.SelectedItems[0].Tag;
175        detailsGroupBox.Enabled = true;
176        viewHost.Content = item;
177      } else {
178        viewHost.Content = null;
179        detailsGroupBox.Enabled = false;
180      }
181    }
182    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
183      if (itemsListView.Columns.Count > 0)
184        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
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) {
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        IView view = MainFormManager.CreateDefaultView(item);
198        if (view != null) view.Show();
199      }
200    }
201    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
202      ListViewItem listViewItem = (ListViewItem)e.Item;
203      T item = (T)listViewItem.Tag;
204      DataObject data = new DataObject();
205      data.SetData("Type", item.GetType());
206      data.SetData("Value", item);
207      if (Content.IsReadOnly) {
208        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
209      } else {
210        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
211        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
212          Content.Remove(item);
213      }
214    }
215    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
216      e.Effect = DragDropEffects.None;
217      Type type = e.Data.GetData("Type") as Type;
218      if ((!Content.IsReadOnly) && (type != null) && (typeof(T).IsAssignableFrom(type))) {
219        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
220        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
221        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
222        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
223        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
224      }
225    }
226    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
227      if (e.Effect != DragDropEffects.None) {
228        T item = e.Data.GetData("Value") as T;
229        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (T)item.Clone();
230        Content.Add(item);
231      }
232    }
233    #endregion
234
235    #region Button Events
236    protected virtual void addButton_Click(object sender, EventArgs e) {
237      T item = CreateItem();
238      if (item != null)
239        Content.Add(item);
240    }
241    protected virtual void sortAscendingButton_Click(object sender, EventArgs e) {
242      SortItemsListView(SortOrder.Ascending);
243    }
244    protected virtual void sortDescendingButton_Click(object sender, EventArgs e) {
245      SortItemsListView(SortOrder.Descending);
246    }
247    protected virtual void removeButton_Click(object sender, EventArgs e) {
248      if (itemsListView.SelectedItems.Count > 0) {
249        foreach (ListViewItem item in itemsListView.SelectedItems)
250          Content.Remove((T)item.Tag);
251        itemsListView.SelectedItems.Clear();
252      }
253    }
254    #endregion
255
256    #region Content Events
257    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
258      if (InvokeRequired)
259        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
260      else
261        foreach (T item in e.Items)
262          AddListViewItem(CreateListViewItem(item));
263    }
264    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
265      if (InvokeRequired)
266        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
267      else {
268        foreach (T item in e.Items) {
269          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
270            RemoveListViewItem(listViewItem);
271            break;
272          }
273        }
274      }
275    }
276    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
277      if (InvokeRequired)
278        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
279      else {
280        foreach (T item in e.OldItems) {
281          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
282            RemoveListViewItem(listViewItem);
283            break;
284          }
285        }
286        foreach (T item in e.Items)
287          AddListViewItem(CreateListViewItem(item));
288      }
289    }
290    #endregion
291
292    #region Item Events
293    protected virtual void Item_Changed(object sender, ChangedEventArgs e) {
294      if (InvokeRequired)
295        Invoke(new ChangedEventHandler(Item_Changed), sender, e);
296      else {
297        T item = (T)sender;
298        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
299          UpdateListViewItem(listViewItem);
300      }
301    }
302    #endregion
303
304    #region Helpers
305    protected virtual void SortItemsListView(SortOrder sortOrder) {
306      itemsListView.Sorting = sortOrder;
307      itemsListView.Sort();
308      itemsListView.Sorting = SortOrder.None;
309    }
310    #endregion
311  }
312}
Note: See TracBrowser for help on using the repository browser.