Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman.Views/3.3/TravelingSalesmanProblemView.cs @ 3454

Last change on this file since 3454 was 3454, checked in by swagner, 14 years ago

Adapted views according the new read-only property (#973)

File size: 4.5 KB
Line 
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
22using System;
23using System.Windows.Forms;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.TravelingSalesman.Views {
30  /// <summary>
31  /// The base class for visual representations of items.
32  /// </summary>
33  [View("Traveling Salesman Problem View")]
34  [Content(typeof(TravelingSalesmanProblem), true)]
35  public sealed partial class TravelingSalesmanProblemView : NamedItemView {
36    private TSPLIBImportDialog tsplibImportDialog;
37
38    public new TravelingSalesmanProblem Content {
39      get { return (TravelingSalesmanProblem)base.Content; }
40      set { base.Content = value; }
41    }
42
43    /// <summary>
44    /// Initializes a new instance of <see cref="ItemBaseView"/>.
45    /// </summary>
46    public TravelingSalesmanProblemView() {
47      InitializeComponent();
48    }
49    /// <summary>
50    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
51    /// </summary>
52    /// <param name="item">The item that should be displayed.</param>
53    public TravelingSalesmanProblemView(TravelingSalesmanProblem content)
54      : this() {
55      Content = content;
56    }
57
58    protected override void DeregisterContentEvents() {
59      Content.CoordinatesParameter.ValueChanged -= new EventHandler(CoordinatesParameter_ValueChanged);
60      Content.BestKnownSolutionParameter.ValueChanged -= new EventHandler(BestKnownSolutionParameter_ValueChanged);
61      base.DeregisterContentEvents();
62    }
63    protected override void RegisterContentEvents() {
64      base.RegisterContentEvents();
65      Content.CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
66      Content.BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        parameterCollectionView.Content = null;
73        pathTSPTourView.Content = null;
74      } else {
75        parameterCollectionView.Content = ((IParameterizedNamedItem)Content).Parameters;
76        pathTSPTourView.Content = new PathTSPTour(Content.Coordinates, Content.BestKnownSolution);
77      }
78      SetEnabledStateOfControls();
79    }
80
81    protected override void OnReadOnlyChanged() {
82      base.OnReadOnlyChanged();
83      SetEnabledStateOfControls();
84    }
85
86    private void SetEnabledStateOfControls() {
87      parameterCollectionView.Enabled = Content != null;
88      parameterCollectionView.ReadOnly = ReadOnly;
89      pathTSPTourView.Enabled = Content != null;
90      pathTSPTourView.ReadOnly = ReadOnly;
91      importButton.Enabled = Content != null && !ReadOnly;
92    }
93
94    private void importButton_Click(object sender, System.EventArgs e) {
95      if (tsplibImportDialog == null) tsplibImportDialog = new TSPLIBImportDialog();
96
97      if (tsplibImportDialog.ShowDialog(this) == DialogResult.OK) {
98        try {
99          if (tsplibImportDialog.Quality == null)
100            Content.ImportFromTSPLIB(tsplibImportDialog.TSPFileName, tsplibImportDialog.TourFileName);
101          else
102            Content.ImportFromTSPLIB(tsplibImportDialog.TSPFileName, tsplibImportDialog.TourFileName, (double)tsplibImportDialog.Quality);
103        }
104        catch (Exception ex) {
105          Auxiliary.ShowErrorMessageBox(ex);
106        }
107      }
108    }
109
110    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
111      pathTSPTourView.Content.Coordinates = Content.Coordinates;
112    }
113    private void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
114      pathTSPTourView.Content.Permutation = Content.BestKnownSolution;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.