Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ValueParameterView.cs @ 2800

Last change on this file since 2800 was 2790, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 5.9 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Parameters.Views {
34  /// <summary>
35  /// The visual representation of a <see cref="Parameter"/>.
36  /// </summary>
37  [Content(typeof(ValueParameter<>), true)]
38  [Content(typeof(IValueParameter<>), false)]
39  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
40    protected TypeSelectorDialog typeSelectorDialog;
41
42    /// <summary>
43    /// Gets or sets the variable to represent visually.
44    /// </summary>
45    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
46    /// No own data storage present.</remarks>
47    public new IValueParameter<T> Content {
48      get { return (IValueParameter<T>)base.Content; }
49      set { base.Content = value; }
50    }
51
52    /// <summary>
53    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
54    /// </summary>
55    public ValueParameterView() {
56      InitializeComponent();
57      Caption = "ValueParameter";
58    }
59    /// <summary>
60    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
61    /// </summary>
62    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
63    /// <param name="variable">The variable to represent visually.</param>
64    public ValueParameterView(IValueParameter<T> content)
65      : this() {
66      Content = content;
67    }
68
69    /// <summary>
70    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
71    /// </summary>
72    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
73    protected override void DeregisterContentEvents() {
74      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
75      base.DeregisterContentEvents();
76    }
77
78    /// <summary>
79    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
80    /// </summary>
81    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.ValueChanged += new EventHandler(Content_ValueChanged);
85    }
86
87    protected override void OnContentChanged() {
88      base.OnContentChanged();
89      if (Content == null) {
90        Caption = "ValueParameter";
91        setValueButton.Enabled = false;
92        clearValueButton.Enabled = false;
93        valueGroupBox.Enabled = false;
94        viewHost.Content = null;
95      } else {
96        Caption = Content.Name + " (" + Content.GetType().Name + ")";
97        setValueButton.Enabled = Content.Value == null;
98        clearValueButton.Enabled = Content.Value != null;
99        valueGroupBox.Enabled = true;
100        viewHost.Content = Content.Value;
101      }
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        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";
118        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
119      }
120      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
121        Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
122    }
123    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
124      Content.Value = null;
125    }
126    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
127      e.Effect = DragDropEffects.None;
128      Type type = e.Data.GetData("Type") as Type;
129      if ((type != null) && (Content.DataType.IsAssignableFrom(type))) {
130        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
131        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
132        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
133        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
134        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
135      }
136    }
137    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
138      if (e.Effect != DragDropEffects.None) {
139        T value = e.Data.GetData("Value") as T;
140        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
141        Content.Value = value;
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.