Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Optimization.Views/3.3/MultiEncodingView.cs @ 17511

Last change on this file since 17511 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

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