Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.2/NotConstraintView.cs @ 4498

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

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

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.Windows.Forms;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Constraints {
34  /// <summary>
35  /// Visual representation of a <see cref="NotConstraint"/>.
36  /// </summary>
37  public partial class NotConstraintView : ViewBase {
38    private Type[] itemTypes;
39
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>
46    public NotConstraint NotConstraint {
47      get { return (NotConstraint)Item; }
48      set {
49        base.Item = value;
50        UpdateSubConstraintComboBox();
51        Refresh();
52      }
53    }
54
55    /// <summary>
56    /// Initializes a new instance of <see cref="NotConstraintView"/>.
57    /// </summary>
58    public NotConstraintView() {
59      InitializeComponent();
60      DiscoveryService discoveryService = new DiscoveryService();
61      itemTypes = discoveryService.GetTypes(typeof(ConstraintBase));
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.