Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ValueLookupParameterView.cs @ 2801

Last change on this file since 2801 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: 6.7 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(ValueLookupParameter<>), true)]
38  [Content(typeof(IValueLookupParameter<>), false)]
39  public partial class ValueLookupParameterView<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 IValueLookupParameter<T> Content {
48      get { return (IValueLookupParameter<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 ValueLookupParameterView() {
56      InitializeComponent();
57      Caption = "ValueLookupParameter";
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 ValueLookupParameterView(IValueLookupParameter<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.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
75      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
76      base.DeregisterContentEvents();
77    }
78
79    /// <summary>
80    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
81    /// </summary>
82    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
83    protected override void RegisterContentEvents() {
84      base.RegisterContentEvents();
85      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
86      Content.ValueChanged += new EventHandler(Content_ValueChanged);
87    }
88
89    protected override void OnContentChanged() {
90      base.OnContentChanged();
91      if (Content == null) {
92        Caption = "ValueLookupParameter";
93        actualNameTextBox.Text = "-";
94        actualNameTextBox.Enabled = false;
95        setValueButton.Enabled = false;
96        clearValueButton.Enabled = false;
97        valueGroupBox.Enabled = false;
98        viewHost.Content = null;
99      } else {
100        Caption = Content.Name + " (" + Content.GetType().Name + ")";
101        actualNameTextBox.Text = Content.ActualName;
102        actualNameTextBox.Enabled = true;
103        setValueButton.Enabled = Content.Value == null;
104        clearValueButton.Enabled = Content.Value != null;
105        valueGroupBox.Enabled = true;
106        viewHost.Content = Content.Value;
107      }
108    }
109
110    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
111      if (InvokeRequired)
112        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
113      else
114        actualNameTextBox.Text = Content.ActualName;
115    }
116    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
117      if (InvokeRequired)
118        Invoke(new EventHandler(Content_ValueChanged), sender, e);
119      else {
120        setValueButton.Enabled = Content.Value == null;
121        clearValueButton.Enabled = Content.Value != null;
122        viewHost.Content = Content.Value;
123      }
124    }
125
126    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
127      Content.ActualName = actualNameTextBox.Text;
128    }
129    protected virtual void setValueButton_Click(object sender, EventArgs e) {
130      if (typeSelectorDialog == null) {
131        typeSelectorDialog = new TypeSelectorDialog();
132        typeSelectorDialog.Caption = "Select Value";
133        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
134      }
135      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
136        Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
137    }
138    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
139      Content.Value = null;
140    }
141    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
142      e.Effect = DragDropEffects.None;
143      Type type = e.Data.GetData("Type") as Type;
144      if ((type != null) && (Content.DataType.IsAssignableFrom(type))) {
145        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
146        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
147        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
148        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
149        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
150      }
151    }
152    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
153      if (e.Effect != DragDropEffects.None) {
154        T value = e.Data.GetData("Value") as T;
155        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
156        Content.Value = value;
157      }
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.