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