Free cookie consent management tool by TermsFeed Policy Generator

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

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

Prevented drag operations of items which are null (#947)

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