Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2608 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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