Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Core.Views/3.3/ParameterCollectionView.cs @ 10843

Last change on this file since 10843 was 10103, checked in by jkarder, 11 years ago

#2116:

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