#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Core.Views;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Views {
[View("GQAPAssignmentView")]
[Content(typeof(GQAPAssignment), IsDefaultView = true)]
public partial class GQAPAssignmentView : ItemView {
public new GQAPAssignment Content {
get { return (GQAPAssignment)base.Content; }
set { base.Content = value; }
}
public GQAPAssignmentView() {
InitializeComponent();
}
#region Register Content Events
protected override void DeregisterContentEvents() {
// TODO: Deregister your event handlers on the Content here
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
// TODO: Register your event handlers on the Content here
}
#endregion
protected override void OnContentChanged() {
base.OnContentChanged();
assignmentTreeView.Nodes.Clear();
if (Content == null) {
qualityTextBox.Text = String.Empty;
} else {
qualityTextBox.Text = Content.Quality.Value.ToString();
BuildAssignmentTree();
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
qualityTextBox.Enabled = !ReadOnly && !Locked && Content != null;
assignmentTreeView.Enabled = !ReadOnly && !Locked && Content != null;
}
#region Event Handlers
// TODO: Put event handlers here
#endregion
private void BuildAssignmentTree() {
IntegerVector assignment = Content.Assignment;
Dictionary locationNodes = new Dictionary();
for (int i = 0; i < assignment.Length; i++) {
if (!locationNodes.ContainsKey(assignment[i])) {
string locationName = assignment[i].ToString();
if (Content.LocationNames != null && Content.LocationNames.Length > 0)
locationName = Content.LocationNames[assignment[i] % Content.LocationNames.Length];
locationNodes.Add(assignment[i], new TreeNode(locationName));
}
string equipmentName = i.ToString();
if (Content.EquipmentNames != null && Content.EquipmentNames.Length > 0)
equipmentName = Content.EquipmentNames[i % Content.EquipmentNames.Length];
locationNodes[assignment[i]].Nodes.Add(equipmentName);
}
assignmentTreeView.BeginUpdate();
try {
foreach (var node in locationNodes.OrderBy(x => x.Key).Select(x => x.Value)) {
assignmentTreeView.Nodes.Add(node);
node.Expand();
}
} finally { assignmentTreeView.EndUpdate(); }
}
}
}