Free cookie consent management tool by TermsFeed Policy Generator

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

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

added setting of locked property after the creation of contentviews (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        IContentView view = MainFormManager.CreateDefaultView(item);
169        if (view != null) {
170          view.ReadOnly = ReadOnly;
171          view.Locked = Locked;
172          view.Show();
173        }
174      }
175    }
176    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
177      ListViewItem listViewItem = (ListViewItem)e.Item;
178      IRun item = (IRun)listViewItem.Tag;
179      DataObject data = new DataObject();
180      data.SetData("Type", item.GetType());
181      data.SetData("Value", item);
182      if (Content.IsReadOnly || ReadOnly) {
183        DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
184      } else {
185        DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
186        if ((result & DragDropEffects.Move) == DragDropEffects.Move)
187          Content.Remove(item);
188      }
189    }
190    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
191      e.Effect = DragDropEffects.None;
192      if (ReadOnly) return;
193      Type type = e.Data.GetData("Type") as Type;
194      if ((!Content.IsReadOnly) && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
195        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
196        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
197        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
198        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
199        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
200      }
201    }
202    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
203      if (e.Effect != DragDropEffects.None) {
204        IRun item = e.Data.GetData("Value") as IRun;
205        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
206        Content.Add(item);
207      }
208    }
209    #endregion
210
211    #region Button Events
212    protected virtual void removeButton_Click(object sender, EventArgs e) {
213      if (itemsListView.SelectedItems.Count > 0) {
214        foreach (ListViewItem item in itemsListView.SelectedItems)
215          Content.Remove((IRun)item.Tag);
216        itemsListView.SelectedItems.Clear();
217      }
218    }
219    #endregion
220
221    #region Content Events
222    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
223      if (InvokeRequired)
224        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
225      else
226        foreach (IRun item in e.Items)
227          AddListViewItem(CreateListViewItem(item));
228    }
229    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
230      if (InvokeRequired)
231        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
232      else {
233        foreach (IRun item in e.Items) {
234          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
235            RemoveListViewItem(listViewItem);
236            break;
237          }
238        }
239      }
240    }
241    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
242      if (InvokeRequired)
243        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
244      else {
245        foreach (IRun item in e.OldItems) {
246          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
247            RemoveListViewItem(listViewItem);
248            break;
249          }
250        }
251        foreach (IRun item in e.Items)
252          AddListViewItem(CreateListViewItem(item));
253      }
254    }
255    #endregion
256
257    #region Item Events
258    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
259      if (InvokeRequired)
260        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
261      else {
262        IRun item = (IRun)sender;
263        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
264          UpdateListViewItemImage(listViewItem);
265      }
266    }
267    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
268      if (InvokeRequired)
269        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
270      else {
271        IRun item = (IRun)sender;
272        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
273          UpdateListViewItemText(listViewItem);
274      }
275    }
276    #endregion
277  }
278}
279
Note: See TracBrowser for help on using the repository browser.