Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839:

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