Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Constraints/NotConstraintView.cs @ 4309

Last change on this file since 4309 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.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;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32
33namespace HeuristicLab.Constraints {
34  public partial class NotConstraintView : ViewBase {
35    private Type[] itemTypes;
36
37    public NotConstraint NotConstraint {
38      get { return (NotConstraint)Item; }
39      set {
40        base.Item = value;
41        UpdateSubConstraintComboBox();
42        Refresh();
43      }
44    }
45
46    public NotConstraintView() {
47      InitializeComponent();
48      DiscoveryService discoveryService = new DiscoveryService();
49      itemTypes = discoveryService.GetTypes(typeof(ConstraintBase));
50      for (int i = 0; i < itemTypes.Length; i++) {
51        subConstraintComboBox.Items.Add(itemTypes[i].Name);
52      }
53      subConstraintComboBox.SelectedIndex = 0;
54      subConstraintComboBox.Enabled = false;
55    }
56
57    public NotConstraintView(NotConstraint notConstraint)
58      : this() {
59      NotConstraint = notConstraint;
60      UpdateSubConstraintComboBox();
61      Refresh();
62    }
63
64    protected override void RemoveItemEvents() {
65      NotConstraint.Changed -= new EventHandler(NotConstraint_Changed);
66      base.RemoveItemEvents();
67    }
68    protected override void AddItemEvents() {
69      base.AddItemEvents();
70      NotConstraint.Changed += new EventHandler(NotConstraint_Changed);
71    }
72
73    protected override void UpdateControls() {
74      base.UpdateControls();
75      if (NotConstraint == null) {
76        subConstraintComboBox.Enabled = false;
77        subConstraintViewBase.Enabled = false;
78      } else {
79        if (subConstraintViewBase != null && Controls.Contains(subConstraintViewBase)) {
80          Controls.Remove(subConstraintViewBase);
81          subConstraintViewBase.Dispose();
82        }
83        subConstraintViewBase = (ViewBase)NotConstraint.SubConstraint.CreateView();
84        if (subConstraintViewBase != null) {
85          subConstraintViewBase.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
86          subConstraintViewBase.Location = new Point(0, 30);
87          subConstraintViewBase.Name = "subConstraintViewBase";
88          subConstraintViewBase.Size = new Size(Width, Height - 30);
89          Controls.Add(subConstraintViewBase);
90        }
91        subConstraintComboBox.Enabled = true;
92        subConstraintViewBase.Enabled = true;
93      }
94    }
95
96    private void subConstraintComboBox_SelectedIndexChanged(object sender, EventArgs e) {
97      if (NotConstraint != null) {
98        try {
99          NotConstraint.SubConstraint = (ConstraintBase)Activator.CreateInstance(itemTypes[subConstraintComboBox.SelectedIndex]);
100        } catch (Exception) {
101          NotConstraint.SubConstraint = new FalseConstraint();
102        }
103      }
104    }
105
106    private void NotConstraint_Changed(object sender, EventArgs e) {
107      Refresh();
108      UpdateSubConstraintComboBox();
109    }
110
111
112    private void UpdateSubConstraintComboBox() {
113      subConstraintComboBox.SelectedIndexChanged -= new EventHandler(subConstraintComboBox_SelectedIndexChanged);
114      for (int i = 0 ; i < itemTypes.Length ; i++)
115        if (itemTypes[i].Name.Equals(NotConstraint.SubConstraint.GetType().Name))
116          subConstraintComboBox.SelectedIndex = i;
117      subConstraintComboBox.SelectedIndexChanged += new EventHandler(subConstraintComboBox_SelectedIndexChanged);
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.