Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.Optimization.Views/3.3/ResultParameterView.cs @ 16147

Last change on this file since 16147 was 15973, checked in by gkronber, 6 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

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