[15401] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.ComponentModel;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Clients.Access;
|
---|
[15412] | 27 | using HeuristicLab.Clients.Hive.Views;
|
---|
[15401] | 28 | using HeuristicLab.Core.Views;
|
---|
| 29 | using HeuristicLab.MainForm;
|
---|
| 30 |
|
---|
| 31 | namespace 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 |
|
---|
| 37 | public new Project Content {
|
---|
| 38 | get { return (Project)base.Content; }
|
---|
[17063] | 39 | set { base.Content = value; }
|
---|
[15401] | 40 | }
|
---|
| 41 |
|
---|
| 42 | public ProjectView() {
|
---|
| 43 | InitializeComponent();
|
---|
| 44 |
|
---|
| 45 | AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
|
---|
| 46 | AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[15412] | 49 | #region Overrides
|
---|
[15401] | 50 | protected override void RegisterContentEvents() {
|
---|
| 51 | base.RegisterContentEvents();
|
---|
| 52 | Content.PropertyChanged += Content_PropertyChanged;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | protected override void DeregisterContentEvents() {
|
---|
| 56 | Content.PropertyChanged -= Content_PropertyChanged;
|
---|
| 57 | base.DeregisterContentEvents();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[16117] | 60 | protected void RegisterControlEvents() {
|
---|
| 61 | nameTextBox.TextChanged += nameTextBox_TextChanged;
|
---|
| 62 | nameTextBox.Validating += nameTextBox_Validating;
|
---|
| 63 | descriptionTextBox.TextChanged += descriptionTextBox_TextChanged;
|
---|
| 64 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
| 65 | startDateTimePicker.ValueChanged += startDateTimePicker_ValueChanged;
|
---|
| 66 | endDateTimePicker.ValueChanged += endDateTimePicker_ValueChanged;
|
---|
| 67 | indefiniteCheckBox.CheckedChanged += indefiniteCheckBox_CheckedChanged;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | protected void DeregisterControlEvents() {
|
---|
| 71 | nameTextBox.TextChanged -= nameTextBox_TextChanged;
|
---|
| 72 | nameTextBox.Validating -= nameTextBox_Validating;
|
---|
| 73 | descriptionTextBox.TextChanged -= descriptionTextBox_TextChanged;
|
---|
| 74 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
| 75 | startDateTimePicker.ValueChanged -= startDateTimePicker_ValueChanged;
|
---|
| 76 | endDateTimePicker.ValueChanged -= endDateTimePicker_ValueChanged;
|
---|
| 77 | indefiniteCheckBox.CheckedChanged -= indefiniteCheckBox_CheckedChanged;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[15401] | 80 | protected override void OnContentChanged() {
|
---|
| 81 | base.OnContentChanged();
|
---|
[16117] | 82 | DeregisterControlEvents();
|
---|
[15401] | 83 | if (Content == null) {
|
---|
[15576] | 84 | idTextBox.Clear();
|
---|
[15401] | 85 | nameTextBox.Clear();
|
---|
| 86 | descriptionTextBox.Clear();
|
---|
| 87 | ownerComboBox.SelectedItem = null;
|
---|
| 88 | createdTextBox.Clear();
|
---|
| 89 | startDateTimePicker.Value = DateTime.Now;
|
---|
| 90 | endDateTimePicker.Value = startDateTimePicker.Value;
|
---|
| 91 | indefiniteCheckBox.Checked = false;
|
---|
| 92 | } else {
|
---|
[15576] | 93 | idTextBox.Text = Content.Id.ToString();
|
---|
[15401] | 94 | nameTextBox.Text = Content.Name;
|
---|
| 95 | descriptionTextBox.Text = Content.Description;
|
---|
[16117] | 96 |
|
---|
| 97 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
[17065] | 98 | if (AccessClient.Instance.UsersAndGroups != null) {
|
---|
| 99 | var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
|
---|
| 100 | if (!Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
|
---|
| 101 | var projectOwnerId = Content.OwnerUserId;
|
---|
| 102 | ownerComboBox.DataSource = users.OrderBy(x => x.UserName).ToList();
|
---|
| 103 | ownerComboBox.SelectedItem = users.FirstOrDefault(x => x.Id == projectOwnerId);
|
---|
| 104 | }
|
---|
[16117] | 105 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
| 106 |
|
---|
[15401] | 107 | createdTextBox.Text = Content.DateCreated.ToString("ddd, dd.MM.yyyy, HH:mm:ss");
|
---|
| 108 | startDateTimePicker.Value = Content.StartDate;
|
---|
[16117] | 109 |
|
---|
| 110 | indefiniteCheckBox.Checked = !Content.EndDate.HasValue;
|
---|
| 111 | if (!indefiniteCheckBox.Checked) endDateTimePicker.Value = Content.EndDate.Value;
|
---|
| 112 | else endDateTimePicker.Value = Content.StartDate;
|
---|
| 113 | endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
|
---|
[15401] | 114 | }
|
---|
[17059] | 115 | SetEnabledStateOfControls();
|
---|
[16117] | 116 | RegisterControlEvents();
|
---|
[15401] | 117 | }
|
---|
| 118 |
|
---|
| 119 | protected override void SetEnabledStateOfControls() {
|
---|
| 120 | base.SetEnabledStateOfControls();
|
---|
[15760] | 121 | bool enabled = Content != null && !Locked && !ReadOnly;
|
---|
[15401] | 122 | nameTextBox.Enabled = enabled;
|
---|
[17062] | 123 | descriptionTextBox.Enabled = enabled;
|
---|
[15401] | 124 | ownerComboBox.Enabled = enabled;
|
---|
| 125 | createdTextBox.Enabled = enabled;
|
---|
| 126 | startDateTimePicker.Enabled = enabled;
|
---|
| 127 | endDateTimePicker.Enabled = enabled && Content.EndDate.HasValue;
|
---|
| 128 | indefiniteCheckBox.Enabled = enabled;
|
---|
[16117] | 129 |
|
---|
| 130 | if (Content != null) {
|
---|
[17059] | 131 | //var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
|
---|
| 132 | var parentProject = HiveAdminClient.Instance.Projects.FirstOrDefault(x => x.Id == Content.ParentProjectId);
|
---|
| 133 | if ((!IsAdmin() && (parentProject == null || parentProject.EndDate.HasValue))
|
---|
| 134 | || (IsAdmin() && parentProject != null && parentProject.EndDate.HasValue)) {
|
---|
[16117] | 135 | indefiniteCheckBox.Enabled = false;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[17059] | 138 | if (Content.Id != Guid.Empty && !IsAdmin() && !HiveAdminClient.Instance.CheckOwnershipOfParentProject(Content, UserInformation.Instance.User.Id)) {
|
---|
[16117] | 139 | ownerComboBox.Enabled = false;
|
---|
| 140 | startDateTimePicker.Enabled = false;
|
---|
| 141 | endDateTimePicker.Enabled = false;
|
---|
| 142 | indefiniteCheckBox.Enabled = false;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[15401] | 145 | }
|
---|
[15412] | 146 | #endregion
|
---|
[15401] | 147 |
|
---|
[15412] | 148 | #region Event Handlers
|
---|
| 149 | private void Content_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
[16117] | 150 | if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)Content_PropertyChanged, sender, e);
|
---|
| 151 | else OnContentChanged();
|
---|
[15412] | 152 | }
|
---|
| 153 |
|
---|
[15401] | 154 | private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
| 155 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
|
---|
| 156 | else {
|
---|
[17062] | 157 | Progress.Show(this, "Refreshing ...", ProgressMode.Indeterminate);
|
---|
[15401] | 158 | SetEnabledStateOfControls();
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
| 163 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
|
---|
| 164 | else {
|
---|
[17062] | 165 | Progress.Hide(this);
|
---|
[15401] | 166 | SetEnabledStateOfControls();
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | private async void ProjectView_Load(object sender, EventArgs e) {
|
---|
[15412] | 171 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 172 | action: () => UpdateUsers(),
|
---|
| 173 | finallyCallback: () => {
|
---|
| 174 | ownerComboBox.SelectedIndexChanged -= ownerComboBox_SelectedIndexChanged;
|
---|
[16117] | 175 | var users = AccessClient.Instance.UsersAndGroups.OfType<LightweightUser>();
|
---|
| 176 | if (Content != null && !Content.ParentProjectId.HasValue) users = users.Where(x => x.Roles.Select(y => y.Name).Contains(HiveRoles.Administrator));
|
---|
[17063] | 177 | ownerComboBox.DataSource = users.OrderBy(x => x.UserName).ToList();
|
---|
[15412] | 178 | ownerComboBox.SelectedIndexChanged += ownerComboBox_SelectedIndexChanged;
|
---|
| 179 | });
|
---|
[15401] | 180 | }
|
---|
| 181 |
|
---|
[17063] | 182 | private void ProjectView_Disposed(object sender, EventArgs e) {
|
---|
| 183 | AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
|
---|
| 184 | AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[15401] | 187 | private void nameTextBox_Validating(object sender, CancelEventArgs e) {
|
---|
| 188 | if (string.IsNullOrEmpty(nameTextBox.Text)) {
|
---|
| 189 | MessageBox.Show(
|
---|
| 190 | "Project must have a name.",
|
---|
| 191 | "HeuristicLab Hive Administrator",
|
---|
| 192 | MessageBoxButtons.OK,
|
---|
| 193 | MessageBoxIcon.Error);
|
---|
| 194 | e.Cancel = true;
|
---|
[15422] | 195 | }
|
---|
[15401] | 196 | }
|
---|
| 197 |
|
---|
[15422] | 198 | private void nameTextBox_TextChanged(object sender, EventArgs e) {
|
---|
[16117] | 199 | if (Content != null && Content.Name != nameTextBox.Text) {
|
---|
| 200 | DeregisterContentEvents();
|
---|
[15422] | 201 | Content.Name = nameTextBox.Text;
|
---|
[16117] | 202 | RegisterContentEvents();
|
---|
| 203 | }
|
---|
[15401] | 204 | }
|
---|
| 205 |
|
---|
[15422] | 206 | private void descriptionTextBox_TextChanged(object sender, EventArgs e) {
|
---|
[16117] | 207 | if (Content != null && Content.Description != descriptionTextBox.Text) {
|
---|
| 208 | DeregisterContentEvents();
|
---|
[15422] | 209 | Content.Description = descriptionTextBox.Text;
|
---|
[16117] | 210 | RegisterContentEvents();
|
---|
| 211 | }
|
---|
[15422] | 212 | }
|
---|
| 213 |
|
---|
[15401] | 214 | private void ownerComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 215 | var selectedItem = (LightweightUser)ownerComboBox.SelectedItem;
|
---|
[15422] | 216 | var selectedOwnerUserId = selectedItem != null ? selectedItem.Id : Guid.Empty;
|
---|
[16117] | 217 | if (Content != null && Content.OwnerUserId != selectedOwnerUserId) {
|
---|
| 218 | DeregisterContentEvents();
|
---|
[15422] | 219 | Content.OwnerUserId = selectedOwnerUserId;
|
---|
[16117] | 220 | RegisterContentEvents();
|
---|
| 221 | }
|
---|
[15401] | 222 | }
|
---|
| 223 |
|
---|
[15422] | 224 | private void startDateTimePicker_ValueChanged(object sender, EventArgs e) {
|
---|
| 225 | if (Content == null) return;
|
---|
[16117] | 226 | startDateTimePicker.ValueChanged -= startDateTimePicker_ValueChanged;
|
---|
| 227 |
|
---|
| 228 | if (!IsAdmin()) {
|
---|
| 229 | var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
|
---|
| 230 | if (parentProject != null) {
|
---|
| 231 | if (startDateTimePicker.Value < parentProject.StartDate)
|
---|
| 232 | startDateTimePicker.Value = parentProject.StartDate;
|
---|
| 233 | } else {
|
---|
| 234 | startDateTimePicker.Value = Content.StartDate;
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[15401] | 238 | if (!Content.EndDate.HasValue || startDateTimePicker.Value > Content.EndDate)
|
---|
| 239 | endDateTimePicker.Value = startDateTimePicker.Value;
|
---|
[16117] | 240 | if (Content.StartDate != startDateTimePicker.Value) {
|
---|
| 241 | DeregisterContentEvents();
|
---|
[15422] | 242 | Content.StartDate = startDateTimePicker.Value;
|
---|
[16117] | 243 | RegisterContentEvents();
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | startDateTimePicker.ValueChanged += startDateTimePicker_ValueChanged;
|
---|
[15401] | 247 | }
|
---|
| 248 |
|
---|
[15422] | 249 | private void endDateTimePicker_ValueChanged(object sender, EventArgs e) {
|
---|
| 250 | if (Content == null) return;
|
---|
[16117] | 251 | endDateTimePicker.ValueChanged -= endDateTimePicker_ValueChanged;
|
---|
| 252 |
|
---|
| 253 | if (!IsAdmin()) {
|
---|
| 254 | var parentProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(Content.Id).LastOrDefault();
|
---|
| 255 | if (parentProject != null) {
|
---|
| 256 | if (parentProject.EndDate.HasValue && endDateTimePicker.Value > parentProject.EndDate.Value) {
|
---|
| 257 | endDateTimePicker.Value = parentProject.EndDate.Value;
|
---|
| 258 | }
|
---|
| 259 | } else if (Content.EndDate.HasValue) {
|
---|
| 260 | endDateTimePicker.Value = Content.EndDate.Value;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[15401] | 264 | if (endDateTimePicker.Value < startDateTimePicker.Value)
|
---|
| 265 | endDateTimePicker.Value = startDateTimePicker.Value;
|
---|
[16117] | 266 | if (Content.EndDate != endDateTimePicker.Value) {
|
---|
| 267 | DeregisterContentEvents();
|
---|
[15422] | 268 | Content.EndDate = endDateTimePicker.Value;
|
---|
[16117] | 269 | RegisterContentEvents();
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | endDateTimePicker.ValueChanged += endDateTimePicker_ValueChanged;
|
---|
[15401] | 273 | }
|
---|
| 274 |
|
---|
| 275 | private void indefiniteCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
[15422] | 276 | if (Content == null) return;
|
---|
[16117] | 277 |
|
---|
| 278 | var newEndDate = indefiniteCheckBox.Checked ? (DateTime?)null : endDateTimePicker.Value;
|
---|
| 279 | endDateTimePicker.Enabled = !indefiniteCheckBox.Checked;
|
---|
| 280 | if (Content.EndDate != newEndDate) {
|
---|
| 281 | DeregisterContentEvents();
|
---|
[15422] | 282 | Content.EndDate = newEndDate;
|
---|
[16117] | 283 | RegisterContentEvents();
|
---|
| 284 | }
|
---|
[15401] | 285 | }
|
---|
[15412] | 286 | #endregion
|
---|
| 287 |
|
---|
| 288 | #region Helpers
|
---|
| 289 | private void UpdateUsers() {
|
---|
| 290 | try {
|
---|
| 291 | AccessClient.Instance.Refresh();
|
---|
| 292 | } catch (AnonymousUserException) {
|
---|
| 293 | ShowHiveInformationDialog();
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
| 296 |
|
---|
[16117] | 297 | private bool IsAdmin() {
|
---|
| 298 | return HiveRoles.CheckAdminUserPermissions();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[15412] | 301 | private void ShowHiveInformationDialog() {
|
---|
| 302 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
| 303 | else {
|
---|
| 304 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
| 305 | dialog.ShowDialog(this);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | #endregion
|
---|
[15401] | 310 | }
|
---|
| 311 | }
|
---|