Free cookie consent management tool by TermsFeed Policy Generator

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

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

Adapted views of HeuristicLab.Parameters.Views according the new read-only property (#973).

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