Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2818 was 2818, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • corrected several bugs in order to get SGA working
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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  [Content(typeof(Variable), true)]
32  [Content(typeof(IVariable), false)]
33  public partial class VariableView : NamedItemView {
34    protected TypeSelectorDialog typeSelectorDialog;
35
36    /// <summary>
37    /// Gets or sets the variable to represent visually.
38    /// </summary>
39    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
40    /// No own data storage present.</remarks>
41    public new IVariable Content {
42      get { return (IVariable)base.Content; }
43      set { base.Content = value; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
48    /// </summary>
49    public VariableView() {
50      InitializeComponent();
51      Caption = "Variable";
52    }
53    /// <summary>
54    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
55    /// </summary>
56    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
57    /// <param name="variable">The variable to represent visually.</param>
58    public VariableView(IVariable content)
59      : this() {
60      Content = content;
61    }
62
63    /// <summary>
64    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
65    /// </summary>
66    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
67    protected override void DeregisterContentEvents() {
68      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
69      base.DeregisterContentEvents();
70    }
71
72    /// <summary>
73    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
74    /// </summary>
75    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
76    protected override void RegisterContentEvents() {
77      base.RegisterContentEvents();
78      Content.ValueChanged += new EventHandler(Content_ValueChanged);
79    }
80
81    protected override void OnContentChanged() {
82      base.OnContentChanged();
83      if (Content == null) {
84        Caption = "Variable";
85        dataTypeTextBox.Text = "-";
86        dataTypeTextBox.Enabled = false;
87        setValueButton.Enabled = false;
88        clearValueButton.Enabled = false;
89        valueGroupBox.Enabled = false;
90        viewHost.Content = null;
91      } else {
92        Caption = Content.Name + " (" + Content.GetType().Name + ")";
93        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
94        dataTypeTextBox.Enabled = Content.Value != null;
95        setValueButton.Enabled = Content.Value == null;
96        clearValueButton.Enabled = Content.Value != null;
97        valueGroupBox.Enabled = true;
98        viewHost.Content = Content.Value;
99      }
100    }
101
102    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
103      if (InvokeRequired)
104        Invoke(new EventHandler(Content_ValueChanged), sender, e);
105      else {
106        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
107        dataTypeTextBox.Enabled = Content.Value != null;
108        setValueButton.Enabled = Content.Value == null;
109        clearValueButton.Enabled = Content.Value != null;
110        viewHost.Content = Content.Value;
111      }
112    }
113
114    protected virtual void setValueButton_Click(object sender, EventArgs e) {
115      if (typeSelectorDialog == null) {
116        typeSelectorDialog = new TypeSelectorDialog();
117        typeSelectorDialog.Caption = "Select Value Type";
118        typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, false);
119      }
120      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
121        Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
122      }
123    }
124    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
125      Content.Value = null;
126    }
127    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
128      e.Effect = DragDropEffects.None;
129      Type type = e.Data.GetData("Type") as Type;
130      if ((type != null) && (typeof(IItem).IsAssignableFrom(type))) {
131        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
132        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
133        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
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;
136      }
137    }
138    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
139      if (e.Effect != DragDropEffects.None) {
140        IItem item = e.Data.GetData("Value") as IItem;
141        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
142        Content.Value = item;
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.