Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 18183

Last change on this file since 18183 was 17312, checked in by gkronber, 5 years ago

#2994: merged r17220:17306 from trunk to branch

File size: 10.5 KB
RevLine 
[15401]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15401]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.ComponentModel;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Access;
[15412]27using HeuristicLab.Clients.Hive.Views;
[15401]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[17312]30using Tpl = System.Threading.Tasks;
[15401]31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = true)]
35  public partial class ProjectView : ItemView {
36    public new Project Content {
37      get { return (Project)base.Content; }
[16446]38      set { base.Content = value; }
[15401]39    }
40
41    public ProjectView() {
42      InitializeComponent();
43
44      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
45      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
46    }
47
[15412]48    #region Overrides
[15401]49    protected override void RegisterContentEvents() {
50      base.RegisterContentEvents();
51      Content.PropertyChanged += Content_PropertyChanged;
52    }
53
54    protected override void DeregisterContentEvents() {
55      Content.PropertyChanged -= Content_PropertyChanged;
56      base.DeregisterContentEvents();
57    }
58
[17312]59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      UpdateView();
[16117]62    }
63
[17312]64    private void UpdateView() {
[15401]65      if (Content == null) {
[15576]66        idTextBox.Clear();
[15401]67        nameTextBox.Clear();
68        descriptionTextBox.Clear();
69        ownerComboBox.SelectedItem = null;
70        createdTextBox.Clear();
71        startDateTimePicker.Value = DateTime.Now;
72        endDateTimePicker.Value = startDateTimePicker.Value;
73        indefiniteCheckBox.Checked = false;
74      } else {
[15576]75        idTextBox.Text = Content.Id.ToString();
[15401]76        nameTextBox.Text = Content.Name;
77        descriptionTextBox.Text = Content.Description;
[17312]78        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups?.OfType<LightweightUser>().SingleOrDefault(x => x.Id == Content.OwnerUserId);
[15401]79        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
80        startDateTimePicker.Value = Content.StartDate;
[17312]81        endDateTimePicker.Value = Content.EndDate.GetValueOrDefault(Content.StartDate);
[16117]82        indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
[15401]83      }
84    }
85
[17312]86    private async Tpl.Task UpdateUsersAsync() {
87      await Tpl.Task.Run(() => UpdateUsers());
88    }
89
90    private void UpdateUsers() {
91      // deregister handler to avoid change of content's owner when data source is updated
92      ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
93      try {
94        ownerComboBox.DataSource = null;
95        SecurityExceptionUtil.TryAndReportSecurityExceptions(AccessClient.Instance.Refresh);
96        ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().OrderBy(x => x.UserName).ToList();
97      } catch (AnonymousUserException) {
98        ShowHiveInformationDialog();
99      } finally {
100        ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
101      }
102    }
103
104    private void ShowHiveInformationDialog() {
105      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
106      else {
107        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
108          dialog.ShowDialog(this);
109        }
110      }
111    }
112
[15401]113    protected override void SetEnabledStateOfControls() {
114      base.SetEnabledStateOfControls();
[17312]115
[15760]116      bool enabled = Content != null && !Locked && !ReadOnly;
[17312]117
[15401]118      nameTextBox.Enabled = enabled;
[16430]119      descriptionTextBox.Enabled = enabled;
[15401]120      ownerComboBox.Enabled = enabled;
[17312]121      refreshButton.Enabled = enabled;
[15401]122      createdTextBox.Enabled = enabled;
123      startDateTimePicker.Enabled = enabled;
[17312]124      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue && Content.EndDate > Content.StartDate;
[15401]125      indefiniteCheckBox.Enabled = enabled;
[16117]126
[17312]127      if (Content == null) return;
[16117]128
[17312]129      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
130      if (parentProject != null && parentProject.EndDate.HasValue)
131        indefiniteCheckBox.Enabled = false;
132
133      if (Content.Id == Guid.Empty) return; // newly created project
134      if (HiveRoles.CheckAdminUserPermissions()) return; // admins can edit any project
135      if (HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)) return; // owner can edit project
136
137      // project was already created and user is neither admin nor owner
138      ownerComboBox.Enabled = false;
139      startDateTimePicker.Enabled = false;
140      endDateTimePicker.Enabled = false;
141      indefiniteCheckBox.Enabled = false;
[15401]142    }
[15412]143    #endregion
[15401]144
[15412]145    #region Event Handlers
[17312]146    private void ProjectView_Load(object sender, EventArgs e) {
147      Locked = true;
148      try {
149        UpdateUsers();
150        UpdateView();
151      } finally {
152        Locked = false;
[15401]153      }
154    }
155
[17312]156    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
157      UpdateView();
[15401]158    }
159
[17312]160    private void nameTextBox_TextChanged(object sender, EventArgs e) {
161      if (Content == null || Content.Name == nameTextBox.Text) return;
162      Content.Name = nameTextBox.Text;
[15401]163    }
164
165    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
[17312]166      if (!string.IsNullOrEmpty(nameTextBox.Text)) return;
[15401]167
[17312]168      MessageBox.Show("Project must have a name.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
169      e.Cancel = true;
[15401]170    }
171
[15422]172    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
[17312]173      if (Content == null || Content.Description == descriptionTextBox.Text) return;
174      Content.Description = descriptionTextBox.Text;
[15422]175    }
176
[15401]177    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
[17312]178      if (Content == null) return;
179
[15401]180      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
[15422]181      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
[17312]182      if (Content.OwnerUserId == selectedOwnerUserId) return;
183
184      Content.OwnerUserId = selectedOwnerUserId;
185    }
186
187    private async void refreshButton_Click(object sender, EventArgs e) {
188      Locked = true;
189      try {
190        await UpdateUsersAsync();
191        UpdateView();
192      } finally {
193        Locked = false;
[16117]194      }
[15401]195    }
196
[15422]197    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
[17312]198      if (Content == null || Content.StartDate == startDateTimePicker.Value) return;
[16117]199
[17312]200      string errorMessage = string.Empty;
201
202      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
203      if (parentProject != null) {
204        if (startDateTimePicker.Value < parentProject.StartDate) {
205          errorMessage = "Project cannot start before its parent project has started.";
206        } else if (startDateTimePicker.Value > parentProject.EndDate) {
207          errorMessage = "Project cannot start after its parent project has ended.";
[16117]208        }
209      }
210
[17312]211      if (startDateTimePicker.Value > endDateTimePicker.Value) {
212        errorMessage = "Project cannot start after it ends.";
[16117]213      }
214
[17312]215      if (!string.IsNullOrEmpty(errorMessage)) {
216        MessageBox.Show(errorMessage, "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
217        startDateTimePicker.Value = Content.StartDate;
218      }
219
220      Content.StartDate = startDateTimePicker.Value;
[15401]221    }
222
[15422]223    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
[17312]224      if (Content == null || Content.EndDate == endDateTimePicker.Value || Content.StartDate == endDateTimePicker.Value) return;
[16117]225
[17312]226      string errorMessage = string.Empty;
227
228      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
229      if (parentProject != null) {
230        if (endDateTimePicker.Value > parentProject.EndDate) {
231          errorMessage = "Project cannot end after its parent project has ended.";
232        } else if (endDateTimePicker.Value < parentProject.StartDate) {
233          errorMessage = "Project cannot end before its parent project has started.";
[16117]234        }
235      }
236
[17312]237      if (endDateTimePicker.Value < startDateTimePicker.Value) {
238        errorMessage = "Project cannot end after it starts.";
[16117]239      }
240
[17312]241      if (!string.IsNullOrEmpty(errorMessage)) {
242        MessageBox.Show(errorMessage, "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
243        endDateTimePicker.Value = Content.EndDate.GetValueOrDefault(Content.StartDate);
244      }
245
246      Content.EndDate = endDateTimePicker.Value;
[15401]247    }
248
249    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
[15422]250      if (Content == null) return;
[16117]251
252      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
[17312]253
254      if (Content.EndDate == newEndDate) return;
255      Content.EndDate = newEndDate;
256
[16117]257      endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
[15401]258    }
[15412]259
[17312]260    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
261      if (InvokeRequired) {
262        Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
263        return;
[15412]264      }
265
[17312]266      Progress.Show(this, "Refreshing ...", ProgressMode.Indeterminate);
267      SetEnabledStateOfControls();
[16117]268    }
269
[17312]270    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
271      if (InvokeRequired) {
272        Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
273        return;
[15412]274      }
[17312]275
276      Progress.Hide(this);
277      SetEnabledStateOfControls();
[15412]278    }
279    #endregion
[15401]280  }
281}
Note: See TracBrowser for help on using the repository browser.