source: branches/PerformanceComparison/HeuristicLab.Optimization.Views/3.3/ResultParameterView.cs @ 14058

Last change on this file since 14058 was 14058, checked in by abeham, 6 years ago

#2431: Refactored ResultsParameter

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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    }
50
51    /// <summary>
52    /// Clean up any resources being used.
53    /// </summary>
54    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
55    protected override void Dispose(bool disposing) {
56      if (disposing) {
57        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
58        if (components != null) components.Dispose();
59      }
60      base.Dispose(disposing);
61    }
62
63    protected override void DeregisterContentEvents() {
64      Content.ResultCollectionNameChanged -= ContentOnResultCollectionNameChanged;
65      Content.DefaultValueChanged -= ContentOnDefaultValueChanged;
66      base.DeregisterContentEvents();
67    }
68
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
71      Content.ResultCollectionNameChanged += ContentOnResultCollectionNameChanged;
72      Content.DefaultValueChanged += ContentOnDefaultValueChanged;
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        resultCollectionNameTextBox.Text = "-";
79        defaultValueViewHost.Content = null;
80      } else {
81        resultCollectionNameTextBox.Text = Content.ResultCollectionName;
82        defaultValueViewHost.Content = Content.DefaultValue;
83      }
84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
88      resultCollectionNameTextBox.Enabled = Content != null;
89      resultCollectionNameTextBox.ReadOnly = ReadOnly;
90    }
91
92    private void ContentOnDefaultValueChanged(object sender, EventArgs e) {
93      if (InvokeRequired) Invoke((Action<object, EventArgs>)ContentOnDefaultValueChanged, sender, e);
94      else defaultValueViewHost.Content = Content.DefaultValue;
95    }
96
97    private void ContentOnResultCollectionNameChanged(object sender, EventArgs e) {
98      if (InvokeRequired) Invoke((Action<object, EventArgs>)ContentOnResultCollectionNameChanged, sender, e);
99      else resultCollectionNameTextBox.Text = Content.ResultCollectionName;
100    }
101
102    private void resultNameTextBox_Validated(object sender, EventArgs e) {
103      if (InvokeRequired) Invoke((Action<object, EventArgs>)resultNameTextBox_Validated, sender, e);
104      else Content.ResultCollectionName = resultCollectionNameTextBox.Text;
105    }
106
107    private void setDefaultValueButton_Click(object sender, EventArgs e) {
108      if (typeSelectorDialog == null) {
109        typeSelectorDialog = new TypeSelectorDialog();
110        typeSelectorDialog.Caption = "Select Value";
111        typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
112      }
113      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
114        try {
115          Content.DefaultValue = (T)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
116        } catch (Exception ex) {
117          ErrorHandling.ShowErrorDialog(this, ex);
118        }
119      }
120    }
121
122    private void removeDefaultValueButton_Click(object sender, EventArgs e) {
123      Content.DefaultValue = null;
124    }
125
126    protected virtual void defaultValueGroupBox_DragEnterOver(object sender, DragEventArgs e) {
127      e.Effect = DragDropEffects.None;
128      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) != null) && Content.DataType.IsAssignableFrom(e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat).GetType())) {
129        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
130        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
131        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
132        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
133        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
134      }
135    }
136    protected virtual void defaultValueGroupBox_DragDrop(object sender, DragEventArgs e) {
137      if (e.Effect != DragDropEffects.None) {
138        T value = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as T;
139        if (e.Effect.HasFlag(DragDropEffects.Copy)) value = (T)value.Clone();
140        Content.DefaultValue = value;
141      }
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.