Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.3/NotConstraintView.cs @ 4062

Last change on this file since 4062 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

File size: 5.8 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.Linq;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Core;
32using HeuristicLab.Data;
33
34namespace HeuristicLab.Constraints {
35  /// <summary>
36  /// Visual representation of a <see cref="NotConstraint"/>.
37  /// </summary>
38  public partial class NotConstraintView : ViewBase {
39    private Type[] itemTypes;
40
41    /// <summary>
42    /// Gets or sets the NotConstraint to represent visually.
43    /// </summary>
44    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
45    /// No own data storage present.<br/>
46    /// Calls <see cref="ViewBase.Refresh"/> in the setter.</remarks>
47    public NotConstraint NotConstraint {
48      get { return (NotConstraint)Item; }
49      set {
50        base.Item = value;
51        UpdateSubConstraintComboBox();
52        Refresh();
53      }
54    }
55
56    /// <summary>
57    /// Initializes a new instance of <see cref="NotConstraintView"/>.
58    /// </summary>
59    public NotConstraintView() {
60      InitializeComponent();
61      itemTypes = ApplicationManager.Manager.GetTypes(typeof(ConstraintBase)).ToArray();
62      for (int i = 0; i < itemTypes.Length; i++) {
63        subConstraintComboBox.Items.Add(itemTypes[i].Name);
64      }
65      subConstraintComboBox.SelectedIndex = 0;
66      subConstraintComboBox.Enabled = false;
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="NotConstraintView"/> with the given
71    /// <paramref name="notConstraint"/> to display.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.Refresh"/> in the setter.</remarks>
74    /// <param name="notConstraint">The constraint to represent visually.</param>
75    public NotConstraintView(NotConstraint notConstraint)
76      : this() {
77      NotConstraint = notConstraint;
78      UpdateSubConstraintComboBox();
79      Refresh();
80    }
81
82    /// <summary>
83    /// Removes the eventhandler from the underlying <see cref="NotConstraint"/>.
84    /// </summary>
85    /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.
86    /// </remarks>
87    protected override void RemoveItemEvents() {
88      NotConstraint.Changed -= new EventHandler(NotConstraint_Changed);
89      base.RemoveItemEvents();
90    }
91    /// <summary>
92    /// Adds an eventhandler to the underlying <see cref="NotConstraint"/>.
93    /// </summary>
94    /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.
95    /// </remarks>
96    protected override void AddItemEvents() {
97      base.AddItemEvents();
98      NotConstraint.Changed += new EventHandler(NotConstraint_Changed);
99    }
100
101    /// <summary>
102    /// Updates all controls with the latest values.
103    /// </summary>
104    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
105    protected override void UpdateControls() {
106      base.UpdateControls();
107      if (NotConstraint == null) {
108        subConstraintComboBox.Enabled = false;
109        subConstraintViewBase.Enabled = false;
110      } else {
111        if (subConstraintViewBase != null && Controls.Contains(subConstraintViewBase)) {
112          Controls.Remove(subConstraintViewBase);
113          subConstraintViewBase.Dispose();
114        }
115        subConstraintViewBase = (ViewBase)NotConstraint.SubConstraint.CreateView();
116        if (subConstraintViewBase != null) {
117          subConstraintViewBase.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
118          subConstraintViewBase.Location = new Point(0, 30);
119          subConstraintViewBase.Name = "subConstraintViewBase";
120          subConstraintViewBase.Size = new Size(Width, Height - 30);
121          Controls.Add(subConstraintViewBase);
122        }
123        subConstraintComboBox.Enabled = true;
124        subConstraintViewBase.Enabled = true;
125      }
126    }
127
128    private void subConstraintComboBox_SelectedIndexChanged(object sender, EventArgs e) {
129      if (NotConstraint != null) {
130        try {
131          NotConstraint.SubConstraint = (ConstraintBase)Activator.CreateInstance(itemTypes[subConstraintComboBox.SelectedIndex]);
132        } catch (Exception) {
133          NotConstraint.SubConstraint = null;
134        }
135      }
136    }
137
138    private void NotConstraint_Changed(object sender, EventArgs e) {
139      Refresh();
140      UpdateSubConstraintComboBox();
141    }
142
143
144    private void UpdateSubConstraintComboBox() {
145      subConstraintComboBox.SelectedIndexChanged -= new EventHandler(subConstraintComboBox_SelectedIndexChanged);
146      for (int i = 0 ; i < itemTypes.Length ; i++)
147        if (itemTypes[i].Name.Equals(NotConstraint.SubConstraint.GetType().Name))
148          subConstraintComboBox.SelectedIndex = i;
149      subConstraintComboBox.SelectedIndexChanged += new EventHandler(subConstraintComboBox_SelectedIndexChanged);
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.