Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3367 was 3367, checked in by mkommend, 13 years ago

adapted HL.Optimization.Views to new ReadOnly mechanism (ticket #973)

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