Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ScheduleView.cs @ 6701

Last change on this file since 6701 was 6696, checked in by ascheibe, 13 years ago

#1233

  • some cleanups
  • removed unused code
File size: 13.8 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("ScheduleView")]
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    private Guid resourceId;
50    public Guid ResourceId {
51      get {
52        return resourceId;
53      }
54      set {
55        resourceId = value;
56      }
57    }
58
59    public ScheduleView() {
60      InitializeComponent();
61      InitCalender();
62    }
63
64    private void InitCalender() {
65      dvOnline.StartDate = DateTime.Now;
66      dvOnline.OnNewAppointment += new EventHandler<NewAppointmentEventArgs>(dvOnline_OnNewAppointment);
67      dvOnline.OnResolveAppointments += new EventHandler<ResolveAppointmentsEventArgs>(dvOnline_OnResolveAppointments);
68    }
69
70    private void dvOnline_OnResolveAppointments(object sender, ResolveAppointmentsEventArgs e) {
71      List<HiveAppointment> apps = new List<HiveAppointment>();
72
73      foreach (HiveAppointment app in offlineTimes) {
74        if (app.StartDate >= e.StartDate && app.StartDate <= e.EndDate && !app.Deleted) {
75          apps.Add(app);
76        }
77      }
78
79      e.Appointments.Clear();
80      foreach (HiveAppointment app in apps) {
81        e.Appointments.Add(app);
82      }
83    }
84
85    private void dvOnline_OnNewAppointment(object sender, NewAppointmentEventArgs e) {
86      HiveAppointment Appointment = new HiveAppointment();
87
88      Appointment.StartDate = e.StartDate;
89      Appointment.EndDate = e.EndDate;
90      offlineTimes.Add(Appointment);
91    }
92
93    private void UpdateCalendarFromContent() {
94      offlineTimes.Clear();
95      foreach (Downtime app in Content) {
96        offlineTimes.Add(new HiveAppointment {
97          AllDayEvent = app.AllDayEvent,
98          EndDate = app.EndDate,
99          StartDate = app.StartDate,
100          Recurring = app.Recurring,
101          RecurringId = app.RecurringId,
102          BorderColor = Color.Red,
103          Locked = true,
104          Subject = "Offline",
105          Changed = false,
106          Id = app.Id
107        });
108      }
109      dvOnline.Invalidate();
110    }
111
112    private bool CreateAppointment() {
113      DateTime from, to;
114
115      if (!string.IsNullOrEmpty(dtpFrom.Text) && !string.IsNullOrEmpty(dtpTo.Text)) {
116        if (chbade.Checked) {
117          //whole day appointment, only dates are visible
118          if (DateTime.TryParse(dtpFrom.Text, out from) && DateTime.TryParse(dtpTo.Text, out to) && from <= to)
119            offlineTimes.Add(CreateAppointment(from, to.AddDays(1), true));
120          else
121            MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
122        } else if (!string.IsNullOrEmpty(txttimeFrom.Text) && !string.IsNullOrEmpty(txttimeTo.Text)) {
123          //Timeframe appointment
124          if (DateTime.TryParse(dtpFrom.Text + " " + txttimeFrom.Text, out from) && DateTime.TryParse(dtpTo.Text + " " + txttimeTo.Text, out to) && from < to) {
125            if (from.Date == to.Date)
126              offlineTimes.Add(CreateAppointment(from, to, false));
127            else {
128              //more than 1 day selected
129              while (from.Date != to.Date) {
130                offlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false));
131                from = from.AddDays(1);
132              }
133              offlineTimes.Add(CreateAppointment(from, new DateTime(from.Year, from.Month, from.Day, to.Hour, to.Minute, 0, 0), false));
134            }
135          } else
136            MessageBox.Show("Incorrect date format", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
137        }
138        dvOnline.Invalidate();
139        return true;
140      } else {
141        MessageBox.Show("Error in create appointment, please fill out all textboxes!", "Schedule Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
142        return false;
143      }
144    }
145
146    private HiveAppointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay) {
147      HiveAppointment app = new HiveAppointment();
148      app.StartDate = startDate;
149      app.EndDate = endDate;
150      app.AllDayEvent = allDay;
151      app.BorderColor = Color.Red;
152      app.Locked = true;
153      app.Subject = "Offline";
154      app.Recurring = false;
155      return app;
156    }
157
158    private HiveAppointment CreateAppointment(DateTime startDate, DateTime endDate, bool allDay, bool recurring, Guid recurringId) {
159      HiveAppointment app = new HiveAppointment();
160      app.StartDate = startDate;
161      app.EndDate = endDate;
162      app.AllDayEvent = allDay;
163      app.BorderColor = Color.Red;
164      app.Locked = true;
165      app.Subject = "Offline";
166      app.Recurring = recurring;
167      app.RecurringId = recurringId;
168      return app;
169    }
170
171    private void DeleteRecurringAppointment(Guid recurringId) {
172      foreach (HiveAppointment app in offlineTimes) {
173        if (app.RecurringId == recurringId) {
174          app.Deleted = true;
175        }
176      }
177    }
178
179    private void ChangeRecurrenceAppointment(Guid recurringId) {
180      int hourfrom = int.Parse(txttimeFrom.Text.Substring(0, txttimeFrom.Text.IndexOf(':')));
181      int hourTo = int.Parse(txttimeTo.Text.Substring(0, txttimeTo.Text.IndexOf(':')));
182      List<HiveAppointment> recurringAppointments = offlineTimes.Where(appointment => ((HiveAppointment)appointment).RecurringId == recurringId).ToList();
183      recurringAppointments.ForEach(appointment => appointment.StartDate = new DateTime(appointment.StartDate.Year, appointment.StartDate.Month, appointment.StartDate.Day, hourfrom, 0, 0));
184      recurringAppointments.ForEach(appointment => appointment.EndDate = new DateTime(appointment.EndDate.Year, appointment.EndDate.Month, appointment.EndDate.Day, hourTo, 0, 0));
185
186      DeleteRecurringAppointment(recurringId);
187      offlineTimes.AddRange(recurringAppointments);
188    }
189
190    public void DialogClosed(RecurrentEvent e) {
191      CreateDailyRecurrenceAppointments(e.DateFrom, e.DateTo, e.AllDay, e.WeekDays);
192    }
193
194    private void CreateDailyRecurrenceAppointments(DateTime dateFrom, DateTime dateTo, bool allDay, HashSet<DayOfWeek> daysOfWeek) {
195      DateTime incDate = dateFrom;
196      Guid guid = Guid.NewGuid();
197
198      while (incDate.Date <= dateTo.Date) {
199        if (daysOfWeek.Contains(incDate.Date.DayOfWeek))
200          offlineTimes.Add(CreateAppointment(incDate, new DateTime(incDate.Year, incDate.Month, incDate.Day, dateTo.Hour, dateTo.Minute, 0), allDay, true, guid));
201        incDate = incDate.AddDays(1);
202      }
203
204      dvOnline.Invalidate();
205    }
206
207    private void btbDelete_Click(object sender, EventArgs e) {
208      HiveAppointment selectedAppointment = (HiveAppointment)dvOnline.SelectedAppointment;
209      if (dvOnline.SelectedAppointment != null) {
210        if (!selectedAppointment.Recurring)
211          DeleteAppointment();
212        else {
213          DialogResult res = MessageBox.Show("Delete all events in this series?", "Delete recurrences", MessageBoxButtons.YesNo);
214          if (res != DialogResult.Yes)
215            DeleteAppointment();
216          else
217            DeleteRecurringAppointment(selectedAppointment.RecurringId);
218        }
219      }
220      dvOnline.Invalidate();
221    }
222
223    private void DeleteAppointment() {
224      try {
225        HiveAppointment app = offlineTimes.First(s => s.Equals((HiveAppointment)dvOnline.SelectedAppointment));
226        app.Deleted = true;
227      }
228      catch (InvalidOperationException) {
229        // this is a ui bug where a selected all day appointment is not properly selected :-/
230      }
231    }
232
233    #region Register Content Events
234    protected override void DeregisterContentEvents() {
235      // TODO: Deregister your event handlers on the Content here
236      base.DeregisterContentEvents();
237    }
238    protected override void RegisterContentEvents() {
239      base.RegisterContentEvents();
240      // TODO: Register your event handlers on the Content here
241    }
242    #endregion
243
244    protected override void OnContentChanged() {
245      base.OnContentChanged();
246      if (Content == null) {
247        // TODO: Put code here when content is null
248      } else {
249        UpdateCalendarFromContent();
250      }
251    }
252
253    protected override void SetEnabledStateOfControls() {
254      base.SetEnabledStateOfControls();
255      // TODO: Put code here to enable or disable controls based on whether the Content is/not null or the view is ReadOnly
256    }
257
258    private void btnClearCal_Click(object sender, System.EventArgs e) {
259      foreach (HiveAppointment app in offlineTimes) {
260        app.Deleted = true;
261      }
262      dvOnline.Invalidate();
263    }
264
265    private void chbade_CheckedChanged(object sender, EventArgs e) {
266      txttimeFrom.Visible = !chbade.Checked;
267      txttimeTo.Visible = !chbade.Checked;
268    }
269
270    private void dvOnline_OnSelectionChanged(object sender, EventArgs e) {
271      //btCreate.Enabled = true;
272      if (dvOnline.Selection == SelectionType.DateRange) {
273        dtpFrom.Text = dvOnline.SelectionStart.ToShortDateString();
274        dtpTo.Text = dvOnline.SelectionEnd.Date.ToShortDateString();
275        txttimeFrom.Text = dvOnline.SelectionStart.ToShortTimeString();
276        txttimeTo.Text = dvOnline.SelectionEnd.ToShortTimeString();
277
278        btCreate.Text = "Save";
279      }
280
281      if (dvOnline.Selection == SelectionType.Appointment) {
282
283        dtpFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortDateString();
284        dtpTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortDateString();
285        txttimeFrom.Text = dvOnline.SelectedAppointment.StartDate.ToShortTimeString();
286        txttimeTo.Text = dvOnline.SelectedAppointment.EndDate.ToShortTimeString();
287
288        if (dvOnline.SelectedAppointment.Recurring)
289          //btCreate.Enabled = false;
290          //also change the caption of the save button
291          btCreate.Text = "Save changes";
292      }
293
294      if (dvOnline.Selection == SelectionType.None) {
295        //also change the caption of the save button
296        btCreate.Text = "Save";
297      }
298    }
299
300    private void mcOnline_DateChanged(object sender, DateRangeEventArgs e) {
301      dvOnline.StartDate = mcOnline.SelectionStart;
302    }
303
304    private void btCreate_Click(object sender, EventArgs e) {
305      if (dvOnline.Selection != SelectionType.Appointment) {
306        CreateAppointment();
307      } else {
308        //now we want to change an existing appointment
309        if (!dvOnline.SelectedAppointment.Recurring) {
310          if (CreateAppointment())
311            DeleteAppointment();
312        } else {
313          //change recurring appointment
314          //check, if only selected appointment has to change or whole recurrence
315          DialogResult res = MessageBox.Show("Change all events in this series?", "Change recurrences", MessageBoxButtons.YesNo);
316          if (res != DialogResult.Yes) {
317            if (CreateAppointment())
318              DeleteAppointment();
319          } else
320            ChangeRecurrenceAppointment(((HiveAppointment)dvOnline.SelectedAppointment).RecurringId);
321        }
322      }
323      dvOnline.Invalidate();
324    }
325
326    private void btnRecurrence_Click(object sender, EventArgs e) {
327      Recurrence recurrence = new Recurrence();
328      recurrence.dialogClosedDelegate = new OnDialogClosedDelegate(this.DialogClosed);
329      recurrence.Show();
330    }
331
332    private void btnSaveCal_Click(object sender, EventArgs e) {
333      List<Downtime> appointments = new List<Downtime>();
334      foreach (HiveAppointment app in offlineTimes) {
335        if (app.Deleted) {
336          ServiceLocator.Instance.CallHiveService(service => {
337            if (app.Id != Guid.Empty) {
338              service.DeleteDowntime(app.Id);
339            }
340          });
341        } else if (app.Changed) {
342          Downtime apdto = new Downtime {
343            AllDayEvent = app.AllDayEvent,
344            EndDate = app.EndDate,
345            Recurring = app.Recurring,
346            RecurringId = app.RecurringId,
347            StartDate = app.StartDate,
348            ResourceId = resourceId
349          };
350          appointments.Add(apdto);
351        }
352      }
353
354      if (appointments.Count > 0) {
355        //TODO: find a sane way to do this
356        ServiceLocator.Instance.CallHiveService(service => {
357          foreach (Downtime app in appointments) {
358            service.AddDowntime(app);
359          }
360        });
361      }
362    }
363  }
364}
Note: See TracBrowser for help on using the repository browser.