#1587: Updated `ViewHost` after node selection with arrow keys and handled item move...
[hl.git] / sources / HeuristicLab.Core.Views / 3.3 / VariableView.cs
blob411c75cd248ef6894b07759c7e5f2b23226b18f6
1 #region License Information
2 /* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 * This file is part of HeuristicLab.
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.
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.
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/>.
20 #endregion
22 using System;
23 using System.Windows.Forms;
24 using HeuristicLab.Common;
25 using HeuristicLab.MainForm;
26 using HeuristicLab.PluginInfrastructure;
28 namespace HeuristicLab.Core.Views {
29 /// <summary>
30 /// The visual representation of a <see cref="Variable"/>.
31 /// </summary>
32 [View("Variable View")]
33 [Content(typeof(Variable), true)]
34 [Content(typeof(IVariable), false)]
35 public partial class VariableView : NamedItemView {
36 protected TypeSelectorDialog typeSelectorDialog;
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>
43 public new IVariable Content {
44 get { return (IVariable)base.Content; }
45 set { base.Content = value; }
48 /// <summary>
49 /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
50 /// </summary>
51 public VariableView() {
52 InitializeComponent();
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();
64 base.Dispose(disposing);
67 /// <summary>
68 /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
69 /// </summary>
70 /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71 protected override void DeregisterContentEvents() {
72 Content.ValueChanged -= new EventHandler(Content_ValueChanged);
73 base.DeregisterContentEvents();
76 /// <summary>
77 /// Adds eventhandlers to the underlying <see cref="Variable"/>.
78 /// </summary>
79 /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
80 protected override void RegisterContentEvents() {
81 base.RegisterContentEvents();
82 Content.ValueChanged += new EventHandler(Content_ValueChanged);
85 protected override void OnContentChanged() {
86 base.OnContentChanged();
87 if (Content == null) {
88 dataTypeTextBox.Text = "-";
89 viewHost.Content = null;
90 } else {
91 dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
92 viewHost.ViewType = null;
93 viewHost.Content = Content.Value;
97 protected override void SetEnabledStateOfControls() {
98 base.SetEnabledStateOfControls();
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;
105 protected virtual void Content_ValueChanged(object sender, EventArgs e) {
106 if (InvokeRequired)
107 Invoke(new EventHandler(Content_ValueChanged), sender, e);
108 else {
109 dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
110 dataTypeTextBox.Enabled = Content.Value != null;
111 clearValueButton.Enabled = Content.Value != null;
112 viewHost.ViewType = null;
113 viewHost.Content = Content.Value;
117 protected virtual void setValueButton_Click(object sender, EventArgs e) {
118 if (typeSelectorDialog == null) {
119 typeSelectorDialog = new TypeSelectorDialog();
120 typeSelectorDialog.Caption = "Select Value";
121 typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
123 if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
124 try {
125 Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
127 catch (Exception ex) {
128 ErrorHandling.ShowErrorDialog(this, ex);
132 protected virtual void clearValueButton_Click(object sender, EventArgs e) {
133 Content.Value = null;
135 protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
136 e.Effect = DragDropEffects.None;
137 if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IItem)) {
138 if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link; // ALT key
139 else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move; // SHIFT key
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;
145 protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
146 if (e.Effect != DragDropEffects.None) {
147 IItem item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
148 if (e.Effect.HasFlag(DragDropEffects.Copy)) item = (IItem)item.Clone();
149 Content.Value = item;