source: branches/eraser6/Eraser/TaskPropertiesForm.cs @ 437

Revision 437, 10.0 KB checked in by lowjoel, 6 years ago (diff)

Select the appropriate scheduling checkbox when a task is loaded into the task editor.

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 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            scheduleTime.CustomFormat = DateTimeFormatInfo.CurrentInfo.ShortTimePattern;
42
43            //Set a default task type
44            typeImmediate.Checked = true;
45            scheduleDaily.Checked = true;
46        }
47
48        /// <summary>
49        /// Sets or retrieves the task object to be edited or being edited.
50        /// </summary>
51        public Task Task
52        {
53            get { UpdateTaskFromUI(); return task; }
54            set { task = value; UpdateUIFromTask(); }
55        }
56
57        /// <summary>
58        /// Updates the local task object from the UI elements.
59        /// </summary>
60        private void UpdateTaskFromUI()
61        {
62            //Set the name of the task
63            task.Name = name.Text;
64
65            //And the schedule, if selected.
66            if (typeImmediate.Checked)
67            {
68                task.Schedule = Schedule.RunNow;
69            }
70            else if (typeRestart.Checked)
71            {
72                task.Schedule = Schedule.RunOnRestart;
73            }
74            else if (typeRecurring.Checked)
75            {
76                RecurringSchedule schedule = new RecurringSchedule();
77                task.Schedule = schedule;
78                schedule.ExecutionTime = new DateTime(1, 1, 1, scheduleTime.Value.Hour,
79                    scheduleTime.Value.Minute, scheduleTime.Value.Second);
80
81                if (scheduleDaily.Checked)
82                {
83                    if (scheduleDailyByDay.Checked)
84                    {
85                        schedule.Type = RecurringSchedule.ScheduleUnit.DAILY;
86                        schedule.Frequency = (int)scheduleDailyByDayFreq.Value;
87                    }
88                    else
89                    {
90                        schedule.Type = RecurringSchedule.ScheduleUnit.WEEKDAYS;
91                    }
92                }
93                else if (scheduleWeekly.Checked)
94                {
95                    schedule.Type = RecurringSchedule.ScheduleUnit.WEEKLY;
96                    schedule.Frequency = (int)scheduleWeeklyFreq.Value;
97                    RecurringSchedule.DaysOfWeek weeklySchedule = 0;
98                    if (scheduleWeeklyMonday.Checked)
99                        weeklySchedule |= RecurringSchedule.DaysOfWeek.MONDAY;
100                    if (scheduleWeeklyTuesday.Checked)
101                        weeklySchedule |= RecurringSchedule.DaysOfWeek.TUESDAY;
102                    if (scheduleWeeklyWednesday.Checked)
103                        weeklySchedule |= RecurringSchedule.DaysOfWeek.WEDNESDAY;
104                    if (scheduleWeeklyThursday.Checked)
105                        weeklySchedule |= RecurringSchedule.DaysOfWeek.THURSDAY;
106                    if (scheduleWeeklyFriday.Checked)
107                        weeklySchedule |= RecurringSchedule.DaysOfWeek.FRIDAY;
108                    if (scheduleWeeklySaturday.Checked)
109                        weeklySchedule |= RecurringSchedule.DaysOfWeek.SATURDAY;
110                    if (scheduleWeeklySunday.Checked)
111                        weeklySchedule |= RecurringSchedule.DaysOfWeek.SUNDAY;
112                    schedule.WeeklySchedule = weeklySchedule;
113                }
114                else if (scheduleMonthly.Checked)
115                {
116                    schedule.Type = RecurringSchedule.ScheduleUnit.MONTHLY;
117                    schedule.Frequency = (int)scheduleMonthlyFreq.Value;
118                    schedule.MonthlySchedule = (int)scheduleMonthlyDayNumber.Value;
119                }
120                else
121                    throw new NotImplementedException("No such scheduling method.");
122            }
123        }
124
125        /// <summary>
126        /// Updates the UI elements to reflect the data in the Task object.
127        /// </summary>
128        private void UpdateUIFromTask()
129        {
130            //Set the name of the task
131            name.Text = task.Name;
132
133            //The data
134            foreach (Task.ErasureTarget target in task.Targets)
135            {
136                ListViewItem item = data.Items.Add(target.UIText);
137                item.SubItems.Add(target.Method.Name);
138            }
139
140            //And the schedule, if selected.
141            if (task.Schedule == Schedule.RunNow)
142            {
143                typeImmediate.Checked = true;
144            }
145            else if (task.Schedule == Schedule.RunOnRestart)
146            {
147                typeRestart.Checked = true;
148            }
149            else
150            {
151                typeRecurring.Checked = true;
152                RecurringSchedule schedule = (RecurringSchedule)task.Schedule;
153                scheduleTime.Value = scheduleTime.MinDate.Add(schedule.ExecutionTime.TimeOfDay);
154
155                switch (schedule.Type)
156                {
157                    case RecurringSchedule.ScheduleUnit.DAILY:
158                        scheduleDailyByDay.Checked = true;
159                        scheduleDailyByDayFreq.Value = schedule.Frequency;
160                        break;
161                    case RecurringSchedule.ScheduleUnit.WEEKDAYS:
162                        scheduleDailyByWeekday.Checked = true;
163                        break;
164                    case RecurringSchedule.ScheduleUnit.WEEKLY:
165                        scheduleWeeklyFreq.Value = schedule.Frequency;
166                        scheduleWeekly.Checked = true;
167                        scheduleWeeklyMonday.Checked =
168                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.MONDAY) != 0;
169                        scheduleWeeklyTuesday.Checked =
170                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.TUESDAY) != 0;
171                        scheduleWeeklyWednesday.Checked =
172                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.WEDNESDAY) != 0;
173                        scheduleWeeklyThursday.Checked =
174                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.THURSDAY) != 0;
175                        scheduleWeeklyFriday.Checked =
176                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.FRIDAY) != 0;
177                        scheduleWeeklySaturday.Checked =
178                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SATURDAY) != 0;
179                        scheduleWeeklySunday.Checked =
180                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SUNDAY) != 0;
181                        break;
182                    case RecurringSchedule.ScheduleUnit.MONTHLY:
183                        scheduleMonthly.Checked = true;
184                        scheduleMonthlyFreq.Value = schedule.Frequency;
185                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
186                        break;
187                    default:
188                        throw new NotImplementedException("Unknown schedule type.");
189                }
190            }
191        }
192
193        /// <summary>
194        /// Triggered when the user clicks on the Add Data button.
195        /// </summary>
196        /// <param name="sender">The button.</param>
197        /// <param name="e">Event argument.</param>
198        private void dataAdd_Click(object sender, EventArgs e)
199        {
200            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
201            {
202                if (form.ShowDialog() == DialogResult.OK)
203                {
204                    Task.ErasureTarget target = form.Target;
205                    ListViewItem item = data.Items.Add(target.UIText);
206                   
207                    item.SubItems.Add(target.Method.Name);
208                    task.Targets.Add(target);
209                    errorProvider.Clear();
210                }
211            }
212        }
213
214        /// <summary>
215        /// Generated when the user double-clicks an item in the list-view.
216        /// </summary>
217        /// <param name="sender">The list-view which generated this event.</param>
218        /// <param name="e">Event argument.</param>
219        private void data_ItemActivate(object sender, EventArgs e)
220        {
221            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
222            {
223                ListViewItem item = data.SelectedItems[0];
224                form.Target = task.Targets[item.Index];
225
226                if (form.ShowDialog() == DialogResult.OK)
227                {
228                    Task.ErasureTarget target = form.Target;
229                    task.Targets[item.Index] = target;
230                    item.Text = target.UIText;
231                    item.SubItems[1].Text = target.Method.Name;
232                }
233            }
234        }
235
236        private void taskType_CheckedChanged(object sender, EventArgs e)
237        {
238            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
239                scheduleDaily.Enabled = scheduleWeekly.Enabled =
240                scheduleMonthly.Enabled = typeRecurring.Checked;
241            nonRecurringPanel.Visible = !typeRecurring.Checked;
242           
243            scheduleSpan_CheckedChanged(sender, e);
244        }
245
246        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
247        {
248            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
249                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
250                typeRecurring.Checked;
251            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
252                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
253                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
254                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
255                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
256                scheduleWeekly.Checked && typeRecurring.Checked;
257            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
258                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
259                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
260                typeRecurring.Checked;
261
262            scheduleDailySpan_CheckedChanged(sender, e);
263        }
264
265        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
266        {
267            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
268                scheduleDaily.Checked && typeRecurring.Checked;
269        }
270
271        private void ok_Click(object sender, EventArgs e)
272        {
273            if (data.Items.Count == 0)
274            {
275                errorProvider.SetIconPadding(data, -16);
276                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
277                errorProvider.SetError(data, S._("The task has no data to erase."));
278                container.SelectedIndex = 0;
279                return;
280            }
281            else if (typeRecurring.Checked && scheduleWeekly.Checked)
282            {
283                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
284                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
285                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
286                    !scheduleWeeklySunday.Checked)
287                {
288                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
289                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
290                        "on at least one day a week"));
291                    container.SelectedIndex = 1;
292                    return;
293                }
294            }
295
296            errorProvider.Clear();
297
298            //Close the dialog
299            DialogResult = DialogResult.OK;
300            Close();
301        }
302
303        private Task task = new Task();
304    }
305}
Note: See TracBrowser for help on using the repository browser.