Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs @ 15486

Last change on this file since 15486 was 15486, checked in by mkommend, 5 years ago

#2860: Cleared filename after a new problemData is set for data analysis solutions.

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Persistence.Default.Xml;
32using HeuristicLab.PluginInfrastructure;
33
34namespace HeuristicLab.Problems.DataAnalysis.Views {
35
36  [View("DataAnalysisSolution View")]
37  [Content(typeof(DataAnalysisSolution), false)]
38  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
39    public DataAnalysisSolutionView() {
40      InitializeComponent();
41      viewHost.ViewsLabelVisible = false;
42    }
43
44    public new DataAnalysisSolution Content {
45      get { return (DataAnalysisSolution)base.Content; }
46      set { base.Content = value; }
47    }
48
49    protected override void SetEnabledStateOfControls() {
50      base.SetEnabledStateOfControls();
51      addButton.Enabled = false;
52      removeButton.Enabled = false;
53      loadProblemDataButton.Enabled = Content != null && !Locked;
54    }
55
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
59    }
60    protected override void DeregisterContentEvents() {
61      base.DeregisterContentEvents();
62      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
63    }
64    private void Content_ProblemDataChanged(object sender, EventArgs e) {
65      OnContentChanged();
66    }
67
68    protected override void OnContentChanged() {
69      string selectedName = null;
70      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
71        selectedName = itemsListView.SelectedItems[0].Text;
72
73      base.OnContentChanged();
74      AddEvaluationViewTypes();
75
76      //recover selection
77      if (selectedName != null) {
78        foreach (ListViewItem item in itemsListView.Items) {
79          if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
80            item.Selected = true;
81        }
82      }
83    }
84
85    protected override IResult CreateItem() {
86      return null;
87    }
88
89    protected virtual void AddEvaluationViewTypes() {
90      if (Content != null && !Content.ProblemData.IsEmpty) {
91        var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
92          .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
93        foreach (var viewType in viewTypes)
94          AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
95      }
96    }
97
98    protected sealed override void itemsListView_DoubleClick(object sender, EventArgs e) {
99      if (itemsListView.SelectedItems.Count != 1) return;
100
101      IResult result = itemsListView.SelectedItems[0].Tag as IResult;
102      Type viewType = itemsListView.SelectedItems[0].Tag as Type;
103      if (result != null) {
104        IContentView view = MainFormManager.MainForm.ShowContent(result.Value);
105        if (view != null) {
106          view.Caption = result.Name;
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
145        solution.ProblemData = problemData;
146        if (!solution.Name.EndsWith(" with loaded problemData"))
147          solution.Name += " with loaded problemData";
148        MainFormManager.MainForm.ShowContent(solution);
149      }
150      catch (InvalidOperationException invalidOperationException) {
151        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
152      }
153      catch (ArgumentException argumentException) {
154        ErrorHandling.ShowErrorDialog(this, argumentException);
155      }
156    }
157
158    protected void AddViewListViewItem(Type viewType, Image image) {
159      ListViewItem listViewItem = new ListViewItem();
160      listViewItem.Text = ViewAttribute.GetViewName(viewType);
161      itemsListView.SmallImageList.Images.Add(image);
162      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
163      listViewItem.Tag = viewType;
164      itemsListView.Items.Add(listViewItem);
165
166      AdjustListViewColumnSizes();
167    }
168
169    protected void RemoveViewListViewItem(Type viewType) {
170      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
171
172      foreach (ListViewItem item in itemsToRemove)
173        itemsListView.Items.Remove(item);
174    }
175
176    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
177      if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
178        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
179        viewHost.ViewType = viewType;
180        viewHost.Content = Content;
181        splitContainer.Panel2Collapsed = false;
182        detailsGroupBox.Enabled = true;
183      } else base.showDetailsCheckBox_CheckedChanged(sender, e);
184    }
185
186    protected override void RebuildImageList() {
187      itemsListView.SmallImageList.Images.Clear();
188      foreach (ListViewItem listViewItem in itemsListView.Items) {
189        IResult result = listViewItem.Tag as IResult;
190        Type viewType = listViewItem.Tag as Type;
191        if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
192        else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
193          itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
194        else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
195
196        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
197      }
198    }
199
200    #region drag and drop
201    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
202      validDragOperation = false;
203      if (ReadOnly) return;
204      if (e.Effect != DragDropEffects.Copy) return;
205
206      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
207      if (dropData is IDataAnalysisProblemData) validDragOperation = true;
208      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
209      else if (dropData is IValueParameter) {
210        var param = (IValueParameter)dropData;
211        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
212      }
213    }
214
215    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
216      if (e.Effect != DragDropEffects.Copy) return;
217
218      IDataAnalysisProblemData problemData = null;
219      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
220      if (dropData is IDataAnalysisProblemData)
221        problemData = (IDataAnalysisProblemData)dropData;
222      else if (dropData is IDataAnalysisProblem)
223        problemData = ((IDataAnalysisProblem)dropData).ProblemData;
224      else if (dropData is IValueParameter) {
225        var param = (IValueParameter)dropData;
226        problemData = param.Value as DataAnalysisProblemData;
227      }
228      if (problemData == null) return;
229
230      problemData = (IDataAnalysisProblemData)problemData.Clone();
231
232      try {
233        problemData.AdjustProblemDataProperties(Content.ProblemData);
234        Content.ProblemData = problemData;
235
236        if (!Content.Name.EndsWith(" with changed problemData"))
237          Content.Name += " with changed problemData";
238        Content.Filename = string.Empty;
239        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().UpdateTitle();
240      }
241      catch (InvalidOperationException invalidOperationException) {
242        ErrorHandling.ShowErrorDialog(this, invalidOperationException);
243      }
244      catch (ArgumentException argumentException) {
245        ErrorHandling.ShowErrorDialog(this, argumentException);
246      }
247    }
248    #endregion
249
250  }
251}
Note: See TracBrowser for help on using the repository browser.