Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 16446

Last change on this file since 16446 was 16446, checked in by jkarder, 5 years ago

#2839: worked on hive project management

  • improved event handler (de-)registration
  • refactored content updates
File size: 12.4 KB
RevLine 
[15401]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.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;
30
31namespace HeuristicLab.Clients.Hive.Administrator.Views {
32  [View("ProjectView")]
33  [Content(typeof(Project), IsDefaultView = true)]
34  public partial class ProjectView : ItemView {
35    private readonly object locker = new object();
36
37    public new Project Content {
38      get { return (Project)base.Content; }
[16446]39      set { base.Content = value; }
[15401]40    }
41
42    public ProjectView() {
43      InitializeComponent();
44
45      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
46      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
47    }
48
[15412]49    #region Overrides
[15401]50    protected override void RegisterContentEvents() {
51      base.RegisterContentEvents();
52      Content.PropertyChanged += Content_PropertyChanged;
53    }
54
55    protected override void DeregisterContentEvents() {
56      Content.PropertyChanged -= Content_PropertyChanged;
57      base.DeregisterContentEvents();
58    }
59
[16117]60    protected void RegisterControlEvents() {
61      nameTextBox.TextChanged += nameTextBox_TextChanged;
62      nameTextBox.Validating += nameTextBox_Validating;
63      descriptionTextBox.TextChanged += descriptionTextBox_TextChanged;
64      ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
65      startDateTimePicker.ValueChanged += startDateTimePicker_ValueChanged;
66      endDateTimePicker.ValueChanged += endDateTimePicker_ValueChanged;
67      indefiniteCheckBox.CheckedChanged += indefiniteCheckBox_CheckedChanged;
68    }
69
70    protected void DeregisterControlEvents() {
71      nameTextBox.TextChanged -= nameTextBox_TextChanged;
72      nameTextBox.Validating -= nameTextBox_Validating;
73      descriptionTextBox.TextChanged -= descriptionTextBox_TextChanged;
74      ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
75      startDateTimePicker.ValueChanged -= startDateTimePicker_ValueChanged;
76      endDateTimePicker.ValueChanged -= endDateTimePicker_ValueChanged;
77      indefiniteCheckBox.CheckedChanged -= indefiniteCheckBox_CheckedChanged;
78    }
79
[15401]80    protected override void OnContentChanged() {
81      base.OnContentChanged();
[16117]82      DeregisterControlEvents();
[15401]83      if (Content == null) {
[15576]84        idTextBox.Clear();
[15401]85        nameTextBox.Clear();
86        descriptionTextBox.Clear();
87        ownerComboBox.SelectedItem = null;
88        createdTextBox.Clear();
89        startDateTimePicker.Value = DateTime.Now;
90        endDateTimePicker.Value = startDateTimePicker.Value;
91        indefiniteCheckBox.Checked = false;
92      } else {
[15576]93        idTextBox.Text = Content.Id.ToString();
[15401]94        nameTextBox.Text = Content.Name;
95        descriptionTextBox.Text = Content.Description;
[16117]96
97        ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
98        var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
99        if (!Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
[16446]100        var projectOwnerId = Content.OwnerUserId;
101        ownerComboBox.DataSource = users.OrderBy(x => x.UserName).ToList();
102        ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == projectOwnerId);
[16117]103        ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
104
[15401]105        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
106        startDateTimePicker.Value = Content.StartDate;
[16117]107
108        indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
109        if (!indefiniteCheckBox.Checked) endDateTimePicker.Value = Content.EndDate.Value;
110        else endDateTimePicker.Value = Content.StartDate;
111        endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
[15401]112      }
[16202]113      SetEnabledStateOfControls();
[16117]114      RegisterControlEvents();
[15401]115    }
116
117    protected override void SetEnabledStateOfControls() {
118      base.SetEnabledStateOfControls();
[15760]119      bool enabled = Content != null && !Locked && !ReadOnly;
[15401]120      nameTextBox.Enabled = enabled;
[16430]121      descriptionTextBox.Enabled = enabled;
[15401]122      ownerComboBox.Enabled = enabled;
123      createdTextBox.Enabled = enabled;
124      startDateTimePicker.Enabled = enabled;
125      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
126      indefiniteCheckBox.Enabled = enabled;
[16117]127
128      if (Content != null) {
[16185]129        //var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
130        var parentProject = HiveAdminClient.Instance.Projects.FirstOrDefault(x => x.Id == Content.ParentProjectId);
131        if ((!IsAdmin() && (parentProject == null || parentProject.EndDate.HasValue))
132           || (IsAdmin() && parentProject != null && parentProject.EndDate.HasValue)) {
[16117]133          indefiniteCheckBox.Enabled = false;
134        }
135
[16202]136        if (Content.Id != Guid.Empty && !IsAdmin() && !HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)) {
[16117]137          ownerComboBox.Enabled = false;
138          startDateTimePicker.Enabled = false;
139          endDateTimePicker.Enabled = false;
140          indefiniteCheckBox.Enabled = false;
141        }
142      }
[15401]143    }
[15412]144    #endregion
[15401]145
[15412]146    #region Event Handlers
147    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
[16117]148      if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
149      else OnContentChanged();
[15412]150    }
151
[15401]152    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
153      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
154      else {
[16430]155        Progress.Show(this, "Refreshing ...", ProgressMode.Indeterminate);
[15401]156        SetEnabledStateOfControls();
157      }
158    }
159
160    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
161      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
162      else {
[16430]163        Progress.Hide(this);
[15401]164        SetEnabledStateOfControls();
165      }
166    }
167
168    private async void ProjectView_Load(object sender, EventArgs e) {
[15412]169      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
170        action: () => UpdateUsers(),
171        finallyCallback: () => {
172          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
[16117]173          var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
174          if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
[16446]175          ownerComboBox.DataSource = users.OrderBy(x => x.UserName).ToList();
[15412]176          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
177        });
[15401]178    }
179
[16446]180    private void ProjectView_Disposed(object sender, EventArgs e) {
181      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
182      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
183    }
184
[15401]185    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
186      if (string.IsNullOrEmpty(nameTextBox.Text)) {
187        MessageBox.Show(
188          "Project must have a name.",
189          "HeuristicLab Hive Administrator",
190          MessageBoxButtons.OK,
191          MessageBoxIcon.Error);
192        e.Cancel = true;
[15422]193      }
[15401]194    }
195
[15422]196    private void nameTextBox_TextChanged(object sender, EventArgs e) {
[16117]197      if (Content != null && Content.Name != nameTextBox.Text) {
198        DeregisterContentEvents();
[15422]199        Content.Name = nameTextBox.Text;
[16117]200        RegisterContentEvents();
201      }
[15401]202    }
203
[15422]204    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
[16117]205      if (Content != null && Content.Description != descriptionTextBox.Text) {
206        DeregisterContentEvents();
[15422]207        Content.Description = descriptionTextBox.Text;
[16117]208        RegisterContentEvents();
209      }
[15422]210    }
211
[15401]212    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
213      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
[15422]214      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
[16117]215      if (Content != null && Content.OwnerUserId != selectedOwnerUserId) {
216        DeregisterContentEvents();
[15422]217        Content.OwnerUserId = selectedOwnerUserId;
[16117]218        RegisterContentEvents();
219      }
[15401]220    }
221
[15422]222    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
223      if (Content == null) return;
[16117]224      startDateTimePicker.ValueChanged -= startDateTimePicker_ValueChanged;
225
226      if (!IsAdmin()) {
227        var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
228        if (parentProject != null) {
229          if (startDateTimePicker.Value < parentProject.StartDate)
230            startDateTimePicker.Value = parentProject.StartDate;
231        } else {
232          startDateTimePicker.Value = Content.StartDate;
233        }
234      }
235
[15401]236      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
237        endDateTimePicker.Value = startDateTimePicker.Value;
[16117]238      if (Content.StartDate != startDateTimePicker.Value) {
239        DeregisterContentEvents();
[15422]240        Content.StartDate = startDateTimePicker.Value;
[16117]241        RegisterContentEvents();
242      }
243
244      startDateTimePicker.ValueChanged += startDateTimePicker_ValueChanged;
[15401]245    }
246
[15422]247    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
248      if (Content == null) return;
[16117]249      endDateTimePicker.ValueChanged -= endDateTimePicker_ValueChanged;
250
251      if (!IsAdmin()) {
252        var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
253        if (parentProject != null) {
254          if (parentProject.EndDate.HasValue && endDateTimePicker.Value > parentProject.EndDate.Value) {
255            endDateTimePicker.Value = parentProject.EndDate.Value;
256          }
257        } else if (Content.EndDate.HasValue) {
258          endDateTimePicker.Value = Content.EndDate.Value;
259        }
260      }
261
[15401]262      if (endDateTimePicker.Value < startDateTimePicker.Value)
263        endDateTimePicker.Value = startDateTimePicker.Value;
[16117]264      if (Content.EndDate != endDateTimePicker.Value) {
265        DeregisterContentEvents();
[15422]266        Content.EndDate = endDateTimePicker.Value;
[16117]267        RegisterContentEvents();
268      }
269
270      endDateTimePicker.ValueChanged += endDateTimePicker_ValueChanged;
[15401]271    }
272
273    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
[15422]274      if (Content == null) return;
[16117]275
276      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
277      endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
278      if (Content.EndDate != newEndDate) {
279        DeregisterContentEvents();
[15422]280        Content.EndDate = newEndDate;
[16117]281        RegisterContentEvents();
282      }
[15401]283    }
[15412]284    #endregion
285
286    #region Helpers
287    private void UpdateUsers() {
288      try {
289        AccessClient.Instance.Refresh();
290      } catch (AnonymousUserException) {
291        ShowHiveInformationDialog();
292      }
293    }
294
[16117]295    private bool IsAdmin() {
296      return HiveRoles.CheckAdminUserPermissions();
297    }
298
[15412]299    private void ShowHiveInformationDialog() {
300      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
301      else {
302        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
303          dialog.ShowDialog(this);
304        }
305      }
306    }
307    #endregion
[15401]308  }
309}
Note: See TracBrowser for help on using the repository browser.