Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/PortParameterView.cs @ 11526

Last change on this file since 11526 was 11526, checked in by swagner, 9 years ago

#2205: Worked on optimization networks

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
23using HeuristicLab.Core.Views;
24using HeuristicLab.MainForm;
25using HeuristicLab.PluginInfrastructure;
26using System;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Core.Networks.Views {
30  [View("PortParameter View")]
31  [Content(typeof(PortParameter<>), true)]
32  [Content(typeof(IPortParameter<>), false)]
33  [Content(typeof(IPortParameter), false)]
34  public partial class PortParameterView : EntityView {
35    protected TypeSelectorDialog typeSelectorDialog;
36
37    public new IPortParameter Content {
38      get { return (IPortParameter)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public PortParameterView() {
43      InitializeComponent();
44    }
45
46    protected override void Dispose(bool disposing) {
47      if (disposing) {
48        if (typeSelectorDialog != null) typeSelectorDialog.Dispose();
49        if (components != null) components.Dispose();
50      }
51      base.Dispose(disposing);
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.TypeChanged -= Content_TypeChanged;
56      Content.DefaultValueChanged -= Content_DefaultValueChanged;
57      base.DeregisterContentEvents();
58    }
59
60    protected override void RegisterContentEvents() {
61      base.RegisterContentEvents();
62      Content.TypeChanged += Content_TypeChanged;
63      Content.DefaultValueChanged += Content_DefaultValueChanged;
64    }
65
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content == null) {
69        dataTypeTextBox.Text = string.Empty;
70        inputTypeCheckBox.Checked = false;
71        outputTypeCheckBox.Checked = false;
72        defaultValueViewHost.Content = null;
73      } else {
74        dataTypeTextBox.Text = Content.DataType.GetPrettyName();
75        inputTypeCheckBox.Checked = Content.Type.HasFlag(PortParameterType.Input);
76        outputTypeCheckBox.Checked = Content.Type.HasFlag(PortParameterType.Output);
77        defaultValueViewHost.ViewType = null;
78        defaultValueViewHost.Content = Content.DefaultValue;
79      }
80    }
81
82    protected override void SetEnabledStateOfControls() {
83      base.SetEnabledStateOfControls();
84      dataTypeTextBox.Enabled = Content != null;
85      inputTypeCheckBox.Enabled = Content != null && !ReadOnly;
86      outputTypeCheckBox.Enabled = Content != null && !ReadOnly;
87      defaultValueGroupBox.Enabled = Content != null;
88      setDefaultValueButton.Enabled = Content != null && !ReadOnly;
89      clearDefaultValueButton.Enabled = Content != null && Content.DefaultValue != null && !ReadOnly;
90    }
91    protected virtual void Content_TypeChanged(object sender, EventArgs e) {
92      if (InvokeRequired)
93        Invoke(new EventHandler(Content_TypeChanged), sender, e);
94      else {
95        inputTypeCheckBox.Checked = Content.Type.HasFlag(PortParameterType.Input);
96        outputTypeCheckBox.Checked = Content.Type.HasFlag(PortParameterType.Output);
97      }
98    }
99    protected virtual void Content_DefaultValueChanged(object sender, System.EventArgs e) {
100      if (InvokeRequired)
101        Invoke(new EventHandler(Content_DefaultValueChanged), sender, e);
102      else {
103        clearDefaultValueButton.Enabled = Content.DefaultValue != null;
104        defaultValueViewHost.ViewType = null;
105        defaultValueViewHost.Content = Content.DefaultValue;
106      }
107    }
108
109    protected virtual void inputTypeCheckBox_CheckedChanged(object sender, EventArgs e) {
110      Content.Type = inputTypeCheckBox.Checked ? Content.Type | PortParameterType.Input : Content.Type & ~PortParameterType.Input;
111    }
112    protected virtual void outputTypeCheckBox_CheckedChanged(object sender, EventArgs e) {
113      Content.Type = outputTypeCheckBox.Checked ? Content.Type | PortParameterType.Output : Content.Type & ~PortParameterType.Output;
114    }
115
116    protected virtual void setDefaultValueButton_Click(object sender, EventArgs e) {
117      if (typeSelectorDialog == null) {
118        typeSelectorDialog = new TypeSelectorDialog();
119        typeSelectorDialog.Caption = "Select Default Value";
120      }
121      typeSelectorDialog.TypeSelector.Configure(Content.DataType, false, true);
122      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
123        try {
124          Content.DefaultValue = (IItem)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
125        }
126        catch (Exception ex) {
127          ErrorHandling.ShowErrorDialog(this, ex);
128        }
129      }
130    }
131    protected virtual void clearDefaultValueButton_Click(object sender, EventArgs e) {
132      Content.DefaultValue = null;
133    }
134
135    protected virtual void defaultValuePanel_DragEnterOver(object sender, DragEventArgs e) {
136      e.Effect = DragDropEffects.None;
137      if (!ReadOnly && (e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) is IItem)) {
138        if ((e.KeyState & 32) == 32) e.Effect = DragDropEffects.Link;  // ALT key
139        else if ((e.KeyState & 4) == 4) e.Effect = DragDropEffects.Move;  // SHIFT key
140        else if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) e.Effect = DragDropEffects.Copy;
141        else if (e.AllowedEffect.HasFlag(DragDropEffects.Move)) e.Effect = DragDropEffects.Move;
142        else if (e.AllowedEffect.HasFlag(DragDropEffects.Link)) e.Effect = DragDropEffects.Link;
143      }
144    }
145    protected virtual void defaultValuePanel_DragDrop(object sender, DragEventArgs e) {
146      if (e.Effect != DragDropEffects.None) {
147        IItem item = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat) as IItem;
148        if (e.Effect.HasFlag(DragDropEffects.Copy)) item = (IItem)item.Clone();
149        Content.DefaultValue = item;
150      }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.