Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4011 was 4011, checked in by mkommend, 14 years ago
  • refactored ViewHost and various views to use fewer nested controls
  • added UnitTests for ContentViews to ensure proper using of the ContentAttribute
  • fixed some views which could not handle null as Content

(ticket #972)

File size: 5.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.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Parameters.Views {
31  /// <summary>
32  /// The visual representation of a <see cref="Parameter"/>.
33  /// </summary>
34  [View("ValueParameter View")]
35  [Content(typeof(OptionalValueParameter<>), true)]
36  [Content(typeof(ValueParameter<>), true)]
37  [Content(typeof(IValueParameter<>), false)]
38  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
39    protected TypeSelectorDialog typeSelectorDialog;
40
41    /// <summary>
42    /// Gets or sets the variable to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No own data storage present.</remarks>
46    public new IValueParameter<T> Content {
47      get { return (IValueParameter<T>)base.Content; }
48      set { base.Content = value; }
49    }
50
51    /// <summary>
52    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
53    /// </summary>
54    public ValueParameterView() {
55      InitializeComponent();
56    }
57
58    /// <summary>
59    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
60    /// </summary>
61    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
62    protected override void DeregisterContentEvents() {
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.ValueChanged += new EventHandler(Content_ValueChanged);
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      if (Content == null) {
79        clearValueButton.Visible = true;
80        valueViewHost.Content = null;
81      } else {
82        clearValueButton.Visible = !(Content is ValueParameter<T>);
83        valueViewHost.ViewType = null;
84        valueViewHost.Content = Content.Value;
85      }
86    }
87
88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
90      setValueButton.Enabled = Content != null && !ReadOnly;
91      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
92      valueGroupBox.Enabled = Content != null;
93    }
94
95    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
96      if (InvokeRequired)
97        Invoke(new EventHandler(Content_ValueChanged), sender, e);
98      else {
99        clearValueButton.Enabled = Content.Value != null && !ReadOnly;
100        valueViewHost.ViewType = null;
101        valueViewHost.Content = Content.Value;
102      }
103    }
104
105    protected virtual void changeValueButton_Click(object sender, EventArgs e) {
106      if (typeSelectorDialog == null) {
107        typeSelectorDialog = new TypeSelectorDialog();
108        typeSelectorDialog.Caption = "Select Value";
109        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
110      }
111      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
112        try {
113          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
114        }
115        catch (Exception ex) {
116          ErrorHandling.ShowErrorDialog(this, ex);
117        }
118      }
119    }
120    protected virtual void setValueButton_Click(object sender, EventArgs e) {
121      Content.Value = null;
122    }
123    protected virtual void valueViewHost_DragEnterOver(object sender, DragEventArgs e) {
124      e.Effect = DragDropEffects.None;
125      Type type = e.Data.GetData("Type") as Type;
126      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
127        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
128        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
129        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
130        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
131        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
132      }
133    }
134    protected virtual void valueViewHost_DragDrop(object sender, DragEventArgs e) {
135      if (e.Effect != DragDropEffects.None) {
136        T value = e.Data.GetData("Value") as T;
137        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
138        Content.Value = value;
139      }
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.