Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented first draft of MainForm support in HeuristicLab.Core/HeuristicLab.Core.Views and all other depending plugins (#770)

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