Repository navigation
Expand file tree
/
Copy pathTaskEditorForm.cs
More file actions
548 lines (479 loc) · 18.6 KB
/
Copy pathTaskEditorForm.cs
File metadata and controls
548 lines (479 loc) · 18.6 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
#pragma warning disable CS8618 // WinForms controls are assigned during InitializeComponent.
namespace WindowsMarginManager
{
public partial class TaskEditorForm : Form
{
public ScheduledTask? Task { get; private set; }
private readonly ProfileManager profileManager;
private readonly ScheduledTask? originalTask;
private TextBox nameTextBox, descriptionTextBox;
private ComboBox triggerTypeComboBox, actionTypeComboBox, profileComboBox;
private DateTimePicker scheduledTimePicker, dailyTimePicker, weeklyTimePicker;
private NumericUpDown intervalUpDown;
private CheckedListBox weeklyDaysCheckedListBox;
private TextBox triggerApplicationsTextBox;
private Panel triggerPanel, actionPanel;
private CheckBox enabledCheckBox;
private Button okButton, cancelButton;
public TaskEditorForm(ScheduledTask? task, ProfileManager profileManager)
{
this.originalTask = task;
this.profileManager = profileManager;
InitializeComponent();
LoadData();
}
private void InitializeComponent()
{
this.Text = originalTask == null ? "New Scheduled Task" : "Edit Scheduled Task";
this.Size = new Size(500, 600);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
CreateBasicInfoPanel();
CreateTriggerPanel();
CreateActionPanel();
CreateButtonPanel();
}
private void CreateBasicInfoPanel()
{
var basicGroupBox = new GroupBox
{
Text = "Basic Information",
Location = new Point(12, 12),
Size = new Size(460, 120)
};
var nameLabel = new Label
{
Text = "Task Name:",
Location = new Point(12, 25),
Size = new Size(80, 23)
};
nameTextBox = new TextBox
{
Location = new Point(98, 22),
Size = new Size(350, 23)
};
var descLabel = new Label
{
Text = "Description:",
Location = new Point(12, 55),
Size = new Size(80, 23)
};
descriptionTextBox = new TextBox
{
Location = new Point(98, 52),
Size = new Size(350, 40),
Multiline = true
};
enabledCheckBox = new CheckBox
{
Text = "Enabled",
Location = new Point(98, 98),
Size = new Size(100, 23),
Checked = true
};
basicGroupBox.Controls.AddRange(new Control[]
{
nameLabel, nameTextBox, descLabel, descriptionTextBox, enabledCheckBox
});
this.Controls.Add(basicGroupBox);
}
private void CreateTriggerPanel()
{
var triggerGroupBox = new GroupBox
{
Text = "Trigger",
Location = new Point(12, 145),
Size = new Size(460, 200)
};
var triggerTypeLabel = new Label
{
Text = "Trigger Type:",
Location = new Point(12, 25),
Size = new Size(80, 23)
};
triggerTypeComboBox = new ComboBox
{
Location = new Point(98, 22),
Size = new Size(150, 23),
DropDownStyle = ComboBoxStyle.DropDownList
};
triggerTypeComboBox.Items.AddRange(Enum.GetNames(typeof(TriggerType)));
triggerTypeComboBox.SelectedIndexChanged += TriggerTypeComboBox_SelectedIndexChanged;
triggerPanel = new Panel
{
Location = new Point(12, 55),
Size = new Size(436, 135),
BorderStyle = BorderStyle.FixedSingle
};
CreateTriggerControls();
triggerGroupBox.Controls.AddRange(new Control[]
{
triggerTypeLabel, triggerTypeComboBox, triggerPanel
});
this.Controls.Add(triggerGroupBox);
}
private void CreateTriggerControls()
{
var timeLabel = new Label
{
Text = "Time:",
Location = new Point(12, 15),
Size = new Size(50, 23),
Visible = false
};
scheduledTimePicker = new DateTimePicker
{
Location = new Point(68, 12),
Size = new Size(200, 23),
Format = DateTimePickerFormat.Custom,
CustomFormat = "yyyy-MM-dd HH:mm",
Visible = false
};
var dailyLabel = new Label
{
Text = "Daily at:",
Location = new Point(12, 15),
Size = new Size(60, 23),
Visible = false
};
dailyTimePicker = new DateTimePicker
{
Location = new Point(78, 12),
Size = new Size(100, 23),
Format = DateTimePickerFormat.Time,
ShowUpDown = true,
Visible = false
};
var weeklyLabel = new Label
{
Text = "Weekly:",
Location = new Point(12, 15),
Size = new Size(60, 23),
Visible = false
};
weeklyDaysCheckedListBox = new CheckedListBox
{
Location = new Point(12, 45),
Size = new Size(200, 80),
CheckOnClick = true,
Visible = false
};
weeklyDaysCheckedListBox.Items.AddRange(Enum.GetNames(typeof(DayOfWeek)));
weeklyTimePicker = new DateTimePicker
{
Location = new Point(220, 45),
Size = new Size(100, 23),
Format = DateTimePickerFormat.Time,
ShowUpDown = true,
Visible = false
};
var intervalLabel = new Label
{
Text = "Every:",
Location = new Point(12, 15),
Size = new Size(50, 23),
Visible = false
};
intervalUpDown = new NumericUpDown
{
Location = new Point(68, 12),
Size = new Size(80, 23),
Minimum = 1,
Maximum = 1440,
Value = 60,
Visible = false
};
var minutesLabel = new Label
{
Text = "minutes",
Location = new Point(154, 15),
Size = new Size(60, 23),
Visible = false
};
var appLabel = new Label
{
Text = "Applications (comma-separated):",
Location = new Point(12, 15),
Size = new Size(200, 23),
Visible = false
};
triggerApplicationsTextBox = new TextBox
{
Location = new Point(12, 45),
Size = new Size(400, 60),
Multiline = true,
ScrollBars = ScrollBars.Vertical,
Visible = false
};
triggerPanel.Controls.AddRange(new Control[]
{
timeLabel, scheduledTimePicker,
dailyLabel, dailyTimePicker,
weeklyLabel, weeklyDaysCheckedListBox, weeklyTimePicker,
intervalLabel, intervalUpDown, minutesLabel,
appLabel, triggerApplicationsTextBox
});
}
private void CreateActionPanel()
{
var actionGroupBox = new GroupBox
{
Text = "Action",
Location = new Point(12, 360),
Size = new Size(460, 120)
};
var actionTypeLabel = new Label
{
Text = "Action Type:",
Location = new Point(12, 25),
Size = new Size(80, 23)
};
actionTypeComboBox = new ComboBox
{
Location = new Point(98, 22),
Size = new Size(150, 23),
DropDownStyle = ComboBoxStyle.DropDownList
};
actionTypeComboBox.Items.AddRange(Enum.GetNames(typeof(ScheduledActionType)));
actionTypeComboBox.SelectedIndexChanged += ActionTypeComboBox_SelectedIndexChanged;
var profileLabel = new Label
{
Text = "Profile:",
Location = new Point(12, 60),
Size = new Size(80, 23),
Visible = false
};
profileComboBox = new ComboBox
{
Location = new Point(98, 57),
Size = new Size(200, 23),
DropDownStyle = ComboBoxStyle.DropDownList,
Visible = false
};
actionPanel = new Panel
{
Location = new Point(12, 55),
Size = new Size(436, 55)
};
actionPanel.Controls.AddRange(new Control[] { profileLabel, profileComboBox });
actionGroupBox.Controls.AddRange(new Control[]
{
actionTypeLabel, actionTypeComboBox, actionPanel
});
this.Controls.Add(actionGroupBox);
}
private void CreateButtonPanel()
{
okButton = new Button
{
Text = "OK",
Location = new Point(316, 500),
Size = new Size(75, 26),
DialogResult = DialogResult.OK
};
cancelButton = new Button
{
Text = "Cancel",
Location = new Point(397, 500),
Size = new Size(75, 26),
DialogResult = DialogResult.Cancel
};
okButton.Click += OkButton_Click;
this.Controls.AddRange(new Control[] { okButton, cancelButton });
this.AcceptButton = okButton;
this.CancelButton = cancelButton;
}
private void LoadData()
{
var profiles = profileManager.GetProfiles();
profileComboBox.Items.Clear();
foreach (var profile in profiles)
{
profileComboBox.Items.Add(profile.Name);
}
if (originalTask != null)
{
nameTextBox.Text = originalTask.Name;
descriptionTextBox.Text = originalTask.Description;
enabledCheckBox.Checked = originalTask.Enabled;
triggerTypeComboBox.SelectedItem = originalTask.TriggerType.ToString();
actionTypeComboBox.SelectedItem = originalTask.ActionType.ToString();
if (!string.IsNullOrEmpty(originalTask.ProfileName))
{
profileComboBox.SelectedItem = originalTask.ProfileName;
}
LoadTriggerData();
}
else
{
triggerTypeComboBox.SelectedIndex = 0;
actionTypeComboBox.SelectedIndex = 0;
}
}
private void LoadTriggerData()
{
if (originalTask == null) return;
switch (originalTask.TriggerType)
{
case TriggerType.Time:
if (originalTask.ScheduledTime.HasValue)
scheduledTimePicker.Value = originalTask.ScheduledTime.Value;
break;
case TriggerType.Daily:
if (originalTask.DailyTime.HasValue)
dailyTimePicker.Value = DateTime.Today.Add(originalTask.DailyTime.Value);
break;
case TriggerType.Weekly:
if (originalTask.WeeklyDays != null)
{
for (int i = 0; i < weeklyDaysCheckedListBox.Items.Count; i++)
{
var dayName = weeklyDaysCheckedListBox.Items[i].ToString();
if (Enum.TryParse<DayOfWeek>(dayName, out var day) && originalTask.WeeklyDays.Contains(day))
{
weeklyDaysCheckedListBox.SetItemChecked(i, true);
}
}
}
if (originalTask.WeeklyTime.HasValue)
weeklyTimePicker.Value = DateTime.Today.Add(originalTask.WeeklyTime.Value);
break;
case TriggerType.Interval:
if (originalTask.IntervalMinutes.HasValue)
intervalUpDown.Value = originalTask.IntervalMinutes.Value;
break;
case TriggerType.ApplicationLaunch:
if (originalTask.TriggerApplications != null)
triggerApplicationsTextBox.Text = string.Join(", ", originalTask.TriggerApplications);
break;
}
}
private void TriggerTypeComboBox_SelectedIndexChanged(object? sender, EventArgs e)
{
HideAllTriggerControls();
if (Enum.TryParse<TriggerType>(triggerTypeComboBox.SelectedItem?.ToString(), out var triggerType))
{
ShowTriggerControls(triggerType);
}
}
private void ActionTypeComboBox_SelectedIndexChanged(object? sender, EventArgs e)
{
HideAllActionControls();
if (Enum.TryParse<ScheduledActionType>(actionTypeComboBox.SelectedItem?.ToString(), out var actionType))
{
ShowActionControls(actionType);
}
}
private void HideAllTriggerControls()
{
foreach (Control control in triggerPanel.Controls)
{
control.Visible = false;
}
}
private void ShowTriggerControls(TriggerType triggerType)
{
switch (triggerType)
{
case TriggerType.Time:
triggerPanel.Controls[0].Visible = true; // timeLabel
triggerPanel.Controls[1].Visible = true; // scheduledTimePicker
break;
case TriggerType.Daily:
triggerPanel.Controls[2].Visible = true; // dailyLabel
triggerPanel.Controls[3].Visible = true; // dailyTimePicker
break;
case TriggerType.Weekly:
triggerPanel.Controls[4].Visible = true; // weeklyLabel
triggerPanel.Controls[5].Visible = true; // weeklyDaysCheckedListBox
triggerPanel.Controls[6].Visible = true; // weeklyTimePicker
break;
case TriggerType.Interval:
triggerPanel.Controls[7].Visible = true; // intervalLabel
triggerPanel.Controls[8].Visible = true; // intervalUpDown
triggerPanel.Controls[9].Visible = true; // minutesLabel
break;
case TriggerType.ApplicationLaunch:
triggerPanel.Controls[10].Visible = true; // appLabel
triggerPanel.Controls[11].Visible = true; // triggerApplicationsTextBox
break;
}
}
private void HideAllActionControls()
{
foreach (Control control in actionPanel.Controls)
{
control.Visible = false;
}
}
private void ShowActionControls(ScheduledActionType actionType)
{
if (actionType == ScheduledActionType.ApplyProfile)
{
actionPanel.Controls[0].Visible = true; // profileLabel
actionPanel.Controls[1].Visible = true; // profileComboBox
}
}
private void OkButton_Click(object? sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(nameTextBox.Text))
{
MessageBox.Show("Please enter a task name.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!Enum.TryParse<TriggerType>(triggerTypeComboBox.SelectedItem?.ToString(), out var triggerType))
{
MessageBox.Show("Please select a trigger type.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!Enum.TryParse<ScheduledActionType>(actionTypeComboBox.SelectedItem?.ToString(), out var actionType))
{
MessageBox.Show("Please select an action type.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
Task = new ScheduledTask
{
Id = originalTask?.Id,
Name = nameTextBox.Text.Trim(),
Description = descriptionTextBox.Text.Trim(),
Enabled = enabledCheckBox.Checked,
TriggerType = triggerType,
ActionType = actionType
};
switch (triggerType)
{
case TriggerType.Time:
Task.ScheduledTime = scheduledTimePicker.Value;
break;
case TriggerType.Daily:
Task.DailyTime = dailyTimePicker.Value.TimeOfDay;
break;
case TriggerType.Weekly:
Task.WeeklyDays = weeklyDaysCheckedListBox.CheckedItems.Cast<string>()
.Select(s => Enum.Parse<DayOfWeek>(s)).ToList();
Task.WeeklyTime = weeklyTimePicker.Value.TimeOfDay;
break;
case TriggerType.Interval:
Task.IntervalMinutes = (int)intervalUpDown.Value;
break;
case TriggerType.ApplicationLaunch:
Task.TriggerApplications = triggerApplicationsTextBox.Text
.Split(',')
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
break;
}
if (actionType == ScheduledActionType.ApplyProfile)
{
Task.ProfileName = profileComboBox.SelectedItem?.ToString();
}
}
}
}