-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathConfigureCommandsForm.cs
More file actions
368 lines (308 loc) · 12.1 KB
/
ConfigureCommandsForm.cs
File metadata and controls
368 lines (308 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using CADPythonShell.App;
using System.IO;
namespace CADPythonShell
{
public partial class ConfigureCommandsForm : Form
{
private List<App.Command> _commands;
private List<string> _searchPaths;
private List<KeyValuePair<string, string>> _variables;
public ConfigureCommandsForm()
{
InitializeComponent();
}
/// <summary>
/// Close the Dialog without saving changes.
/// </summary>
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
/// Read in the commands from the XML file and display them in the
/// list.
/// </summary>
private void ConfigureCommandsForm_Load(object sender, EventArgs e)
{
_commands = CADPythonShellApplication.GetCommands(
CADPythonShellApplication.GetSettings()).ToList();
lstCommands.DataSource = _commands;
_searchPaths = CADPythonShellApplication.GetConfig().GetSearchPaths().ToList();
lstSearchPaths.DataSource = _searchPaths;
_variables = CADPythonShellApplication.GetConfig().GetVariables().AsEnumerable().ToList();
lstVariables.DataSource = _variables;
lstVariables.DisplayMember = "Key";
string initScriptPath = CADPythonShellApplication.GetInitScriptPath();
txtInitScript.Text = initScriptPath;
string startupScriptPath = CADPythonShellApplication.GetStartupScriptPath();
txtStartupScript.Text = startupScriptPath;
}
/// <summary>
/// Display information about the selected command in the textboxes (Name, Path).
/// </summary>
private void lstCommands_SelectedIndexChanged(object sender, EventArgs e)
{
var command = (App.Command)lstCommands.SelectedItem;
txtCommandName.Text = command.Name;
txtCommandPath.Text = command.Source;
txtCommandGroup.Text = command.Group;
}
/// <summary>
/// Update changes in list.
/// </summary>
private void txtCommandName_TextChanged(object sender, EventArgs e)
{
var command = (App.Command)lstCommands.SelectedItem;
command.Name = txtCommandName.Text;
RefreshBindingContext(lstCommands, _commands);
}
private void txtCommandGroup_TextChanged(object sender, EventArgs e)
{
var command = (App.Command)lstCommands.SelectedItem;
command.Group = txtCommandGroup.Text;
RefreshBindingContext(lstCommands, _commands);
}
/// <summary>
/// Update changes in list.
/// </summary>
private void txtCommandPath_TextChanged(object sender, EventArgs e)
{
var command = (App.Command)lstCommands.SelectedItem;
command.Source = txtCommandPath.Text;
RefreshBindingContext(lstCommands, _commands);
}
/// <summary>
/// show a FileOpen Dialog.
/// </summary>
private void btnCommandBrowse_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Multiselect = false;
dialog.FileName = txtCommandPath.Text;
dialog.ShowDialog(this);
txtCommandPath.Text = dialog.FileName;
}
/// <summary>
/// Add a new command.
/// </summary>
private void btnCommandAdd_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Multiselect = false;
// chances are, the user wants to add a script from the same folder
// as the other scripts
if (_commands.Count > 0)
{
dialog.InitialDirectory = Path.GetDirectoryName(_commands.First().Source);
}
if (dialog.ShowDialog(this) == DialogResult.OK)
{
var command = new App.Command();
command.Name = "";
command.Group = "";
command.Source = dialog.FileName;
_commands.Add(command);
RefreshBindingContext(lstCommands, _commands);
lstCommands.SelectedIndex = _commands.Count - 1;
txtCommandPath.Text = command.Source;
txtCommandName.Text = command.Name;
txtCommandName.Focus();
}
}
/// <summary>
/// Add a new search path.
/// </summary>
private void btnSearchPathAdd_Click(object sender, EventArgs e)
{
var dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
_searchPaths.Add(dialog.SelectedPath);
RefreshBindingContext(lstSearchPaths, _searchPaths);
lstSearchPaths.SelectedIndex = _searchPaths.Count - 1;
txtSearchPath.Text = dialog.SelectedPath;
}
}
private void RefreshBindingContext(ListBox listBox, object dataSource)
{
((CurrencyManager)listBox.BindingContext[dataSource]).Refresh();
}
/// <summary>
/// Remove the selected item.
/// </summary>
private void btnCommandRemove_Click(object sender, EventArgs e)
{
if (lstCommands.SelectedIndex >= 0)
{
_commands.RemoveAt(lstCommands.SelectedIndex);
RefreshBindingContext(lstCommands, _commands);
}
}
/// <summary>
/// Move the selected item up one.
/// </summary>
private void btnCommandMoveUp_Click(object sender, EventArgs e)
{
int oldPosition = lstCommands.SelectedIndex;
int newPosition = Math.Max(0, oldPosition - 1);
SwapPositions(_commands, oldPosition, newPosition);
RefreshBindingContext(lstCommands, _commands);
lstCommands.SelectedIndex = newPosition;
}
private void SwapPositions<T>(List<T> container, int oldPosition, int newPosition)
{
var temp = container[newPosition];
container[newPosition] = container[oldPosition];
container[oldPosition] = temp;
}
private void btnCommandMoveDown_Click(object sender, EventArgs e)
{
int oldPosition = lstCommands.SelectedIndex;
int newPosition = Math.Min(_commands.Count - 1, oldPosition + 1);
SwapPositions(_commands, oldPosition, newPosition);
RefreshBindingContext(lstCommands, _commands);
lstCommands.SelectedIndex = newPosition;
}
/// <summary>
/// Save changes to CADPythonShell.xml and close Dialog.
/// </summary>
private void btnCommandSave_Click(object sender, EventArgs e)
{
CADPythonShellApplication.WriteSettings(_commands, _searchPaths, _variables, txtInitScript.Text, txtStartupScript.Text);
Close();
}
private void btnSearchPathRemove_Click(object sender, EventArgs e)
{
if (lstSearchPaths.SelectedIndex >= 0)
{
_searchPaths.RemoveAt(lstSearchPaths.SelectedIndex);
RefreshBindingContext(lstSearchPaths, _searchPaths);
}
}
private void btnSearchPathBrowse_Click(object sender, EventArgs e)
{
if (lstSearchPaths.SelectedIndex < 0)
{
return;
}
var dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
_searchPaths[lstSearchPaths.SelectedIndex] = dialog.SelectedPath;
RefreshBindingContext(lstSearchPaths, _searchPaths);
txtSearchPath.Text = dialog.SelectedPath;
}
}
/// <summary>
/// Show the currently selected search path in the textbox for editing.
/// </summary>
private void lstSearchPaths_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstSearchPaths.SelectedIndex < 0)
{
return;
}
txtSearchPath.Text = _searchPaths[lstSearchPaths.SelectedIndex];
}
/// <summary>
/// Update the search path to reflect changes.
/// </summary>
private void txtSearchPath_TextChanged(object sender, EventArgs e)
{
if (lstSearchPaths.SelectedIndex < 0)
{
return;
}
_searchPaths[lstSearchPaths.SelectedIndex] = txtSearchPath.Text;
RefreshBindingContext(lstSearchPaths, _searchPaths);
}
/// <summary>
/// Display selected variable in the textboxes for editing.
/// </summary>
private void lstVariables_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstVariables.SelectedIndex < 0)
{
return;
}
var variable = _variables[lstVariables.SelectedIndex];
txtVariableName.Text = variable.Key;
txtVariableValue.Text = variable.Value;
}
/// <summary>
/// Update variable name to reflect changes in textbox.
/// </summary>
private void txtVariableName_TextChanged(object sender, EventArgs e)
{
if (lstVariables.SelectedIndex < 0)
{
return;
}
var variable = _variables[lstVariables.SelectedIndex];
_variables[lstVariables.SelectedIndex] = new KeyValuePair<string, string>(txtVariableName.Text, variable.Value);
RefreshBindingContext(lstVariables, _variables);
}
/// <summary>
/// Update variable value to reflect changes in textbox.
/// </summary>
private void txtVariableValue_TextChanged(object sender, EventArgs e)
{
if (lstVariables.SelectedIndex < 0)
{
return;
}
var variable = _variables[lstVariables.SelectedIndex];
_variables[lstVariables.SelectedIndex] = new KeyValuePair<string, string>(variable.Key, txtVariableValue.Text);
RefreshBindingContext(lstVariables, _variables);
}
/// <summary>
/// Add a new variable to the list and select it for editing.
/// </summary>
private void btnVariableAdd_Click(object sender, EventArgs e)
{
_variables.Add(new KeyValuePair<string, string>("<name>", "<value>"));
RefreshBindingContext(lstVariables, _variables);
lstVariables.SelectedIndex = _variables.Count - 1;
}
/// <summary>
/// Remove variable from the list.
/// </summary>
private void btnVariableRemove_Click(object sender, EventArgs e)
{
if (lstVariables.SelectedIndex < 0)
{
return;
}
_variables.RemoveAt(lstVariables.SelectedIndex);
RefreshBindingContext(lstVariables, _variables);
}
private void btnInitScriptBrowse_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Multiselect = false;
dialog.FileName = txtCommandPath.Text;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
txtInitScript.Text = dialog.FileName;
}
}
private void btnStartupScriptBrowse_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog();
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
dialog.Multiselect = false;
dialog.FileName = txtCommandPath.Text;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
txtStartupScript.Text = dialog.FileName;
}
}
}
}