Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 16040

Last change on this file since 16040 was 16040, checked in by jzenisek, 6 years ago

#2839: fixed handling of project related updates for project owners, who are no admins and do not own parent projects

File size: 10.2 KB
Line 
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;
27using HeuristicLab.Clients.Hive.Views;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using System.Drawing;
31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = true)]
35  public partial class ProjectView : ItemView {
36    private readonly object locker = new object();
37
38    private Guid persistedOwnerUserId;
39
40    public new Project Content {
41      get { return (Project)base.Content; }
42      set { base.Content = value; persistedOwnerUserId = Content != null ? Content.OwnerUserId : Guid.Empty; }
43    }
44
45    public ProjectView() {
46      InitializeComponent();
47
48      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
49      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
50    }
51
52    #region Overrides
53    protected override void OnClosing(FormClosingEventArgs e) {
54      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
55      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
56      base.OnClosing(e);
57    }
58
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.PropertyChanged += Content_PropertyChanged;
62    }
63
64    protected override void DeregisterContentEvents() {
65      Content.PropertyChanged -= Content_PropertyChanged;
66      base.DeregisterContentEvents();
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        idTextBox.Clear();
73        nameTextBox.Clear();
74        descriptionTextBox.Clear();
75        ownerComboBox.SelectedItem = null;
76        createdTextBox.Clear();
77        startDateTimePicker.Value = DateTime.Now;
78        endDateTimePicker.Value = startDateTimePicker.Value;
79        indefiniteCheckBox.Checked = false;
80      } else {
81        idTextBox.Text = Content.Id.ToString();
82        nameTextBox.Text = Content.Name;
83        descriptionTextBox.Text = Content.Description;
84
85        ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
86        var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
87        if (!Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
88        ownerComboBox.DataSource = users.ToList();
89        ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == Content.OwnerUserId);
90        ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
91
92        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
93        startDateTimePicker.Value = Content.StartDate;
94
95        indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
96        if(!indefiniteCheckBox.Checked) endDateTimePicker.Value = Content.EndDate.Value;
97        else endDateTimePicker.Value = Content.StartDate;
98        endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
99      }
100    }
101
102    protected override void SetEnabledStateOfControls() {
103      base.SetEnabledStateOfControls();
104      bool enabled = Content != null && !Locked && !ReadOnly;
105      nameTextBox.Enabled = enabled;
106      descriptionTextBox.Enabled = enabled;
107      refreshButton.Enabled = enabled;
108      ownerComboBox.Enabled = enabled;
109      createdTextBox.Enabled = enabled;
110      startDateTimePicker.Enabled = enabled;
111      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
112      indefiniteCheckBox.Enabled = enabled;
113
114      if (!IsAdmin() && !HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)) {
115        ownerComboBox.Enabled = false;
116        startDateTimePicker.Enabled = false;
117        endDateTimePicker.Enabled = false;
118        indefiniteCheckBox.Enabled = false;
119      }
120    }
121    #endregion
122
123    #region Event Handlers
124    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
125      OnContentChanged();
126    }
127
128    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
129      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
130      else {
131        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
132        mainForm.AddOperationProgressToView(this, "Refreshing ...");
133        SetEnabledStateOfControls();
134      }
135    }
136
137    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
138      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
139      else {
140        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
141        mainForm.RemoveOperationProgressFromView(this);
142        SetEnabledStateOfControls();
143      }
144    }
145
146    private async void ProjectView_Load(object sender, EventArgs e) {
147      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
148        action: () => UpdateUsers(),
149        finallyCallback: () => {
150          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
151          var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
152          if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
153          ownerComboBox.DataSource = users.ToList();
154          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
155        });
156    }
157
158    private async void refreshButton_Click(object sender, EventArgs e) {
159      lock (locker) {
160        if (!refreshButton.Enabled) return;
161        refreshButton.Enabled = false;
162      }
163
164      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
165        action: () => UpdateUsers(),
166        finallyCallback: () => {
167          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
168          var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
169          if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
170          ownerComboBox.DataSource = users.ToList();
171          ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == persistedOwnerUserId);
172          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
173          refreshButton.Enabled = true;
174        });
175    }
176
177    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
178      if (string.IsNullOrEmpty(nameTextBox.Text)) {
179        MessageBox.Show(
180          "Project must have a name.",
181          "HeuristicLab Hive Administrator",
182          MessageBoxButtons.OK,
183          MessageBoxIcon.Error);
184        e.Cancel = true;
185      }
186    }
187
188    private void nameTextBox_TextChanged(object sender, EventArgs e) {
189      if (Content != null && Content.Name != nameTextBox.Text)
190        Content.Name = nameTextBox.Text;
191    }
192
193    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
194      if (Content != null && Content.Description != descriptionTextBox.Text)
195        Content.Description = descriptionTextBox.Text;
196    }
197
198    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
199      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
200      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
201      if (Content != null && Content.OwnerUserId != selectedOwnerUserId)
202        Content.OwnerUserId = selectedOwnerUserId;
203    }
204
205    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
206      if (Content == null) return;
207 
208      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
209        endDateTimePicker.Value = startDateTimePicker.Value;
210      if (Content.StartDate != startDateTimePicker.Value)
211        Content.StartDate = startDateTimePicker.Value;
212    }
213
214    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
215      if (Content == null) return;
216
217      if (endDateTimePicker.Value < startDateTimePicker.Value)
218        endDateTimePicker.Value = startDateTimePicker.Value;
219      if (Content.EndDate != endDateTimePicker.Value)
220        Content.EndDate = endDateTimePicker.Value;
221    }
222
223    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
224      if (Content == null) return;
225
226      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
227      endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
228      if (Content.EndDate != newEndDate) {
229        DeregisterContentEvents();
230        Content.EndDate = newEndDate;
231        RegisterContentEvents();
232      }
233    }
234    #endregion
235
236    #region Helpers
237    private void UpdateUsers() {
238      try {
239        AccessClient.Instance.Refresh();
240      } catch (AnonymousUserException) {
241        ShowHiveInformationDialog();
242      }
243    }
244
245    private bool IsAdmin() {
246      return HiveRoles.CheckAdminUserPermissions();
247    }
248
249    private void ShowHiveInformationDialog() {
250      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
251      else {
252        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
253          dialog.ShowDialog(this);
254        }
255      }
256    }
257    #endregion
258  }
259}
Note: See TracBrowser for help on using the repository browser.