Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ConstrainedItemBaseView.cs @ 1548

Last change on this file since 1548 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 7.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29
30namespace HeuristicLab.Core {
31  /// <summary>
32  /// The visual representation of an <see cref="IConstrainedItem"/>.
33  /// </summary>
34  public partial class ConstrainedItemBaseView : ViewBase {
35    private ChooseItemDialog chooseItemDialog;
36
37    /// <summary>
38    /// Gets or sets the current item to represent visually.
39    /// </summary>
40    public IConstrainedItem ConstrainedItem {
41      get { return (IConstrainedItem)Item; }
42      set { base.Item = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="ConstrainedItemBaseView"/>
47    /// with the caption "Constrained Item".
48    /// </summary>
49    public ConstrainedItemBaseView() {
50      InitializeComponent();
51      constraintsListView.Columns[0].Width = Math.Max(0, constraintsListView.Width - 25);
52      Caption = "Constrained Item";
53    }
54    /// <summary>
55    /// Initializes a new instance of <see cref="ConstrainedItemBaseView"/> with the given
56    /// <paramref name="constraintItem"/>.
57    /// </summary>
58    /// <param name="constraintItem">The item to represent visually.</param>
59    public ConstrainedItemBaseView(IConstrainedItem constraintItem)
60      : this() {
61      ConstrainedItem = constraintItem;
62    }
63
64    /// <summary>
65    /// Removes the event handlers from the underlying <see cref="IConstrainedItem"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
68    protected override void RemoveItemEvents() {
69      ConstrainedItem.ConstraintAdded -= new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintAdded);
70      ConstrainedItem.ConstraintRemoved -= new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintRemoved);
71      base.RemoveItemEvents();
72    }
73    /// <summary>
74    /// Adds event handlers to the underlying <see cref="IConstrainedItem"/>.
75    /// </summary>
76    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
77    protected override void AddItemEvents() {
78      base.AddItemEvents();
79      ConstrainedItem.ConstraintAdded += new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintAdded);
80      ConstrainedItem.ConstraintRemoved += new EventHandler<ConstraintEventArgs>(ConstrainedItemBase_ConstraintRemoved);
81    }
82
83    /// <summary>
84    /// Updates all controls with the latest data of the model.
85    /// </summary>
86    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
87    protected override void UpdateControls() {
88      base.UpdateControls();
89      constraintDetailsGroupBox.Controls.Clear();
90      if (ConstrainedItem == null) {
91        Caption = "Constrained Item";
92        constraintsListView.Enabled = false;
93        constraintDetailsGroupBox.Enabled = false;
94        removeConstraintButton.Enabled = false;
95      } else {
96        Caption = "Constrained Item (" + ConstrainedItem.GetType().Name + ")";
97        constraintsListView.Enabled = true;
98        constraintsListView.Items.Clear();
99        foreach (IConstraint constraint in ConstrainedItem.Constraints) {
100          ListViewItem item = new ListViewItem();
101          item.Text = constraint.GetType().Name;
102          item.Tag = constraint;
103          constraintsListView.Items.Add(item);
104        }
105        if (constraintsListView.Items.Count > 0)
106          constraintsListView.SelectedIndices.Add(0);
107      }
108    }
109
110    private void constraintsListView_SelectedIndexChanged(object sender, EventArgs e) {
111      if (constraintDetailsGroupBox.Controls.Count > 0)
112        constraintDetailsGroupBox.Controls[0].Dispose();
113      constraintDetailsGroupBox.Controls.Clear();
114      constraintDetailsGroupBox.Enabled = false;
115      removeConstraintButton.Enabled = false;
116      if (constraintsListView.SelectedItems.Count > 0) {
117        removeConstraintButton.Enabled = true;
118      }
119      if (constraintsListView.SelectedItems.Count == 1) {
120        IConstraint constraint = (IConstraint)constraintsListView.SelectedItems[0].Tag;
121        Control view = (Control)constraint.CreateView();
122        if (view != null) {
123          constraintDetailsGroupBox.Controls.Add(view);
124          view.Dock = DockStyle.Fill;
125          constraintDetailsGroupBox.Enabled = true;
126        }
127      }
128    }
129
130    #region Size Changed Events
131    private void constraintsListView_SizeChanged(object sender, EventArgs e) {
132      if (constraintsListView.Columns.Count > 0)
133        constraintsListView.Columns[0].Width = Math.Max(0, constraintsListView.Width - 25);
134    }
135    #endregion
136
137    #region Key Events
138    private void constraintsListView_KeyDown(object sender, KeyEventArgs e) {
139      if (e.KeyCode == Keys.Delete) {
140        if (constraintsListView.SelectedItems.Count > 0) {
141          foreach (ListViewItem item in constraintsListView.SelectedItems)
142            ConstrainedItem.RemoveConstraint((IConstraint)item.Tag);
143        }
144      }
145    }
146    #endregion
147
148    #region Button Events
149    private void addConstraintButton_Click(object sender, EventArgs e) {
150      if (chooseItemDialog == null) {
151        chooseItemDialog = new ChooseItemDialog(typeof(IConstraint));
152        chooseItemDialog.Caption = "Add Constraint";
153      }
154      if (chooseItemDialog.ShowDialog(this) == DialogResult.OK)
155        ConstrainedItem.AddConstraint((IConstraint)chooseItemDialog.Item);
156    }
157    private void removeConstraintButton_Click(object sender, EventArgs e) {
158      if (constraintsListView.SelectedItems.Count > 0) {
159        foreach (ListViewItem item in constraintsListView.SelectedItems)
160          ConstrainedItem.RemoveConstraint((IConstraint)item.Tag);
161      }
162    }
163    #endregion
164
165    #region ConstrainedItemBase Events
166    private void ConstrainedItemBase_ConstraintAdded(object sender, ConstraintEventArgs e) {
167      ListViewItem item = new ListViewItem();
168      item.Text = e.Constraint.GetType().Name;
169      item.Tag = e.Constraint;
170      constraintsListView.Items.Add(item);
171    }
172    private void ConstrainedItemBase_ConstraintRemoved(object sender, ConstraintEventArgs e) {
173      ListViewItem itemToDelete = null;
174      foreach (ListViewItem item in constraintsListView.Items) {
175        if (item.Tag == e.Constraint)
176          itemToDelete = item;
177      }
178      constraintsListView.Items.Remove(itemToDelete);
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.