1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using System.Xml.Serialization;
|
---|
28 | using Calendar;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Core.Views;
|
---|
31 | using HeuristicLab.MainForm;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Clients.Hive.Administrator.Views {
|
---|
34 | [View("Schedule View")]
|
---|
35 | [Content(typeof(IItemList<Downtime>), IsDefaultView = true)]
|
---|
36 | public partial class ScheduleView : ItemView {
|
---|
37 | public new IItemList<Downtime> Content {
|
---|
38 | get { return (IItemList<Downtime>)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [XmlArray("Appointments")]
|
---|
43 | [XmlArrayItem("HiveAppointment", typeof(HiveAppointment))]
|
---|
44 | public List<HiveAppointment> offlineTimes = new List<HiveAppointment>();
|
---|
45 |
|
---|
46 | //delegate fired, if a dialog is being closed
|
---|
47 | public delegate void OnDialogClosedDelegate(RecurrentEvent e);
|
---|
48 |
|
---|
49 | public ScheduleView() {
|
---|
50 | InitializeComponent();
|
---|
51 | InitCalender();
|
---|
52 | }
|
---|
53 |
|
---|
54 | private void InitCalender() {
|
---|
55 | dvOnline.StartDate = DateTime.Now;
|
---|
56 | dvOnline.OnNewAppointment += new EventHandler<NewAppointmentEventArgs>(dvOnline_OnNewAppointment);
|
---|
57 | dvOnline.OnResolveAppointments += new EventHandler<ResolveAppointmentsEventArgs>(dvOnline_OnResolveAppointments);
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void dvOnline_OnResolveAppointments(object sender, ResolveAppointmentsEventArgs e) {
|
---|
61 | List<HiveAppointment> apps = new List<HiveAppointment>();
|
---|
62 |
|
---|
63 | foreach (HiveAppointment app in offlineTimes) {
|
---|
64 | if (app.StartDate >= e.StartDate && app.StartDate <= e.EndDate && !app.Deleted) {
|
---|
65 | apps.Add(app);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | e.Appointments.Clear();
|
---|
70 | foreach (HiveAppointment app in apps) {
|
---|
71 | e.Appointments.Add(app);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void dvOnline_OnNewAppointment(object sender, NewAppointmentEventArgs e) {
|
---|
76 | HiveAppointment Appointment = new HiveAppointment();
|
---|
77 |
|
---|
78 | Appointment.StartDate = e.StartDate;
|
---|
79 | Appointment.EndDate = e.EndDate;
|
---|
80 | offlineTimes.Add(Appointment);
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void UpdateCalendarFromContent() {
|
---|
84 | offlineTimes.Clear();
|
---|
85 | if (Content != null) {
|
---|
86 | foreach (Downtime app in Content) {
|
---|
87 | offlineTimes.Add(ToHiveAppointment(app));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | dvOnline.Invalidate();
|
---|
91 | }
|
---|
92 |
|
---|
93 | private bool CreateAppointment(DowntimeType dtType) {
|
---|
94 | DateTime from, to;
|
---|
95 |
|
---|
96 | if (!string.IsNullOrEmpty(dtpFrom.Text) && !string.IsNullOrEmpty(dtpTo.Text)) {
|
---|
97 | if (chbade.Checked) {
|
---|
98 | //whole day appointment, only dates are visible
|
---|
99 | if (DateTime.TryParse(dtpFrom.Text, out from) && DateTime.TryParse(dtpTo.Text, out to) && from <= to)
|
---|
100 | offlineTimes.Add(CreateAppointment(from, to.AddDays(1), true, dtType));
|
---|
101 | else
|
---|
102 | MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
103 | } else if (!string.IsNullOrEmpty(txttimeFrom.Text) && !string.IsNullOrEmpty(txttimeTo.Text)) {
|
---|
104 | //Timeframe appointment
|
---|
105 | if (DateTime.TryParse(dtpFrom.Text + " " + txttimeFrom.Text, out from) && DateTime.TryParse(dtpTo.Text + " " + txttimeTo.Text, out to) && from < to) {
|
---|
106 | if (from.Date == to.Date)
|
---|
107 | offlineTimes.Add(CreateAppointment(from, to, false, dtType));
|
---|
108 | else {
|
---|
109 | //more than 1 day selected
|
---|
110 | while (from.Date != to.Date) {
|
---|
111 | offlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false, dtType));
|
---|
112 | from = from.AddDays(1);
|
---|
113 | }
|
---|
114 | offlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false, dtType));
|
---|
115 | }
|
---|
116 | } else
|
---|
117 | MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
118 | }
|
---|
119 | dvOnline.Invalidate();
|
---|
120 | return true;
|
---|
121 | } else {
|
---|
122 | MessageBox.Show("Error in create appointment, please fill out all textboxes!", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private HiveAppointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay, DowntimeType downtimeType) {
|
---|
128 | HiveAppointment app = new HiveAppointment();
|
---|
129 | app.StartDate = startDate;
|
---|
130 | app.EndDate = endDate;
|
---|
131 | app.AllDayEvent = allDay;
|
---|
132 | app.BorderColor = Color.Red;
|
---|
133 | app.Locked = true;
|
---|
134 | app.Subject = downtimeType.ToString();
|
---|
135 | app.Recurring = false;
|
---|
136 | return app;
|
---|
137 | }
|
---|
138 |
|
---|
139 | private HiveAppointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay, bool recurring, Guid recurringId, DowntimeType downtimeType) {
|
---|
140 | HiveAppointment app = new HiveAppointment();
|
---|
141 | app.StartDate = startDate;
|
---|
142 | app.EndDate = endDate;
|
---|
143 | app.AllDayEvent = allDay;
|
---|
144 | app.BorderColor = Color.Red;
|
---|
145 | app.Locked = true;
|
---|
146 | app.Subject = downtimeType.ToString();
|
---|
147 | app.Recurring = recurring;
|
---|
148 | app.RecurringId = recurringId;
|
---|
149 | return app;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void DeleteRecurringAppointment(Guid recurringId) {
|
---|
153 | foreach (HiveAppointment app in offlineTimes) {
|
---|
154 | if (app.RecurringId == recurringId) {
|
---|
155 | app.Deleted = true;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void ChangeRecurrenceAppointment(Guid recurringId) {
|
---|
161 | int hourfrom = int.Parse(txttimeFrom.Text.Substring(0, txttimeFrom.Text.IndexOf(':')));
|
---|
162 | int hourTo = int.Parse(txttimeTo.Text.Substring(0, txttimeTo.Text.IndexOf(':')));
|
---|
163 | List<HiveAppointment> recurringAppointments = offlineTimes.Where(appointment => ((HiveAppointment)appointment).RecurringId == recurringId).ToList();
|
---|
164 | recurringAppointments.ForEach(appointment => appointment.StartDate = new DateTime(appointment.StartDate.Year, appointment.StartDate.Month, appointment.StartDate.Day, hourfrom, 0, 0));
|
---|
165 | recurringAppointments.ForEach(appointment => appointment.EndDate = new DateTime(appointment.EndDate.Year, appointment.EndDate.Month, appointment.EndDate.Day, hourTo, 0, 0));
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void DialogClosed(RecurrentEvent e) {
|
---|
169 | CreateDailyRecurrenceAppointments(e.DateFrom, e.DateTo, e.AllDay, e.WeekDays, e.AppointmentType);
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void CreateDailyRecurrenceAppointments(DateTime dateFrom, DateTime dateTo, bool allDay, HashSet<DayOfWeek> daysOfWeek, DowntimeType appointmentType) {
|
---|
173 | DateTime incDate = dateFrom;
|
---|
174 | Guid guid = Guid.NewGuid();
|
---|
175 |
|
---|
176 | while (incDate.Date <= dateTo.Date) {
|
---|
177 | if (daysOfWeek.Contains(incDate.Date.DayOfWeek))
|
---|
178 | offlineTimes.Add(CreateAppointment(incDate, new DateTime(incDate.Year, incDate.Month, incDate.Day, dateTo.Hour, dateTo.Minute, 0), allDay, true, guid, appointmentType));
|
---|
179 | incDate = incDate.AddDays(1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | dvOnline.Invalidate();
|
---|
183 | }
|
---|
184 |
|
---|
185 | private void btbDelete_Click(object sender, EventArgs e) {
|
---|
186 | HiveAppointment selectedAppointment = (HiveAppointment)dvOnline.SelectedAppointment;
|
---|
187 | if (dvOnline.SelectedAppointment != null) {
|
---|
188 | if (!selectedAppointment.Recurring)
|
---|
189 | DeleteAppointment();
|
---|
190 | else {
|
---|
191 | DialogResult res = MessageBox.Show("Delete all events in this series?", "Delete recurrences", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
192 | if (res != DialogResult.Yes)
|
---|
193 | DeleteAppointment();
|
---|
194 | else
|
---|
195 | DeleteRecurringAppointment(selectedAppointment.RecurringId);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | dvOnline.Invalidate();
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void DeleteAppointment() {
|
---|
202 | try {
|
---|
203 | HiveAppointment app = offlineTimes.First(s => s.Equals((HiveAppointment)dvOnline.SelectedAppointment));
|
---|
204 | app.Deleted = true;
|
---|
205 | }
|
---|
206 | catch (InvalidOperationException) {
|
---|
207 | // this is a ui bug where a selected all day appointment is not properly selected :-/
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | #region Register Content Events
|
---|
212 | protected override void DeregisterContentEvents() {
|
---|
213 | base.DeregisterContentEvents();
|
---|
214 | }
|
---|
215 | protected override void RegisterContentEvents() {
|
---|
216 | base.RegisterContentEvents();
|
---|
217 | }
|
---|
218 | #endregion
|
---|
219 |
|
---|
220 | protected override void OnContentChanged() {
|
---|
221 | base.OnContentChanged();
|
---|
222 | UpdateCalendarFromContent();
|
---|
223 | }
|
---|
224 |
|
---|
225 | protected override void SetEnabledStateOfControls() {
|
---|
226 | base.SetEnabledStateOfControls();
|
---|
227 | }
|
---|
228 |
|
---|
229 | public virtual void SetEnabledStateOfSchedule(bool state) {
|
---|
230 | if (InvokeRequired) {
|
---|
231 | Invoke(new Action(() => SetEnabledStateOfSchedule(state)));
|
---|
232 | } else {
|
---|
233 | if (Content == null) state = false;
|
---|
234 | groupBox1.Enabled = state;
|
---|
235 | btnClearCal.Enabled = state;
|
---|
236 | btnSaveCal.Enabled = state;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void btnClearCal_Click(object sender, System.EventArgs e) {
|
---|
241 | foreach (HiveAppointment app in offlineTimes) {
|
---|
242 | app.Deleted = true;
|
---|
243 | }
|
---|
244 | dvOnline.Invalidate();
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void chbade_CheckedChanged(object sender, EventArgs e) {
|
---|
248 | txttimeFrom.Visible = !chbade.Checked;
|
---|
249 | txttimeTo.Visible = !chbade.Checked;
|
---|
250 | }
|
---|
251 |
|
---|
252 | private void dvOnline_OnSelectionChanged(object sender, EventArgs e) {
|
---|
253 | if (dvOnline.Selection == SelectionType.DateRange) {
|
---|
254 | dtpFrom.Text = dvOnline.SelectionStart.ToShortDateString();
|
---|
255 | dtpTo.Text = dvOnline.SelectionEnd.Date.ToShortDateString();
|
---|
256 | txttimeFrom.Text = dvOnline.SelectionStart.ToShortTimeString();
|
---|
257 | txttimeTo.Text = dvOnline.SelectionEnd.ToShortTimeString();
|
---|
258 | btCreate.Text = "Create Appointment";
|
---|
259 | }
|
---|
260 |
|
---|
261 | if (dvOnline.Selection == SelectionType.Appointment) {
|
---|
262 | dtpFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortDateString();
|
---|
263 | dtpTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortDateString();
|
---|
264 | txttimeFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortTimeString();
|
---|
265 | txttimeTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortTimeString();
|
---|
266 |
|
---|
267 | if (dvOnline.SelectedAppointment.Recurring)
|
---|
268 | //also change the caption of the save button
|
---|
269 | btCreate.Text = "Save changes";
|
---|
270 | }
|
---|
271 | if (dvOnline.Selection == SelectionType.None) {
|
---|
272 | //also change the caption of the save button
|
---|
273 | btCreate.Text = "Create Appointment";
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | private void mcOnline_DateChanged(object sender, DateRangeEventArgs e) {
|
---|
278 | dvOnline.StartDate = mcOnline.SelectionStart;
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void btCreate_Click(object sender, EventArgs e) {
|
---|
282 | if (dvOnline.Selection != SelectionType.Appointment) {
|
---|
283 | DowntimeType dtType;
|
---|
284 | DialogResult result;
|
---|
285 | AppointmentTypeDialog dialog = new AppointmentTypeDialog();
|
---|
286 | result = dialog.ShowDialog(this);
|
---|
287 | dtType = dialog.AppointmentType;
|
---|
288 | dialog.Dispose();
|
---|
289 | if (result == DialogResult.Cancel) return;
|
---|
290 | CreateAppointment(dtType);
|
---|
291 | } else {
|
---|
292 | //now we want to change an existing appointment
|
---|
293 | if (!dvOnline.SelectedAppointment.Recurring) {
|
---|
294 | if (CreateAppointment(GetDowntimeTypeOfSelectedAppointment()))
|
---|
295 | DeleteAppointment();
|
---|
296 | } else {
|
---|
297 | //change recurring appointment
|
---|
298 | //check, if only selected appointment has to change or whole recurrence
|
---|
299 | DialogResult res = MessageBox.Show("Change all events in this series?", "Change recurrences", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
300 | if (res != DialogResult.Yes) {
|
---|
301 | if (CreateAppointment(GetDowntimeTypeOfSelectedAppointment()))
|
---|
302 | DeleteAppointment();
|
---|
303 | } else
|
---|
304 | ChangeRecurrenceAppointment(((HiveAppointment)dvOnline.SelectedAppointment).RecurringId);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | dvOnline.Invalidate();
|
---|
308 | }
|
---|
309 |
|
---|
310 | private void btnRecurrence_Click(object sender, EventArgs e) {
|
---|
311 | Recurrence recurrence = new Recurrence();
|
---|
312 | recurrence.dialogClosedDelegate = new OnDialogClosedDelegate(this.DialogClosed);
|
---|
313 | recurrence.Show();
|
---|
314 | }
|
---|
315 |
|
---|
316 | private void btnSaveCal_Click(object sender, EventArgs e) {
|
---|
317 | if (HiveAdminClient.Instance.DowntimeForResourceId == null || HiveAdminClient.Instance.DowntimeForResourceId == Guid.Empty) {
|
---|
318 | MessageBox.Show("You have to save the goup before you can save the schedule. ", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
|
---|
319 | } else {
|
---|
320 | List<Downtime> appointments = new List<Downtime>();
|
---|
321 | foreach (HiveAppointment app in offlineTimes) {
|
---|
322 | if (app.Deleted && app.Id != Guid.Empty) {
|
---|
323 | HiveAdminClient.Delete(ToDowntime(app));
|
---|
324 | } else if (app.Changed || app.Id == null || app.Id == Guid.Empty) {
|
---|
325 | Downtime dt = ToDowntime(app);
|
---|
326 | appointments.Add(dt);
|
---|
327 | }
|
---|
328 | }
|
---|
329 | foreach (Downtime dt in appointments) {
|
---|
330 | dt.Store();
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | private HiveAppointment ToHiveAppointment(Downtime downtime) {
|
---|
336 | HiveAppointment app = new HiveAppointment {
|
---|
337 | AllDayEvent = downtime.AllDayEvent,
|
---|
338 | EndDate = downtime.EndDate,
|
---|
339 | StartDate = downtime.StartDate,
|
---|
340 | Recurring = downtime.Recurring,
|
---|
341 | RecurringId = downtime.RecurringId,
|
---|
342 | Deleted = false,
|
---|
343 | BorderColor = Color.Red,
|
---|
344 | Locked = true,
|
---|
345 | Subject = downtime.DowntimeType.ToString(),
|
---|
346 | Changed = downtime.Modified,
|
---|
347 | Id = downtime.Id
|
---|
348 | };
|
---|
349 | return app;
|
---|
350 | }
|
---|
351 |
|
---|
352 | private Downtime ToDowntime(HiveAppointment app) {
|
---|
353 | Downtime downtime = new Downtime {
|
---|
354 | AllDayEvent = app.AllDayEvent,
|
---|
355 | EndDate = app.EndDate,
|
---|
356 | StartDate = app.StartDate,
|
---|
357 | Recurring = app.Recurring,
|
---|
358 | RecurringId = app.RecurringId,
|
---|
359 | ResourceId = HiveAdminClient.Instance.DowntimeForResourceId,
|
---|
360 | Id = app.Id,
|
---|
361 | DowntimeType = (DowntimeType)Enum.Parse(typeof(DowntimeType), app.Subject)
|
---|
362 | };
|
---|
363 | return downtime;
|
---|
364 | }
|
---|
365 |
|
---|
366 | private DowntimeType GetDowntimeTypeOfSelectedAppointment() {
|
---|
367 | return (DowntimeType)Enum.Parse(typeof(DowntimeType), ((HiveAppointment)dvOnline.SelectedAppointment).Subject);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|