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 |
|
---|
22 | using System;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Clients.Access;
|
---|
27 | using HeuristicLab.Clients.Hive.Views;
|
---|
28 | using HeuristicLab.Core.Views;
|
---|
29 | using HeuristicLab.MainForm;
|
---|
30 | using System.Drawing;
|
---|
31 |
|
---|
32 | namespace 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 |
|
---|
85 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
86 | var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
|
---|
87 | if (!Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
|
---|
88 | ownerComboBox.DataSource = users.ToList();
|
---|
89 | ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == Content.OwnerUserId);
|
---|
90 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
91 |
|
---|
92 | createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
|
---|
93 | startDateTimePicker.Value = Content.StartDate;
|
---|
94 |
|
---|
95 | indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
|
---|
96 | if(!indefiniteCheckBox.Checked) endDateTimePicker.Value = Content.EndDate.Value;
|
---|
97 | else endDateTimePicker.Value = Content.StartDate;
|
---|
98 | endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected override void SetEnabledStateOfControls() {
|
---|
103 | base.SetEnabledStateOfControls();
|
---|
104 | bool enabled = Content != null && !Locked && !ReadOnly;
|
---|
105 | nameTextBox.Enabled = enabled;
|
---|
106 | descriptionTextBox.Enabled = enabled;
|
---|
107 | refreshButton.Enabled = enabled;
|
---|
108 | ownerComboBox.Enabled = enabled;
|
---|
109 | createdTextBox.Enabled = enabled;
|
---|
110 | startDateTimePicker.Enabled = enabled;
|
---|
111 | endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
|
---|
112 | indefiniteCheckBox.Enabled = enabled;
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | #region Event Handlers
|
---|
117 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
118 | OnContentChanged();
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
122 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
|
---|
123 | else {
|
---|
124 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
125 | mainForm.AddOperationProgressToView(this, "Refreshing ...");
|
---|
126 | SetEnabledStateOfControls();
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
131 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
|
---|
132 | else {
|
---|
133 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
134 | mainForm.RemoveOperationProgressFromView(this);
|
---|
135 | SetEnabledStateOfControls();
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | private async void ProjectView_Load(object sender, EventArgs e) {
|
---|
140 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
141 | action: () => UpdateUsers(),
|
---|
142 | finallyCallback: () => {
|
---|
143 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
144 | var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
|
---|
145 | if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
|
---|
146 | ownerComboBox.DataSource = users.ToList();
|
---|
147 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
148 | });
|
---|
149 | }
|
---|
150 |
|
---|
151 | private async void refreshButton_Click(object sender, EventArgs e) {
|
---|
152 | lock (locker) {
|
---|
153 | if (!refreshButton.Enabled) return;
|
---|
154 | refreshButton.Enabled = false;
|
---|
155 | }
|
---|
156 |
|
---|
157 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
158 | action: () => UpdateUsers(),
|
---|
159 | finallyCallback: () => {
|
---|
160 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
161 | var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
|
---|
162 | if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
|
---|
163 | ownerComboBox.DataSource = users.ToList();
|
---|
164 | ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == persistedOwnerUserId);
|
---|
165 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
166 | refreshButton.Enabled = true;
|
---|
167 | });
|
---|
168 | }
|
---|
169 |
|
---|
170 | private void nameTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
171 | if (string.IsNullOrEmpty(nameTextBox.Text)) {
|
---|
172 | MessageBox.Show(
|
---|
173 | "Project must have a name.",
|
---|
174 | "HeuristicLab Hive Administrator",
|
---|
175 | MessageBoxButtons.OK,
|
---|
176 | MessageBoxIcon.Error);
|
---|
177 | e.Cancel = true;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | private void nameTextBox_TextChanged(object sender, EventArgs e) {
|
---|
182 | if (Content != null && Content.Name != nameTextBox.Text)
|
---|
183 | Content.Name = nameTextBox.Text;
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
|
---|
187 | if (Content != null && Content.Description != descriptionTextBox.Text)
|
---|
188 | Content.Description = descriptionTextBox.Text;
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
192 | var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
|
---|
193 | var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
|
---|
194 | if (Content != null && Content.OwnerUserId != selectedOwnerUserId)
|
---|
195 | Content.OwnerUserId = selectedOwnerUserId;
|
---|
196 | }
|
---|
197 |
|
---|
198 | private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
|
---|
199 | if (Content == null) return;
|
---|
200 | if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
|
---|
201 | endDateTimePicker.Value = startDateTimePicker.Value;
|
---|
202 | if (Content.StartDate != startDateTimePicker.Value)
|
---|
203 | Content.StartDate = startDateTimePicker.Value;
|
---|
204 | }
|
---|
205 |
|
---|
206 | private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
|
---|
207 | if (Content == null) return;
|
---|
208 | if (endDateTimePicker.Value < startDateTimePicker.Value)
|
---|
209 | endDateTimePicker.Value = startDateTimePicker.Value;
|
---|
210 | if (Content.EndDate != endDateTimePicker.Value)
|
---|
211 | Content.EndDate = endDateTimePicker.Value;
|
---|
212 | }
|
---|
213 |
|
---|
214 | private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
215 | if (Content == null) return;
|
---|
216 | var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
|
---|
217 | endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
|
---|
218 | if (Content.EndDate != newEndDate) {
|
---|
219 | DeregisterContentEvents();
|
---|
220 | Content.EndDate = newEndDate;
|
---|
221 | RegisterContentEvents();
|
---|
222 | }
|
---|
223 | }
|
---|
224 | #endregion
|
---|
225 |
|
---|
226 | #region Helpers
|
---|
227 | private void UpdateUsers() {
|
---|
228 | try {
|
---|
229 | AccessClient.Instance.Refresh();
|
---|
230 | } catch (AnonymousUserException) {
|
---|
231 | ShowHiveInformationDialog();
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void ShowHiveInformationDialog() {
|
---|
236 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
237 | else {
|
---|
238 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
239 | dialog.ShowDialog(this);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | #endregion
|
---|
244 | }
|
---|
245 | }
|
---|