1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Windows.Forms;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.PluginInfrastructure;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Parameters.Views {
|
---|
31 | /// <summary>
|
---|
32 | /// The visual representation of a <see cref="Parameter"/>.
|
---|
33 | /// </summary>
|
---|
34 | [View("ValueParameter View")]
|
---|
35 | [Content(typeof(OptionalValueParameter<>), true)]
|
---|
36 | [Content(typeof(ValueParameter<>), true)]
|
---|
37 | [Content(typeof(IValueParameter<>), false)]
|
---|
38 | public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
|
---|
39 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Gets or sets the variable to represent visually.
|
---|
43 | /// </summary>
|
---|
44 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
45 | /// No own data storage present.</remarks>
|
---|
46 | public new IValueParameter<T> Content {
|
---|
47 | get { return (IValueParameter<T>)base.Content; }
|
---|
48 | set { base.Content = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
53 | /// </summary>
|
---|
54 | public ValueParameterView() {
|
---|
55 | InitializeComponent();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Clean up any resources being used.
|
---|
60 | /// </summary>
|
---|
61 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
62 | protected override void Dispose(bool disposing) {
|
---|
63 | if (disposing) {
|
---|
64 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
65 | if (components != null) components.Dispose();
|
---|
66 | }
|
---|
67 | base.Dispose(disposing);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
|
---|
72 | /// </summary>
|
---|
73 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
74 | protected override void DeregisterContentEvents() {
|
---|
75 | Content.GetsCollectedChanged -= new EventHandler(Content_GetsCollectedChanged);
|
---|
76 | Content.ValueChanged -= new EventHandler(Content_ValueChanged);
|
---|
77 | base.DeregisterContentEvents();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
|
---|
82 | /// </summary>
|
---|
83 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
84 | protected override void RegisterContentEvents() {
|
---|
85 | base.RegisterContentEvents();
|
---|
86 | Content.GetsCollectedChanged += new EventHandler(Content_GetsCollectedChanged);
|
---|
87 | Content.ValueChanged += new EventHandler(Content_ValueChanged);
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected override void OnContentChanged() {
|
---|
91 | base.OnContentChanged();
|
---|
92 | if (Content == null) {
|
---|
93 | showInRunCheckBox.Checked = false;
|
---|
94 | valueViewHost.Content = null;
|
---|
95 | } else {
|
---|
96 | SetDataTypeTextBoxText();
|
---|
97 | showInRunCheckBox.Checked = Content.GetsCollected;
|
---|
98 | valueViewHost.ViewType = null;
|
---|
99 | valueViewHost.Content = Content.Value;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected override void SetEnabledStateOfControls() {
|
---|
104 | base.SetEnabledStateOfControls();
|
---|
105 | setValueButton.Enabled = Content != null && !ReadOnly;
|
---|
106 | clearValueButton.Enabled = Content != null && Content.Value != null && !(Content is ValueParameter<T>) && !ReadOnly;
|
---|
107 | showInRunCheckBox.Enabled = Content != null && !ReadOnly;
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected virtual void Content_ValueChanged(object sender, EventArgs e) {
|
---|
111 | if (InvokeRequired)
|
---|
112 | Invoke(new EventHandler(Content_ValueChanged), sender, e);
|
---|
113 | else {
|
---|
114 | SetDataTypeTextBoxText();
|
---|
115 | clearValueButton.Enabled = Content != null && Content.Value != null && !(Content is ValueParameter<T>) && !ReadOnly;
|
---|
116 | valueViewHost.ViewType = null;
|
---|
117 | valueViewHost.Content = Content != null ? Content.Value : null;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | protected virtual void Content_GetsCollectedChanged(object sender, EventArgs e) {
|
---|
121 | if (InvokeRequired)
|
---|
122 | Invoke(new EventHandler(Content_GetsCollectedChanged), sender, e);
|
---|
123 | else
|
---|
124 | showInRunCheckBox.Checked = Content != null && Content.GetsCollected;
|
---|
125 | }
|
---|
126 |
|
---|
127 | protected virtual void setValueButton_Click(object sender, EventArgs e) {
|
---|
128 | if (typeSelectorDialog == null) {
|
---|
129 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
130 | typeSelectorDialog.Caption = "Select Value";
|
---|
131 | typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
|
---|
132 | }
|
---|
133 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
134 | try {
|
---|
135 | Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
136 | }
|
---|
137 | catch (Exception ex) {
|
---|
138 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | protected virtual void clearValueButton_Click(object sender, EventArgs e) {
|
---|
143 | Content.Value = null;
|
---|
144 | }
|
---|
145 | protected virtual void showInRunCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
146 | if (Content != null) Content.GetsCollected = showInRunCheckBox.Checked;
|
---|
147 | }
|
---|
148 | protected virtual void valueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
|
---|
149 | e.Effect = DragDropEffects.None;
|
---|
150 | if (!ReadOnly && (e.Data.GetData("HeuristicLab") != null) && Content.DataType.IsAssignableFrom(e.Data.GetData("HeuristicLab").GetType())) {
|
---|
151 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
152 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
153 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
154 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
155 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | protected virtual void valueGroupBox_DragDrop(object sender, DragEventArgs e) {
|
---|
159 | if (e.Effect != DragDropEffects.None) {
|
---|
160 | T value = e.Data.GetData("HeuristicLab") as T;
|
---|
161 | if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
|
---|
162 | Content.Value = value;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | #region Helpers
|
---|
167 | protected void SetDataTypeTextBoxText() {
|
---|
168 | if (Content == null) {
|
---|
169 | dataTypeTextBox.Text = "-";
|
---|
170 | } else {
|
---|
171 | if ((Content.Value != null) && (Content.Value.GetType() != Content.DataType))
|
---|
172 | dataTypeTextBox.Text = Content.DataType.GetPrettyName() + " (" + Content.Value.GetType().GetPrettyName() + ")";
|
---|
173 | else
|
---|
174 | dataTypeTextBox.Text = Content.DataType.GetPrettyName();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | #endregion
|
---|
178 | }
|
---|
179 | }
|
---|