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