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

Last change on this file since 11949 was 11949, checked in by mkommend, 8 years ago

#2174: Distributed files in programmable problem branch to the correct plugins.

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