source: trunk/eraser6/Eraser/TaskPropertiesForm.cs @ 1359

Revision 1359, 11.5 KB checked in by lowjoel, 4 years ago (diff)

Set svn:eol-style to native

  • Property svn:eol-style set to native
  • 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            UXThemeApi.UpdateControlTheme(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[item.Index] = target;
241                    item.Text = target.UIText;
242                    item.SubItems[1].Text = target.MethodDefined ? target.Method.Name : S._("(default)");
243                }
244            }
245        }
246
247        /// <summary>
248        /// Generated when the user right-clicks on the data selection list-view.
249        /// </summary>
250        /// <param name="sender">The menu being opened.</param>
251        /// <param name="e">Event argument.</param>
252        private void dataContextMenuStrip_Opening(object sender, CancelEventArgs e)
253        {
254            if (data.SelectedIndices.Count == 0)
255            {
256                e.Cancel = true;
257                return;
258            }
259        }
260
261        /// <summary>
262        /// Generated when the user selects the menu itm to remove the selected
263        /// data from the list of data to erase.
264        /// </summary>
265        /// <param name="sender">The object triggering the event.</param>
266        /// <param name="e">Event argument.</param>
267        private void deleteDataToolStripMenuItem_Click(object sender, EventArgs e)
268        {
269            if (data.SelectedIndices.Count == 0)
270                return;
271
272            foreach (ListViewItem obj in data.SelectedItems)
273            {
274                task.Targets.Remove((ErasureTarget)obj.Tag);
275                data.Items.Remove(obj);
276            }
277        }
278
279        /// <summary>
280        /// Generated when the task schedule type changes.
281        /// </summary>
282        /// <param name="sender">The object triggering the event.</param>
283        /// <param name="e">Event argument.</param>
284        private void taskType_CheckedChanged(object sender, EventArgs e)
285        {
286            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
287                scheduleDaily.Enabled = scheduleWeekly.Enabled =
288                scheduleMonthly.Enabled = typeRecurring.Checked;
289            nonRecurringPanel.Visible = !typeRecurring.Checked;
290           
291            scheduleSpan_CheckedChanged(sender, e);
292        }
293
294        /// <summary>
295        /// Generated when the scheduling frequency is changed.
296        /// </summary>
297        /// <param name="sender">The object triggering the event.</param>
298        /// <param name="e">Event argument.</param>
299        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
300        {
301            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
302                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
303                typeRecurring.Checked;
304            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
305                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
306                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
307                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
308                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
309                scheduleWeekly.Checked && typeRecurring.Checked;
310            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
311                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
312                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
313                typeRecurring.Checked;
314
315            scheduleDailySpan_CheckedChanged(sender, e);
316        }
317
318        /// <summary>
319        /// Generated when the daily frequency argument is changed.
320        /// </summary>
321        /// <param name="sender">The object triggering the event.</param>
322        /// <param name="e">Event argument.</param>
323        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
324        {
325            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
326                scheduleDaily.Checked && typeRecurring.Checked;
327        }
328
329        /// <summary>
330        /// Generated when the dialog is closed.
331        /// </summary>
332        /// <param name="sender">The object triggering the event.</param>
333        /// <param name="e">Event argument.</param>
334        private void ok_Click(object sender, EventArgs e)
335        {
336            if (data.Items.Count == 0)
337            {
338                errorProvider.SetIconPadding(data, -16);
339                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
340                errorProvider.SetError(data, S._("The task has no data to erase."));
341                container.SelectedIndex = 0;
342                return;
343            }
344            else if (typeRecurring.Checked && scheduleWeekly.Checked)
345            {
346                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
347                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
348                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
349                    !scheduleWeeklySunday.Checked)
350                {
351                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
352                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
353                        "on at least one day a week"));
354                    container.SelectedIndex = 1;
355                    return;
356                }
357            }
358
359            errorProvider.Clear();
360
361            //Close the dialog
362            DialogResult = DialogResult.OK;
363            Close();
364        }
365
366        /// <summary>
367        /// The task being edited.
368        /// </summary>
369        private Task task = new Task();
370    }
371}
Note: See TracBrowser for help on using the repository browser.