1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Parameters.Views {
|
---|
30 | /// <summary>
|
---|
31 | /// The visual representation of a <see cref="Parameter"/>.
|
---|
32 | /// </summary>
|
---|
33 | [View("ConstrainedValueParameter View")]
|
---|
34 | [Content(typeof(OptionalConstrainedValueParameter<>), true)]
|
---|
35 | [Content(typeof(ConstrainedValueParameter<>), true)]
|
---|
36 | public partial class ConstrainedValueParameterView<T> : ParameterView where T : class, IItem {
|
---|
37 | private List<T> valueComboBoxItems;
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Gets or sets the variable to represent visually.
|
---|
41 | /// </summary>
|
---|
42 | /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
|
---|
43 | /// No own data storage present.</remarks>
|
---|
44 | public new OptionalConstrainedValueParameter<T> Content {
|
---|
45 | get { return (OptionalConstrainedValueParameter<T>)base.Content; }
|
---|
46 | set { base.Content = value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Initializes a new instance of <see cref="VariableView"/> with caption "Variable".
|
---|
51 | /// </summary>
|
---|
52 | public ConstrainedValueParameterView() {
|
---|
53 | InitializeComponent();
|
---|
54 | Caption = "ConstrainedValueParameter";
|
---|
55 | valueComboBoxItems = new List<T>();
|
---|
56 | }
|
---|
57 | /// <summary>
|
---|
58 | /// Initializes a new instance of <see cref="VariableView"/> with the given <paramref name="variable"/>.
|
---|
59 | /// </summary>
|
---|
60 | /// <remarks>Calls <see cref="VariableView()"/>.</remarks>
|
---|
61 | /// <param name="variable">The variable to represent visually.</param>
|
---|
62 | public ConstrainedValueParameterView(OptionalConstrainedValueParameter<T> content)
|
---|
63 | : this() {
|
---|
64 | Content = content;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Removes the eventhandlers from the underlying <see cref="IVariable"/>.
|
---|
69 | /// </summary>
|
---|
70 | /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
71 | protected override void DeregisterContentEvents() {
|
---|
72 | Content.ValidValues.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
73 | Content.ValidValues.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
74 | Content.ValidValues.CollectionReset -= new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
75 | Content.ValueChanged -= new EventHandler(Content_ValueChanged);
|
---|
76 | base.DeregisterContentEvents();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Adds eventhandlers to the underlying <see cref="IVariable"/>.
|
---|
81 | /// </summary>
|
---|
82 | /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
|
---|
83 | protected override void RegisterContentEvents() {
|
---|
84 | base.RegisterContentEvents();
|
---|
85 | Content.ValidValues.ItemsAdded += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded);
|
---|
86 | Content.ValidValues.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved);
|
---|
87 | Content.ValidValues.CollectionReset += new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset);
|
---|
88 | Content.ValueChanged += new EventHandler(Content_ValueChanged);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void OnContentChanged() {
|
---|
92 | base.OnContentChanged();
|
---|
93 | if (Content == null) {
|
---|
94 | Caption = "ConstrainedValueParameter";
|
---|
95 | viewHost.Content = null;
|
---|
96 | valueGroupBox.Enabled = false;
|
---|
97 | FillValueComboBox();
|
---|
98 | } else {
|
---|
99 | Caption = Content.Name + " (" + Content.GetType().Name + ")";
|
---|
100 | valueGroupBox.Enabled = true;
|
---|
101 | FillValueComboBox();
|
---|
102 | viewHost.ViewType = null;
|
---|
103 | viewHost.Content = Content.Value;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void FillValueComboBox() {
|
---|
108 | valueComboBox.SelectedIndexChanged -= new EventHandler(valueComboBox_SelectedIndexChanged);
|
---|
109 | valueComboBoxItems.Clear();
|
---|
110 | valueComboBox.Items.Clear();
|
---|
111 | if (!(Content is ConstrainedValueParameter<T>)) {
|
---|
112 | valueComboBoxItems.Add(null);
|
---|
113 | valueComboBox.Items.Add("-");
|
---|
114 | }
|
---|
115 | if (Content != null) {
|
---|
116 | foreach (T item in Content.ValidValues) {
|
---|
117 | valueComboBoxItems.Add(item);
|
---|
118 | valueComboBox.Items.Add(item.ToString());
|
---|
119 | }
|
---|
120 | valueComboBox.Enabled = valueComboBox.Items.Count > 0;
|
---|
121 | valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
|
---|
122 | }
|
---|
123 | valueComboBox.SelectedIndexChanged += new EventHandler(valueComboBox_SelectedIndexChanged);
|
---|
124 | }
|
---|
125 |
|
---|
126 | #region Content Events
|
---|
127 | private void Content_ValueChanged(object sender, EventArgs e) {
|
---|
128 | if (InvokeRequired)
|
---|
129 | Invoke(new EventHandler(Content_ValueChanged), sender, e);
|
---|
130 | else {
|
---|
131 | valueComboBox.SelectedIndex = valueComboBoxItems.IndexOf(Content.Value);
|
---|
132 | viewHost.ViewType = null;
|
---|
133 | viewHost.Content = Content.Value;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | private void ValidValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
137 | if (InvokeRequired)
|
---|
138 | Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsAdded), sender, e);
|
---|
139 | else
|
---|
140 | FillValueComboBox();
|
---|
141 | }
|
---|
142 | private void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
143 | if (InvokeRequired)
|
---|
144 | Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved), sender, e);
|
---|
145 | else
|
---|
146 | FillValueComboBox();
|
---|
147 | }
|
---|
148 | private void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
149 | if (InvokeRequired)
|
---|
150 | Invoke(new CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset), sender, e);
|
---|
151 | else
|
---|
152 | FillValueComboBox();
|
---|
153 | }
|
---|
154 | #endregion
|
---|
155 |
|
---|
156 | private void valueComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
157 | Content.Value = valueComboBoxItems[valueComboBox.SelectedIndex];
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|