Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 5.9 KB
RevLine 
[2740]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2740]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;
[3376]24using HeuristicLab.Common;
[2740]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>
[2917]33  [View("ValueParameter View")]
[2948]34  [Content(typeof(OptionalValueParameter<>), true)]
[2891]35  [Content(typeof(ValueParameter<>), true)]
[2948]36  [Content(typeof(IValueParameter<>), false)]
[2756]37  public partial class ValueParameterView<T> : ParameterView where T : class, IItem {
[2948]38    protected TypeSelectorDialog typeSelectorDialog;
39
[2740]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>
[2948]45    public new IValueParameter<T> Content {
46      get { return (IValueParameter<T>)base.Content; }
[2740]47      set { base.Content = value; }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
52    /// </summary>
[2756]53    public ValueParameterView() {
[2740]54      InitializeComponent();
[2756]55      Caption = "ValueParameter";
[2740]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) {
[2756]79        Caption = "ValueParameter";
[2948]80        clearValueButton.Visible = true;
[2870]81        viewHost.Content = null;
[2740]82      } else {
83        Caption = Content.Name + " (" + Content.GetType().Name + ")";
[2948]84        clearValueButton.Visible = !(Content is ValueParameter<T>);
[2949]85        viewHost.ViewType = null;
[2740]86        viewHost.Content = Content.Value;
87      }
[3365]88      SetEnabledStateOfControls();
[2740]89    }
90
[3365]91    protected override void OnReadOnlyChanged() {
92      base.OnReadOnlyChanged();
93      SetEnabledStateOfControls();
94    }
95
96    private void SetEnabledStateOfControls() {
97      setValueButton.Enabled = Content != null && !ReadOnly;
98      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
99      valueGroupBox.Enabled = Content != null;
100    }
101
[2740]102    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
103      if (InvokeRequired)
104        Invoke(new EventHandler(Content_ValueChanged), sender, e);
105      else {
[3365]106        clearValueButton.Enabled = Content.Value != null && !ReadOnly;
[2949]107        viewHost.ViewType = null;
[2740]108        viewHost.Content = Content.Value;
109      }
110    }
111
[2948]112    protected virtual void changeValueButton_Click(object sender, EventArgs e) {
113      if (typeSelectorDialog == null) {
114        typeSelectorDialog = new TypeSelectorDialog();
115        typeSelectorDialog.Caption = "Select Value";
[3588]116        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
[2948]117      }
[3407]118      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
119        try {
120          Content.Value = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
121        }
122        catch (Exception ex) {
123          Auxiliary.ShowErrorMessageBox(ex);
124        }
125      }
[2948]126    }
127    protected virtual void setValueButton_Click(object sender, EventArgs e) {
128      Content.Value = null;
129    }
[2740]130    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
131      e.Effect = DragDropEffects.None;
132      Type type = e.Data.GetData("Type") as Type;
[3365]133      if (!ReadOnly && (type != null) && (Content.DataType.IsAssignableFrom(type))) {
[3526]134        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
[2740]135        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
136        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
137        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]138        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[2740]139      }
140    }
141    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
142      if (e.Effect != DragDropEffects.None) {
[2756]143        T value = e.Data.GetData("Value") as T;
144        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) value = (T)value.Clone();
[2740]145        Content.Value = value;
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.