Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893)

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