Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.2/ConstrainedItemBaseView.cs @ 4383

Last change on this file since 4383 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

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