Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperatorView.cs @ 2575

Last change on this file since 2575 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 5.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.Core;
30using HeuristicLab.Operators;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33
34namespace HeuristicLab.Operators.Programmable {
35  [Content(typeof(ProgrammableOperator), true)]
36  public partial class ProgrammableOperatorView : ItemViewBase {
37    public ProgrammableOperator ProgrammableOperator {
38      get { return (ProgrammableOperator)Item; }
39      set { base.Item = value; }
40    }
41
42    public ProgrammableOperatorView() {
43      InitializeComponent();
44    }
45    public ProgrammableOperatorView(ProgrammableOperator programmableOperator)
46      : this() {
47      ProgrammableOperator = programmableOperator;
48    }
49
50    protected override void RemoveItemEvents() {
51      operatorBaseVariableInfosView.Operator = null;
52      operatorBaseVariablesView.Operator = null;
53      ProgrammableOperator.CodeChanged -= new EventHandler(ProgrammableOperator_CodeChanged);
54      ProgrammableOperator.DescriptionChanged -= new EventHandler(ProgrammableOperator_DescriptionChanged);
55      base.RemoveItemEvents();
56    }
57
58    protected override void AddItemEvents() {
59      base.AddItemEvents();
60      operatorBaseVariableInfosView.Operator = ProgrammableOperator;
61      operatorBaseVariablesView.Operator = ProgrammableOperator;
62      ProgrammableOperator.CodeChanged += new EventHandler(ProgrammableOperator_CodeChanged);
63      ProgrammableOperator.DescriptionChanged += new EventHandler(ProgrammableOperator_DescriptionChanged);
64    }
65
66    protected override void UpdateControls() {
67      base.UpdateControls();
68      if (ProgrammableOperator == null) {
69        codeTextBox.Text = "";
70        codeTextBox.Enabled = false;
71        addVariableInfoButton.Enabled = false;
72        removeVariableInfoButton.Enabled = false;
73        descriptionTextBox.Text = "";
74        descriptionTextBox.Enabled = false;
75      } else {
76        codeTextBox.Text = ProgrammableOperator.Code;
77        codeTextBox.Enabled = true;
78        addVariableInfoButton.Enabled = true;
79        removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
80        descriptionTextBox.Text = ProgrammableOperator.Description;
81        descriptionTextBox.Enabled = true;
82      }
83    }
84
85    private void operatorBaseVariableInfosView_SelectedVariableInfosChanged(object sender, EventArgs e) {
86      removeVariableInfoButton.Enabled = operatorBaseVariableInfosView.SelectedVariableInfos.Count > 0;
87    }
88
89    #region Validated Events
90    private void codeTextBox_Validated(object sender, EventArgs e) {
91      ProgrammableOperator.Code = codeTextBox.Text;
92    }
93    private void descriptionTextBox_Validated(object sender, EventArgs e) {
94      ProgrammableOperator.SetDescription(descriptionTextBox.Text);
95    }
96    #endregion
97
98    #region Click Events
99    private void compileButton_Click(object sender, EventArgs e) {
100      try {
101        ProgrammableOperator.Compile();
102        MessageBox.Show("Compilation successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
103      }
104      catch (Exception ex) {
105        HeuristicLab.Core.Views.Auxiliary.ShowErrorMessageBox(ex);
106      }
107    }
108    private void addVariableInfoButton_Click(object sender, EventArgs e) {
109      AddVariableInfoDialog dialog = new AddVariableInfoDialog();
110      if (dialog.ShowDialog(this) == DialogResult.OK) {
111        if (ProgrammableOperator.GetVariableInfo(dialog.VariableInfo.FormalName) != null)
112          HeuristicLab.Core.Views.Auxiliary.ShowErrorMessageBox("A variable info with the same formal name already exists.");
113        else
114          ProgrammableOperator.AddVariableInfo(dialog.VariableInfo);
115      }
116      dialog.Dispose();
117    }
118    private void removeVariableInfoButton_Click(object sender, EventArgs e) {
119      IVariableInfo[] selected = new IVariableInfo[operatorBaseVariableInfosView.SelectedVariableInfos.Count];
120      operatorBaseVariableInfosView.SelectedVariableInfos.CopyTo(selected, 0);
121      for (int i = 0; i < selected.Length; i++)
122        ProgrammableOperator.RemoveVariableInfo(selected[i].FormalName);
123    }
124    #endregion
125
126    #region ProgrammableOperator Events
127    private void ProgrammableOperator_CodeChanged(object sender, EventArgs e) {
128      codeTextBox.Text = ProgrammableOperator.Code;
129    }
130    private void ProgrammableOperator_DescriptionChanged(object sender, EventArgs e) {
131      descriptionTextBox.Text = ProgrammableOperator.Description;
132    }
133    #endregion
134  }
135}
Note: See TracBrowser for help on using the repository browser.