Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12776 was 5913, checked in by abeham, 14 years ago

#567

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