Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Data/ConstrainedDoubleDataView.cs @ 1181

Last change on this file since 1181 was 737, checked in by vdorfer, 16 years ago

Created API documentation for HeuristLab.Data namespace (#331)

File size: 4.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30
31namespace HeuristicLab.Data {
32  /// <summary>
33  /// The visual representation of the class <see cref="ConstrainedDoubleData"/>,
34  /// symbolizing a double value being restricted to some constraints.
35  /// </summary>
36  public partial class ConstrainedDoubleDataView : ViewBase {
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>
42    public ConstrainedDoubleData ConstrainedDoubleData {
43      get { return (ConstrainedDoubleData)Item; }
44      set {
45        base.Item = value;
46        constrainedDataBaseView.ConstrainedItem = value;
47      }
48    }
49
50    /// <summary>
51    /// Initializes a new instance of the class <see cref="ConstrainedDoubleDataView"/>.
52    /// </summary>
53    public ConstrainedDoubleDataView() {
54      InitializeComponent();
55    }
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>
62    public ConstrainedDoubleDataView(ConstrainedDoubleData constraintDoubleData)
63      : this() {
64      ConstrainedDoubleData = constraintDoubleData;
65    }
66
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>
72    protected override void RemoveItemEvents() {
73      ConstrainedDoubleData.Changed -= new EventHandler(ConstrainedDoubleData_Changed);
74      base.RemoveItemEvents();
75    }
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>
81    protected override void AddItemEvents() {
82      base.AddItemEvents();
83      ConstrainedDoubleData.Changed += new EventHandler(ConstrainedDoubleData_Changed);
84    }
85
86    /// <summary>
87    /// Updates the constrols with the latest double value.
88    /// </summary>
89    protected override void UpdateControls() {
90      base.UpdateControls();
91      if (ConstrainedDoubleData == null) {
92        dataTextBox.Enabled = false;
93      } else {
94        dataTextBox.Enabled = true;
95        dataTextBox.Text = ConstrainedDoubleData.Data.ToString("r");
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}
Note: See TracBrowser for help on using the repository browser.