Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4096 was 4096, checked in by swagner, 14 years ago

Enabled hiding details in all collection views (#1095)

File size: 6.5 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 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      viewHost.Enabled = Content != null;
80      changeColorButton.Enabled = Content != null;
81      showAlgorithmButton.Enabled = Content != null && !Locked;
82    }
83
84    private void changeColorButton_Click(object sender, EventArgs e) {
85      if (colorDialog.ShowDialog(this) == DialogResult.OK) {
86        this.Content.Color = this.colorDialog.Color;
87      }
88    }
89    private void UpdateColor() {
90      this.colorDialog.Color = this.Content.Color;
91      this.colorArea.BackColor = this.Content.Color;
92    }
93
94    private string selectedName;
95    private void FillListView() {
96      if (listView.SelectedItems.Count == 1) selectedName = listView.SelectedItems[0].SubItems[0].Text;
97
98      listView.Items.Clear();
99      listView.SmallImageList.Images.Clear();
100      if (Content != null) {
101        foreach (string key in Content.Parameters.Keys)
102          CreateListViewItem(key, Content.Parameters[key], listView.Groups["parametersGroup"], selectedName);
103        foreach (string key in Content.Results.Keys)
104          CreateListViewItem(key, Content.Results[key], listView.Groups["resultsGroup"], selectedName);
105        if (listView.Items.Count > 0) {
106          for (int i = 0; i < listView.Columns.Count; i++)
107            listView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
108          selectedName = null;
109        }
110      }
111    }
112
113    private void CreateListViewItem(string name, IItem value, ListViewGroup group, string selectedName) {
114      ListViewItem item = new ListViewItem(new string[] { name, value != null ? value.ToString() : "-" });
115      item.Tag = value;
116      item.Group = group;
117      listView.SmallImageList.Images.Add(value == null ? HeuristicLab.Common.Resources.VS2008ImageLibrary.Nothing : value.ItemImage);
118      item.ImageIndex = listView.SmallImageList.Images.Count - 1;
119      listView.Items.Add(item);
120      if ((selectedName != null) && name.Equals(selectedName)) item.Selected = true;
121    }
122
123    private void listView_SelectedIndexChanged(object sender, EventArgs e) {
124      if (showDetailsCheckBox.Checked) {
125        if (listView.SelectedItems.Count == 1) {
126          detailsGroupBox.Enabled = true;
127          viewHost.Content = listView.SelectedItems[0].Tag as IContent;
128        } else {
129          viewHost.Content = null;
130          detailsGroupBox.Enabled = false;
131        }
132      }
133    }
134    private void listView_DoubleClick(object sender, EventArgs e) {
135      if (listView.SelectedItems.Count == 1) {
136        IItem item = (IItem)listView.SelectedItems[0].Tag;
137        IContentView view = MainFormManager.MainForm.ShowContent(item);
138        if (view != null) {
139          view.ReadOnly = true;
140          view.Locked = Locked;
141        }
142      }
143    }
144    private void listView_ItemDrag(object sender, ItemDragEventArgs e) {
145      if (!Locked) {
146        ListViewItem listViewItem = (ListViewItem)e.Item;
147        IItem item = (IItem)listViewItem.Tag;
148        if (item != null) {
149          DataObject data = new DataObject();
150          data.SetData("Type", item.GetType());
151          data.SetData("Value", item);
152          DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
153        }
154      }
155    }
156    private void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
157      if (showDetailsCheckBox.Checked) {
158        splitContainer.Panel2Collapsed = false;
159        detailsGroupBox.Enabled = listView.SelectedItems.Count == 1;
160        viewHost.Content = listView.SelectedItems.Count == 1 ? (IContent)listView.SelectedItems[0].Tag : null;
161      } else {
162        splitContainer.Panel2Collapsed = true;
163        viewHost.Content = null;
164        viewHost.ClearCache();
165      }
166    }
167    private void showAlgorithmButton_Click(object sender, EventArgs e) {
168      if (!Locked) {
169        MainFormManager.MainForm.ShowContent((IContent)Content.Algorithm.Clone());
170      }
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.