Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed manual propagation of ReadOnly property (#973)

File size: 6.3 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("ValueParameter View")]
34  [Content(typeof(OptionalValueParameter<>), true)]
35  [Content(typeof(ValueParameter<>), true)]
36  [Content(typeof(IValueParameter<>), false)]
37  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
38    protected TypeSelectorDialog typeSelectorDialog;
39
40    /// <summary>
41    /// Gets or sets the variable to represent visually.
42    /// </summary>
43    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
44    /// No own data storage present.</remarks>
45    public new IValueParameter<T> Content {
46      get { return (IValueParameter<T>)base.Content; }
47      set { base.Content = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
52    /// </summary>
53    public ValueParameterView() {
54      InitializeComponent();
55      Caption = "ValueParameter";
56    }
57    /// <summary>
58    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
59    /// </summary>
60    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
61    /// <param name="variable">The variable to represent visually.</param>
62    public ValueParameterView(IValueParameter<T> content)
63      : this() {
64      Content = content;
65    }
66
67    /// <summary>
68    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
69    /// </summary>
70    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
71    protected override void DeregisterContentEvents() {
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.ValueChanged += new EventHandler(Content_ValueChanged);
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      if (Content == null) {
88        Caption = "ValueParameter";
89        clearValueButton.Visible = true;
90        viewHost.Content = null;
91      } else {
92        Caption = Content.Name + " (" + Content.GetType().Name + ")";
93        clearValueButton.Visible = !(Content is ValueParameter<T>);
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      setValueButton.Enabled = Content != null && !ReadOnly;
107      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
108      valueGroupBox.Enabled = Content != null;
109    }
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 changeValueButton_Click(object sender, EventArgs e) {
122      if (typeSelectorDialog == null) {
123        typeSelectorDialog = new TypeSelectorDialog();
124        typeSelectorDialog.Caption = "Select Value";
125        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
126      }
127      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
128        try {
129          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
130        }
131        catch (Exception ex) {
132          Auxiliary.ShowErrorMessageBox(ex);
133        }
134      }
135    }
136    protected virtual void setValueButton_Click(object sender, EventArgs e) {
137      Content.Value = null;
138    }
139    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
140      e.Effect = DragDropEffects.None;
141      Type type = e.Data.GetData("Type") as Type;
142      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
143        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
144        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
145        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
146        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
147        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
148      }
149    }
150    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
151      if (e.Effect != DragDropEffects.None) {
152        T value = e.Data.GetData("Value") as T;
153        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
154        Content.Value = value;
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.