Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable.Views/3.3/MultiEncodingView.cs @ 11952

Last change on this file since 11952 was 11952, checked in by abeham, 9 years ago

#2174:

  • Made encodingOperators hash set an ItemSet and private
  • Added a parameter that displays the operators of an encoding (fixedvalue showing a readonly version of the itemset)
  • Added protected methods to Encoding to add and remove operators
  • Adapted MultiEncodingView to derive from ParameterizedNamedItemView
  • Added name of MultiEncoding itself to list of forbidden names
File size: 5.8 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.Drawing;
24using System.Linq;
25using System.Reflection;
26using System.Windows.Forms;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.Problems.Programmable.Views {
33  [View("MultiEncoding View")]
34  [Content(typeof(MultiEncoding), IsDefaultView = true)]
35  public sealed partial class MultiEncodingView : ParameterizedNamedItemView {
36
37    public new MultiEncoding Content {
38      get { return (MultiEncoding)base.Content; }
39      set { base.Content = value; }
40    }
41
42    public MultiEncodingView() {
43      InitializeComponent();
44      Controls.Remove(parameterCollectionView);
45      parameterCollectionView.Dock = DockStyle.Fill;
46      parametersTabPage.Controls.Add(parameterCollectionView);
47      addEncodingButton.Text = string.Empty;
48      addEncodingButton.Image = VSImageLibrary.Add;
49      removeEncodingButton.Text = string.Empty;
50      removeEncodingButton.Image = VSImageLibrary.Remove;
51    }
52
53    protected override void DeregisterContentEvents() {
54      base.DeregisterContentEvents();
55      Content.EncodingsChanged -= ContentOnEncodingsChanged;
56    }
57
58    protected override void RegisterContentEvents() {
59      Content.EncodingsChanged += ContentOnEncodingsChanged;
60      base.RegisterContentEvents();
61    }
62
63    private void ContentOnEncodingsChanged(object sender, EventArgs eventArgs) {
64      RecreateEncodingsList();
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      encodingDetailViewHost.Content = null;
70      RecreateEncodingsList();
71    }
72
73    protected override void SetEnabledStateOfControls() {
74      base.SetEnabledStateOfControls();
75      addEncodingButton.Enabled = !Locked && !ReadOnly && Content != null;
76      removeEncodingButton.Enabled = !Locked && !ReadOnly && Content != null
77                                     && encodingsListView.SelectedItems.Count > 0;
78    }
79
80    private void addEncodingButton_Click(object sender, EventArgs e) {
81      using (var dialog = new CreateNewSingleEncodingDialog()) {
82        dialog.ForbiddenNames = Content.Encodings.Select(x => x.Name).Concat(new[] { Content.Name });
83        if (dialog.ShowDialog() == DialogResult.OK) {
84          IEncoding encoding;
85          try {
86            encoding = (IEncoding)Activator.CreateInstance(dialog.EncodingType, dialog.EncodingName);
87          } catch (MissingMethodException mmex) {
88            PluginInfrastructure.ErrorHandling.ShowErrorDialog("The encoding must have a constructor that takes the name as a single string argument", mmex);
89            return;
90          } catch (TargetInvocationException tiex) {
91            PluginInfrastructure.ErrorHandling.ShowErrorDialog("The encoding could not be created due to an error in the constructor.", tiex);
92            return;
93          } catch (MethodAccessException maex) {
94            PluginInfrastructure.ErrorHandling.ShowErrorDialog("The encoding's string constructor is not public.", maex);
95            return;
96          }
97          Content.Add(encoding);
98        }
99      }
100    }
101
102    private void removeEncodingButton_Click(object sender, EventArgs e) {
103      var encoding = GetSelectedEncoding();
104      var success = Content.Remove(encoding);
105      encodingDetailViewHost.Content = null;
106      if (!success) MessageBox.Show("Could not remove encoding.", "Error while removing encoding.", MessageBoxButtons.OK, MessageBoxIcon.Error);
107    }
108
109    private void encodingsListView_SelectedIndexChanged(object sender, EventArgs e) {
110      if (encodingsListView.SelectedItems.Count > 0) {
111        var encoding = GetSelectedEncoding();
112        encodingDetailViewHost.Content = encoding;
113      } else encodingDetailViewHost.Content = null;
114      SetEnabledStateOfControls();
115    }
116
117    private IEncoding GetSelectedEncoding() {
118      var selected = encodingsListView.SelectedItems[0];
119      var encodingName = selected.Text;
120      var encoding = Content.Encodings.Single(x => x.Name == encodingName);
121      return encoding;
122    }
123
124    private void RecreateEncodingsList() {
125      encodingsListView.SelectedIndices.Clear();
126      encodingsListView.Items.Clear();
127      if (encodingsListView.SmallImageList != null)
128        foreach (Image img in encodingsListView.SmallImageList.Images) img.Dispose();
129      encodingsListView.SmallImageList = new ImageList();
130      if (Content != null) {
131        foreach (var enc in Content.Encodings)
132          CreateListViewItem(enc);
133      }
134    }
135
136    private void CreateListViewItem(IEncoding enc) {
137      encodingsListView.SmallImageList.Images.Add(enc.ItemImage);
138      var item = new ListViewItem() {
139        ImageIndex = encodingsListView.SmallImageList.Images.Count - 1,
140        Text = enc.Name
141      };
142      item.SubItems.Add(new ListViewItem.ListViewSubItem() {
143        Text = enc.GetType().Name
144      });
145      encodingsListView.Items.Add(item);
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.