[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Data {
|
---|
[737] | 32 | /// <summary>
|
---|
| 33 | /// The visual representation of the class <see cref="ConstrainedDoubleData"/>,
|
---|
| 34 | /// symbolizing a double value being restricted to some constraints.
|
---|
| 35 | /// </summary>
|
---|
[2] | 36 | public partial class ConstrainedDoubleDataView : ViewBase {
|
---|
[737] | 37 | /// <summary>
|
---|
| 38 | /// Gets or sets the double value to represent visually.
|
---|
| 39 | /// </summary>
|
---|
| 40 | /// <remarks>Uses property <see cref="HeuristicLab.Core.ViewBase.Item"/> of base class <see cref="ViewBase"/>,
|
---|
| 41 | /// but also own data storage present.</remarks>
|
---|
[2] | 42 | public ConstrainedDoubleData ConstrainedDoubleData {
|
---|
| 43 | get { return (ConstrainedDoubleData)Item; }
|
---|
| 44 | set {
|
---|
| 45 | base.Item = value;
|
---|
| 46 | constrainedDataBaseView.ConstrainedItem = value;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[737] | 50 | /// <summary>
|
---|
| 51 | /// Initializes a new instance of the class <see cref="ConstrainedDoubleDataView"/>.
|
---|
| 52 | /// </summary>
|
---|
[2] | 53 | public ConstrainedDoubleDataView() {
|
---|
| 54 | InitializeComponent();
|
---|
| 55 | }
|
---|
[737] | 56 | /// <summary>
|
---|
| 57 | /// Initializes a new instance of the class <see cref="ConstrainedDoubleDataView"/> with the given
|
---|
| 58 | /// <paramref name="constraintDoubleData"/>.
|
---|
| 59 | /// <note type="caution"> No CopyConstructor! <paramref name="constraintDoubleData"/> is not copied!</note>
|
---|
| 60 | /// </summary>
|
---|
| 61 | /// <param name="constraintDoubleData">The double value to represent visually.</param>
|
---|
[2] | 62 | public ConstrainedDoubleDataView(ConstrainedDoubleData constraintDoubleData)
|
---|
| 63 | : this() {
|
---|
| 64 | ConstrainedDoubleData = constraintDoubleData;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[737] | 67 | /// <summary>
|
---|
| 68 | /// Removes the eventhandler from the underlying <see cref="ConstrainedDoubleData"/>.
|
---|
| 69 | /// </summary>
|
---|
| 70 | /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
|
---|
| 71 | /// </remarks>
|
---|
[2] | 72 | protected override void RemoveItemEvents() {
|
---|
| 73 | ConstrainedDoubleData.Changed -= new EventHandler(ConstrainedDoubleData_Changed);
|
---|
| 74 | base.RemoveItemEvents();
|
---|
| 75 | }
|
---|
[737] | 76 | /// <summary>
|
---|
| 77 | /// Adds an eventhandler to the underlying <see cref="ConstrainedDoubleData"/>.
|
---|
| 78 | /// </summary>
|
---|
| 79 | /// <remarks>Calls <see cref="HeuristicLab.Core.ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
|
---|
| 80 | /// </remarks>
|
---|
[2] | 81 | protected override void AddItemEvents() {
|
---|
| 82 | base.AddItemEvents();
|
---|
| 83 | ConstrainedDoubleData.Changed += new EventHandler(ConstrainedDoubleData_Changed);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[737] | 86 | /// <summary>
|
---|
| 87 | /// Updates the constrols with the latest double value.
|
---|
| 88 | /// </summary>
|
---|
[2] | 89 | protected override void UpdateControls() {
|
---|
| 90 | base.UpdateControls();
|
---|
| 91 | if (ConstrainedDoubleData == null) {
|
---|
| 92 | dataTextBox.Enabled = false;
|
---|
| 93 | } else {
|
---|
| 94 | dataTextBox.Enabled = true;
|
---|
[344] | 95 | dataTextBox.Text = ConstrainedDoubleData.Data.ToString("r");
|
---|
[2] | 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void dataTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 100 | e.Cancel = false;
|
---|
| 101 | double value;
|
---|
| 102 | try {
|
---|
| 103 | value = double.Parse(dataTextBox.Text);
|
---|
| 104 | ICollection<IConstraint> violatedConstraints;
|
---|
| 105 | if (!ConstrainedDoubleData.TrySetData(value, out violatedConstraints)) {
|
---|
| 106 | if (Auxiliary.ShowIgnoreConstraintViolationMessageBox(violatedConstraints) == DialogResult.Yes)
|
---|
| 107 | ConstrainedDoubleData.Data = value;
|
---|
| 108 | else
|
---|
| 109 | e.Cancel = true;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | catch (Exception) {
|
---|
| 113 | dataTextBox.SelectAll();
|
---|
| 114 | e.Cancel = true;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | private void ConstrainedDoubleData_Changed(object sender, EventArgs e) {
|
---|
| 119 | Refresh();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|