Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/VariableView.cs @ 11973

Last change on this file since 11973 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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;
26using HeuristicLab.PluginInfrastructure;
27
28namespace 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;
37
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; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
50    /// </summary>
51    public VariableView() {
52      InitializeComponent();
53    }
54
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
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();
74    }
75
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);
83    }
84
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;
94      }
95    }
96
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;
103    }
104
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;
114      }
115    }
116
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);
122      }
123      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
124        try {
125          Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
126        }
127        catch (Exception ex) {
128          ErrorHandling.ShowErrorDialog(this, ex);
129        }
130      }
131    }
132    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
133      Content.Value = null;
134    }
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;
143      }
144    }
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;
150      }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.