source: branches/ProblemRefactoring/HeuristicLab.Optimization.Views/3.3/MultiEncodingView.cs @ 13388

Last change on this file since 13388 was 13388, checked in by abeham, 4 years ago

#2521:

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