Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/Templates/HeuristicLab.VS2010Wizards/ParametersControl.cs @ 5483

Last change on this file since 5483 was 4188, checked in by abeham, 14 years ago

#567

  • Started adding problem wizard
File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
28using System.Text;
29using System.Windows.Forms;
30
31namespace HeuristicLab.VS2010Wizards {
32  public partial class ParametersControl : UserControl {
33    public ParametersControl() {
34      InitializeComponent();
35    }
36
37    public void AddParameter(string name, string type, string dataType, string description, string defaultValue) {
38      ListViewItem item = new ListViewItem(name);
39      item.SubItems.Add(type);
40      item.SubItems.Add(dataType);
41      item.SubItems.Add(description);
42      item.SubItems.Add(defaultValue);
43      parametersListView.Items.Add(item);
44    }
45
46    public string GetParameterProperties(string accessModifier) {
47      StringBuilder builder = new StringBuilder();
48      foreach (ListViewItem item in parametersListView.Items) {
49        string name = item.Text.Trim();
50        string type = item.SubItems[typeColumnHeader.DisplayIndex].Text.Trim();
51        string dataType = item.SubItems[dataTypeColumnHeader.DisplayIndex].Text.Trim();
52        builder.Append(accessModifier);
53        builder.Append(" " + type + "Parameter");
54        if (dataType != string.Empty)
55          builder.Append("<" + dataType + ">");
56        builder.Append(" " + name + "Parameter {" + Environment.NewLine);
57        builder.Append("\tget { return ");
58        builder.Append("(" + type + "Parameter");
59        if (dataType != string.Empty)
60          builder.Append("<" + dataType + ">");
61        builder.Append(")Parameters[\"" + name + "\"]; }" + Environment.NewLine);
62        builder.Append("}" + Environment.NewLine);
63      }
64      return builder.ToString();
65    }
66
67    public string GetProperties(string accessModifier) {
68      StringBuilder builder = new StringBuilder();
69      foreach (ListViewItem item in parametersListView.Items) {
70        string name = item.Text.Trim();
71        string type = item.SubItems[typeColumnHeader.DisplayIndex].Text.Trim();
72        string dataType = item.SubItems[dataTypeColumnHeader.DisplayIndex].Text.Trim();
73        if (type.ToLower().Equals("value")) {
74          builder.Append(accessModifier + " " + dataType);
75          builder.Append(" " + name + " {" + Environment.NewLine);
76          builder.Append("\tget { return " + name + "Parameter.Value; }" + Environment.NewLine);
77          builder.Append("\tset { " + name + "Parameter.Value = value; }" + Environment.NewLine);
78          builder.Append("}" + Environment.NewLine);
79        }
80      }
81      return builder.ToString();
82    }
83
84    public string GetInitializers() {
85      StringBuilder builder = new StringBuilder();
86      foreach (ListViewItem item in parametersListView.Items) {
87        string name = item.Text.Trim();
88        string type = item.SubItems[typeColumnHeader.DisplayIndex].Text.Trim();
89        string dataType = item.SubItems[dataTypeColumnHeader.DisplayIndex].Text.Trim();
90        string description = item.SubItems[descriptionColumnHeader.DisplayIndex].Text.Trim();
91        string initialValue = item.SubItems[defaultValueColumnHeader.DisplayIndex].Text.Trim();
92        builder.Append("\tParameters.Add(new " + type + "Parameter");
93        if (dataType != string.Empty)
94          builder.Append("<" + dataType + ">");
95        builder.Append("(\"" + name + "\", ");
96        builder.Append("\"" + description + "\"");
97        if (initialValue != string.Empty)
98          builder.Append(", " + initialValue);
99        builder.Append("));" + Environment.NewLine);
100      }
101      return builder.ToString();
102    }
103
104    #region Button Event Handlers
105    private void removeButton_Click(object sender, EventArgs e) {
106      if (parametersListView.SelectedIndices.Count > 0) {
107        int row = parametersListView.SelectedIndices[0];
108        parametersListView.Items.RemoveAt(row);
109        if (parametersListView.Items.Count > 0) {
110          int index = Math.Max(Math.Min(row, parametersListView.Items.Count - 1), 0);
111          parametersListView.Items[index].Selected = true;
112        }
113      }
114    }
115
116    private void upButton_Click(object sender, EventArgs e) {
117      if (parametersListView.SelectedIndices.Count > 0) {
118        ListViewItem selected = parametersListView.SelectedItems[0];
119        int row = parametersListView.SelectedIndices[0];
120        parametersListView.Items.Remove(selected);
121        parametersListView.Items.Insert(Math.Max(row - 1, 0), selected);
122      }
123    }
124
125    private void downButton_Click(object sender, EventArgs e) {
126      if (parametersListView.SelectedIndices.Count > 0) {
127        ListViewItem selected = parametersListView.SelectedItems[0];
128        int row = parametersListView.SelectedIndices[0];
129        parametersListView.Items.Remove(selected);
130        if (row == parametersListView.Items.Count)
131          parametersListView.Items.Add(selected);
132        else
133          parametersListView.Items.Insert(row + 1, selected);
134      }
135    }
136
137    private void addButton_Click(object sender, EventArgs e) {
138      string name = "FormalName";
139      bool uniqueNameFound;
140      int i = 1;
141      do {
142        uniqueNameFound = true;
143        foreach (ListViewItem li in parametersListView.Items) {
144          if (li.Text.Equals(name)) {
145            name = "FormalName" + i.ToString();
146            i++;
147            uniqueNameFound = false;
148          }
149        }
150      } while (!uniqueNameFound);
151      ListViewItem item = new ListViewItem();
152      item.Text = name;
153      item.SubItems.Add("Value");
154      item.SubItems.Add("IItem");
155      item.SubItems.Add("Add a description.");
156      item.SubItems.Add("null");
157      parametersListView.Items.Add(item);
158    }
159    #endregion
160
161    private void parametersListView_MouseDoubleClick(object sender, MouseEventArgs e) {
162      var hit = parametersListView.HitTest(e.Location);
163      if (hit.Item.SubItems[typeColumnHeader.DisplayIndex] == hit.SubItem) {
164        parameterTypeComboBox.Left = parametersListView.Left + hit.SubItem.Bounds.Left + 3;
165        parameterTypeComboBox.Top = parametersListView.Top + hit.SubItem.Bounds.Top;
166        parameterTypeComboBox.Width = hit.SubItem.Bounds.Width;
167        parameterTypeComboBox.Height = hit.SubItem.Bounds.Height;
168        parameterTypeComboBox.Text = hit.SubItem.Text;
169        parameterTypeComboBox.Tag = hit.SubItem;
170        parameterTypeComboBox.Visible = true;
171        parameterTypeComboBox.BringToFront();
172        Refresh();
173        parameterTypeComboBox.Focus();
174        parameterTypeComboBox.SelectAll();
175      } else {
176        customInputTextBox.Left = parametersListView.Left + hit.SubItem.Bounds.Left + 3;
177        customInputTextBox.Top = parametersListView.Top + hit.SubItem.Bounds.Top;
178        if (hit.SubItem != hit.Item.SubItems[0])
179          customInputTextBox.Width = hit.SubItem.Bounds.Width;
180        else customInputTextBox.Width = hit.Item.SubItems[1].Bounds.Left;
181        customInputTextBox.Height = hit.SubItem.Bounds.Height;
182        customInputTextBox.Text = hit.SubItem.Text;
183        customInputTextBox.Tag = hit.SubItem;
184        customInputTextBox.Visible = true;
185        customInputTextBox.BringToFront();
186        Refresh();
187        customInputTextBox.Focus();
188        customInputTextBox.SelectAll();
189      }
190    }
191
192    private void parametersListView_SelectedIndexChanged(object sender, EventArgs e) {
193      if (parametersListView.SelectedIndices.Count > 0) {
194        removeButton.Enabled = true;
195        upButton.Enabled = true;
196        downButton.Enabled = true;
197      } else {
198        removeButton.Enabled = false;
199        upButton.Enabled = false;
200        downButton.Enabled = false;
201      }
202    }
203
204    private void customInputTextBox_Leave(object sender, EventArgs e) {
205      TextBox t = (TextBox)sender;
206      System.Windows.Forms.ListViewItem.ListViewSubItem subItem = t.Tag as System.Windows.Forms.ListViewItem.ListViewSubItem;
207      subItem.Text = t.Text;
208      t.Visible = false;
209    }
210
211    private void customInputTextBox_KeyUp(object sender, KeyEventArgs e) {
212      if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
213        customInputTextBox_Leave(sender, EventArgs.Empty);
214    }
215
216    private void parameterTypeComboBox_Leave(object sender, EventArgs e) {
217      ComboBox c = (ComboBox)sender;
218      System.Windows.Forms.ListViewItem.ListViewSubItem subItem = c.Tag as System.Windows.Forms.ListViewItem.ListViewSubItem;
219      subItem.Text = c.Text;
220      c.Visible = false;
221    }
222
223    private void parameterTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
224      if (parameterTypeComboBox.Visible)
225        parameterTypeComboBox_Leave(sender, e);
226    }
227
228    private void parameterTypeComboBox_KeyUp(object sender, KeyEventArgs e) {
229      if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
230        parameterTypeComboBox_Leave(sender, EventArgs.Empty);
231    }
232  }
233}
Note: See TracBrowser for help on using the repository browser.