Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on UI support for choosing generic type parameters (#42)

File size: 6.5 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
57    /// <summary>
58    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
59    /// </summary>
60    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
61    protected override void DeregisterContentEvents() {
62      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
63      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
64      base.DeregisterContentEvents();
65    }
66
67    /// <summary>
68    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
73      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
74      Content.ValueChanged += new EventHandler(Content_ValueChanged);
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        Caption = "ValueLookupParameter";
81        actualNameTextBox.Text = "-";
82        viewHost.Content = null;
83      } else {
84        Caption = Content.Name + " (" + Content.GetType().Name + ")";
85        actualNameTextBox.Text = Content.ActualName;
86        viewHost.ViewType = null;
87        viewHost.Content = Content.Value;
88      }
89      SetEnabledStateOfControls();
90    }
91
92    protected override void OnReadOnlyChanged() {
93      base.OnReadOnlyChanged();
94      SetEnabledStateOfControls();
95    }
96
97    private void SetEnabledStateOfControls() {
98      actualNameTextBox.Enabled = Content != null;
99      actualNameTextBox.ReadOnly = ReadOnly;
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_ActualNameChanged(object sender, EventArgs e) {
106      if (InvokeRequired)
107        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
108      else
109        actualNameTextBox.Text = Content.ActualName;
110    }
111    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
112      if (InvokeRequired)
113        Invoke(new EventHandler(Content_ValueChanged), sender, e);
114      else {
115        clearValueButton.Enabled = Content.Value != null && !ReadOnly;
116        viewHost.ViewType = null;
117        viewHost.Content = Content.Value;
118      }
119    }
120
121    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
122      Content.ActualName = actualNameTextBox.Text;
123    }
124    protected virtual void setValueButton_Click(object sender, EventArgs e) {
125      if (typeSelectorDialog == null) {
126        typeSelectorDialog = new TypeSelectorDialog();
127        typeSelectorDialog.Caption = "Select Value";
128        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
129      }
130      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
131        try {
132          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
133        }
134        catch (Exception ex) {
135          Auxiliary.ShowErrorMessageBox(ex);
136        }
137      }
138    }
139    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
140      Content.Value = null;
141    }
142    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
143      e.Effect = DragDropEffects.None;
144      Type type = e.Data.GetData("Type") as Type;
145      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
146        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
147        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
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        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
151      }
152    }
153    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
154      if (e.Effect != DragDropEffects.None) {
155        T value = e.Data.GetData("Value") as T;
156        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
157        Content.Value = value;
158      }
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.