Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16095 was 16060, checked in by jkarder, 6 years ago

#2839: worked on hive project management

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