Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2892_LR-prediction-intervals/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 16388

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

#2892: Merging r15750:16382 (HEAD) from trunk to branch, resolving conflicts

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