Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3764 was 3764, checked in by mkommend, 14 years ago

adapted view captions (ticket #893)

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