Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunCollectionView.cs @ 3416

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

implemented ContentViews and propagation of view state changes (ticket #982)

File size: 11.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 HeuristicLab.Collections;
23using HeuristicLab.Core;
24using HeuristicLab.Core.Views;
25using HeuristicLab.MainForm;
26using HeuristicLab.MainForm.WindowsForms;
27using System.Windows.Forms;
28using System;
29using System.Collections.Generic;
30
31namespace HeuristicLab.Optimization.Views {
32  [View("RunCollection View")]
33  [Content(typeof(RunCollection), true)]
34  [Content(typeof(IItemCollection<IRun>), false)]
35  public partial class RunCollectionView : AsynchronousContentView {
36    public new IItemCollection<IRun> Content {
37      get { return (IItemCollection<IRun>)base.Content; }
38      set { base.Content = value; }
39    }
40
41    public ListView ItemsListView {
42      get { return itemsListView; }
43    }
44
45    public RunCollectionView() {
46      InitializeComponent();
47      Caption = "Run Collection";
48    }
49
50    public RunCollectionView(IItemCollection<IRun> content)
51      : this() {
52      Content = content;
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
57      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
58      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
59      base.DeregisterContentEvents();
60    }
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
64      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
65      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      Caption = "Run Collection";
71      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
72      viewHost.Content = null;
73
74      if (Content != null) {
75        Caption += " (" + Content.GetType().Name + ")";
76        foreach (IRun item in Content)
77          AddListViewItem(CreateListViewItem(item));
78      }
79      SetEnabledStateOfControls();
80    }
81
82    protected override void OnReadOnlyChanged() {
83      base.OnReadOnlyChanged();
84      SetEnabledStateOfControls();
85    }
86    private void SetEnabledStateOfControls() {
87      if (Content == null) {
88        itemsListView.Enabled = false;
89        detailsGroupBox.Enabled = false;
90        viewHost.Enabled = false;
91        removeButton.Enabled = false;
92      } else {
93        itemsListView.Enabled = true;
94        detailsGroupBox.Enabled = true;
95        removeButton.Enabled = !ReadOnly;
96        viewHost.Enabled = true;
97        viewHost.ReadOnly = ReadOnly;
98      }
99    }
100
101    protected virtual ListViewItem CreateListViewItem(IRun item) {
102      ListViewItem listViewItem = new ListViewItem();
103      listViewItem.Text = item.ToString();
104      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
105      itemsListView.SmallImageList.Images.Add(item.ItemImage);
106      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
107      listViewItem.Tag = item;
108      return listViewItem;
109    }
110    protected virtual void AddListViewItem(ListViewItem listViewItem) {
111      itemsListView.Items.Add(listViewItem);
112      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
113      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
114    }
115    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
116      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
117      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
118      listViewItem.Remove();
119      foreach (ListViewItem other in itemsListView.Items)
120        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
121      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
122    }
123    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
124      int i = listViewItem.ImageIndex;
125      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
126      listViewItem.ImageIndex = -1;
127      listViewItem.ImageIndex = i;
128    }
129    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
130      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
131        listViewItem.Text = listViewItem.Tag.ToString();
132    }
133    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
134      foreach (ListViewItem listViewItem in itemsListView.Items) {
135        if (((IRun)listViewItem.Tag) == item)
136          yield return listViewItem;
137      }
138    }
139
140    #region ListView Events
141    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
142      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly;
143      if (itemsListView.SelectedItems.Count == 1) {
144        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
145        detailsGroupBox.Enabled = true;
146        viewHost.ViewType = null;
147        viewHost.Content = item;
148      } else {
149        viewHost.Content = null;
150        detailsGroupBox.Enabled = false;
151      }
152    }
153    protected virtual void itemsListView_SizeChanged(object sender, EventArgs e) {
154      if (itemsListView.Columns.Count > 0)
155        itemsListView.Columns[0].Width = Math.Max(0, itemsListView.Width - 25);
156    }
157    protected virtual void itemsListView_KeyDown(object sender, KeyEventArgs e) {
158      if (e.KeyCode == Keys.Delete) {
159        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly) {
160          foreach (ListViewItem item in itemsListView.SelectedItems)
161            Content.Remove((IRun)item.Tag);
162        }
163      }
164    }
165    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
166      if (itemsListView.SelectedItems.Count == 1) {
167        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
168        IView view = MainFormManager.CreateDefaultView(item);
169        if (view != null) {
170          view.ReadOnly = ReadOnly;
171          view.Show();
172        }
173      }
174    }
175    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
176      ListViewItem listViewItem = (ListViewItem)e.Item;
177      IRun item = (IRun)listViewItem.Tag;
178      DataObject data = new DataObject();
179      data.SetData("Type", item.GetType());
180      data.SetData("Value", item);
181      if (Content.IsReadOnly || ReadOnly) {
182        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
183      } else {
184        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
185        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
186          Content.Remove(item);
187      }
188    }
189    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
190      e.Effect = DragDropEffects.None;
191      if (ReadOnly) return;
192      Type type = e.Data.GetData("Type") as Type;
193      if ((!Content.IsReadOnly) && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
194        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
195        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
196        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
197        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
198        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
199      }
200    }
201    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
202      if (e.Effect != DragDropEffects.None) {
203        IRun item = e.Data.GetData("Value") as IRun;
204        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
205        Content.Add(item);
206      }
207    }
208    #endregion
209
210    #region Button Events
211    protected virtual void removeButton_Click(object sender, EventArgs e) {
212      if (itemsListView.SelectedItems.Count > 0) {
213        foreach (ListViewItem item in itemsListView.SelectedItems)
214          Content.Remove((IRun)item.Tag);
215        itemsListView.SelectedItems.Clear();
216      }
217    }
218    #endregion
219
220    #region Content Events
221    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
222      if (InvokeRequired)
223        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
224      else
225        foreach (IRun item in e.Items)
226          AddListViewItem(CreateListViewItem(item));
227    }
228    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
229      if (InvokeRequired)
230        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
231      else {
232        foreach (IRun item in e.Items) {
233          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
234            RemoveListViewItem(listViewItem);
235            break;
236          }
237        }
238      }
239    }
240    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
241      if (InvokeRequired)
242        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
243      else {
244        foreach (IRun item in e.OldItems) {
245          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
246            RemoveListViewItem(listViewItem);
247            break;
248          }
249        }
250        foreach (IRun item in e.Items)
251          AddListViewItem(CreateListViewItem(item));
252      }
253    }
254    #endregion
255
256    #region Item Events
257    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
258      if (InvokeRequired)
259        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
260      else {
261        IRun item = (IRun)sender;
262        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
263          UpdateListViewItemImage(listViewItem);
264      }
265    }
266    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
267      if (InvokeRequired)
268        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
269      else {
270        IRun item = (IRun)sender;
271        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
272          UpdateListViewItemText(listViewItem);
273      }
274    }
275    #endregion
276  }
277}
278
Note: See TracBrowser for help on using the repository browser.