Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Optimization.Views/3.3/RunView.cs @ 8694

Last change on this file since 8694 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    public override bool ReadOnly {
47      get { return true; }
48      set { /*not needed because runs are always readonly */}
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
53    /// </summary>
54    public RunView() {
55      InitializeComponent();
56    }
57
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.Changed += new EventHandler(Content_Changed);
61    }
62    protected override void DeregisterContentEvents() {
63      base.DeregisterContentEvents();
64      Content.Changed -= new EventHandler(Content_Changed);
65    }
66    private void Content_Changed(object sender, EventArgs e) {
67      if (InvokeRequired)
68        this.Invoke(new EventHandler(Content_Changed), sender, e);
69      else
70        UpdateColor();
71    }
72
73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      viewHost.Content = null;
76      if (Content != null)
77        UpdateColor();
78      FillListView();
79    }
80
81    protected override void SetEnabledStateOfControls() {
82      base.SetEnabledStateOfControls();
83      listView.Enabled = Content != null;
84      detailsGroupBox.Enabled = (Content != null) && (listView.SelectedItems.Count == 1);
85      viewHost.Enabled = Content != null;
86      viewHost.ReadOnly = ReadOnly;
87      changeColorButton.Enabled = Content != null;
88      showAlgorithmButton.Enabled = Content != null && Content.Algorithm != null && !Locked;
89    }
90
91    private void changeColorButton_Click(object sender, EventArgs e) {
92      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
93        this.Content.Color = this.colorDialog.Color;
94      }
95    }
96    private void UpdateColor() {
97      this.colorDialog.Color = this.Content.Color;
98      this.colorArea.BackColor = this.Content.Color;
99    }
100
101    private string selectedName;
102    private void FillListView() {
103      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
104
105      listView.Items.Clear();
106      listView.SmallImageList.Images.Clear();
107      if (Content != null) {
108        foreach (string key in Content.Parameters.Keys)
109          CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"], selectedName);
110        foreach (string key in Content.Results.Keys)
111          CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"], selectedName);
112        if (listView.Items.Count > 0) {
113          for (int i = 0; i < listView.Columns.Count; i++)
114            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
115          selectedName = null;
116        }
117      }
118    }
119
120    private void CreateListViewItem(string name, IItem value, ListViewGroup group, string selectedName) {
121      ListViewItem item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
122      item.Tag = value;
123      item.Group = group;
124      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : value.ItemImage);
125      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
126      listView.Items.Add(item);
127      if ((selectedName != null) && name.Equals(selectedName)) item.Selected = true;
128    }
129
130    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
131      if (showDetailsCheckBox.Checked) {
132        if (listView.SelectedItems.Count == 1) {
133          detailsGroupBox.Enabled = true;
134          viewHost.Content = listView.SelectedItems[0].Tag as IContent;
135        } else {
136          viewHost.Content = null;
137          detailsGroupBox.Enabled = false;
138        }
139      }
140    }
141    private void listView_DoubleClick(object sender, EventArgs e) {
142      if (listView.SelectedItems.Count == 1) {
143        IItem item = (IItem)listView.SelectedItems[0].Tag;
144        IContentView view = MainFormManager.MainForm.ShowContent(item);
145        if (view != null) {
146          view.ReadOnly = true;
147          view.Locked = Locked;
148        }
149      }
150    }
151    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
152      if (!Locked) {
153        ListViewItem listViewItem = (ListViewItem)e.Item;
154        IItem item = (IItem)listViewItem.Tag;
155        if (item != null) {
156          DataObject data = new DataObject();
157          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
158          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
159        }
160      }
161    }
162    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
163      if (showDetailsCheckBox.Checked) {
164        splitContainer.Panel2Collapsed = false;
165        detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
166        viewHost.Content = listView.SelectedItems.Count == 1 ? (IContent)listView.SelectedItems[0].Tag : null;
167      } else {
168        splitContainer.Panel2Collapsed = true;
169        viewHost.Content = null;
170      }
171    }
172    private void showAlgorithmButton_Click(object sender, EventArgs e) {
173      if (!Locked) {
174        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
175      }
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.