Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2431: Refactored ResultsParameter

File size: 6.0 KB
RevLine 
[12764]1#region License Information
2/* HeuristicLab
[14058]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12764]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;
[14058]23using System.Windows.Forms;
24using HeuristicLab.Common.Resources;
[12764]25using HeuristicLab.Core;
[14058]26using HeuristicLab.Core.Views;
[12764]27using HeuristicLab.MainForm;
28using HeuristicLab.Parameters.Views;
[14058]29using HeuristicLab.PluginInfrastructure;
[12764]30
31namespace HeuristicLab.Optimization.Views {
32  [View("ResultParameter View")]
33  [Content(typeof(ResultParameter<>), true)]
34  [Content(typeof(IResultParameter<>), false)]
[14058]35  public partial class ResultParameterView<T> : LookupParameterView<T> where T : class, IItem {
36    protected TypeSelectorDialog typeSelectorDialog;
[12764]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();
[14058]45      setDefaultValueButton.Text = string.Empty;
46      setDefaultValueButton.Image = VSImageLibrary.Edit;
47      removeDefaultValueButton.Text = string.Empty;
48      removeDefaultValueButton.Image = VSImageLibrary.Remove;
[12764]49    }
50
[14058]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
[12764]63    protected override void DeregisterContentEvents() {
[14058]64      Content.ResultCollectionNameChanged -= ContentOnResultCollectionNameChanged;
65      Content.DefaultValueChanged -= ContentOnDefaultValueChanged;
[12764]66      base.DeregisterContentEvents();
67    }
68
69    protected override void RegisterContentEvents() {
70      base.RegisterContentEvents();
[14058]71      Content.ResultCollectionNameChanged += ContentOnResultCollectionNameChanged;
72      Content.DefaultValueChanged += ContentOnDefaultValueChanged;
[12764]73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
[14058]77      if (Content == null) {
78        resultCollectionNameTextBox.Text = "-";
79        defaultValueViewHost.Content = null;
80      } else {
81        resultCollectionNameTextBox.Text = Content.ResultCollectionName;
82        defaultValueViewHost.Content = Content.DefaultValue;
83      }
[12764]84    }
85
86    protected override void SetEnabledStateOfControls() {
87      base.SetEnabledStateOfControls();
[14058]88      resultCollectionNameTextBox.Enabled = Content != null;
89      resultCollectionNameTextBox.ReadOnly = ReadOnly;
[12764]90    }
91
[14058]92    private void ContentOnDefaultValueChanged(object sender, EventArgs e) {
93      if (InvokeRequired) Invoke((Action<object, EventArgs>)ContentOnDefaultValueChanged, sender, e);
94      else defaultValueViewHost.Content = Content.DefaultValue;
[12764]95    }
96
[14058]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
[12764]102    private void resultNameTextBox_Validated(object sender, EventArgs e) {
[14058]103      if (InvokeRequired) Invoke((Action<object, EventArgs>)resultNameTextBox_Validated, sender, e);
104      else Content.ResultCollectionName = resultCollectionNameTextBox.Text;
[12764]105    }
[14058]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    }
[12764]143  }
144}
Note: See TracBrowser for help on using the repository browser.