Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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.Core;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27using HeuristicLab.PluginInfrastructure;
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    }
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.ValueChanged -= new EventHandler(Content_ValueChanged);
63      base.DeregisterContentEvents();
64    }
65
66    /// <summary>
67    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
68    /// </summary>
69    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
70    protected override void RegisterContentEvents() {
71      base.RegisterContentEvents();
72      Content.ValueChanged += new EventHandler(Content_ValueChanged);
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        clearValueButton.Visible = true;
79        valueViewHost.Content = null;
80      } else {
81        clearValueButton.Visible = !(Content is ValueParameter<T>);
82        valueViewHost.ViewType = null;
83        valueViewHost.Content = Content.Value;
84      }
85    }
86
87    protected override void SetEnabledStateOfControls() {
88      base.SetEnabledStateOfControls();
89      setValueButton.Enabled = Content != null && !ReadOnly;
90      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
91      valueGroupBox.Enabled = Content != null;
92    }
93
94    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
95      if (InvokeRequired)
96        Invoke(new EventHandler(Content_ValueChanged), sender, e);
97      else {
98        clearValueButton.Enabled = Content.Value != null && !ReadOnly;
99        valueViewHost.ViewType = null;
100        valueViewHost.Content = Content.Value;
101      }
102    }
103
104    protected virtual void changeValueButton_Click(object sender, EventArgs e) {
105      if (typeSelectorDialog == null) {
106        typeSelectorDialog = new TypeSelectorDialog();
107        typeSelectorDialog.Caption = "Select Value";
108        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
109      }
110      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
111        try {
112          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
113        }
114        catch (Exception ex) {
115          ErrorHandling.ShowErrorDialog(this, ex);
116        }
117      }
118    }
119    protected virtual void setValueButton_Click(object sender, EventArgs e) {
120      Content.Value = null;
121    }
122    protected virtual void valueViewHost_DragEnterOver(object sender, DragEventArgs e) {
123      e.Effect = DragDropEffects.None;
124      Type type = e.Data.GetData("Type") as Type;
125      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
126        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
127        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
128        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
129        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
130        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
131      }
132    }
133    protected virtual void valueViewHost_DragDrop(object sender, DragEventArgs e) {
134      if (e.Effect != DragDropEffects.None) {
135        T value = e.Data.GetData("Value") as T;
136        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
137        Content.Value = value;
138      }
139    }
140  }
141}
Note: See TracBrowser for help on using the repository browser.