1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core.Views;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
30 |
|
---|
31 | namespace 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":
|
---|
99 | qualityView.Content = Content.Quality;
|
---|
100 | break;
|
---|
101 | case "Assignment":
|
---|
102 | if (Content.Assignment != null)
|
---|
103 | Content.Assignment.ItemChanged += new EventHandler<EventArgs<int>>(Assignment_ItemChanged);
|
---|
104 | assignmentViewHost.Content = Content.Assignment;
|
---|
105 | UpdateAssignmentMatrix();
|
---|
106 | break;
|
---|
107 | case "RowNames":
|
---|
108 | if (Content.RowNames != null)
|
---|
109 | Content.RowNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
|
---|
110 | UpdateAssignmentMatrix();
|
---|
111 | break;
|
---|
112 | case "ColumnNames":
|
---|
113 | if (Content.ColumnNames != null)
|
---|
114 | Content.ColumnNames.ItemChanged += new EventHandler<EventArgs<int>>(Names_ItemChanged);
|
---|
115 | UpdateAssignmentMatrix();
|
---|
116 | break;
|
---|
117 | default: break;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | #endregion
|
---|
121 |
|
---|
122 | private void UpdateAssignmentMatrix() {
|
---|
123 | assignmentDataGridView.Rows.Clear();
|
---|
124 | if (Content.Assignment != null) {
|
---|
125 | string rowName, colName;
|
---|
126 | var rows = new DataGridViewRow[Content.Assignment.Length];
|
---|
127 | for (int i = 0; i < Content.Assignment.Length; i++) {
|
---|
128 | if (Content.RowNames != null && Content.RowNames.Length > i)
|
---|
129 | rowName = Content.RowNames[i];
|
---|
130 | else rowName = "Row " + (i + 1).ToString();
|
---|
131 | if (Content.ColumnNames != null && Content.ColumnNames.Length > Content.Assignment[i])
|
---|
132 | colName = Content.ColumnNames[Content.Assignment[i]];
|
---|
133 | else colName = "Column " + (Content.Assignment[i] + 1).ToString();
|
---|
134 | rows[i] = new DataGridViewRow();
|
---|
135 | rows[i].CreateCells(assignmentDataGridView, new string[] { rowName, colName });
|
---|
136 | }
|
---|
137 | assignmentDataGridView.Rows.AddRange(rows);
|
---|
138 | assignmentDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 | }
|
---|