Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ScheduleView.cs @ 18242

Last change on this file since 18242 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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