[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[2818] | 24 | using HeuristicLab.Common;
|
---|
[2520] | 25 | using HeuristicLab.MainForm;
|
---|
[3758] | 26 | using HeuristicLab.PluginInfrastructure;
|
---|
[2] | 27 |
|
---|
[2520] | 28 | namespace HeuristicLab.Core.Views {
|
---|
[776] | 29 | /// <summary>
|
---|
[2655] | 30 | /// The visual representation of a <see cref="Variable"/>.
|
---|
[776] | 31 | /// </summary>
|
---|
[2917] | 32 | [View("Variable View")]
|
---|
[2520] | 33 | [Content(typeof(Variable), true)]
|
---|
[2727] | 34 | [Content(typeof(IVariable), false)]
|
---|
[2664] | 35 | public partial class VariableView : NamedItemView {
|
---|
[2676] | 36 | protected TypeSelectorDialog typeSelectorDialog;
|
---|
[2] | 37 |
|
---|
[776] | 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>
|
---|
[2727] | 43 | public new IVariable Content {
|
---|
| 44 | get { return (IVariable)base.Content; }
|
---|
[2713] | 45 | set { base.Content = value; }
|
---|
[2] | 46 | }
|
---|
| 47 |
|
---|
[776] | 48 | /// <summary>
|
---|
| 49 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
| 50 | /// </summary>
|
---|
[2] | 51 | public VariableView() {
|
---|
| 52 | InitializeComponent();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[5237] | 55 | /// <summary>
|
---|
| 56 | /// Clean up any resources being used.
|
---|
| 57 | /// </summary>
|
---|
| 58 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
---|
| 59 | protected override void Dispose(bool disposing) {
|
---|
| 60 | if (disposing) {
|
---|
| 61 | if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
|
---|
| 62 | if (components != null) components.Dispose();
|
---|
| 63 | }
|
---|
| 64 | base.Dispose(disposing);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[776] | 67 | /// <summary>
|
---|
[2687] | 68 | /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
|
---|
[776] | 69 | /// </summary>
|
---|
| 70 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
[2713] | 71 | protected override void DeregisterContentEvents() {
|
---|
| 72 | Content.ValueChanged -= new EventHandler(Content_ValueChanged);
|
---|
| 73 | base.DeregisterContentEvents();
|
---|
[2] | 74 | }
|
---|
[2655] | 75 |
|
---|
[776] | 76 | /// <summary>
|
---|
[2687] | 77 | /// Adds eventhandlers to the underlying <see cref="Variable"/>.
|
---|
[776] | 78 | /// </summary>
|
---|
| 79 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
[2713] | 80 | protected override void RegisterContentEvents() {
|
---|
| 81 | base.RegisterContentEvents();
|
---|
| 82 | Content.ValueChanged += new EventHandler(Content_ValueChanged);
|
---|
[2] | 83 | }
|
---|
| 84 |
|
---|
[2713] | 85 | protected override void OnContentChanged() {
|
---|
| 86 | base.OnContentChanged();
|
---|
| 87 | if (Content == null) {
|
---|
[2655] | 88 | dataTypeTextBox.Text = "-";
|
---|
[2713] | 89 | viewHost.Content = null;
|
---|
[2] | 90 | } else {
|
---|
[2818] | 91 | dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
|
---|
[2949] | 92 | viewHost.ViewType = null;
|
---|
[2713] | 93 | viewHost.Content = Content.Value;
|
---|
[2] | 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[3904] | 97 | protected override void SetEnabledStateOfControls() {
|
---|
| 98 | base.SetEnabledStateOfControls();
|
---|
[3362] | 99 | dataTypeTextBox.Enabled = Content != null && Content.Value != null;
|
---|
| 100 | setValueButton.Enabled = Content != null && !ReadOnly;
|
---|
| 101 | clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
|
---|
| 102 | valueGroupBox.Enabled = Content != null;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2713] | 105 | protected virtual void Content_ValueChanged(object sender, EventArgs e) {
|
---|
[2] | 106 | if (InvokeRequired)
|
---|
[2713] | 107 | Invoke(new EventHandler(Content_ValueChanged), sender, e);
|
---|
[2655] | 108 | else {
|
---|
[2818] | 109 | dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
|
---|
[2713] | 110 | dataTypeTextBox.Enabled = Content.Value != null;
|
---|
| 111 | clearValueButton.Enabled = Content.Value != null;
|
---|
[2949] | 112 | viewHost.ViewType = null;
|
---|
[2713] | 113 | viewHost.Content = Content.Value;
|
---|
[2655] | 114 | }
|
---|
[2] | 115 | }
|
---|
| 116 |
|
---|
[2676] | 117 | protected virtual void setValueButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 118 | if (typeSelectorDialog == null) {
|
---|
| 119 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
[3407] | 120 | typeSelectorDialog.Caption = "Select Value";
|
---|
[3588] | 121 | typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
|
---|
[2655] | 122 | }
|
---|
| 123 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3407] | 124 | try {
|
---|
| 125 | Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 126 | }
|
---|
| 127 | catch (Exception ex) {
|
---|
[3758] | 128 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 129 | }
|
---|
[2655] | 130 | }
|
---|
[2] | 131 | }
|
---|
[2676] | 132 | protected virtual void clearValueButton_Click(object sender, EventArgs e) {
|
---|
[2713] | 133 | Content.Value = null;
|
---|
[2655] | 134 | }
|
---|
[2676] | 135 | protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[2655] | 136 | e.Effect = DragDropEffects.None;
|
---|
[5837] | 137 | if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IItem)) {
|
---|
[3694] | 138 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[2694] | 139 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
[5744] | 140 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
|
---|
| 141 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
|
---|
| 142 | else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
|
---|
[2] | 143 | }
|
---|
| 144 | }
|
---|
[2676] | 145 | protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
|
---|
[2655] | 146 | if (e.Effect != DragDropEffects.None) {
|
---|
[5837] | 147 | IItem item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
|
---|
[5744] | 148 | if (e.Effect.HasFlag(DragDropEffects.Copy)) item = (IItem)item.Clone();
|
---|
[2713] | 149 | Content.Value = item;
|
---|
[2655] | 150 | }
|
---|
| 151 | }
|
---|
[2] | 152 | }
|
---|
| 153 | }
|
---|