Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2839: Fixed several bugs and added project duration handling

  • fixed project duration handling (client- & service-side)
  • fixed tagging in HiveJobAdmin
  • added ProjectJobs view (under construction)
  • added necessary service methods
File size: 9.2 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;
30using System.Drawing;
31
32namespace HeuristicLab.Clients.Hive.Administrator.Views {
33  [View("ProjectView")]
34  [Content(typeof(Project), IsDefaultView = true)]
35  public partial class ProjectView : ItemView {
36    private readonly object locker = new object();
37
38    private Guid persistedOwnerUserId;
39
40    public new Project Content {
41      get { return (Project)base.Content; }
42      set { base.Content = value; persistedOwnerUserId = Content != null ? Content.OwnerUserId : Guid.Empty; }
43    }
44
45    public ProjectView() {
46      InitializeComponent();
47
48      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
49      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
50    }
51
52    #region Overrides
53    protected override void OnClosing(FormClosingEventArgs e) {
54      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
55      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
56      base.OnClosing(e);
57    }
58
59    protected override void RegisterContentEvents() {
60      base.RegisterContentEvents();
61      Content.PropertyChanged += Content_PropertyChanged;
62    }
63
64    protected override void DeregisterContentEvents() {
65      Content.PropertyChanged -= Content_PropertyChanged;
66      base.DeregisterContentEvents();
67    }
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        idTextBox.Clear();
73        nameTextBox.Clear();
74        descriptionTextBox.Clear();
75        ownerComboBox.SelectedItem = null;
76        createdTextBox.Clear();
77        startDateTimePicker.Value = DateTime.Now;
78        endDateTimePicker.Value = startDateTimePicker.Value;
79        indefiniteCheckBox.Checked = false;
80      } else {
81        idTextBox.Text = Content.Id.ToString();
82        nameTextBox.Text = Content.Name;
83        descriptionTextBox.Text = Content.Description;
84        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
85        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
86        startDateTimePicker.Value = Content.StartDate;
87
88        indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
89        if(!indefiniteCheckBox.Checked) endDateTimePicker.Value = Content.EndDate.Value;
90        else endDateTimePicker.Value = Content.StartDate;
91        endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
92      }
93    }
94
95    protected override void SetEnabledStateOfControls() {
96      base.SetEnabledStateOfControls();
97      bool enabled = Content != null && !Locked && !ReadOnly;
98      nameTextBox.Enabled = enabled;
99      descriptionTextBox.Enabled = enabled;
100      refreshButton.Enabled = enabled;
101      ownerComboBox.Enabled = enabled;
102      createdTextBox.Enabled = enabled;
103      startDateTimePicker.Enabled = enabled;
104      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
105      indefiniteCheckBox.Enabled = enabled;
106    }
107    #endregion
108
109    #region Event Handlers
110    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
111      OnContentChanged();
112    }
113
114    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
115      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
116      else {
117        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
118        mainForm.AddOperationProgressToView(this, "Refreshing ...");
119        SetEnabledStateOfControls();
120      }
121    }
122
123    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
124      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
125      else {
126        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
127        mainForm.RemoveOperationProgressFromView(this);
128        SetEnabledStateOfControls();
129      }
130    }
131
132    private async void ProjectView_Load(object sender, EventArgs e) {
133      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
134        action: () => UpdateUsers(),
135        finallyCallback: () => {
136          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
137          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
138          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
139        });
140    }
141
142    private async void refreshButton_Click(object sender, EventArgs e) {
143      lock (locker) {
144        if (!refreshButton.Enabled) return;
145        refreshButton.Enabled = false;
146      }
147
148      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
149        action: () => UpdateUsers(),
150        finallyCallback: () => {
151          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
152          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
153          ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == persistedOwnerUserId);
154          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
155          refreshButton.Enabled = true;
156        });
157    }
158
159    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
160      if (string.IsNullOrEmpty(nameTextBox.Text)) {
161        MessageBox.Show(
162          "Project must have a name.",
163          "HeuristicLab Hive Administrator",
164          MessageBoxButtons.OK,
165          MessageBoxIcon.Error);
166        e.Cancel = true;
167      }
168    }
169
170    private void nameTextBox_TextChanged(object sender, EventArgs e) {
171      if (Content != null && Content.Name != nameTextBox.Text)
172        Content.Name = nameTextBox.Text;
173    }
174
175    private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
176      if (Content != null && Content.Description != descriptionTextBox.Text)
177        Content.Description = descriptionTextBox.Text;
178    }
179
180    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
181      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
182      var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
183      if (Content != null && Content.OwnerUserId != selectedOwnerUserId)
184        Content.OwnerUserId = selectedOwnerUserId;
185    }
186
187    private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
188      if (Content == null) return;
189      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
190        endDateTimePicker.Value = startDateTimePicker.Value;
191      if (Content.StartDate != startDateTimePicker.Value)
192        Content.StartDate = startDateTimePicker.Value;
193    }
194
195    private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
196      if (Content == null) return;
197      if (endDateTimePicker.Value < startDateTimePicker.Value)
198        endDateTimePicker.Value = startDateTimePicker.Value;
199      if (Content.EndDate != endDateTimePicker.Value)
200        Content.EndDate = endDateTimePicker.Value;
201    }
202
203    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
204      if (Content == null) return;
205      var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : Content.StartDate;
206      endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
207      if (Content.EndDate != newEndDate) {
208        DeregisterContentEvents();
209        Content.EndDate = newEndDate;
210        endDateTimePicker.Value = newEndDate.HasValue ? newEndDate.Value : Content.StartDate;
211        RegisterContentEvents();
212      }
213    }
214    #endregion
215
216    #region Helpers
217    private void UpdateUsers() {
218      try {
219        AccessClient.Instance.Refresh();
220      } catch (AnonymousUserException) {
221        ShowHiveInformationDialog();
222      }
223    }
224
225    private void ShowHiveInformationDialog() {
226      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
227      else {
228        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
229          dialog.ShowDialog(this);
230        }
231      }
232    }
233    #endregion
234  }
235}
Note: See TracBrowser for help on using the repository browser.