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