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

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

#2116: fixed readonly property assignment in ParameterCollectionView

File size: 11.1 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 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        var item = itemsListView.SelectedItems[0].Tag as IParameter;
156        if (item != null) {
157          ViewHost outmostViewHost = null;
158          for (var control = Parent; control != null; control = control.Parent) {
159            var vh = control as ViewHost;
160            if (vh != null) outmostViewHost = vh;
161          }
162          if (outmostViewHost != null) {
163            var oldCrumbs = outmostViewHost.Breadcrumbs;
164            var newCrumbs = BuildBreadcrumbTrail();
165            outmostViewHost.Content = null;
166            var viewType = MainFormManager.GetDefaultViewType(item.GetType());
167            outmostViewHost.ViewType = viewType;
168            outmostViewHost.Content = item;
169            outmostViewHost.UpdateBreadcrumbTrail(oldCrumbs, newCrumbs.Concat(new[] { item }));
170            outmostViewHost.ActiveView.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
171            outmostViewHost.ActiveView.Locked = Locked;
172          }
173        }
174      }
175    }
176    protected virtual void itemsListViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
177      showHideParametersToolStripMenuItem.Text = "Show/Hide Parameters";
178      showHideParametersToolStripMenuItem.Enabled = false;
179      if (itemsListView.SelectedItems.Count == 0) {
180        e.Cancel = true;
181      } else if (!ReadOnly && !Locked && AllowEditingOfHiddenParameters) {
182        List<IParameter> parameters = new List<IParameter>();
183        foreach (ListViewItem listViewItem in itemsListView.SelectedItems) {
184          IParameter parameter = listViewItem.Tag as IParameter;
185          if (parameter != null) parameters.Add(parameter);
186        }
187        showHideParametersToolStripMenuItem.Enabled = parameters.Count > 0;
188        if (parameters.Count == 1) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameter" : "Hide Parameter";
189        else if ((parameters.Count > 1) && parameters.All(x => x.Hidden == parameters[0].Hidden)) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameters" : "Hide Parameters";
190        showHideParametersToolStripMenuItem.Tag = parameters;
191      }
192    }
193    protected virtual void showHideParametersToolStripMenuItem_Click(object sender, System.EventArgs e) {
194      foreach (IParameter parameter in (IEnumerable<IParameter>)showHideParametersToolStripMenuItem.Tag)
195        parameter.Hidden = !parameter.Hidden;
196    }
197    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
198      base.showDetailsCheckBox_CheckedChanged(sender, e);
199      SetEnabledStateOfControls();
200    }
201    #endregion
202
203    #region Content Events
204    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
205      if (InvokeRequired)
206        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsAdded), sender, e);
207      else {
208        base.Content_ItemsAdded(sender, e);
209        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
210      }
211    }
212    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
213      if (InvokeRequired)
214        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsRemoved), sender, e);
215      else {
216        base.Content_ItemsRemoved(sender, e);
217        if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
218        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
219      }
220    }
221    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
222      if (InvokeRequired)
223        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_CollectionReset), sender, e);
224      else {
225        base.Content_CollectionReset(sender, e);
226        if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
227        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
228      }
229    }
230    #endregion
231
232    #region Item Events
233    protected virtual void Item_HiddenChanged(object sender, EventArgs e) {
234      if (InvokeRequired)
235        Invoke(new EventHandler(Item_HiddenChanged), sender, e);
236      else
237        UpdateParameterVisibility((IParameter)sender);
238    }
239    #endregion
240
241    #region Helpers
242    protected override void RebuildImageList() {
243      base.RebuildImageList();
244      if (!showHiddenParametersCheckBox.Checked) {
245        // update image of hidden list view items
246        foreach (IParameter parameter in itemListViewItemMapping.Keys.Where(x => x.Hidden)) {
247          foreach (ListViewItem listViewItem in GetListViewItemsForItem(parameter)) {
248            itemsListView.SmallImageList.Images.Add(parameter.ItemImage);
249            listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
250          }
251        }
252      }
253    }
254    #endregion
255  }
256}
Note: See TracBrowser for help on using the repository browser.