Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.2/NotConstraintView.cs @ 3005

Last change on this file since 3005 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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