Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gp-crossover/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ScheduleView.cs @ 7317

Last change on this file since 7317 was 6976, checked in by ascheibe, 13 years ago

#1672 integrate the Hive client projects into trunk (Hive Job Manager and Administrator)

File size: 13.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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);
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    private void btnClearCal_Click(object sender, System.EventArgs e) {
233      foreach (HiveAppointment app in offlineTimes) {
234        app.Deleted = true;
235      }
236      dvOnline.Invalidate();
237    }
238
239    private void chbade_CheckedChanged(object sender, EventArgs e) {
240      txttimeFrom.Visible = !chbade.Checked;
241      txttimeTo.Visible = !chbade.Checked;
242    }
243
244    private void dvOnline_OnSelectionChanged(object sender, EventArgs e) {
245      //btCreate.Enabled = true;
246      if (dvOnline.Selection == SelectionType.DateRange) {
247        dtpFrom.Text = dvOnline.SelectionStart.ToShortDateString();
248        dtpTo.Text = dvOnline.SelectionEnd.Date.ToShortDateString();
249        txttimeFrom.Text = dvOnline.SelectionStart.ToShortTimeString();
250        txttimeTo.Text = dvOnline.SelectionEnd.ToShortTimeString();
251
252        btCreate.Text = "Save";
253      }
254
255      if (dvOnline.Selection == SelectionType.Appointment) {
256
257        dtpFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortDateString();
258        dtpTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortDateString();
259        txttimeFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortTimeString();
260        txttimeTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortTimeString();
261
262        if (dvOnline.SelectedAppointment.Recurring)
263          //btCreate.Enabled = false;
264          //also change the caption of the save button
265          btCreate.Text = "Save changes";
266      }
267
268      if (dvOnline.Selection == SelectionType.None) {
269        //also change the caption of the save button
270        btCreate.Text = "Save";
271      }
272    }
273
274    private void mcOnline_DateChanged(object sender, DateRangeEventArgs e) {
275      dvOnline.StartDate = mcOnline.SelectionStart;
276    }
277
278    private void btCreate_Click(object sender, EventArgs e) {
279      if (dvOnline.Selection != SelectionType.Appointment) {
280        CreateAppointment();
281      } else {
282        //now we want to change an existing appointment
283        if (!dvOnline.SelectedAppointment.Recurring) {
284          if (CreateAppointment())
285            DeleteAppointment();
286        } else {
287          //change recurring appointment
288          //check, if only selected appointment has to change or whole recurrence
289          DialogResult res = MessageBox.Show("Change all events in this series?", "Change recurrences", MessageBoxButtons.YesNo);
290          if (res != DialogResult.Yes) {
291            if (CreateAppointment())
292              DeleteAppointment();
293          } else
294            ChangeRecurrenceAppointment(((HiveAppointment)dvOnline.SelectedAppointment).RecurringId);
295        }
296      }
297      dvOnline.Invalidate();
298    }
299
300    private void btnRecurrence_Click(object sender, EventArgs e) {
301      Recurrence recurrence = new Recurrence();
302      recurrence.dialogClosedDelegate = new OnDialogClosedDelegate(this.DialogClosed);
303      recurrence.Show();
304    }
305
306    private void btnSaveCal_Click(object sender, EventArgs e) {
307      if (HiveAdminClient.Instance.DowntimeForResourceId == null || HiveAdminClient.Instance.DowntimeForResourceId == Guid.Empty) {
308        MessageBox.Show("You have to save the goup before you can save the schedule. ", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
309      } else {
310        List<Downtime> appointments = new List<Downtime>();
311        foreach (HiveAppointment app in offlineTimes) {
312          if (app.Deleted && app.Id != Guid.Empty) {
313            HiveAdminClient.Delete(ToDowntime(app));
314          } else if (app.Changed || app.Id == null || app.Id == Guid.Empty) {
315            Downtime dt = ToDowntime(app);
316            appointments.Add(dt);
317          }
318        }
319        foreach (Downtime dt in appointments) {
320          dt.Store();
321        }
322      }
323    }
324
325    private HiveAppointment ToHiveAppointment(Downtime downtime) {
326      HiveAppointment app = new HiveAppointment {
327        AllDayEvent = downtime.AllDayEvent,
328        EndDate = downtime.EndDate,
329        StartDate = downtime.StartDate,
330        Recurring = downtime.Recurring,
331        RecurringId = downtime.RecurringId,
332        Deleted = false,
333        BorderColor = Color.Red,
334        Locked = true,
335        Subject = "Offline",
336        Changed = downtime.Modified,
337        Id = downtime.Id
338      };
339      return app;
340    }
341
342    private Downtime ToDowntime(HiveAppointment app) {
343      Downtime downtime = new Downtime {
344        AllDayEvent = app.AllDayEvent,
345        EndDate = app.EndDate,
346        StartDate = app.StartDate,
347        Recurring = app.Recurring,
348        RecurringId = app.RecurringId,
349        ResourceId = HiveAdminClient.Instance.DowntimeForResourceId,
350        Id = app.Id
351      };
352      return downtime;
353    }
354  }
355}
Note: See TracBrowser for help on using the repository browser.