Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core.Views/3.3/ParameterCollectionView.cs @ 16824

Last change on this file since 16824 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 10.4 KB
RevLine 
[2796]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2796]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;
[5768]23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
[2655]26using System.Windows.Forms;
[5783]27using HeuristicLab.Collections;
[2655]28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Core.Views {
[2917]31  [View("ParameterCollection View")]
[2655]32  [Content(typeof(ParameterCollection), true)]
[3393]33  [Content(typeof(IKeyedItemCollection<string, IParameter>), false)]
[2655]34  public partial class ParameterCollectionView : NamedItemCollectionView<IParameter> {
[2676]35    protected CreateParameterDialog createParameterDialog;
[5783]36
[5901]37    protected bool allowEditingOfHiddenParameters;
38    public virtual bool AllowEditingOfHiddenParameters {
39      get { return allowEditingOfHiddenParameters; }
40      set {
41        if (value != allowEditingOfHiddenParameters) {
42          allowEditingOfHiddenParameters = value;
43          SetEnabledStateOfControls();
44        }
45      }
46    }
47
[2655]48    public ParameterCollectionView() {
49      InitializeComponent();
[2924]50      itemsGroupBox.Text = "Parameters";
[5901]51      allowEditingOfHiddenParameters = true;
[2655]52    }
53
[5237]54    protected override void Dispose(bool disposing) {
55      if (disposing) {
56        if (createParameterDialog != null) createParameterDialog.Dispose();
57        if (components != null) components.Dispose();
58      }
59      base.Dispose(disposing);
60    }
61
[5768]62    protected override void DeregisterItemEvents(IParameter item) {
63      item.HiddenChanged -= new EventHandler(Item_HiddenChanged);
64      base.DeregisterItemEvents(item);
65    }
66    protected override void RegisterItemEvents(IParameter item) {
67      base.RegisterItemEvents(item);
68      item.HiddenChanged += new EventHandler(Item_HiddenChanged);
69    }
70
[5783]71    protected override void OnContentChanged() {
72      base.OnContentChanged();
73      if ((Content == null) || !Content.Any(x => x.Hidden))
74        showHiddenParametersCheckBox.Checked = false;
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      base.SetEnabledStateOfControls();
79      showHiddenParametersCheckBox.Enabled = (Content != null) && Content.Any(x => x.Hidden);
[5901]80      viewHost.ReadOnly = ReadOnly || ((viewHost.Content is IParameter) && (((IParameter)viewHost.Content).Hidden) && !AllowEditingOfHiddenParameters);
[5783]81    }
82
[2655]83    protected override IParameter CreateItem() {
84      if (createParameterDialog == null) createParameterDialog = new CreateParameterDialog();
85
86      if (createParameterDialog.ShowDialog(this) == DialogResult.OK) {
87        IParameter param = createParameterDialog.Parameter;
[3407]88        if ((param != null) && Content.ContainsKey(param.Name))
[2655]89          param = (IParameter)Activator.CreateInstance(param.GetType(), GetUniqueName(param.Name), param.Description);
90        return param;
[3407]91      }
92      return null;
[2655]93    }
[5768]94
[5783]95    protected override ListViewItem CreateListViewItem(IParameter item) {
96      ListViewItem listViewItem = base.CreateListViewItem(item);
97      if ((item != null) && item.Hidden) {
[5768]98        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
99        listViewItem.ForeColor = Color.LightGray;
100      }
[5783]101      return listViewItem;
102    }
103
104    protected override void AddListViewItem(ListViewItem listViewItem) {
[5768]105      base.AddListViewItem(listViewItem);
[5783]106      IParameter parameter = listViewItem.Tag as IParameter;
107      if ((parameter != null) && parameter.Hidden && !showHiddenParametersCheckBox.Checked) {
108        itemsListView.Items.Remove(listViewItem);
109        RebuildImageList();
110      }
[5768]111    }
112
113    protected virtual void UpdateParameterVisibility(IParameter parameter) {
[5783]114      foreach (ListViewItem listViewItem in GetListViewItemsForItem(parameter)) {
115        if (parameter.Hidden) {
116          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
117          listViewItem.ForeColor = Color.LightGray;
118          if (!showHiddenParametersCheckBox.Checked)
119            itemsListView.Items.Remove(listViewItem);
[5768]120        } else {
[5783]121          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
122          listViewItem.ForeColor = itemsListView.ForeColor;
123          if (!showHiddenParametersCheckBox.Checked)
124            itemsListView.Items.Add(listViewItem);
[5768]125        }
126      }
[5783]127      RebuildImageList();
[5768]128      AdjustListViewColumnSizes();
[5783]129      if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
130      showHiddenParametersCheckBox.Enabled = (Content != null) && Content.Any(x => x.Hidden);
[5768]131    }
132
133    #region Control Events
134    protected virtual void showHiddenParametersCheckBox_CheckedChanged(object sender, System.EventArgs e) {
[5783]135      if (Content != null) {
136        foreach (IParameter parameter in itemListViewItemMapping.Keys.Where(x => x.Hidden).OrderBy(x => x.ToString())) {
137          foreach (ListViewItem listViewItem in GetListViewItemsForItem(parameter)) {
138            if (showHiddenParametersCheckBox.Checked)
139              itemsListView.Items.Add(listViewItem);
140            else
141              itemsListView.Items.Remove(listViewItem);
142          }
[5768]143        }
144        RebuildImageList();
[5783]145        AdjustListViewColumnSizes();
[5768]146      }
147    }
[5901]148    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
149      base.itemsListView_SelectedIndexChanged(sender, e);
150      SetEnabledStateOfControls();
151    }
152    protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
153      if (itemsListView.SelectedItems.Count == 1) {
154        IParameter item = itemsListView.SelectedItems[0].Tag as IParameter;
155        if (item != null) {
[13244]156          IContentView view = MainFormManager.MainForm.ShowContent(item);
157          if (view != null) {
158            view.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
159            view.Locked = Locked;
[5901]160          }
161        }
162      }
163    }
[5768]164    protected virtual void itemsListViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
[5783]165      showHideParametersToolStripMenuItem.Text = "Show/Hide Parameters";
166      showHideParametersToolStripMenuItem.Enabled = false;
[5929]167      if (itemsListView.SelectedItems.Count == 0) {
168        e.Cancel = true;
169      } else if (!ReadOnly && !Locked && AllowEditingOfHiddenParameters) {
[5768]170        List<IParameter> parameters = new List<IParameter>();
171        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
172          IParameter parameter = listViewItem.Tag as IParameter;
173          if (parameter != null) parameters.Add(parameter);
174        }
[5929]175        showHideParametersToolStripMenuItem.Enabled = parameters.Count > 0;
[5768]176        if (parameters.Count == 1) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameter" : "Hide Parameter";
[5929]177        else if ((parameters.Count > 1) && parameters.All(x => x.Hidden == parameters[0].Hidden)) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameters" : "Hide Parameters";
[5768]178        showHideParametersToolStripMenuItem.Tag = parameters;
179      }
180    }
181    protected virtual void showHideParametersToolStripMenuItem_Click(object sender, System.EventArgs e) {
182      foreach (IParameter parameter in (IEnumerable<IParameter>)showHideParametersToolStripMenuItem.Tag)
183        parameter.Hidden = !parameter.Hidden;
184    }
[5901]185    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
186      base.showDetailsCheckBox_CheckedChanged(sender, e);
187      SetEnabledStateOfControls();
188    }
[5768]189    #endregion
190
[5783]191    #region Content Events
192    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
193      if (InvokeRequired)
194        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsAdded), sender, e);
195      else {
196        base.Content_ItemsAdded(sender, e);
197        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
198      }
199    }
200    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
201      if (InvokeRequired)
202        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsRemoved), sender, e);
203      else {
204        base.Content_ItemsRemoved(sender, e);
205        if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
206        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
207      }
208    }
209    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
210      if (InvokeRequired)
211        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_CollectionReset), sender, e);
212      else {
213        base.Content_CollectionReset(sender, e);
214        if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
215        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
216      }
217    }
218    #endregion
219
[5768]220    #region Item Events
221    protected virtual void Item_HiddenChanged(object sender, EventArgs e) {
222      if (InvokeRequired)
223        Invoke(new EventHandler(Item_HiddenChanged), sender, e);
224      else
225        UpdateParameterVisibility((IParameter)sender);
226    }
227    #endregion
[5783]228
229    #region Helpers
230    protected override void RebuildImageList() {
231      base.RebuildImageList();
232      if (!showHiddenParametersCheckBox.Checked) {
233        // update image of hidden list view items
234        foreach (IParameter parameter in itemListViewItemMapping.Keys.Where(x => x.Hidden)) {
235          foreach (ListViewItem listViewItem in GetListViewItemsForItem(parameter)) {
236            itemsListView.SmallImageList.Images.Add(parameter.ItemImage);
237            listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
238          }
239        }
240      }
241    }
242    #endregion
[2655]243  }
244}
Note: See TracBrowser for help on using the repository browser.