Free cookie consent management tool by TermsFeed Policy Generator

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

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

Restricted types of child operators in MultiOperator (#979)

File size: 7.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.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Parameters.Views {
30  /// <summary>
31  /// The visual representation of a <see cref="Parameter"/>.
32  /// </summary>
33  [View("ValueLookupParameter View")]
34  [Content(typeof(ValueLookupParameter<>), true)]
35  [Content(typeof(IValueLookupParameter<>), false)]
36  public partial class ValueLookupParameterView<T> : ParameterView where T : class, IItem {
37    protected TypeSelectorDialog typeSelectorDialog;
38
39    /// <summary>
40    /// Gets or sets the variable to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public new IValueLookupParameter<T> Content {
45      get { return (IValueLookupParameter<T>)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
51    /// </summary>
52    public ValueLookupParameterView() {
53      InitializeComponent();
54      Caption = "ValueLookupParameter";
55    }
56    /// <summary>
57    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
60    /// <param name="variable">The variable to represent visually.</param>
61    public ValueLookupParameterView(IValueLookupParameter<T> content)
62      : this() {
63      Content = content;
64    }
65
66    /// <summary>
67    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
68    /// </summary>
69    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
70    protected override void DeregisterContentEvents() {
71      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
72      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
73      base.DeregisterContentEvents();
74    }
75
76    /// <summary>
77    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
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.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
83      Content.ValueChanged += new EventHandler(Content_ValueChanged);
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      if (Content == null) {
89        Caption = "ValueLookupParameter";
90        actualNameTextBox.Text = "-";
91        viewHost.Content = null;
92      } else {
93        Caption = Content.Name + " (" + Content.GetType().Name + ")";
94        actualNameTextBox.Text = Content.ActualName;
95        viewHost.ViewType = null;
96        viewHost.Content = Content.Value;
97      }
98      SetEnabledStateOfControls();
99    }
100
101    protected override void OnReadOnlyChanged() {
102      base.OnReadOnlyChanged();
103      SetEnabledStateOfControls();
104    }
105
106    private void SetEnabledStateOfControls() {
107      actualNameTextBox.Enabled = Content != null;
108      actualNameTextBox.ReadOnly = ReadOnly;
109      setValueButton.Enabled = Content != null && !ReadOnly;
110      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
111      valueGroupBox.Enabled = Content != null;
112      viewHost.ReadOnly = ReadOnly;
113    }
114
115    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
116      if (InvokeRequired)
117        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
118      else
119        actualNameTextBox.Text = Content.ActualName;
120    }
121    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
122      if (InvokeRequired)
123        Invoke(new EventHandler(Content_ValueChanged), sender, e);
124      else {
125        clearValueButton.Enabled = Content.Value != null && !ReadOnly;
126        viewHost.ViewType = null;
127        viewHost.Content = Content.Value;
128      }
129    }
130
131    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
132      Content.ActualName = actualNameTextBox.Text;
133    }
134    protected virtual void setValueButton_Click(object sender, EventArgs e) {
135      if (typeSelectorDialog == null) {
136        typeSelectorDialog = new TypeSelectorDialog();
137        typeSelectorDialog.Caption = "Select Value";
138        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
139      }
140      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
141        try {
142          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
143        }
144        catch (Exception ex) {
145          Auxiliary.ShowErrorMessageBox(ex);
146        }
147      }
148    }
149    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
150      Content.Value = null;
151    }
152    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
153      e.Effect = DragDropEffects.None;
154      Type type = e.Data.GetData("Type") as Type;
155      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
156        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
157        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
158        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
159        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
160        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
161      }
162    }
163    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
164      if (e.Effect != DragDropEffects.None) {
165        T value = e.Data.GetData("Value") as T;
166        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
167        Content.Value = value;
168      }
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.