[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;
|
---|
[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 |
|
---|
[776] | 55 | /// <summary>
|
---|
[2687] | 56 | /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
|
---|
[776] | 57 | /// </summary>
|
---|
| 58 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
[2713] | 59 | protected override void DeregisterContentEvents() {
|
---|
| 60 | Content.ValueChanged -= new EventHandler(Content_ValueChanged);
|
---|
| 61 | base.DeregisterContentEvents();
|
---|
[2] | 62 | }
|
---|
[2655] | 63 |
|
---|
[776] | 64 | /// <summary>
|
---|
[2687] | 65 | /// Adds eventhandlers to the underlying <see cref="Variable"/>.
|
---|
[776] | 66 | /// </summary>
|
---|
| 67 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
[2713] | 68 | protected override void RegisterContentEvents() {
|
---|
| 69 | base.RegisterContentEvents();
|
---|
| 70 | Content.ValueChanged += new EventHandler(Content_ValueChanged);
|
---|
[2] | 71 | }
|
---|
| 72 |
|
---|
[2713] | 73 | protected override void OnContentChanged() {
|
---|
| 74 | base.OnContentChanged();
|
---|
| 75 | if (Content == null) {
|
---|
[2655] | 76 | dataTypeTextBox.Text = "-";
|
---|
[2713] | 77 | viewHost.Content = null;
|
---|
[2] | 78 | } else {
|
---|
[2818] | 79 | dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
|
---|
[2949] | 80 | viewHost.ViewType = null;
|
---|
[2713] | 81 | viewHost.Content = Content.Value;
|
---|
[2] | 82 | }
|
---|
[3362] | 83 | SetEnabledStateOfControls();
|
---|
[2] | 84 | }
|
---|
| 85 |
|
---|
[3362] | 86 | protected override void OnReadOnlyChanged() {
|
---|
| 87 | base.OnReadOnlyChanged();
|
---|
| 88 | SetEnabledStateOfControls();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private void SetEnabledStateOfControls() {
|
---|
| 92 | dataTypeTextBox.Enabled = Content != null && Content.Value != null;
|
---|
| 93 | setValueButton.Enabled = Content != null && !ReadOnly;
|
---|
| 94 | clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
|
---|
| 95 | valueGroupBox.Enabled = Content != null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2713] | 98 | protected virtual void Content_ValueChanged(object sender, EventArgs e) {
|
---|
[2] | 99 | if (InvokeRequired)
|
---|
[2713] | 100 | Invoke(new EventHandler(Content_ValueChanged), sender, e);
|
---|
[2655] | 101 | else {
|
---|
[2818] | 102 | dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
|
---|
[2713] | 103 | dataTypeTextBox.Enabled = Content.Value != null;
|
---|
| 104 | clearValueButton.Enabled = Content.Value != null;
|
---|
[2949] | 105 | viewHost.ViewType = null;
|
---|
[2713] | 106 | viewHost.Content = Content.Value;
|
---|
[2655] | 107 | }
|
---|
[2] | 108 | }
|
---|
| 109 |
|
---|
[2676] | 110 | protected virtual void setValueButton_Click(object sender, EventArgs e) {
|
---|
[2655] | 111 | if (typeSelectorDialog == null) {
|
---|
| 112 | typeSelectorDialog = new TypeSelectorDialog();
|
---|
[3407] | 113 | typeSelectorDialog.Caption = "Select Value";
|
---|
[3588] | 114 | typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
|
---|
[2655] | 115 | }
|
---|
| 116 | if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
[3407] | 117 | try {
|
---|
| 118 | Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
|
---|
| 119 | }
|
---|
| 120 | catch (Exception ex) {
|
---|
[3758] | 121 | ErrorHandling.ShowErrorDialog(this, ex);
|
---|
[3407] | 122 | }
|
---|
[2655] | 123 | }
|
---|
[2] | 124 | }
|
---|
[2676] | 125 | protected virtual void clearValueButton_Click(object sender, EventArgs e) {
|
---|
[2713] | 126 | Content.Value = null;
|
---|
[2655] | 127 | }
|
---|
[2676] | 128 | protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[2655] | 129 | e.Effect = DragDropEffects.None;
|
---|
| 130 | Type type = e.Data.GetData("Type") as Type;
|
---|
[3362] | 131 | if (!ReadOnly && (type != null) && (typeof(IItem).IsAssignableFrom(type))) {
|
---|
[3694] | 132 | if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
|
---|
[2694] | 133 | else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
|
---|
| 134 | else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
|
---|
| 135 | else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
|
---|
[3526] | 136 | else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
|
---|
[2] | 137 | }
|
---|
| 138 | }
|
---|
[2676] | 139 | protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
|
---|
[2655] | 140 | if (e.Effect != DragDropEffects.None) {
|
---|
| 141 | IItem item = e.Data.GetData("Value") as IItem;
|
---|
| 142 | if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
|
---|
[2713] | 143 | Content.Value = item;
|
---|
[2655] | 144 | }
|
---|
| 145 | }
|
---|
[2] | 146 | }
|
---|
| 147 | }
|
---|