Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5955 was 5638, checked in by ascheibe, 14 years ago

#1233 worked on Administration UI

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