Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs

Last change on this file was 17333, checked in by mkommend, 5 years ago

#2521: Updated branch with most recent trunk changes.

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