Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3519 was 3507, checked in by swagner, 15 years ago

Continued work on algorithm batch processing (#947)

File size: 14.1 KB
RevLine 
[3260]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
[3274]22using HeuristicLab.Collections;
[3507]23using HeuristicLab.Common;
[3393]24using HeuristicLab.Core;
[3260]25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
[3277]27using HeuristicLab.MainForm.WindowsForms;
28using System.Windows.Forms;
29using System;
[3449]30using System.Drawing;
[3277]31using System.Collections.Generic;
[3507]32using System.Linq;
[3260]33
34namespace HeuristicLab.Optimization.Views {
35  [View("RunCollection View")]
36  [Content(typeof(RunCollection), true)]
[3393]37  [Content(typeof(IItemCollection<IRun>), false)]
[3483]38  public partial class RunCollectionView : ItemView {
[3393]39    public new IItemCollection<IRun> Content {
40      get { return (IItemCollection<IRun>)base.Content; }
[3277]41      set { base.Content = value; }
42    }
43
44    public ListView ItemsListView {
45      get { return itemsListView; }
46    }
47
[3260]48    public RunCollectionView() {
49      InitializeComponent();
[3277]50      Caption = "Run Collection";
[3505]51      itemsGroupBox.Text = "Runs";
[3260]52    }
[3277]53
[3393]54    public RunCollectionView(IItemCollection<IRun> content)
[3260]55      : this() {
56      Content = content;
57    }
58
[3277]59    protected override void DeregisterContentEvents() {
[3280]60      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
61      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
62      Content.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3449]63      DeregisterRunEvents(Content);
[3277]64      base.DeregisterContentEvents();
[3260]65    }
[3277]66    protected override void RegisterContentEvents() {
67      base.RegisterContentEvents();
[3280]68      Content.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded);
69      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved);
70      Content.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset);
[3449]71      RegisterRunEvents(Content);
[3277]72    }
[3449]73    protected virtual void RegisterRunEvents(IEnumerable<IRun> runs) {
74      foreach (IRun run in runs)
[3507]75        run.Changed += new EventHandler(Run_Changed);
[3449]76    }
77    protected virtual void DeregisterRunEvents(IEnumerable<IRun> runs) {
78      foreach (IRun run in runs)
[3507]79        run.Changed -= new EventHandler(Run_Changed);
[3449]80    }
[3277]81
[3507]82    protected override void OnInitialized(EventArgs e) {
83      base.OnInitialized(e);
84      var viewTypes = MainFormManager.GetViewTypes(typeof(RunCollection), true);
85      foreach (Type viewType in viewTypes.OrderBy(x => ViewAttribute.GetViewName(x))) {
86        if ((viewType != typeof(ItemCollectionView<IRun>)) && (viewType != typeof(ViewHost))) {
87          ToolStripMenuItem menuItem = new ToolStripMenuItem();
88          menuItem.Text = ViewAttribute.GetViewName(viewType);
89          menuItem.Tag = viewType;
90          menuItem.Click += new EventHandler(menuItem_Click);
91          analyzeRunsToolStripDropDownButton.DropDownItems.Add(menuItem);
92        }
93      }
94    }
95
[3277]96    protected override void OnContentChanged() {
97      base.OnContentChanged();
98      Caption = "Run Collection";
99      while (itemsListView.Items.Count > 0) RemoveListViewItem(itemsListView.Items[0]);
100      viewHost.Content = null;
101
102      if (Content != null) {
103        Caption += " (" + Content.GetType().Name + ")";
[3280]104        foreach (IRun item in Content)
[3277]105          AddListViewItem(CreateListViewItem(item));
106      }
[3362]107      SetEnabledStateOfControls();
[3277]108    }
109
[3350]110    protected override void OnReadOnlyChanged() {
111      base.OnReadOnlyChanged();
[3362]112      SetEnabledStateOfControls();
[3350]113    }
[3362]114    private void SetEnabledStateOfControls() {
[3350]115      if (Content == null) {
[3507]116        analyzeRunsToolStripDropDownButton.Enabled = false;
[3350]117        itemsListView.Enabled = false;
118        detailsGroupBox.Enabled = false;
119        viewHost.Enabled = false;
120        removeButton.Enabled = false;
121      } else {
[3507]122        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3350]123        itemsListView.Enabled = true;
124        detailsGroupBox.Enabled = true;
[3435]125        removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && !Content.IsReadOnly && !ReadOnly;
[3350]126        viewHost.Enabled = true;
127      }
128    }
129
[3280]130    protected virtual ListViewItem CreateListViewItem(IRun item) {
[3277]131      ListViewItem listViewItem = new ListViewItem();
132      listViewItem.Text = item.ToString();
133      listViewItem.ToolTipText = item.ItemName + ": " + item.ItemDescription;
[3341]134      itemsListView.SmallImageList.Images.Add(item.ItemImage);
135      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
[3277]136      listViewItem.Tag = item;
137      return listViewItem;
138    }
139    protected virtual void AddListViewItem(ListViewItem listViewItem) {
140      itemsListView.Items.Add(listViewItem);
[3341]141      ((IRun)listViewItem.Tag).ItemImageChanged += new EventHandler(Item_ItemImageChanged);
[3280]142      ((IRun)listViewItem.Tag).ToStringChanged += new EventHandler(Item_ToStringChanged);
[3277]143    }
144    protected virtual void RemoveListViewItem(ListViewItem listViewItem) {
[3341]145      ((IRun)listViewItem.Tag).ItemImageChanged -= new EventHandler(Item_ItemImageChanged);
[3280]146      ((IRun)listViewItem.Tag).ToStringChanged -= new EventHandler(Item_ToStringChanged);
[3277]147      listViewItem.Remove();
[3341]148      foreach (ListViewItem other in itemsListView.Items)
149        if (other.ImageIndex > listViewItem.ImageIndex) other.ImageIndex--;
150      itemsListView.SmallImageList.Images.RemoveAt(listViewItem.ImageIndex);
[3277]151    }
[3341]152    protected virtual void UpdateListViewItemImage(ListViewItem listViewItem) {
153      int i = listViewItem.ImageIndex;
154      listViewItem.ImageList.Images[i] = ((IRun)listViewItem.Tag).ItemImage;
155      listViewItem.ImageIndex = -1;
156      listViewItem.ImageIndex = i;
157    }
158    protected virtual void UpdateListViewItemText(ListViewItem listViewItem) {
159      if (!listViewItem.Text.Equals(listViewItem.Tag.ToString()))
[3277]160        listViewItem.Text = listViewItem.Tag.ToString();
161    }
[3280]162    protected virtual IEnumerable<ListViewItem> GetListViewItemsForItem(IRun item) {
[3277]163      foreach (ListViewItem listViewItem in itemsListView.Items) {
[3280]164        if (((IRun)listViewItem.Tag) == item)
[3277]165          yield return listViewItem;
166      }
167    }
168
169    #region ListView Events
170    protected virtual void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
[3456]171      removeButton.Enabled = itemsListView.SelectedItems.Count > 0 && (Content != null) && !Content.IsReadOnly && !ReadOnly;
[3277]172      if (itemsListView.SelectedItems.Count == 1) {
[3280]173        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
[3277]174        detailsGroupBox.Enabled = true;
175        viewHost.ViewType = null;
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) {
[3435]188        if ((itemsListView.SelectedItems.Count > 0) && !Content.IsReadOnly && !ReadOnly) {
[3277]189          foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]190            Content.Remove((IRun)item.Tag);
[3277]191        }
192      }
193    }
194    protected virtual void itemsListView_DoubleClick(object sender, EventArgs e) {
195      if (itemsListView.SelectedItems.Count == 1) {
[3280]196        IRun item = (IRun)itemsListView.SelectedItems[0].Tag;
[3423]197        IContentView view = MainFormManager.CreateDefaultView(item);
[3416]198        if (view != null) {
199          view.ReadOnly = ReadOnly;
[3423]200          view.Locked = Locked;
[3416]201          view.Show();
202        }
[3277]203      }
204    }
205    protected virtual void itemsListView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]206      if (!Locked) {
207        ListViewItem listViewItem = (ListViewItem)e.Item;
208        IRun item = (IRun)listViewItem.Tag;
209        DataObject data = new DataObject();
210        data.SetData("Type", item.GetType());
211        data.SetData("Value", item);
[3435]212        if (Content.IsReadOnly || ReadOnly) {
[3432]213          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
214        } else {
215          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link | DragDropEffects.Move);
216          if ((result & DragDropEffects.Move) == DragDropEffects.Move)
217            Content.Remove(item);
218        }
[3277]219      }
220    }
221    protected virtual void itemsListView_DragEnterOver(object sender, DragEventArgs e) {
222      e.Effect = DragDropEffects.None;
223      Type type = e.Data.GetData("Type") as Type;
[3435]224      if (!Content.IsReadOnly && !ReadOnly && (type != null) && (typeof(IRun).IsAssignableFrom(type))) {
[3277]225        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
226        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
227        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
228        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
229        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
230      }
231    }
232    protected virtual void itemsListView_DragDrop(object sender, DragEventArgs e) {
233      if (e.Effect != DragDropEffects.None) {
[3280]234        IRun item = e.Data.GetData("Value") as IRun;
235        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IRun)item.Clone();
[3277]236        Content.Add(item);
237      }
238    }
239    #endregion
240
241    #region Button Events
[3507]242    protected virtual void menuItem_Click(object sender, EventArgs e) {
243      ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
244      IContentView view = MainFormManager.CreateView((Type)menuItem.Tag, Content);
245      if (view != null) {
246        view.Locked = Locked;
247        view.ReadOnly = ReadOnly;
248        view.Show();
249      }
250    }
[3277]251    protected virtual void removeButton_Click(object sender, EventArgs e) {
252      if (itemsListView.SelectedItems.Count > 0) {
253        foreach (ListViewItem item in itemsListView.SelectedItems)
[3280]254          Content.Remove((IRun)item.Tag);
[3277]255        itemsListView.SelectedItems.Clear();
256      }
257    }
258    #endregion
259
260    #region Content Events
[3280]261    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]262      if (InvokeRequired)
[3280]263        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsAdded), sender, e);
[3449]264      else {
265        RegisterRunEvents(e.Items);
[3280]266        foreach (IRun item in e.Items)
[3277]267          AddListViewItem(CreateListViewItem(item));
[3507]268        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3449]269      }
[3277]270    }
[3280]271    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]272      if (InvokeRequired)
[3280]273        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_ItemsRemoved), sender, e);
[3277]274      else {
[3449]275        DeregisterRunEvents(e.Items);
[3280]276        foreach (IRun item in e.Items) {
[3277]277          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
278            RemoveListViewItem(listViewItem);
279            break;
280          }
281        }
[3507]282        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3277]283      }
284    }
[3280]285    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
[3277]286      if (InvokeRequired)
[3280]287        Invoke(new CollectionItemsChangedEventHandler<IRun>(Content_CollectionReset), sender, e);
[3277]288      else {
[3449]289        DeregisterRunEvents(e.OldItems);
[3280]290        foreach (IRun item in e.OldItems) {
[3277]291          foreach (ListViewItem listViewItem in GetListViewItemsForItem(item)) {
292            RemoveListViewItem(listViewItem);
293            break;
294          }
295        }
[3449]296        RegisterRunEvents(e.Items);
[3280]297        foreach (IRun item in e.Items)
[3277]298          AddListViewItem(CreateListViewItem(item));
[3507]299        analyzeRunsToolStripDropDownButton.Enabled = itemsListView.Items.Count > 0;
[3277]300      }
301    }
302    #endregion
303
304    #region Item Events
[3341]305    protected virtual void Item_ItemImageChanged(object sender, EventArgs e) {
306      if (InvokeRequired)
307        Invoke(new EventHandler(Item_ItemImageChanged), sender, e);
308      else {
309        IRun item = (IRun)sender;
310        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
311          UpdateListViewItemImage(listViewItem);
312      }
313    }
[3277]314    protected virtual void Item_ToStringChanged(object sender, EventArgs e) {
315      if (InvokeRequired)
316        Invoke(new EventHandler(Item_ToStringChanged), sender, e);
317      else {
[3280]318        IRun item = (IRun)sender;
[3277]319        foreach (ListViewItem listViewItem in GetListViewItemsForItem(item))
[3341]320          UpdateListViewItemText(listViewItem);
[3277]321      }
322    }
[3507]323    protected virtual void Run_Changed(object sender, EventArgs e) {
324      IRun run = (IRun)sender;
325      foreach (ListViewItem listViewItem in GetListViewItemsForItem(run)) {
326        if (run.Visible) {
327          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
328          listViewItem.ForeColor = run.Color;
329        } else {
330          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
331          listViewItem.ForeColor = Color.LightGray;
332        }
333      }
334    }
[3277]335    #endregion
[3260]336  }
337}
[3277]338
Note: See TracBrowser for help on using the repository browser.