Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3082_Scaling/HeuristicLab.Optimization.Views/3.3/ResultParameterView.cs @ 17759

Last change on this file since 17759 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Resources;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.Parameters.Views;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Optimization.Views {
32  [View("ResultParameter View")]
33  [Content(typeof(ResultParameter<>), true)]
34  [Content(typeof(IResultParameter<>), false)]
35  public partial class ResultParameterView<T> : LookupParameterView<T> where T : class, IItem {
36    protected TypeSelectorDialog typeSelectorDialog;
37
38    public new IResultParameter<T> Content {
39      get { return (IResultParameter<T>)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ResultParameterView() {
44      InitializeComponent();
45      setDefaultValueButton.Text = string.Empty;
46      setDefaultValueButton.Image = VSImageLibrary.Edit;
47      removeDefaultValueButton.Text = string.Empty;
48      removeDefaultValueButton.Image = VSImageLibrary.Remove;
49      actualNameLabel.Text = "Result Name:";
50      dataTypeLabel.Text = "Result Type:";
51    }
52
53    /// <summary>
54    /// Clean up any resources being used.
55    /// </summary>
56    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
57    protected override void Dispose(bool disposing) {
58      if (disposing) {
59        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
60        if (components != null) components.Dispose();
61      }
62      base.Dispose(disposing);
63    }
64
65    protected override void DeregisterContentEvents() {
66      Content.ResultCollectionNameChanged -= ContentOnResultCollectionNameChanged;
67      Content.DefaultValueChanged -= ContentOnDefaultValueChanged;
68      base.DeregisterContentEvents();
69    }
70
71    protected override void RegisterContentEvents() {
72      base.RegisterContentEvents();
73      Content.ResultCollectionNameChanged += ContentOnResultCollectionNameChanged;
74      Content.DefaultValueChanged += ContentOnDefaultValueChanged;
75    }
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        resultCollectionNameTextBox.Text = "-";
81        defaultValueViewHost.Content = null;
82      } else {
83        resultCollectionNameTextBox.Text = Content.ResultCollectionName;
84        defaultValueViewHost.Content = Content.DefaultValue;
85      }
86    }
87
88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
90      resultCollectionNameTextBox.Enabled = Content != null;
91      resultCollectionNameTextBox.ReadOnly = ReadOnly;
92      setDefaultValueButton.Enabled = Content != null && !ReadOnly && !Locked;
93      removeDefaultValueButton.Enabled = Content != null && !ReadOnly && !Locked;
94    }
95
96    private void ContentOnDefaultValueChanged(object sender, EventArgs e) {
97      if (InvokeRequired) Invoke((Action<object, EventArgs>)ContentOnDefaultValueChanged, sender, e);
98      else defaultValueViewHost.Content = Content.DefaultValue;
99    }
100
101    private void ContentOnResultCollectionNameChanged(object sender, EventArgs e) {
102      if (InvokeRequired) Invoke((Action<object, EventArgs>)ContentOnResultCollectionNameChanged, sender, e);
103      else resultCollectionNameTextBox.Text = Content.ResultCollectionName;
104    }
105
106    private void resultNameTextBox_Validated(object sender, EventArgs e) {
107      if (InvokeRequired) Invoke((Action<object, EventArgs>)resultNameTextBox_Validated, sender, e);
108      else Content.ResultCollectionName = resultCollectionNameTextBox.Text;
109    }
110
111    private void setDefaultValueButton_Click(object sender, EventArgs e) {
112      if (typeSelectorDialog == null) {
113        typeSelectorDialog = new TypeSelectorDialog();
114        typeSelectorDialog.Caption = "Select Value";
115        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
116      }
117      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
118        try {
119          Content.DefaultValue = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
120        } catch (Exception ex) {
121          ErrorHandling.ShowErrorDialog(this, ex);
122        }
123      }
124    }
125
126    private void removeDefaultValueButton_Click(object sender, EventArgs e) {
127      Content.DefaultValue = null;
128    }
129
130    protected virtual void defaultValueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
131      e.Effect = DragDropEffects.None;
132      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
133        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
134        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
135        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
136        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
137        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
138      }
139    }
140    protected virtual void defaultValueGroupBox_DragDrop(object sender, DragEventArgs e) {
141      if (e.Effect != DragDropEffects.None) {
142        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
143        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
144        Content.DefaultValue = value;
145      }
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.