Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/VariableView.cs @ 3633

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

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

File size: 5.9 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.MainForm;
26
27namespace HeuristicLab.Core.Views {
28  /// <summary>
29  /// The visual representation of a <see cref="Variable"/>.
30  /// </summary>
31  [View("Variable View")]
32  [Content(typeof(Variable), true)]
33  [Content(typeof(IVariable), false)]
34  public partial class VariableView : NamedItemView {
35    protected TypeSelectorDialog typeSelectorDialog;
36
37    /// <summary>
38    /// Gets or sets the variable to represent visually.
39    /// </summary>
40    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
41    /// No own data storage present.</remarks>
42    public new IVariable Content {
43      get { return (IVariable)base.Content; }
44      set { base.Content = value; }
45    }
46
47    /// <summary>
48    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
49    /// </summary>
50    public VariableView() {
51      InitializeComponent();
52      Caption = "Variable";
53    }
54
55    /// <summary>
56    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
57    /// </summary>
58    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
59    protected override void DeregisterContentEvents() {
60      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
61      base.DeregisterContentEvents();
62    }
63
64    /// <summary>
65    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      Content.ValueChanged += new EventHandler(Content_ValueChanged);
71    }
72
73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      if (Content == null) {
76        Caption = "Variable";
77        dataTypeTextBox.Text = "-";
78        viewHost.Content = null;
79      } else {
80        Caption = Content.Name + " (" + Content.GetType().Name + ")";
81        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
82        viewHost.ViewType = null;
83        viewHost.Content = Content.Value;
84      }
85      SetEnabledStateOfControls();
86    }
87
88    protected override void OnReadOnlyChanged() {
89      base.OnReadOnlyChanged();
90      SetEnabledStateOfControls();
91    }
92
93    private void SetEnabledStateOfControls() {
94      dataTypeTextBox.Enabled = Content != null && Content.Value != null;
95      setValueButton.Enabled = Content != null && !ReadOnly;
96      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
97      valueGroupBox.Enabled = Content != null;
98    }
99
100    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
101      if (InvokeRequired)
102        Invoke(new EventHandler(Content_ValueChanged), sender, e);
103      else {
104        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
105        dataTypeTextBox.Enabled = Content.Value != null;
106        clearValueButton.Enabled = Content.Value != null;
107        viewHost.ViewType = null;
108        viewHost.Content = Content.Value;
109      }
110    }
111
112    protected virtual void setValueButton_Click(object sender, EventArgs e) {
113      if (typeSelectorDialog == null) {
114        typeSelectorDialog = new TypeSelectorDialog();
115        typeSelectorDialog.Caption = "Select Value";
116        typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
117      }
118      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
119        try {
120          Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
121        }
122        catch (Exception ex) {
123          Auxiliary.ShowErrorMessageBox(ex);
124        }
125      }
126    }
127    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
128      Content.Value = null;
129    }
130    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
131      e.Effect = DragDropEffects.None;
132      Type type = e.Data.GetData("Type") as Type;
133      if (!ReadOnly && (type != null) && (typeof(IItem).IsAssignableFrom(type))) {
134        if ((e.KeyState & 8) == 8) e.Effect = DragDropEffects.Link;  // CTRL key
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;
138        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
139      }
140    }
141    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
142      if (e.Effect != DragDropEffects.None) {
143        IItem item = e.Data.GetData("Value") as IItem;
144        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
145        Content.Value = item;
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.