Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ItemTreeViews/3.3/Views/ParameterCollectionTreeView.cs @ 15632

Last change on this file since 15632 was 14335, checked in by bwerth, 8 years ago

#2684 initial commit of TreeView

File size: 9.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Core;
29using HeuristicLab.Core.Views;
30using HeuristicLab.MainForm;
31
32namespace HeuristicLab.ItemTreeViews.Views {
33  [View("ParameterCollection Tree View")]
34  [Content(typeof(ParameterCollection), false)]
35  [Content(typeof(IKeyedItemCollection<string, IParameter>), false)]
36  public partial class ParameterCollectionTreeView : NamedItemCollectionTreeView<IParameter> {
37    protected CreateParameterDialog createParameterDialog;
38
39    protected bool allowEditingOfHiddenParameters;
40    public virtual bool AllowEditingOfHiddenParameters
41    {
42      get { return allowEditingOfHiddenParameters; }
43      set
44      {
45        if (value != allowEditingOfHiddenParameters) {
46          allowEditingOfHiddenParameters = value;
47          SetEnabledStateOfControls();
48        }
49      }
50    }
51
52    public ParameterCollectionTreeView() {
53      InitializeComponent();
54      itemsGroupBox.Text = "Parameters";
55      allowEditingOfHiddenParameters = true;
56    }
57
58    protected override void Dispose(bool disposing) {
59      if (disposing) {
60        if (createParameterDialog != null)
61          createParameterDialog.Dispose();
62        if (components != null)
63          components.Dispose();
64      }
65      base.Dispose(disposing);
66    }
67    protected override void DeregisterItemEvents(IParameter item) {
68      item.HiddenChanged -= Item_HiddenChanged;
69      base.DeregisterItemEvents(item);
70    }
71    protected override void RegisterItemEvents(IParameter item) {
72      base.RegisterItemEvents(item);
73      item.HiddenChanged += Item_HiddenChanged;
74    }
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if ((Content == null) || !Content.Any(x => x.Hidden))
78        showHiddenParametersCheckBox.Checked = false;
79    }
80    protected override void SetEnabledStateOfControls() {
81      base.SetEnabledStateOfControls();
82      showHiddenParametersCheckBox.Enabled = (Content != null) && Content.Any(x => x.Hidden);
83      viewHost.ReadOnly = ReadOnly;
84    }
85    protected override IParameter CreateItem() {
86      if (createParameterDialog == null) createParameterDialog = new CreateParameterDialog();
87
88      if (createParameterDialog.ShowDialog(this) == DialogResult.OK) {
89        IParameter param = createParameterDialog.Parameter;
90        if ((param != null) && Content.ContainsKey(param.Name))
91          param = (IParameter)Activator.CreateInstance(param.GetType(), GetUniqueName(param.Name), param.Description);
92        return param;
93      }
94      return null;
95    }
96    protected override TreeNode CreateTreeViewNode(IParameter item) {
97      var viewItem = base.CreateTreeViewNode(item);
98      if ((item != null) && item.Hidden) {
99        viewItem.NodeFont = new Font(viewItem.NodeFont, FontStyle.Italic);
100        viewItem.ForeColor = Color.LightGray;
101      }
102      return viewItem;
103    }
104
105    protected override void AddTreeNode(IParameter p) {
106      base.AddTreeNode(p);
107      if (p != null) UpdateParameterVisibility(p);
108    }
109    protected virtual void UpdateParameterVisibility(IParameter parameter) {
110      var treeNode = GetTreeNodeForItem(parameter);
111      if (parameter.Hidden) {
112        treeNode.NodeFont = new Font(treeNode.NodeFont, FontStyle.Italic);
113        treeNode.ForeColor = Color.LightGray;
114      } else {
115        treeNode.NodeFont = new Font(treeNode.NodeFont, FontStyle.Regular);
116        treeNode.ForeColor = ItemsTreeView.ForeColor;
117      }
118
119      if (!showHiddenParametersCheckBox.Checked && parameter.Hidden) //remove hidden nodes
120        treeNode.Remove();
121      else if (treeNode.Parent == null && !ItemsTreeView.Nodes.Contains(treeNode)) {// nonadded  Nodes
122        var p = GetParent(parameter);
123        (p == null ? ItemsTreeView.Nodes : GetTreeNodeForItem(p).Nodes).Add(treeNode);
124      }
125
126    }
127    protected override List<IParameter> ExpandTreeViewItem(object parent) {
128      var p = parent as IParameterizedItem;
129      if (p == null || p.Parameters == null) return base.ExpandTreeViewItem(parent);
130      return p.Parameters.ToList();
131    }
132
133
134    #region Control Events
135    protected virtual void showHiddenParametersCheckBox_CheckedChanged(object sender, EventArgs e) {
136      if (Content == null) return;
137      var items = itemTreeViewItemMapping.Keys.Where(x => x.Hidden).OrderBy(x => x.ToString()).ToArray();
138      ItemsTreeView.BeginUpdate();
139      Array.ForEach(items, UpdateParameterVisibility);
140      ItemsTreeView.EndUpdate();
141    }
142    protected override void itemsTreeView_SelectedIndexChanged(object sender, EventArgs e) {
143      base.itemsTreeView_SelectedIndexChanged(sender, e);
144      SetEnabledStateOfControls();
145    }
146    protected override void itemsTreeView_DoubleClick(object sender, EventArgs e) {
147      var item = ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as IParameter : null;
148      if (item == null) return;
149      var view = MainFormManager.MainForm.ShowContent(item);
150      if (view == null) return;
151      view.ReadOnly = ReadOnly || (item.Hidden && !AllowEditingOfHiddenParameters);
152      view.Locked = Locked;
153
154    }
155    protected virtual void itemsTreeViewContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e) {
156      showHideParametersToolStripMenuItem.Text = "Show/Hide Parameters";
157      showHideParametersToolStripMenuItem.Enabled = false;
158      if (ItemsTreeView.SelectedNode == null) {
159        e.Cancel = true;
160      } else if (!ReadOnly && !Locked && AllowEditingOfHiddenParameters) {
161        List<IParameter> parameters = new List<IParameter>();
162        IParameter parameter = ItemsTreeView.SelectedNode.Tag as IParameter;
163        if (parameter != null) parameters.Add(parameter);
164
165        showHideParametersToolStripMenuItem.Enabled = parameters.Count > 0;
166        if (parameters.Count == 1) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameter" : "Hide Parameter";
167        else if ((parameters.Count > 1) && parameters.All(x => x.Hidden == parameters[0].Hidden)) showHideParametersToolStripMenuItem.Text = parameters[0].Hidden ? "Show Parameters" : "Hide Parameters";
168        showHideParametersToolStripMenuItem.Tag = parameters;
169      }
170    }
171    protected virtual void showHideParametersToolStripMenuItem_Click(object sender, EventArgs e) {
172      foreach (IParameter parameter in (IEnumerable<IParameter>)showHideParametersToolStripMenuItem.Tag)
173        parameter.Hidden = !parameter.Hidden;
174    }
175    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
176      base.showDetailsCheckBox_CheckedChanged(sender, e);
177      SetEnabledStateOfControls();
178    }
179    #endregion
180
181    #region Content Events
182    protected override void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
183      if (InvokeRequired)
184        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsAdded), sender, e);
185      else {
186        base.Content_ItemsAdded(sender, e);
187        showHiddenParametersCheckBox.Enabled = Content.Any(x => x.Hidden);
188      }
189    }
190    protected override void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
191      if (InvokeRequired)
192        Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_ItemsRemoved), sender, e);
193      else {
194        base.Content_ItemsRemoved(sender, e);
195      }
196    }
197    protected override void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
198      if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler<IParameter>(Content_CollectionReset), sender, e);
199      else base.Content_CollectionReset(sender, e);
200    }
201    #endregion
202
203    #region Item Events
204    protected virtual void Item_HiddenChanged(object sender, EventArgs e) {
205      if (InvokeRequired)
206        Invoke(new EventHandler(Item_HiddenChanged), sender, e);
207      else {
208        ItemsTreeView.BeginUpdate();
209        UpdateParameterVisibility((IParameter)sender);
210        ItemsTreeView.EndUpdate();
211      }
212    }
213    #endregion
214
215    #region Helpers
216    protected override void RebuildTreeImageList() {
217      base.RebuildTreeImageList();
218      if (!showHiddenParametersCheckBox.Checked) {
219        // update image of hidden list view items
220        foreach (IParameter parameter in itemTreeViewItemMapping.Keys.Where(x => x.Hidden)) {
221          var treeNode = GetTreeNodeForItem(parameter);
222          try {
223            ItemsTreeView.ImageList.Images.Add(parameter == null ? Common.Resources.VSImageLibrary.Nothing : parameter.ItemImage);
224            treeNode.ImageIndex = ItemsTreeView.ImageList.Images.Count - 1;
225            treeNode.SelectedImageIndex = treeNode.ImageIndex;
226          }
227          catch (ArgumentException) {
228            treeNode.SelectedImageIndex = ItemsTreeView.ImageList.Images.Count + 10000;
229          }
230        }
231      }
232    }
233    #endregion
234  }
235}
Note: See TracBrowser for help on using the repository browser.