Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 10089

Last change on this file since 10089 was 10089, checked in by jkarder, 10 years ago

#2116:

  • added hotlinking functionality
  • added methods for outermost view host detection and manipulation
File size: 8.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Optimization.Views {
33  /// <summary>
34  /// The visual representation of a <see cref="Variable"/>.
35  /// </summary>
36  [View("Run View")]
37  [Content(typeof(IRun), true)]
38  public sealed partial class RunView : NamedItemView {
39    /// <summary>
40    /// Gets or sets the variable to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public new IRun Content {
45      get { return (IRun)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
51    /// </summary>
52    public RunView() {
53      InitializeComponent();
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.Changed += new EventHandler(Content_Changed);
59    }
60    protected override void DeregisterContentEvents() {
61      base.DeregisterContentEvents();
62      Content.Changed -= new EventHandler(Content_Changed);
63    }
64    private void Content_Changed(object sender, EventArgs e) {
65      if (InvokeRequired)
66        this.Invoke(new EventHandler(Content_Changed), sender, e);
67      else
68        UpdateColor();
69    }
70
71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      viewHost.Content = null;
74      if (Content != null)
75        UpdateColor();
76      FillListView();
77    }
78
79    protected override void SetEnabledStateOfControls() {
80      base.SetEnabledStateOfControls();
81      listView.Enabled = Content != null;
82      detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
83      changeColorButton.Enabled = Content != null;
84      showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
85    }
86
87    protected override void PropagateStateChanges(Control control, Type type, System.Reflection.PropertyInfo propertyInfo) {
88      if (propertyInfo.Name == "ReadOnly") return;
89      base.PropagateStateChanges(control, type, propertyInfo);
90    }
91
92    private void changeColorButton_Click(object sender, EventArgs e) {
93      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
94        this.Content.Color = this.colorDialog.Color;
95      }
96    }
97    private void UpdateColor() {
98      this.colorDialog.Color = this.Content.Color;
99      this.colorArea.BackColor = this.Content.Color;
100    }
101
102    private string selectedName;
103    private void FillListView() {
104      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
105
106      listView.Items.Clear();
107      listView.SmallImageList.Images.Clear();
108      if (Content != null) {
109        foreach (string key in Content.Parameters.Keys)
110          CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"], selectedName);
111        foreach (string key in Content.Results.Keys)
112          CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"], selectedName);
113        if (listView.Items.Count > 0) {
114          for (int i = 0; i < listView.Columns.Count; i++)
115            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
116          selectedName = null;
117        }
118      }
119    }
120
121    private void CreateListViewItem(string name, IItem value, ListViewGroup group, string selectedName) {
122      ListViewItem item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
123      item.Tag = value;
124      item.Group = group;
125      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
126      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
127      listView.Items.Add(item);
128      if ((selectedName != null) && name.Equals(selectedName)) item.Selected = true;
129    }
130
131    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
132      if (listView.SelectedItems.Count == 1) {
133        var item = (IItem)listView.SelectedItems[0].Tag;
134        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
135        var outermostViewHost = mainForm.GetOutermostViewOfType<ViewHost>(this);
136        if (outermostViewHost != null && outermostViewHost.HotlinkingEnabled) {
137          var oldCrumbs = outermostViewHost.Breadcrumbs;
138          var newCrumbs = BuildBreadcrumbTrail();
139          mainForm.ShowContentInSpecificViewHost(item, outermostViewHost);
140          outermostViewHost.UpdateBreadcrumbTrail(oldCrumbs, newCrumbs.Concat(new[] { item }));
141          outermostViewHost.ActiveView.ReadOnly = ReadOnly;
142          outermostViewHost.ActiveView.Locked = Locked;
143        } else {
144          if (showDetailsCheckBox.Checked) {
145            detailsGroupBox.Enabled = true;
146            viewHost.Content = item;
147          } else {
148            viewHost.Content = null;
149            detailsGroupBox.Enabled = false;
150          }
151        }
152      }
153    }
154    private void listView_DoubleClick(object sender, EventArgs e) {
155      if (listView.SelectedItems.Count == 1) {
156        var item = (IItem)listView.SelectedItems[0].Tag;
157        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
158        var outermostViewHost = mainForm.GetOutermostViewOfType<ViewHost>(this);
159        var oldCrumbs = outermostViewHost.Breadcrumbs;
160        var newCrumbs = BuildBreadcrumbTrail();
161        mainForm.ShowContentInSpecificViewHost(item, outermostViewHost);
162        outermostViewHost.UpdateBreadcrumbTrail(oldCrumbs, newCrumbs.Concat(new[] { item }));
163        outermostViewHost.ActiveView.ReadOnly = ReadOnly;
164        outermostViewHost.ActiveView.Locked = Locked;
165      }
166    }
167    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
168      if (!Locked) {
169        ListViewItem listViewItem = (ListViewItem)e.Item;
170        IItem item = (IItem)listViewItem.Tag;
171        if (item != null) {
172          DataObject data = new DataObject();
173          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
174          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
175        }
176      }
177    }
178    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
179      if (showDetailsCheckBox.Checked) {
180        splitContainer.Panel2Collapsed = false;
181        detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
182        viewHost.Content = listView.SelectedItems.Count == 1 ? (IContent)listView.SelectedItems[0].Tag : null;
183      } else {
184        splitContainer.Panel2Collapsed = true;
185        viewHost.Content = null;
186      }
187    }
188    private void showAlgorithmButton_Click(object sender, EventArgs e) {
189      if (!Locked) {
190        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
191      }
192    }
193    private IEnumerable<IContent> BuildBreadcrumbTrail() {
194      var l = new LinkedList<IContent>();
195      for (var control = (Control)this; control != null; control = control.Parent) {
196        var vh = control as ViewHost;
197        if (vh != null && vh.Content != null)
198          l.AddFirst(vh.Content);
199      }
200      return l;
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.