Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.Core.Views/3.3/VariableView.cs @ 16811

Last change on this file since 16811 was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

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