[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 | using System.Data;
|
---|
| 27 | using System.Text;
|
---|
| 28 | using System.Windows.Forms;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Core {
|
---|
| 31 | public partial class ConstrainedItemBaseView : ViewBase {
|
---|
| 32 | private ChooseItemDialog chooseItemDialog;
|
---|
| 33 |
|
---|
| 34 | public IConstrainedItem ConstrainedItem {
|
---|
| 35 | get { return (IConstrainedItem)Item; }
|
---|
| 36 | set { base.Item = value; }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public ConstrainedItemBaseView() {
|
---|
| 40 | InitializeComponent();
|
---|
| 41 | constraintsListView.Columns[0].Width = Math.Max(0, constraintsListView.Width - 25);
|
---|
| 42 | Caption = "Constrained Item";
|
---|
| 43 | }
|
---|
| 44 | public ConstrainedItemBaseView(IConstrainedItem constraintItem)
|
---|
| 45 | : this() {
|
---|
| 46 | ConstrainedItem = constraintItem;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | protected override void RemoveItemEvents() {
|
---|
| 50 | ConstrainedItem.ConstraintAdded -= new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintAdded);
|
---|
| 51 | ConstrainedItem.ConstraintRemoved -= new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintRemoved);
|
---|
| 52 | base.RemoveItemEvents();
|
---|
| 53 | }
|
---|
| 54 | protected override void AddItemEvents() {
|
---|
| 55 | base.AddItemEvents();
|
---|
| 56 | ConstrainedItem.ConstraintAdded += new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintAdded);
|
---|
| 57 | ConstrainedItem.ConstraintRemoved += new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintRemoved);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | protected override void UpdateControls() {
|
---|
| 61 | base.UpdateControls();
|
---|
| 62 | constraintDetailsGroupBox.Controls.Clear();
|
---|
| 63 | if (ConstrainedItem == null) {
|
---|
| 64 | Caption = "Constrained Item";
|
---|
| 65 | constraintsListView.Enabled = false;
|
---|
| 66 | constraintDetailsGroupBox.Enabled = false;
|
---|
| 67 | removeConstraintButton.Enabled = false;
|
---|
| 68 | } else {
|
---|
| 69 | Caption = "Constrained Item (" + ConstrainedItem.GetType().Name + ")";
|
---|
| 70 | constraintsListView.Enabled = true;
|
---|
| 71 | constraintsListView.Items.Clear();
|
---|
| 72 | foreach (IConstraint constraint in ConstrainedItem.Constraints) {
|
---|
| 73 | ListViewItem item = new ListViewItem();
|
---|
| 74 | item.Text = constraint.GetType().Name;
|
---|
| 75 | item.Tag = constraint;
|
---|
| 76 | constraintsListView.Items.Add(item);
|
---|
| 77 | }
|
---|
| 78 | if (constraintsListView.Items.Count > 0)
|
---|
| 79 | constraintsListView.SelectedIndices.Add(0);
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private void constraintsListView_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 84 | if (constraintDetailsGroupBox.Controls.Count > 0)
|
---|
| 85 | constraintDetailsGroupBox.Controls[0].Dispose();
|
---|
| 86 | constraintDetailsGroupBox.Controls.Clear();
|
---|
| 87 | constraintDetailsGroupBox.Enabled = false;
|
---|
| 88 | removeConstraintButton.Enabled = false;
|
---|
| 89 | if (constraintsListView.SelectedItems.Count > 0) {
|
---|
| 90 | removeConstraintButton.Enabled = true;
|
---|
| 91 | }
|
---|
| 92 | if (constraintsListView.SelectedItems.Count == 1) {
|
---|
| 93 | IConstraint constraint = (IConstraint)constraintsListView.SelectedItems[0].Tag;
|
---|
| 94 | Control view = (Control)constraint.CreateView();
|
---|
| 95 | if (view != null) {
|
---|
| 96 | constraintDetailsGroupBox.Controls.Add(view);
|
---|
| 97 | view.Dock = DockStyle.Fill;
|
---|
| 98 | constraintDetailsGroupBox.Enabled = true;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | #region Size Changed Events
|
---|
| 104 | private void constraintsListView_SizeChanged(object sender, EventArgs e) {
|
---|
| 105 | if (constraintsListView.Columns.Count > 0)
|
---|
| 106 | constraintsListView.Columns[0].Width = Math.Max(0, constraintsListView.Width - 25);
|
---|
| 107 | }
|
---|
| 108 | #endregion
|
---|
| 109 |
|
---|
| 110 | #region Key Events
|
---|
| 111 | private void constraintsListView_KeyDown(object sender, KeyEventArgs e) {
|
---|
| 112 | if (e.KeyCode == Keys.Delete) {
|
---|
| 113 | if (constraintsListView.SelectedItems.Count > 0) {
|
---|
| 114 | foreach (ListViewItem item in constraintsListView.SelectedItems)
|
---|
| 115 | ConstrainedItem.RemoveConstraint((IConstraint)item.Tag);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | #endregion
|
---|
| 120 |
|
---|
| 121 | #region Button Events
|
---|
| 122 | private void addConstraintButton_Click(object sender, EventArgs e) {
|
---|
| 123 | if (chooseItemDialog == null) {
|
---|
| 124 | chooseItemDialog = new ChooseItemDialog(typeof(IConstraint));
|
---|
| 125 | chooseItemDialog.Caption = "Add Constraint";
|
---|
| 126 | }
|
---|
| 127 | if (chooseItemDialog.ShowDialog(this) == DialogResult.OK)
|
---|
| 128 | ConstrainedItem.AddConstraint((IConstraint)chooseItemDialog.Item);
|
---|
| 129 | }
|
---|
| 130 | private void removeConstraintButton_Click(object sender, EventArgs e) {
|
---|
| 131 | if (constraintsListView.SelectedItems.Count > 0) {
|
---|
| 132 | foreach (ListViewItem item in constraintsListView.SelectedItems)
|
---|
| 133 | ConstrainedItem.RemoveConstraint((IConstraint)item.Tag);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 |
|
---|
| 138 | #region ConstrainedItemBase Events
|
---|
| 139 | private void ConstrainedItemBase_ConstraintAdded(object sender, ConstraintEventArgs e) {
|
---|
| 140 | ListViewItem item = new ListViewItem();
|
---|
| 141 | item.Text = e.Constraint.GetType().Name;
|
---|
| 142 | item.Tag = e.Constraint;
|
---|
| 143 | constraintsListView.Items.Add(item);
|
---|
| 144 | }
|
---|
| 145 | private void ConstrainedItemBase_ConstraintRemoved(object sender, ConstraintEventArgs e) {
|
---|
| 146 | ListViewItem itemToDelete = null;
|
---|
| 147 | foreach (ListViewItem item in constraintsListView.Items) {
|
---|
| 148 | if (item.Tag == e.Constraint)
|
---|
| 149 | itemToDelete = item;
|
---|
| 150 | }
|
---|
| 151 | constraintsListView.Items.Remove(itemToDelete);
|
---|
| 152 | }
|
---|
| 153 | #endregion
|
---|
| 154 | }
|
---|
| 155 | }
|
---|