Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ProjectView.cs @ 15412

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

#2839:

  • worked on resources and projects views
  • changed resource selector to be able to select projects and assigned resources
  • updated service clients
File size: 8.0 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;
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    private bool updatingUsers = false;
37
38    public new Project Content {
39      get { return (Project)base.Content; }
40      set { base.Content = value; }
41    }
42
43    public ProjectView() {
44      InitializeComponent();
45
46      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
47      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
48    }
49
50    #region Overrides
51    protected override void OnClosing(FormClosingEventArgs e) {
52      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
53      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
54      base.OnClosing(e);
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.PropertyChanged += Content_PropertyChanged;
60    }
61
62    protected override void DeregisterContentEvents() {
63      Content.PropertyChanged -= Content_PropertyChanged;
64      base.DeregisterContentEvents();
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null) {
70        nameTextBox.Clear();
71        descriptionTextBox.Clear();
72        ownerComboBox.SelectedItem = null;
73        createdTextBox.Clear();
74        startDateTimePicker.Value = DateTime.Now;
75        endDateTimePicker.Value = startDateTimePicker.Value;
76        indefiniteCheckBox.Checked = false;
77      } else {
78        nameTextBox.Text = Content.Name;
79        descriptionTextBox.Text = Content.Description;
80        ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
81        createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
82        startDateTimePicker.Value = Content.StartDate;
83        if (indefiniteCheckBox.Checked = !Content.EndDate.HasValue) {
84          endDateTimePicker.Value = startDateTimePicker.Value;
85        } else {
86          endDateTimePicker.Value = Content.EndDate.Value;
87        }
88      }
89    }
90
91    protected override void SetEnabledStateOfControls() {
92      base.SetEnabledStateOfControls();
93      bool enabled = Content != null;
94      nameTextBox.Enabled = enabled;
95      descriptionTextBox.Enabled = enabled;
96      refreshButton.Enabled = enabled;
97      ownerComboBox.Enabled = enabled;
98      createdTextBox.Enabled = enabled;
99      startDateTimePicker.Enabled = enabled;
100      endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
101      indefiniteCheckBox.Enabled = enabled;
102    }
103    #endregion
104
105    #region Event Handlers
106    private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
107      OnContentChanged();
108    }
109
110    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
111      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
112      else {
113        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
114        mainForm.AddOperationProgressToView(this, "Refreshing ...");
115        SetEnabledStateOfControls();
116      }
117    }
118
119    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
120      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
121      else {
122        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
123        mainForm.RemoveOperationProgressFromView(this);
124        SetEnabledStateOfControls();
125      }
126    }
127
128    private async void ProjectView_Load(object sender, EventArgs e) {
129      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
130        action: () => UpdateUsers(),
131        finallyCallback: () => {
132          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
133          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
134          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
135        });
136    }
137
138    private async void refreshButton_Click(object sender, EventArgs e) {
139      lock (locker) {
140        if (updatingUsers) return;
141        updatingUsers = true;
142      }
143
144      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
145        action: () => UpdateUsers(),
146        finallyCallback: () => {
147          ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
148          ownerComboBox.DataSource = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>().ToList();
149          ownerComboBox.SelectedItem = AccessClient.Instance.UsersAndGroups.FirstOrDefault(x => x.Id == Content.OwnerUserId);
150          ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
151          updatingUsers = false;
152        });
153    }
154
155    private void nameTextBox_Validating(object sender, CancelEventArgs e) {
156      if (string.IsNullOrEmpty(nameTextBox.Text)) {
157        MessageBox.Show(
158          "Project must have a name.",
159          "HeuristicLab Hive Administrator",
160          MessageBoxButtons.OK,
161          MessageBoxIcon.Error);
162        e.Cancel = true;
163      } else Content.Name = nameTextBox.Text;
164    }
165
166    private void descriptionTextBox_Validating(object sender, CancelEventArgs e) {
167      Content.Description = descriptionTextBox.Text;
168    }
169
170    private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
171      var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
172      Content.OwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
173    }
174
175    private void startDateTimePicker_Validating(object sender, CancelEventArgs e) {
176      if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
177        endDateTimePicker.Value = startDateTimePicker.Value;
178      Content.StartDate = startDateTimePicker.Value;
179    }
180
181    private void endDateTimePicker_Validating(object sender, CancelEventArgs e) {
182      if (endDateTimePicker.Value < startDateTimePicker.Value)
183        endDateTimePicker.Value = startDateTimePicker.Value;
184      Content.EndDate = endDateTimePicker.Value;
185    }
186
187    private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
188      Content.EndDate = indefiniteCheckBox.Checked ? (DateTime?)null : Content.StartDate;
189    }
190    #endregion
191
192    #region Helpers
193    private void UpdateUsers() {
194      try {
195        AccessClient.Instance.Refresh();
196      } catch (AnonymousUserException) {
197        ShowHiveInformationDialog();
198      }
199    }
200
201    private void ShowHiveInformationDialog() {
202      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
203      else {
204        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
205          dialog.ShowDialog(this);
206        }
207      }
208    }
209    #endregion
210  }
211}
Note: See TracBrowser for help on using the repository browser.