Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.LinearAssignment.Views/3.3/LAPAssignmentView.cs @ 11479

Last change on this file since 11479 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.ComponentModel;
24using System.Windows.Forms;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30
31namespace HeuristicLab.Problems.LinearAssignment.Views {
32  [View("LAPAssignmentView")]
33  [Content(typeof(LAPAssignment), IsDefaultView = true)]
34  public partial class LAPAssignmentView : ItemView {
35    private ViewHost assignmentViewHost;
36
37    public new LAPAssignment Content {
38      get { return (LAPAssignment)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public LAPAssignmentView() {
43      InitializeComponent();
44      assignmentViewHost = new ViewHost();
45      assignmentViewHost.Dock = DockStyle.Fill;
46      assignmentViewHost.ViewsLabelVisible = true;
47      splitContainer.Panel2.Controls.Add(assignmentViewHost);
48    }
49
50    #region Register Content Events
51    protected override void DeregisterContentEvents() {
52      Content.PropertyChanged -= new PropertyChangedEventHandler(Content_PropertyChanged);
53      if (Content.Assignment != null) Content.Assignment.ItemChanged -= new EventHandler<EventArgs<int>>(Assignment_ItemChanged);
54      if (Content.RowNames != null) Content.RowNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
55      if (Content.ColumnNames != null) Content.ColumnNames.ItemChanged -= new EventHandler<EventArgs<int>>(Names_ItemChanged);
56      base.DeregisterContentEvents();
57    }
58    protected override void RegisterContentEvents() {
59      base.RegisterContentEvents();
60      Content.PropertyChanged += new PropertyChangedEventHandler(Content_PropertyChanged);
61      if (Content.Assignment != null) Content.Assignment.ItemChanged += new EventHandler<EventArgs<int>>(Assignment_ItemChanged);
62      if (Content.RowNames != null) Content.RowNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
63      if (Content.ColumnNames != null) Content.ColumnNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
64    }
65
66    private void Assignment_ItemChanged(object sender, EventArgs<int> e) {
67      if (sender != Content.Assignment)
68        ((Permutation)sender).ItemChanged -= new EventHandler<EventArgs<int>>(Assignment_ItemChanged);
69      else UpdateAssignmentMatrix();
70    }
71
72    private void Names_ItemChanged(object sender, EventArgs<int> e) {
73      UpdateAssignmentMatrix();
74    }
75    #endregion
76
77    protected override void OnContentChanged() {
78      base.OnContentChanged();
79      if (Content == null) {
80        qualityView.Content = null;
81        assignmentViewHost.Content = null;
82        assignmentDataGridView.Rows.Clear();
83      } else {
84        qualityView.Content = Content.Quality;
85        assignmentViewHost.Content = Content.Assignment;
86        UpdateAssignmentMatrix();
87      }
88    }
89
90    protected override void SetEnabledStateOfControls() {
91      base.SetEnabledStateOfControls();
92      assignmentDataGridView.Enabled = Content != null;
93    }
94
95    #region Event Handlers
96    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
97      switch (e.PropertyName) {
98        case "Quality": qualityView.Content = Content.Quality;
99          break;
100        case "Assignment":
101          if (Content.Assignment != null)
102            Content.Assignment.ItemChanged += new EventHandler<EventArgs<int>>(Assignment_ItemChanged);
103          assignmentViewHost.Content = Content.Assignment;
104          UpdateAssignmentMatrix();
105          break;
106        case "RowNames":
107          if (Content.RowNames != null)
108            Content.RowNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
109          UpdateAssignmentMatrix();
110          break;
111        case "ColumnNames":
112          if (Content.ColumnNames != null)
113            Content.ColumnNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
114          UpdateAssignmentMatrix();
115          break;
116        default: break;
117      }
118    }
119    #endregion
120
121    private void UpdateAssignmentMatrix() {
122      assignmentDataGridView.Rows.Clear();
123      if (Content.Assignment != null) {
124        string rowName, colName;
125        var rows = new DataGridViewRow[Content.Assignment.Length];
126        for (int i = 0; i < Content.Assignment.Length; i++) {
127          if (Content.RowNames != null && Content.RowNames.Length > i)
128            rowName = Content.RowNames[i];
129          else rowName = "Row " + (i + 1).ToString();
130          if (Content.ColumnNames != null && Content.ColumnNames.Length > Content.Assignment[i])
131            colName = Content.ColumnNames[Content.Assignment[i]];
132          else colName = "Column " + (Content.Assignment[i] + 1).ToString();
133          rows[i] = new DataGridViewRow();
134          rows[i].CreateCells(assignmentDataGridView, new string[] { rowName, colName });
135        }
136        assignmentDataGridView.Rows.AddRange(rows);
137        assignmentDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
138      }
139    }
140
141  }
142}
Note: See TracBrowser for help on using the repository browser.