Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Core.Views/3.3/ParameterCollectionView.cs @ 15906

Last change on this file since 15906 was 5929, checked in by swagner, 14 years ago

Adapted context menu of ParameterCollectionView (#1377)

File size: 10.4 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    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
48    public ParameterCollectionView() {
49      InitializeComponent();
50      itemsGroupBox.Text = "Parameters";
51      allowEditingOfHiddenParameters = true;
52    }
53
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
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
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);
80      viewHost.ReadOnly = ReadOnly || ((viewHost.Content is IParameter) && (((IParameter)viewHost.Content).Hidden) && !AllowEditingOfHiddenParameters);
81    }
82
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;
88        if ((param != null) && Content.ContainsKey(param.Name))
89          param = (IParameter)Activator.CreateInstance(param.GetType(), GetUniqueName(param.Name), param.Description);
90        return param;
91      }
92      return null;
93    }
94
95    protected override ListViewItem CreateListViewItem(IParameter item) {
96      ListViewItem listViewItem = base.CreateListViewItem(item);
97      if ((item != null) && item.Hidden) {
98        listViewItem.Font = new Font(listViewItem.Font, FontStyle.Italic);
99        listViewItem.ForeColor = Color.LightGray;
100      }
101      return listViewItem;
102    }
103
104    protected override void AddListViewItem(ListViewItem listViewItem) {
105      base.AddListViewItem(listViewItem);
106      IParameter parameter = listViewItem.Tag as IParameter;
107      if ((parameter != null) && parameter.Hidden && !showHiddenParametersCheckBox.Checked) {
108        itemsListView.Items.Remove(listViewItem);
109        RebuildImageList();
110      }
111    }
112
113    protected virtual void UpdateParameterVisibility(IParameter parameter) {
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);
120        } else {
121          listViewItem.Font = new Font(listViewItem.Font, FontStyle.Regular);
122          listViewItem.ForeColor = itemsListView.ForeColor;
123          if (!showHiddenParametersCheckBox.Checked)
124            itemsListView.Items.Add(listViewItem);
125        }
126      }
127      RebuildImageList();
128      AdjustListViewColumnSizes();
129      if (!Content.Any(x => x.Hidden)) showHiddenParametersCheckBox.Checked = false;
130      showHiddenParametersCheckBox.Enabled = (Content != null) && Content.Any(x => x.Hidden);
131    }
132
133    #region Control Events
134    protected virtual void showHiddenParametersCheckBox_CheckedChanged(object sender, System.EventArgs e) {
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          }
143        }
144        RebuildImageList();
145        AdjustListViewColumnSizes();
146      }
147    }
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) {
156          IContentView view = MainFormManager.MainForm.ShowContent(item);
157          if (view != null) {
158            view.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
159            view.Locked = Locked;
160          }
161        }
162      }
163    }
164    protected virtual void itemsListViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
165      showHideParametersToolStripMenuItem.Text = "Show/Hide Parameters";
166      showHideParametersToolStripMenuItem.Enabled = false;
167      if (itemsListView.SelectedItems.Count == 0) {
168        e.Cancel = true;
169      } else if (!ReadOnly && !Locked && AllowEditingOfHiddenParameters) {
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        }
175        showHideParametersToolStripMenuItem.Enabled = parameters.Count > 0;
176        if (parameters.Count == 1) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameter" : "Hide Parameter";
177        else if ((parameters.Count > 1) && parameters.All(x => x.Hidden == parameters[0].Hidden)) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameters" : "Hide Parameters";
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    }
185    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
186      base.showDetailsCheckBox_CheckedChanged(sender, e);
187      SetEnabledStateOfControls();
188    }
189    #endregion
190
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
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
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
243  }
244}
Note: See TracBrowser for help on using the repository browser.