Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Routing.TSP/TSPInjectorView.cs @ 1314

Last change on this file since 1314 was 884, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Routing.TSP namespace (#331)

File size: 5.9 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;
30using HeuristicLab.Data;
31using HeuristicLab.Operators;
32
33namespace HeuristicLab.Routing.TSP {
34  /// <summary>
35  /// Class to represent a <see cref="TSPInjector"/> visually.
36  /// </summary>
37  public partial class TSPInjectorView : ViewBase {
38    /// <summary>
39    /// Gets or set the <see cref="TSPInjector"/> to represent visually.
40    /// </summary>       
41    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
42    /// No own data storage present.</remarks>
43    public TSPInjector TSPInjector {
44      get { return (TSPInjector)Item; }
45      set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="TSPInjectorView"/>.
50    /// </summary>
51    public TSPInjectorView() {
52      InitializeComponent();
53    }
54    /// <summary>
55    /// Initializes a new instance of <see cref="TSPInjectorView"/> with the given
56    /// <paramref name="tspInjector"/>.
57    /// </summary>
58    /// <param name="tspInjector">The <see cref="TSPInjector"/> to display.</param>
59    public TSPInjectorView(TSPInjector tspInjector)
60      : this() {
61      TSPInjector = tspInjector;
62    }
63
64    /// <summary>
65    /// Removes the event handlers in all children.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      operatorBaseVariableInfosView.Operator = null;
70      operatorBaseDescriptionView.Operator = null;
71      base.RemoveItemEvents();
72    }
73    /// <summary>
74    /// Adds event handlers in all children.
75    /// </summary>
76    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
77    protected override void AddItemEvents() {
78      base.AddItemEvents();
79      operatorBaseVariableInfosView.Operator = TSPInjector;
80      operatorBaseDescriptionView.Operator = TSPInjector;
81    }
82
83    /// <summary>
84    /// Updates all controls with the latest data of the model.
85    /// </summary>
86    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
87    protected override void UpdateControls() {
88      base.UpdateControls();
89      if (TSPInjector == null) {
90        citiesTextBox.Text = "-";
91        bestKnownQualityAvailableCheckBox.Checked = false;
92        bestKnownQualityAvailableCheckBox.Enabled = false;
93        bestKnownQualityTextBox.Text = "-";
94        bestKnownQualityTextBox.Enabled = false;
95        importInstanceButton.Enabled = false;
96      } else {
97        citiesTextBox.Text = TSPInjector.GetVariable("Cities").Value.ToString();
98        bestKnownQualityAvailableCheckBox.Checked = TSPInjector.GetVariable("InjectBestKnownQuality").GetValue<BoolData>().Data;
99        bestKnownQualityAvailableCheckBox.Enabled = true;
100        if (bestKnownQualityAvailableCheckBox.Checked) {
101          bestKnownQualityTextBox.Text = TSPInjector.GetVariable("BestKnownQuality").Value.ToString();
102          bestKnownQualityTextBox.Enabled = true;
103        } else {
104          bestKnownQualityTextBox.Text = "-";
105          bestKnownQualityTextBox.Enabled = false;
106        }
107        importInstanceButton.Enabled = true;
108      }
109    }
110
111    private void bestKnownQualityAvailableCheckBox_CheckedChanged(object sender, EventArgs e) {
112      TSPInjector.GetVariable("InjectBestKnownQuality").GetValue<BoolData>().Data = bestKnownQualityAvailableCheckBox.Checked;
113      if (bestKnownQualityAvailableCheckBox.Checked) {
114        bestKnownQualityTextBox.Text = TSPInjector.GetVariable("BestKnownQuality").Value.ToString();
115        bestKnownQualityTextBox.Enabled = true;
116      } else {
117        bestKnownQualityTextBox.Text = "-";
118        bestKnownQualityTextBox.Enabled = false;
119      }
120    }
121
122    private void bestKnownQualityTextBox_Validating(object sender, CancelEventArgs e) {
123      e.Cancel = false;
124      try {
125        TSPInjector.GetVariable("BestKnownQuality").GetValue<DoubleData>().Data = double.Parse(bestKnownQualityTextBox.Text);
126      }
127      catch (Exception) {
128        bestKnownQualityTextBox.SelectAll();
129        e.Cancel = true;
130      }
131    }
132
133    private void importInstanceButton_Click(object sender, EventArgs e) {
134      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
135        TSPParser parser = null;
136        bool success = false;
137        try {
138          parser = new TSPParser(openFileDialog.FileName);
139          parser.Parse();
140          success = true;
141        }
142        catch (Exception ex) {
143          MessageBox.Show(this, ex.Message, ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error);
144        }
145        if (success) {
146          TSPInjector.GetVariable("Cities").Value = new IntData(parser.Vertices.GetLength(0));
147          TSPInjector.GetVariable("Coordinates").Value = new DoubleMatrixData(parser.Vertices);
148          Refresh();
149        }
150      }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.