Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13231 was 13014, checked in by jkarder, 9 years ago

#2116: added new breadcrumb navigation prototype

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
150      base.itemsListView_SelectedIndexChanged(sender, e);
151      SetEnabledStateOfControls();
152    }
153    protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
154      if (itemsListView.SelectedItems.Count == 1) {
155        IParameter item = itemsListView.SelectedItems[0].Tag as IParameter;
156        if (item != null) {
157          Control c = this;
158          BreadcrumbViewHost bcvh;
159
160          do {
161            c = c.Parent;
162            bcvh = c as BreadcrumbViewHost;
163          } while ((bcvh == null || !bcvh.EnableBreadcrumbs) && c != null);
164
165          if (bcvh != null) {
166            bcvh.AddBreadcrumbs(bcvh.Content);
167            bcvh.AddBreadcrumbs(item);
168            bcvh.Content = item;
169            bcvh.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
170            bcvh.Locked = Locked;
171          } else {
172            IContentView view = MainFormManager.MainForm.ShowContent(item);
173            if (view != null) {
174              view.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
175              view.Locked = Locked;
176            }
177          }
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.