Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5849 was 5783, checked in by swagner, 14 years ago

Continued implementation of parameter hiding (#1377)

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