Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 10696

Last change on this file since 10696 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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