Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5142 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 5.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2818]24using HeuristicLab.Common;
[2520]25using HeuristicLab.MainForm;
[3758]26using HeuristicLab.PluginInfrastructure;
[2]27
[2520]28namespace HeuristicLab.Core.Views {
[776]29  /// <summary>
[2655]30  /// The visual representation of a <see cref="Variable"/>.
[776]31  /// </summary>
[2917]32  [View("Variable View")]
[2520]33  [Content(typeof(Variable), true)]
[2727]34  [Content(typeof(IVariable), false)]
[2664]35  public partial class VariableView : NamedItemView {
[2676]36    protected TypeSelectorDialog typeSelectorDialog;
[2]37
[776]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>
[2727]43    public new IVariable Content {
44      get { return (IVariable)base.Content; }
[2713]45      set { base.Content = value; }
[2]46    }
47
[776]48    /// <summary>
49    /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
50    /// </summary>
[2]51    public VariableView() {
52      InitializeComponent();
53    }
54
[776]55    /// <summary>
[2687]56    /// Removes the eventhandlers from the underlying <see cref="Variable"/>.
[776]57    /// </summary>
58    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]59    protected override void DeregisterContentEvents() {
60      Content.ValueChanged -= new EventHandler(Content_ValueChanged);
61      base.DeregisterContentEvents();
[2]62    }
[2655]63
[776]64    /// <summary>
[2687]65    /// Adds eventhandlers to the underlying <see cref="Variable"/>.
[776]66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]68    protected override void RegisterContentEvents() {
69      base.RegisterContentEvents();
70      Content.ValueChanged += new EventHandler(Content_ValueChanged);
[2]71    }
72
[2713]73    protected override void OnContentChanged() {
74      base.OnContentChanged();
75      if (Content == null) {
[2655]76        dataTypeTextBox.Text = "-";
[2713]77        viewHost.Content = null;
[2]78      } else {
[2818]79        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
[2949]80        viewHost.ViewType = null;
[2713]81        viewHost.Content = Content.Value;
[2]82      }
83    }
84
[3904]85    protected override void SetEnabledStateOfControls() {
86      base.SetEnabledStateOfControls();
[3362]87      dataTypeTextBox.Enabled = Content != null && Content.Value != null;
88      setValueButton.Enabled = Content != null && !ReadOnly;
89      clearValueButton.Enabled = Content != null && Content.Value != null && !ReadOnly;
90      valueGroupBox.Enabled = Content != null;
91    }
92
[2713]93    protected virtual void Content_ValueChanged(object sender, EventArgs e) {
[2]94      if (InvokeRequired)
[2713]95        Invoke(new EventHandler(Content_ValueChanged), sender, e);
[2655]96      else {
[2818]97        dataTypeTextBox.Text = Content.Value == null ? "-" : Content.Value.GetType().GetPrettyName();
[2713]98        dataTypeTextBox.Enabled = Content.Value != null;
99        clearValueButton.Enabled = Content.Value != null;
[2949]100        viewHost.ViewType = null;
[2713]101        viewHost.Content = Content.Value;
[2655]102      }
[2]103    }
104
[2676]105    protected virtual void setValueButton_Click(object sender, EventArgs e) {
[2655]106      if (typeSelectorDialog == null) {
107        typeSelectorDialog = new TypeSelectorDialog();
[3407]108        typeSelectorDialog.Caption = "Select Value";
[3588]109        typeSelectorDialog.TypeSelector.Configure(typeof(IItem), false, true);
[2655]110      }
111      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
[3407]112        try {
113          Content.Value = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
114        }
115        catch (Exception ex) {
[3758]116          ErrorHandling.ShowErrorDialog(this, ex);
[3407]117        }
[2655]118      }
[2]119    }
[2676]120    protected virtual void clearValueButton_Click(object sender, EventArgs e) {
[2713]121      Content.Value = null;
[2655]122    }
[2676]123    protected virtual void valuePanel_DragEnterOver(object sender, DragEventArgs e) {
[2655]124      e.Effect = DragDropEffects.None;
125      Type type = e.Data.GetData("Type") as Type;
[3362]126      if (!ReadOnly && (type != null) && (typeof(IItem).IsAssignableFrom(type))) {
[3694]127        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
[2694]128        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
129        else if ((e.AllowedEffect & DragDropEffects.Copy) == DragDropEffects.Copy) e.Effect = DragDropEffects.Copy;
130        else if ((e.AllowedEffect & DragDropEffects.Move) == DragDropEffects.Move) e.Effect = DragDropEffects.Move;
[3526]131        else if ((e.AllowedEffect & DragDropEffects.Link) == DragDropEffects.Link) e.Effect = DragDropEffects.Link;
[2]132      }
133    }
[2676]134    protected virtual void valuePanel_DragDrop(object sender, DragEventArgs e) {
[2655]135      if (e.Effect != DragDropEffects.None) {
136        IItem item = e.Data.GetData("Value") as IItem;
137        if ((e.Effect & DragDropEffects.Copy) == DragDropEffects.Copy) item = (IItem)item.Clone();
[2713]138        Content.Value = item;
[2655]139      }
140    }
[2]141  }
142}
Note: See TracBrowser for help on using the repository browser.