source: trunk/eraser/Eraser/TaskPropertiesForm.cs @ 1964

Revision 1802, 11.6 KB checked in by lowjoel, 4 years ago (diff)

Merged the CodeReview? Branch back to trunk. (Finally!) Closes #275: Code Review.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Text;
28using System.Windows.Forms;
29
30using System.Globalization;
31using Eraser.Manager;
32using Eraser.Util;
33
34namespace Eraser
35{
36    public partial class TaskPropertiesForm : Form
37    {
38        public TaskPropertiesForm()
39        {
40            InitializeComponent();
41            Theming.ApplyTheme(this);
42            scheduleTime.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
43
44            //Set a default task type
45            typeManual.Checked = true;
46            scheduleDaily.Checked = true;
47        }
48
49        /// <summary>
50        /// Sets or retrieves the task object to be edited or being edited.
51        /// </summary>
52        public Task Task
53        {
54            get { UpdateTaskFromUI(); return task; }
55            set { task = value; UpdateUIFromTask(); }
56        }
57
58        /// <summary>
59        /// Updates the local task object from the UI elements.
60        /// </summary>
61        private void UpdateTaskFromUI()
62        {
63            //Set the name of the task
64            task.Name = name.Text;
65
66            //And the schedule, if selected.
67            if (typeManual.Checked)
68            {
69                task.Schedule = Schedule.RunManually;
70            }
71            else if (typeImmediate.Checked)
72            {
73                task.Schedule = Schedule.RunNow;
74            }
75            else if (typeRestart.Checked)
76            {
77                task.Schedule = Schedule.RunOnRestart;
78            }
79            else if (typeRecurring.Checked)
80            {
81                RecurringSchedule schedule = new RecurringSchedule();
82                task.Schedule = schedule;
83                schedule.ExecutionTime = new DateTime(1, 1, 1, scheduleTime.Value.Hour,
84                    scheduleTime.Value.Minute, scheduleTime.Value.Second);
85
86                if (scheduleDaily.Checked)
87                {
88                    if (scheduleDailyByDay.Checked)
89                    {
90                        schedule.ScheduleType = RecurringScheduleUnit.Daily;
91                        schedule.Frequency = (int)scheduleDailyByDayFreq.Value;
92                    }
93                    else
94                    {
95                        schedule.ScheduleType = RecurringScheduleUnit.Weekdays;
96                    }
97                }
98                else if (scheduleWeekly.Checked)
99                {
100                    schedule.ScheduleType = RecurringScheduleUnit.Weekly;
101                    schedule.Frequency = (int)scheduleWeeklyFreq.Value;
102                    DaysOfWeek weeklySchedule = 0;
103                    if (scheduleWeeklyMonday.Checked)
104                        weeklySchedule |= DaysOfWeek.Monday;
105                    if (scheduleWeeklyTuesday.Checked)
106                        weeklySchedule |= DaysOfWeek.Tuesday;
107                    if (scheduleWeeklyWednesday.Checked)
108                        weeklySchedule |= DaysOfWeek.Wednesday;
109                    if (scheduleWeeklyThursday.Checked)
110                        weeklySchedule |= DaysOfWeek.Thursday;
111                    if (scheduleWeeklyFriday.Checked)
112                        weeklySchedule |= DaysOfWeek.Friday;
113                    if (scheduleWeeklySaturday.Checked)
114                        weeklySchedule |= DaysOfWeek.Saturday;
115                    if (scheduleWeeklySunday.Checked)
116                        weeklySchedule |= DaysOfWeek.Sunday;
117                    schedule.WeeklySchedule = weeklySchedule;
118                }
119                else if (scheduleMonthly.Checked)
120                {
121                    schedule.ScheduleType = RecurringScheduleUnit.Monthly;
122                    schedule.Frequency = (int)scheduleMonthlyFreq.Value;
123                    schedule.MonthlySchedule = (int)scheduleMonthlyDayNumber.Value;
124                }
125                else
126                    throw new ArgumentException("No such scheduling method.");
127            }
128        }
129
130        /// <summary>
131        /// Updates the UI elements to reflect the data in the Task object.
132        /// </summary>
133        private void UpdateUIFromTask()
134        {
135            //Set the name of the task
136            name.Text = task.Name;
137
138            //The data
139            foreach (ErasureTarget target in task.Targets)
140            {
141                ListViewItem item = data.Items.Add(target.UIText);
142                item.SubItems.Add(target.MethodDefined ? target.Method.Name : S._("(default)"));
143                item.Tag = target;
144            }
145
146            //And the schedule, if selected.
147            if (task.Schedule == Schedule.RunManually)
148            {
149                typeManual.Checked = true;
150            }
151            else if (task.Schedule == Schedule.RunNow)
152            {
153                typeImmediate.Checked = true;
154            }
155            else if (task.Schedule == Schedule.RunOnRestart)
156            {
157                typeRestart.Checked = true;
158            }
159            else
160            {
161                typeRecurring.Checked = true;
162                RecurringSchedule schedule = (RecurringSchedule)task.Schedule;
163                scheduleTime.Value = scheduleTime.MinDate.Add(schedule.ExecutionTime.TimeOfDay);
164
165                switch (schedule.ScheduleType)
166                {
167                    case RecurringScheduleUnit.Daily:
168                        scheduleDailyByDay.Checked = true;
169                        scheduleDailyByDayFreq.Value = schedule.Frequency;
170                        break;
171                    case RecurringScheduleUnit.Weekdays:
172                        scheduleDailyByWeekday.Checked = true;
173                        break;
174                    case RecurringScheduleUnit.Weekly:
175                        scheduleWeeklyFreq.Value = schedule.Frequency;
176                        scheduleWeekly.Checked = true;
177                        scheduleWeeklyMonday.Checked =
178                            (schedule.WeeklySchedule & DaysOfWeek.Monday) != 0;
179                        scheduleWeeklyTuesday.Checked =
180                            (schedule.WeeklySchedule & DaysOfWeek.Tuesday) != 0;
181                        scheduleWeeklyWednesday.Checked =
182                            (schedule.WeeklySchedule & DaysOfWeek.Wednesday) != 0;
183                        scheduleWeeklyThursday.Checked =
184                            (schedule.WeeklySchedule & DaysOfWeek.Thursday) != 0;
185                        scheduleWeeklyFriday.Checked =
186                            (schedule.WeeklySchedule & DaysOfWeek.Friday) != 0;
187                        scheduleWeeklySaturday.Checked =
188                            (schedule.WeeklySchedule & DaysOfWeek.Saturday) != 0;
189                        scheduleWeeklySunday.Checked =
190                            (schedule.WeeklySchedule & DaysOfWeek.Sunday) != 0;
191                        break;
192                    case RecurringScheduleUnit.Monthly:
193                        scheduleMonthly.Checked = true;
194                        scheduleMonthlyFreq.Value = schedule.Frequency;
195                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
196                        break;
197                    default:
198                        throw new ArgumentException("Unknown schedule type.");
199                }
200            }
201        }
202
203        /// <summary>
204        /// Triggered when the user clicks on the Add Data button.
205        /// </summary>
206        /// <param name="sender">The button.</param>
207        /// <param name="e">Event argument.</param>
208        private void dataAdd_Click(object sender, EventArgs e)
209        {
210            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
211            {
212                if (form.ShowDialog() == DialogResult.OK)
213                {
214                    ErasureTarget target = form.Target;
215                    ListViewItem item = data.Items.Add(target.UIText);
216                    item.SubItems.Add(target.MethodDefined ? target.Method.Name : S._("(default)"));
217                    item.Tag = target;
218
219                    task.Targets.Add(target);
220                    errorProvider.Clear();
221                }
222            }
223        }
224
225        /// <summary>
226        /// Generated when the user double-clicks an item in the list-view.
227        /// </summary>
228        /// <param name="sender">The list-view which generated this event.</param>
229        /// <param name="e">Event argument.</param>
230        private void data_ItemActivate(object sender, EventArgs e)
231        {
232            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
233            {
234                ListViewItem item = data.SelectedItems[0];
235                form.Target = task.Targets[item.Index];
236
237                if (form.ShowDialog() == DialogResult.OK)
238                {
239                    ErasureTarget target = form.Target;
240                    task.Targets.RemoveAt(item.Index);
241                    task.Targets.Insert(item.Index, target);
242
243                    item.Tag = target;
244                    item.Text = target.UIText;
245                    item.SubItems[1].Text = target.MethodDefined ? target.Method.Name : S._("(default)");
246                }
247            }
248        }
249
250        /// <summary>
251        /// Generated when the user right-clicks on the data selection list-view.
252        /// </summary>
253        /// <param name="sender">The menu being opened.</param>
254        /// <param name="e">Event argument.</param>
255        private void dataContextMenuStrip_Opening(object sender, CancelEventArgs e)
256        {
257            if (data.SelectedIndices.Count == 0)
258            {
259                e.Cancel = true;
260                return;
261            }
262        }
263
264        /// <summary>
265        /// Generated when the user selects the menu itm to remove the selected
266        /// data from the list of data to erase.
267        /// </summary>
268        /// <param name="sender">The object triggering the event.</param>
269        /// <param name="e">Event argument.</param>
270        private void deleteDataToolStripMenuItem_Click(object sender, EventArgs e)
271        {
272            if (data.SelectedIndices.Count == 0)
273                return;
274
275            foreach (ListViewItem obj in data.SelectedItems)
276            {
277                task.Targets.Remove((ErasureTarget)obj.Tag);
278                data.Items.Remove(obj);
279            }
280        }
281
282        /// <summary>
283        /// Generated when the task schedule type changes.
284        /// </summary>
285        /// <param name="sender">The object triggering the event.</param>
286        /// <param name="e">Event argument.</param>
287        private void taskType_CheckedChanged(object sender, EventArgs e)
288        {
289            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
290                scheduleDaily.Enabled = scheduleWeekly.Enabled =
291                scheduleMonthly.Enabled = typeRecurring.Checked;
292            nonRecurringPanel.Visible = !typeRecurring.Checked;
293           
294            scheduleSpan_CheckedChanged(sender, e);
295        }
296
297        /// <summary>
298        /// Generated when the scheduling frequency is changed.
299        /// </summary>
300        /// <param name="sender">The object triggering the event.</param>
301        /// <param name="e">Event argument.</param>
302        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
303        {
304            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
305                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
306                typeRecurring.Checked;
307            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
308                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
309                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
310                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
311                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
312                scheduleWeekly.Checked && typeRecurring.Checked;
313            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
314                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
315                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
316                typeRecurring.Checked;
317
318            scheduleDailySpan_CheckedChanged(sender, e);
319        }
320
321        /// <summary>
322        /// Generated when the daily frequency argument is changed.
323        /// </summary>
324        /// <param name="sender">The object triggering the event.</param>
325        /// <param name="e">Event argument.</param>
326        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
327        {
328            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
329                scheduleDaily.Checked && typeRecurring.Checked;
330        }
331
332        /// <summary>
333        /// Generated when the dialog is closed.
334        /// </summary>
335        /// <param name="sender">The object triggering the event.</param>
336        /// <param name="e">Event argument.</param>
337        private void ok_Click(object sender, EventArgs e)
338        {
339            if (data.Items.Count == 0)
340            {
341                errorProvider.SetIconPadding(data, -16);
342                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
343                errorProvider.SetError(data, S._("The task has no data to erase."));
344                container.SelectedIndex = 0;
345                return;
346            }
347            else if (typeRecurring.Checked && scheduleWeekly.Checked)
348            {
349                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
350                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
351                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
352                    !scheduleWeeklySunday.Checked)
353                {
354                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
355                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
356                        "on at least one day a week"));
357                    container.SelectedIndex = 1;
358                    return;
359                }
360            }
361
362            errorProvider.Clear();
363
364            //Close the dialog
365            DialogResult = DialogResult.OK;
366            Close();
367        }
368
369        /// <summary>
370        /// The task being edited.
371        /// </summary>
372        private Task task = new Task();
373    }
374}
Note: See TracBrowser for help on using the repository browser.