Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3024: refactored ProjectView and added button to refresh users

File size: 10.3 KB
RevLine 
[15401]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15401]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;
[17268]30using Tpl = System.Threading.Tasks;
[15401]31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = true)]
35  public partial class ProjectView : ItemView {
36    public new Project Content {
37      get { return (Project)base.Content; }
[16446]38      set { base.Content = value; }
[15401]39    }
40
41    public ProjectView() {
42      InitializeComponent();
43
44      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
45      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
[17268]46
47      UpdateUsers();
[15401]48    }
49
[15412]50    #region Overrides
[15401]51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.PropertyChanged += Content_PropertyChanged;
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.PropertyChanged -= Content_PropertyChanged;
58      base.DeregisterContentEvents();
59    }
60
[17268]61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      UpdateView();
[16117]64    }
65
[17268]66    private void UpdateView() {
[15401]67      if (Content == null) {
[15576]68        idTextBox.Clear();
[15401]69        nameTextBox.Clear();
70        descriptionTextBox.Clear();
71        ownerComboBox.SelectedItem = null;
72        createdTextBox.Clear();
73        startDateTimePicker.Value = DateTime.Now;
74        endDateTimePicker.Value = startDateTimePicker.Value;
75        indefiniteCheckBox.Checked = false;
76      } else {
[15576]77        idTextBox.Text = Content.Id.ToString();
[15401]78        nameTextBox.Text = Content.Name;
79        descriptionTextBox.Text = Content.Description;
[17268]80        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().SingleOrDefault(x => x.Id == Content.OwnerUserId);
[15401]81        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
82        startDateTimePicker.Value = Content.StartDate;
[17268]83        endDateTimePicker.Value = Content.EndDate.GetValueOrDefault(Content.StartDate);
[16117]84        indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
[15401]85      }
86    }
87
[17268]88    private async Tpl.Task UpdateUsersAsync() {
89      await Tpl.Task.Run(UpdateUsers);
90    }
91
92    private void UpdateUsers() {
93      // deregister handler to avoid change of content's owner when data source is updated
94      ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
95      try {
96        ownerComboBox.DataSource = null;
97        SecurityExceptionUtil.TryAndReportSecurityExceptions(AccessClient.Instance.Refresh);
98        ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().OrderBy(x => x.UserName).ToList();
99      } catch (AnonymousUserException) {
100        ShowHiveInformationDialog();
101      } finally {
102        ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
103      }
104    }
105
106    private void ShowHiveInformationDialog() {
107      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
108      else {
109        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
110          dialog.ShowDialog(this);
111        }
112      }
113    }
114
[15401]115    protected override void SetEnabledStateOfControls() {
116      base.SetEnabledStateOfControls();
[17268]117
[15760]118      bool enabled = Content != null && !Locked && !ReadOnly;
[17268]119
[15401]120      nameTextBox.Enabled = enabled;
[16430]121      descriptionTextBox.Enabled = enabled;
[15401]122      ownerComboBox.Enabled = enabled;
[17268]123      refreshButton.Enabled = enabled;
[15401]124      createdTextBox.Enabled = enabled;
125      startDateTimePicker.Enabled = enabled;
[17268]126      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue && Content.EndDate > Content.StartDate;
[15401]127      indefiniteCheckBox.Enabled = enabled;
[16117]128
[17268]129      if (Content == null) return;
[16117]130
[17268]131      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
132      if (parentProject != null && parentProject.EndDate.HasValue)
133        indefiniteCheckBox.Enabled = false;
134
135      if (Content.Id == Guid.Empty) return; // newly created project
136      if (HiveRoles.CheckAdminUserPermissions()) return; // admins can edit any project
137      if (HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)) return; // owner can edit project
138
139      // project was already created and user is neither admin nor owner
140      ownerComboBox.Enabled = false;
141      startDateTimePicker.Enabled = false;
142      endDateTimePicker.Enabled = false;
143      indefiniteCheckBox.Enabled = false;
[15401]144    }
[15412]145    #endregion
[15401]146
[15412]147    #region Event Handlers
148    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
[17268]149      UpdateView();
[15412]150    }
151
[17268]152    private void nameTextBox_TextChanged(object sender, EventArgs e) {
153      if (Content == null || Content.Name == nameTextBox.Text) return;
154      Content.Name = nameTextBox.Text;
[15401]155    }
156
157    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
[17268]158      if (!string.IsNullOrEmpty(nameTextBox.Text)) return;
[15401]159
[17268]160      MessageBox.Show("Project must have a name.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
161      e.Cancel = true;
[15401]162    }
163
[15422]164    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
[17268]165      if (Content == null || Content.Description == descriptionTextBox.Text) return;
166      Content.Description = descriptionTextBox.Text;
[15422]167    }
168
[15401]169    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
[17268]170      if (Content == null) return;
171
[15401]172      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
[15422]173      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
[17268]174      if (Content.OwnerUserId == selectedOwnerUserId) return;
175
176      Content.OwnerUserId = selectedOwnerUserId;
[15401]177    }
178
[15422]179    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
[17268]180      if (Content == null || Content.StartDate == startDateTimePicker.Value) return;
[16117]181
[17268]182      string errorMessage = string.Empty;
183
184      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
185      if (parentProject != null) {
186        if (startDateTimePicker.Value < parentProject.StartDate) {
187          errorMessage = "Project cannot start before its parent project has started.";
188        } else if (startDateTimePicker.Value > parentProject.EndDate) {
189          errorMessage = "Project cannot start after its parent project has ended.";
[16117]190        }
191      }
192
[17268]193      if (startDateTimePicker.Value > endDateTimePicker.Value) {
194        errorMessage = "Project cannot start after it ends.";
[16117]195      }
196
[17268]197      if (!string.IsNullOrEmpty(errorMessage)) {
198        MessageBox.Show(errorMessage, "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
199        startDateTimePicker.Value = Content.StartDate;
200      }
201
202      Content.StartDate = startDateTimePicker.Value;
[15401]203    }
204
[15422]205    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
[17268]206      if (Content == null || Content.EndDate == endDateTimePicker.Value || Content.StartDate == endDateTimePicker.Value) return;
[16117]207
[17268]208      string errorMessage = string.Empty;
209
210      var parentProject = HiveAdminClient.Instance.Projects.SingleOrDefault(x => x.Id == Content.ParentProjectId);
211      if (parentProject != null) {
212        if (endDateTimePicker.Value > parentProject.EndDate) {
213          errorMessage = "Project cannot end after its parent project has ended.";
214        } else if (endDateTimePicker.Value < parentProject.StartDate) {
215          errorMessage = "Project cannot end before its parent project has started.";
[16117]216        }
217      }
218
[17268]219      if (endDateTimePicker.Value < startDateTimePicker.Value) {
220        errorMessage = "Project cannot end after it starts.";
[16117]221      }
222
[17268]223      if (!string.IsNullOrEmpty(errorMessage)) {
224        MessageBox.Show(errorMessage, "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
225        endDateTimePicker.Value = Content.EndDate.GetValueOrDefault(Content.StartDate);
226      }
227
228      Content.EndDate = endDateTimePicker.Value;
[15401]229    }
230
231    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
[15422]232      if (Content == null) return;
[16117]233
234      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
[17268]235
236      if (Content.EndDate == newEndDate) return;
237      Content.EndDate = newEndDate;
238
[16117]239      endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
[15401]240    }
[15412]241
[17268]242    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
243      if (InvokeRequired) {
244        Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
245        return;
[15412]246      }
[17268]247
248      Progress.Show(this, "Refreshing ...", ProgressMode.Indeterminate);
249      SetEnabledStateOfControls();
[15412]250    }
251
[17268]252    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
253      if (InvokeRequired) {
254        Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
255        return;
256      }
257
258      Progress.Hide(this);
259      SetEnabledStateOfControls();
[16117]260    }
[17268]261    #endregion
[16117]262
[17268]263    private async void refreshButton_Click(object sender, EventArgs e) {
264      Locked = true;
265      try {
266        await UpdateUsersAsync();
267        UpdateView();
268      } finally {
269        Locked = false;
[15412]270      }
271    }
[15401]272  }
273}
Note: See TracBrowser for help on using the repository browser.