Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Administration/3.4/Views/ScheduleView.cs @ 6437

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

#1233
Admin UI:

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