Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs @ 15651

Last change on this file since 15651 was 13338, checked in by gkronber, 9 years ago

#2522:

  • moved UI components out of HeuristicLab.PluginInfrastructure -> HeuristicLab.PluginInfrastructure.UI
  • moved ErrorDialog to HeuristicLab.MainForm.WindowsForms
  • moved ErrorHandling (for building an error message string) to HeuristicLab.Common
  • Changed exception handlers in Views to use MainForm.ShowError()
  • Changed usages for ErrorDialog in non-UI components to throw exceptions instead.
File size: 10.2 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Views;
32using HeuristicLab.Persistence.Default.Xml;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.DataAnalysis.Views {
36
37  [View("DataAnalysisSolution View")]
38  [Content(typeof(DataAnalysisSolution), false)]
39  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
40    public DataAnalysisSolutionView() {
41      InitializeComponent();
42      viewHost.ViewsLabelVisible = false;
43    }
44
45    public new DataAnalysisSolution Content {
46      get { return (DataAnalysisSolution)base.Content; }
47      set { base.Content = value; }
48    }
49
50    protected override void SetEnabledStateOfControls() {
51      base.SetEnabledStateOfControls();
52      addButton.Enabled = false;
53      removeButton.Enabled = false;
54      loadProblemDataButton.Enabled = Content != null && !Locked;
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
60    }
61    protected override void DeregisterContentEvents() {
62      base.DeregisterContentEvents();
63      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
64    }
65    private void Content_ProblemDataChanged(object sender, EventArgs e) {
66      OnContentChanged();
67    }
68
69    protected override void OnContentChanged() {
70      string selectedName = null;
71      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
72        selectedName = itemsListView.SelectedItems[0].Text;
73
74      base.OnContentChanged();
75      AddEvaluationViewTypes();
76
77      //recover selection
78      if (selectedName != null) {
79        foreach (ListViewItem item in itemsListView.Items) {
80          if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
81            item.Selected = true;
82        }
83      }
84    }
85
86    protected override IResult CreateItem() {
87      return null;
88    }
89
90    protected virtual void AddEvaluationViewTypes() {
91      if (Content != null && !Content.ProblemData.IsEmpty) {
92        var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
93          .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
94        foreach (var viewType in viewTypes)
95          AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
96      }
97    }
98
99    protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
100      if (itemsListView.SelectedItems.Count != 1) return;
101
102      IResult result = itemsListView.SelectedItems[0].Tag as IResult;
103      Type viewType = itemsListView.SelectedItems[0].Tag as Type;
104      if (result != null) {
105        IContentView view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
106        if (view != null) {
107          view.ReadOnly = ReadOnly;
108          view.Locked = Locked;
109        }
110      } else if (viewType != null) {
111        MainFormManager.MainForm.ShowContent(Content, viewType);
112      }
113    }
114
115    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
116      if (itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
117        detailsGroupBox.Enabled = true;
118        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
119        viewHost.ViewType = viewType;
120        viewHost.Content = Content;
121      } else
122        base.itemsListView_SelectedIndexChanged(sender, e);
123    }
124
125    protected virtual void loadProblemDataButton_Click(object sender, EventArgs e) {
126      if (loadProblemDataFileDialog.ShowDialog(this) != DialogResult.OK) return;
127      try {
128        object hlFile = XmlParser.Deserialize(loadProblemDataFileDialog.FileName);
129
130        IDataAnalysisProblemData problemData = null;
131        if (hlFile is IDataAnalysisProblemData) {
132          problemData = (IDataAnalysisProblemData)hlFile;
133        } else if (hlFile is IDataAnalysisProblem) {
134          problemData = ((IDataAnalysisProblem)hlFile).ProblemData;
135        } else if (hlFile is IDataAnalysisSolution) {
136          problemData = ((IDataAnalysisSolution)hlFile).ProblemData;
137        }
138
139        if (problemData == null)
140          throw new InvalidOperationException("The chosen HeuristicLab file does not contain a ProblemData, Problem, or DataAnalysisSolution.");
141
142        var solution = (IDataAnalysisSolution)Content.Clone();
143        problemData.AdjustProblemDataProperties(solution.ProblemData);
144        solution.ProblemData = problemData;
145        if (!solution.Name.EndsWith(" with loaded problemData"))
146          solution.Name += " with loaded problemData";
147        MainFormManager.MainForm.ShowContent(solution);
148      } catch (InvalidOperationException invalidOperationException) {
149        MainFormManager.MainForm.ShowError(invalidOperationException.Message, invalidOperationException);
150      } catch (ArgumentException argumentException) {
151        MainFormManager.MainForm.ShowError(argumentException.Message, argumentException);
152      }
153    }
154
155    protected void AddViewListViewItem(Type viewType, Image image) {
156      ListViewItem listViewItem = new ListViewItem();
157      listViewItem.Text = ViewAttribute.GetViewName(viewType);
158      itemsListView.SmallImageList.Images.Add(image);
159      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
160      listViewItem.Tag = viewType;
161      itemsListView.Items.Add(listViewItem);
162
163      AdjustListViewColumnSizes();
164    }
165
166    protected void RemoveViewListViewItem(Type viewType) {
167      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
168
169      foreach (ListViewItem item in itemsToRemove)
170        itemsListView.Items.Remove(item);
171    }
172
173    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
174      if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
175        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
176        viewHost.ViewType = viewType;
177        viewHost.Content = Content;
178        splitContainer.Panel2Collapsed = false;
179        detailsGroupBox.Enabled = true;
180      } else base.showDetailsCheckBox_CheckedChanged(sender, e);
181    }
182
183    protected override void RebuildImageList() {
184      itemsListView.SmallImageList.Images.Clear();
185      foreach (ListViewItem listViewItem in itemsListView.Items) {
186        IResult result = listViewItem.Tag as IResult;
187        Type viewType = listViewItem.Tag as Type;
188        if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
189        else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
190          itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
191        else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
192
193        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
194      }
195    }
196
197    #region drag and drop
198    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
199      validDragOperation = false;
200      if (ReadOnly) return;
201      if (e.Effect != DragDropEffects.Copy) return;
202
203      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
204      if (dropData is IDataAnalysisProblemData) validDragOperation = true;
205      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
206      else if (dropData is IValueParameter) {
207        var param = (IValueParameter)dropData;
208        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
209      }
210    }
211
212    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
213      if (e.Effect == DragDropEffects.None) return;
214
215      IDataAnalysisProblemData problemData = null;
216      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
217      if (dropData is IDataAnalysisProblemData)
218        problemData = (IDataAnalysisProblemData)dropData;
219      else if (dropData is IDataAnalysisProblem)
220        problemData = ((IDataAnalysisProblem)dropData).ProblemData;
221      else if (dropData is IValueParameter) {
222        var param = (IValueParameter)dropData;
223        problemData = param.Value as DataAnalysisProblemData;
224      }
225      if (problemData == null) return;
226
227      try {
228        problemData.AdjustProblemDataProperties(Content.ProblemData);
229        Content.ProblemData = problemData;
230
231        if (!Content.Name.EndsWith(" with changed problemData"))
232          Content.Name += " with changed problemData";
233      } catch (InvalidOperationException invalidOperationException) {
234        MainFormManager.MainForm.ShowError(invalidOperationException.Message, invalidOperationException);
235      } catch (ArgumentException argumentException) {
236        MainFormManager.MainForm.ShowError(argumentException.Message, argumentException);
237      }
238    }
239    #endregion
240
241  }
242}
Note: See TracBrowser for help on using the repository browser.