Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ItemParameterView.cs @ 2713

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

Adapted HL 3.3 plugins according to changes in MainForm (#857)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Core.Views {
32  /// <summary>
33  /// The visual representation of a <see cref="Parameter"/>.
34  /// </summary>
35  [Content(typeof(ItemParameter), true)]
36  public partial class ItemParameterView : ParameterView {
37    protected TypeSelectorDialog typeSelectorDialog;
38
39    /// <summary>
40    /// Gets or sets the variable to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public new ItemParameter Content {
45      get { return (ItemParameter)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
51    /// </summary>
52    public ItemParameterView() {
53      InitializeComponent();
54      Caption = "Parameter";
55    }
56    /// <summary>
57    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
58    /// </summary>
59    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
60    /// <param name="variable">The variable to represent visually.</param>
61    public ItemParameterView(ItemParameter parameter)
62      : this() {
63      Content = parameter;
64    }
65
66    /// <summary>
67    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
68    /// </summary>
69    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
70    protected override void DeregisterContentEvents() {
71      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
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.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
83      Content.ValueChanged += new EventHandler(Content_ValueChanged);
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      if (Content == null) {
89        Caption = "Parameter";
90        actualNameTextBox.Text = "-";
91        actualNameTextBox.Enabled = false;
92        setValueButton.Enabled = false;
93        clearValueButton.Enabled = false;
94        valueGroupBox.Enabled = false;
95        viewHost.Content = null;
96      } else {
97        Caption = Content.Name + " (" + Content.GetType().Name + ")";
98        actualNameTextBox.Text = Content.ActualName;
99        actualNameTextBox.Enabled = Content.Value == null;
100        setValueButton.Enabled = Content.Value == null;
101        clearValueButton.Enabled = Content.Value != null;
102        valueGroupBox.Enabled = true;
103        viewHost.Content = Content.Value;
104      }
105    }
106
107    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
108      if (InvokeRequired)
109        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
110      else
111        actualNameTextBox.Text = Content.ActualName;
112    }
113    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
114      if (InvokeRequired)
115        Invoke(new EventHandler(Content_ValueChanged), sender, e);
116      else {
117        actualNameTextBox.Enabled = Content.Value == null;
118        setValueButton.Enabled = Content.Value == null;
119        clearValueButton.Enabled = Content.Value != null;
120        viewHost.Content = Content.Value;
121      }
122    }
123
124    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
125      Content.ActualName = actualNameTextBox.Text;
126    }
127    protected virtual void setValueButton_Click(object sender, EventArgs e) {
128      if (typeSelectorDialog == null) {
129        typeSelectorDialog = new TypeSelectorDialog();
130        typeSelectorDialog.Caption = "Select Value Type";
131      }
132      typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
133      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
134        Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
135    }
136    protected virtual void clearValueButton_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 ((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        IItem item = e.Data.GetData("Value") as IItem;
153        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
154        Content.Value = item;
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.