Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ScheduleView.cs @ 12147

Last change on this file since 12147 was 8085, checked in by gkronber, 13 years ago

#1847: merged trunk changes r7800:HEAD into gp move operators branch

File size: 14.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using System.Xml.Serialization;
28using Calendar;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32
33namespace 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() {
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));
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));
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));
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));
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) {
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 = "Offline";
135      app.Recurring = false;
136      return app;
137    }
138
139    private HiveAppointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay, bool recurring, Guid recurringId) {
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 = "Offline";
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      DeleteRecurringAppointment(recurringId);
168      offlineTimes.AddRange(recurringAppointments);
169    }
170
171    public void DialogClosed(RecurrentEvent e) {
172      CreateDailyRecurrenceAppointments(e.DateFrom, e.DateTo, e.AllDay, e.WeekDays);
173    }
174
175    private void CreateDailyRecurrenceAppointments(DateTime dateFrom, DateTime dateTo, bool allDay, HashSet<DayOfWeek> daysOfWeek) {
176      DateTime incDate = dateFrom;
177      Guid guid = Guid.NewGuid();
178
179      while (incDate.Date <= dateTo.Date) {
180        if (daysOfWeek.Contains(incDate.Date.DayOfWeek))
181          offlineTimes.Add(CreateAppointment(incDate, new DateTime(incDate.Year, incDate.Month, incDate.Day, dateTo.Hour, dateTo.Minute, 0), allDay, true, guid));
182        incDate = incDate.AddDays(1);
183      }
184
185      dvOnline.Invalidate();
186    }
187
188    private void btbDelete_Click(object sender, EventArgs e) {
189      HiveAppointment selectedAppointment = (HiveAppointment)dvOnline.SelectedAppointment;
190      if (dvOnline.SelectedAppointment != null) {
191        if (!selectedAppointment.Recurring)
192          DeleteAppointment();
193        else {
194          DialogResult res = MessageBox.Show("Delete all events in this series?", "Delete recurrences", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
195          if (res != DialogResult.Yes)
196            DeleteAppointment();
197          else
198            DeleteRecurringAppointment(selectedAppointment.RecurringId);
199        }
200      }
201      dvOnline.Invalidate();
202    }
203
204    private void DeleteAppointment() {
205      try {
206        HiveAppointment app = offlineTimes.First(s => s.Equals((HiveAppointment)dvOnline.SelectedAppointment));
207        app.Deleted = true;
208      }
209      catch (InvalidOperationException) {
210        // this is a ui bug where a selected all day appointment is not properly selected :-/
211      }
212    }
213
214    #region Register Content Events
215    protected override void DeregisterContentEvents() {
216      base.DeregisterContentEvents();
217    }
218    protected override void RegisterContentEvents() {
219      base.RegisterContentEvents();
220    }
221    #endregion
222
223    protected override void OnContentChanged() {
224      base.OnContentChanged();
225      UpdateCalendarFromContent();
226    }
227
228    protected override void SetEnabledStateOfControls() {
229      base.SetEnabledStateOfControls();
230    }
231
232    public virtual void SetEnabledStateOfSchedule(bool state) {
233      if (InvokeRequired) {
234        Invoke(new Action(() => SetEnabledStateOfSchedule(state)));
235      } else {
236        if (Content == null) state = false;
237        groupBox1.Enabled = state;
238        btnClearCal.Enabled = state;
239        btnSaveCal.Enabled = state;
240      }
241    }
242
243    private void btnClearCal_Click(object sender, System.EventArgs e) {
244      foreach (HiveAppointment app in offlineTimes) {
245        app.Deleted = true;
246      }
247      dvOnline.Invalidate();
248    }
249
250    private void chbade_CheckedChanged(object sender, EventArgs e) {
251      txttimeFrom.Visible = !chbade.Checked;
252      txttimeTo.Visible = !chbade.Checked;
253    }
254
255    private void dvOnline_OnSelectionChanged(object sender, EventArgs e) {
256      if (dvOnline.Selection == SelectionType.DateRange) {
257        dtpFrom.Text = dvOnline.SelectionStart.ToShortDateString();
258        dtpTo.Text = dvOnline.SelectionEnd.Date.ToShortDateString();
259        txttimeFrom.Text = dvOnline.SelectionStart.ToShortTimeString();
260        txttimeTo.Text = dvOnline.SelectionEnd.ToShortTimeString();
261        btCreate.Text = "Create Appointment";
262      }
263
264      if (dvOnline.Selection == SelectionType.Appointment) {
265        dtpFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortDateString();
266        dtpTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortDateString();
267        txttimeFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortTimeString();
268        txttimeTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortTimeString();
269
270        if (dvOnline.SelectedAppointment.Recurring)
271          //also change the caption of the save button
272          btCreate.Text = "Save changes";
273      }
274      if (dvOnline.Selection == SelectionType.None) {
275        //also change the caption of the save button
276        btCreate.Text = "Create Appointment";
277      }
278    }
279
280    private void mcOnline_DateChanged(object sender, DateRangeEventArgs e) {
281      dvOnline.StartDate = mcOnline.SelectionStart;
282    }
283
284    private void btCreate_Click(object sender, EventArgs e) {
285      if (dvOnline.Selection != SelectionType.Appointment) {
286        CreateAppointment();
287      } else {
288        //now we want to change an existing appointment
289        if (!dvOnline.SelectedAppointment.Recurring) {
290          if (CreateAppointment())
291            DeleteAppointment();
292        } else {
293          //change recurring appointment
294          //check, if only selected appointment has to change or whole recurrence
295          DialogResult res = MessageBox.Show("Change all events in this series?", "Change recurrences", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
296          if (res != DialogResult.Yes) {
297            if (CreateAppointment())
298              DeleteAppointment();
299          } else
300            ChangeRecurrenceAppointment(((HiveAppointment)dvOnline.SelectedAppointment).RecurringId);
301        }
302      }
303      dvOnline.Invalidate();
304    }
305
306    private void btnRecurrence_Click(object sender, EventArgs e) {
307      Recurrence recurrence = new Recurrence();
308      recurrence.dialogClosedDelegate = new OnDialogClosedDelegate(this.DialogClosed);
309      recurrence.Show();
310    }
311
312    private void btnSaveCal_Click(object sender, EventArgs e) {
313      if (HiveAdminClient.Instance.DowntimeForResourceId == null || HiveAdminClient.Instance.DowntimeForResourceId == Guid.Empty) {
314        MessageBox.Show("You have to save the goup before you can save the schedule. ", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
315      } else {
316        List<Downtime> appointments = new List<Downtime>();
317        foreach (HiveAppointment app in offlineTimes) {
318          if (app.Deleted && app.Id != Guid.Empty) {
319            HiveAdminClient.Delete(ToDowntime(app));
320          } else if (app.Changed || app.Id == null || app.Id == Guid.Empty) {
321            Downtime dt = ToDowntime(app);
322            appointments.Add(dt);
323          }
324        }
325        foreach (Downtime dt in appointments) {
326          dt.Store();
327        }
328      }
329    }
330
331    private HiveAppointment ToHiveAppointment(Downtime downtime) {
332      HiveAppointment app = new HiveAppointment {
333        AllDayEvent = downtime.AllDayEvent,
334        EndDate = downtime.EndDate,
335        StartDate = downtime.StartDate,
336        Recurring = downtime.Recurring,
337        RecurringId = downtime.RecurringId,
338        Deleted = false,
339        BorderColor = Color.Red,
340        Locked = true,
341        Subject = "Offline",
342        Changed = downtime.Modified,
343        Id = downtime.Id
344      };
345      return app;
346    }
347
348    private Downtime ToDowntime(HiveAppointment app) {
349      Downtime downtime = new Downtime {
350        AllDayEvent = app.AllDayEvent,
351        EndDate = app.EndDate,
352        StartDate = app.StartDate,
353        Recurring = app.Recurring,
354        RecurringId = app.RecurringId,
355        ResourceId = HiveAdminClient.Instance.DowntimeForResourceId,
356        Id = app.Id
357      };
358      return downtime;
359    }
360  }
361}
Note: See TracBrowser for help on using the repository browser.