Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5729 was 5704, checked in by abeham, 14 years ago

#567

  • Added item template
  • Adapted problem template
  • Enabled tab key to continue editing the next column in the grid
File size: 10.4 KB
RevLine 
[4181]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
[4188]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
[4181]41    public string GetParameterProperties(string accessModifier) {
[4187]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();
[4181]60    }
61
62    public string GetProperties(string accessModifier) {
[4187]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();
[4181]77    }
78
79    public string GetInitializers() {
[4187]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);
[4181]95      }
[4187]96      return builder.ToString();
[4181]97    }
98
[4187]99    #region Button Event Handlers
[4181]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.");
[4187]151      item.SubItems.Add("null");
[4181]152      parametersListView.Items.Add(item);
153    }
[4187]154    #endregion
155
156    private void parametersListView_MouseDoubleClick(object sender, MouseEventArgs e) {
157      var hit = parametersListView.HitTest(e.Location);
[5704]158      if (hit.Item == null || hit.SubItem == null) return;
[4187]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
[5704]200    protected override bool ProcessDialogKey(Keys keyData) {
201      if (keyData == Keys.Tab) {
202        if (customInputTextBox.Focused) {
203          if (customInputTextBox.Right + 5 < parametersListView.Right) {
204            parametersListView_MouseDoubleClick(parametersListView, new MouseEventArgs(MouseButtons.Left, 2, customInputTextBox.Right + 5, customInputTextBox.Top + 3, 0));
205            return true;
206          } else return base.ProcessDialogKey(keyData);
207        } else if (parameterTypeComboBox.Focused) {
208          parametersListView_MouseDoubleClick(parametersListView, new MouseEventArgs(MouseButtons.Left, 2, parameterTypeComboBox.Right + 5, parameterTypeComboBox.Top + 3, 0));
209          return true;
210        } else return base.ProcessDialogKey(keyData);
211      } else return base.ProcessDialogKey(keyData);
212    }
213
[4187]214    private void customInputTextBox_Leave(object sender, EventArgs e) {
215      TextBox t = (TextBox)sender;
216      System.Windows.Forms.ListViewItem.ListViewSubItem subItem = t.Tag as System.Windows.Forms.ListViewItem.ListViewSubItem;
217      subItem.Text = t.Text;
218      t.Visible = false;
219    }
220
221    private void customInputTextBox_KeyUp(object sender, KeyEventArgs e) {
222      if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
223        customInputTextBox_Leave(sender, EventArgs.Empty);
[5704]224      e.Handled = (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter);
[4187]225    }
226
227    private void parameterTypeComboBox_Leave(object sender, EventArgs e) {
228      ComboBox c = (ComboBox)sender;
229      System.Windows.Forms.ListViewItem.ListViewSubItem subItem = c.Tag as System.Windows.Forms.ListViewItem.ListViewSubItem;
230      subItem.Text = c.Text;
231      c.Visible = false;
232    }
233
234    private void parameterTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) {
235      if (parameterTypeComboBox.Visible)
236        parameterTypeComboBox_Leave(sender, e);
237    }
238
239    private void parameterTypeComboBox_KeyUp(object sender, KeyEventArgs e) {
240      if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
241        parameterTypeComboBox_Leave(sender, EventArgs.Empty);
242    }
[4181]243  }
244}
Note: See TracBrowser for help on using the repository browser.