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

Revision 366, 9.9 KB checked in by lowjoel, 6 years ago (diff)

Replace GetText? with the .NET internationalisation framework (it scales better for .NET, gettext doesn't work well for Microsoft .NET): Commit part 2

  • 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                        scheduleWeeklyMonday.Checked =
167                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.MONDAY) != 0;
168                        scheduleWeeklyTuesday.Checked =
169                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.TUESDAY) != 0;
170                        scheduleWeeklyWednesday.Checked =
171                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.WEDNESDAY) != 0;
172                        scheduleWeeklyThursday.Checked =
173                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.THURSDAY) != 0;
174                        scheduleWeeklyFriday.Checked =
175                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.FRIDAY) != 0;
176                        scheduleWeeklySaturday.Checked =
177                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SATURDAY) != 0;
178                        scheduleWeeklySunday.Checked =
179                            (schedule.WeeklySchedule & RecurringSchedule.DaysOfWeek.SUNDAY) != 0;
180                        break;
181                    case RecurringSchedule.ScheduleUnit.MONTHLY:
182                        scheduleMonthlyFreq.Value = schedule.Frequency;
183                        scheduleMonthlyDayNumber.Value = schedule.MonthlySchedule;
184                        break;
185                    default:
186                        throw new NotImplementedException("Unknown schedule type.");
187                }
188            }
189        }
190
191        /// <summary>
192        /// Triggered when the user clicks on the Add Data button.
193        /// </summary>
194        /// <param name="sender">The button.</param>
195        /// <param name="e">Event argument.</param>
196        private void dataAdd_Click(object sender, EventArgs e)
197        {
198            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
199            {
200                if (form.ShowDialog() == DialogResult.OK)
201                {
202                    Task.ErasureTarget target = form.Target;
203                    ListViewItem item = data.Items.Add(target.UIText);
204                   
205                    item.SubItems.Add(target.Method.Name);
206                    task.Targets.Add(target);
207                    errorProvider.Clear();
208                }
209            }
210        }
211
212        /// <summary>
213        /// Generated when the user double-clicks an item in the list-view.
214        /// </summary>
215        /// <param name="sender">The list-view which generated this event.</param>
216        /// <param name="e">Event argument.</param>
217        private void data_ItemActivate(object sender, EventArgs e)
218        {
219            using (TaskDataSelectionForm form = new TaskDataSelectionForm())
220            {
221                ListViewItem item = data.SelectedItems[0];
222                form.Target = task.Targets[item.Index];
223
224                if (form.ShowDialog() == DialogResult.OK)
225                {
226                    Task.ErasureTarget target = form.Target;
227                    task.Targets[item.Index] = target;
228                    item.Text = target.UIText;
229                    item.SubItems[1].Text = target.Method.Name;
230                }
231            }
232        }
233
234        private void taskType_CheckedChanged(object sender, EventArgs e)
235        {
236            scheduleTimeLbl.Enabled = scheduleTime.Enabled = schedulePattern.Enabled =
237                scheduleDaily.Enabled = scheduleWeekly.Enabled =
238                scheduleMonthly.Enabled = typeRecurring.Checked;
239            nonRecurringPanel.Visible = !typeRecurring.Checked;
240           
241            scheduleSpan_CheckedChanged(sender, e);
242        }
243
244        private void scheduleSpan_CheckedChanged(object sender, EventArgs e)
245        {
246            scheduleDailyByDay.Enabled = scheduleDailyByDayLbl.Enabled =
247                scheduleDailyByWeekday.Enabled = scheduleDaily.Checked &&
248                typeRecurring.Checked;
249            scheduleWeeklyLbl.Enabled = scheduleWeeklyFreq.Enabled =
250                scheduleWeeklyFreqLbl.Enabled = scheduleWeeklyMonday.Enabled =
251                scheduleWeeklyTuesday.Enabled = scheduleWeeklyWednesday.Enabled =
252                scheduleWeeklyThursday.Enabled = scheduleWeeklyFriday.Enabled =
253                scheduleWeeklySaturday.Enabled = scheduleWeeklySunday.Enabled =
254                scheduleWeekly.Checked && typeRecurring.Checked;
255            scheduleMonthlyLbl.Enabled = scheduleMonthlyDayNumber.Enabled =
256                scheduleMonthlyEveryLbl.Enabled = scheduleMonthlyFreq.Enabled =
257                scheduleMonthlyMonthLbl.Enabled = scheduleMonthly.Checked &&
258                typeRecurring.Checked;
259
260            scheduleDailySpan_CheckedChanged(sender, e);
261        }
262
263        private void scheduleDailySpan_CheckedChanged(object sender, EventArgs e)
264        {
265            scheduleDailyByDayFreq.Enabled = scheduleDailyByDay.Checked &&
266                scheduleDaily.Checked && typeRecurring.Checked;
267        }
268
269        private void ok_Click(object sender, EventArgs e)
270        {
271            if (data.Items.Count == 0)
272            {
273                errorProvider.SetIconPadding(data, -16);
274                errorProvider.SetIconAlignment(data, ErrorIconAlignment.BottomRight);
275                errorProvider.SetError(data, S._("The task has no data to erase."));
276                container.SelectedIndex = 0;
277                return;
278            }
279            else if (typeRecurring.Checked && scheduleWeekly.Checked)
280            {
281                if (!scheduleWeeklyMonday.Checked && !scheduleWeeklyTuesday.Checked &&
282                    !scheduleWeeklyWednesday.Checked && !scheduleWeeklyThursday.Checked &&
283                    !scheduleWeeklyFriday.Checked && !scheduleWeeklySaturday.Checked &&
284                    !scheduleWeeklySunday.Checked)
285                {
286                    errorProvider.SetIconPadding(scheduleWeeklyDays, -16);
287                    errorProvider.SetError(scheduleWeeklyDays, S._("The task needs to run " +
288                        "on at least one day a week"));
289                    container.SelectedIndex = 1;
290                    return;
291                }
292            }
293
294            errorProvider.Clear();
295
296            //Close the dialog
297            DialogResult = DialogResult.OK;
298            Close();
299        }
300
301        private Task task = new Task();
302    }
303}
Note: See TracBrowser for help on using the repository browser.