Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters.Views/3.3/ItemParameterView.cs @ 2714

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

Operator architecture refactoring (#95)

  • moved parameters and parameter views into new plugins
File size: 6.6 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.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace HeuristicLab.Parameters.Views {
34  /// <summary>
35  /// The visual representation of a <see cref="Parameter"/>.
36  /// </summary>
37  [Content(typeof(ItemParameter), true)]
38  public partial class ItemParameterView : ParameterView {
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 ItemParameter Content {
47      get { return (ItemParameter)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 ItemParameterView() {
55      InitializeComponent();
56      Caption = "Parameter";
57    }
58    /// <summary>
59    /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
60    /// </summary>
61    /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
62    /// <param name="variable">The variable to represent visually.</param>
63    public ItemParameterView(ItemParameter parameter)
64      : this() {
65      Content = parameter;
66    }
67
68    /// <summary>
69    /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
70    /// </summary>
71    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
72    protected override void DeregisterContentEvents() {
73      Content.ActualNameChanged -= new EventHandler(Content_ActualNameChanged);
74      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
75      base.DeregisterContentEvents();
76    }
77
78    /// <summary>
79    /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
80    /// </summary>
81    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
82    protected override void RegisterContentEvents() {
83      base.RegisterContentEvents();
84      Content.ActualNameChanged += new EventHandler(Content_ActualNameChanged);
85      Content.ValueChanged += new EventHandler(Content_ValueChanged);
86    }
87
88    protected override void OnContentChanged() {
89      base.OnContentChanged();
90      if (Content == null) {
91        Caption = "Parameter";
92        actualNameTextBox.Text = "-";
93        actualNameTextBox.Enabled = false;
94        setValueButton.Enabled = false;
95        clearValueButton.Enabled = false;
96        valueGroupBox.Enabled = false;
97        viewHost.Content = null;
98      } else {
99        Caption = Content.Name + " (" + Content.GetType().Name + ")";
100        actualNameTextBox.Text = Content.ActualName;
101        actualNameTextBox.Enabled = Content.Value == null;
102        setValueButton.Enabled = Content.Value == null;
103        clearValueButton.Enabled = Content.Value != null;
104        valueGroupBox.Enabled = true;
105        viewHost.Content = Content.Value;
106      }
107    }
108
109    protected virtual void Content_ActualNameChanged(object sender, EventArgs e) {
110      if (InvokeRequired)
111        Invoke(new EventHandler(Content_ActualNameChanged), sender, e);
112      else
113        actualNameTextBox.Text = Content.ActualName;
114    }
115    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
116      if (InvokeRequired)
117        Invoke(new EventHandler(Content_ValueChanged), sender, e);
118      else {
119        actualNameTextBox.Enabled = Content.Value == null;
120        setValueButton.Enabled = Content.Value == null;
121        clearValueButton.Enabled = Content.Value != null;
122        viewHost.Content = Content.Value;
123      }
124    }
125
126    protected virtual void actualNameTextBox_Validated(object sender, EventArgs e) {
127      Content.ActualName = actualNameTextBox.Text;
128    }
129    protected virtual void setValueButton_Click(object sender, EventArgs e) {
130      if (typeSelectorDialog == null) {
131        typeSelectorDialog = new TypeSelectorDialog();
132        typeSelectorDialog.Caption = "Select Value Type";
133      }
134      typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, false);
135      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK)
136        Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
137    }
138    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
139      Content.Value = null;
140    }
141    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
142      e.Effect = DragDropEffects.None;
143      Type type = e.Data.GetData("Type") as Type;
144      if ((type != null) && (Content.DataType.IsAssignableFrom(type))) {
145        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Copy;  // CTRL key
146        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
147        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
148        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
149        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
150      }
151    }
152    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
153      if (e.Effect != DragDropEffects.None) {
154        IItem item = e.Data.GetData("Value") as IItem;
155        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
156        Content.Value = item;
157      }
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.