Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/DataAnalysisSolutionView.cs @ 12858

Last change on this file since 12858 was 10175, checked in by mkommend, 11 years ago

#1758: Added button to load new data into data analysis solutions.

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.ComponentModel;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.Optimization;
33using HeuristicLab.Optimization.Views;
34using HeuristicLab.Persistence.Default.Xml;
35using HeuristicLab.PluginInfrastructure;
36
37namespace HeuristicLab.Problems.DataAnalysis.Views {
38
39  [View("DataAnalysisSolution View")]
40  [Content(typeof(DataAnalysisSolution), false)]
41  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
42    public DataAnalysisSolutionView() {
43      InitializeComponent();
44      viewHost.ViewsLabelVisible = false;
45    }
46
47    public new DataAnalysisSolution Content {
48      get { return (DataAnalysisSolution)base.Content; }
49      set { base.Content = value; }
50    }
51
52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      addButton.Enabled = false;
55      removeButton.Enabled = false;
56      if (Content == null) {
57        exportButton.Enabled = false;
58        loadProblemDataButton.Enabled = false;
59      } else {
60        exportButton.Enabled = !Locked;
61        loadProblemDataButton.Enabled = !Locked;
62      }
63    }
64
65    protected override void RegisterContentEvents() {
66      base.RegisterContentEvents();
67      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
68    }
69    protected override void DeregisterContentEvents() {
70      base.DeregisterContentEvents();
71      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
72    }
73    private void Content_ProblemDataChanged(object sender, EventArgs e) {
74      OnContentChanged();
75    }
76
77    protected override void OnContentChanged() {
78      string selectedName = null;
79      if ((itemsListView.SelectedItems.Count == 1) && (itemsListView.SelectedItems[0].Tag != null && itemsListView.SelectedItems[0].Tag is Type))
80        selectedName = itemsListView.SelectedItems[0].Text;
81
82      base.OnContentChanged();
83      AddEvaluationViewTypes();
84
85      //recover selection
86      if (selectedName != null) {
87        foreach (ListViewItem item in itemsListView.Items) {
88          if (item.Tag != null && item.Tag is Type && item.Text == selectedName)
89            item.Selected = true;
90        }
91      }
92    }
93
94    protected override IResult CreateItem() {
95      return null;
96    }
97
98    protected virtual void AddEvaluationViewTypes() {
99      if (Content != null && !Content.ProblemData.IsEmpty) {
100        var viewTypes = MainFormManager.GetViewTypes(Content.GetType(), true)
101          .Where(t => typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(t));
102        foreach (var viewType in viewTypes)
103          AddViewListViewItem(viewType, ((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
104      }
105    }
106
107    protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
108      if (itemsListView.SelectedItems.Count != 1) return;
109
110      IResult result = itemsListView.SelectedItems[0].Tag as IResult;
111      Type viewType = itemsListView.SelectedItems[0].Tag as Type;
112      if (result != null) {
113        IContentView view = MainFormManager.MainForm.ShowContent(result, typeof(ResultView));
114        if (view != null) {
115          view.ReadOnly = ReadOnly;
116          view.Locked = Locked;
117        }
118      } else if (viewType != null) {
119        MainFormManager.MainForm.ShowContent(Content, viewType);
120      }
121    }
122
123    protected override void itemsListView_SelectedIndexChanged(object sender, EventArgs e) {
124      if (itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
125        detailsGroupBox.Enabled = true;
126        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
127        viewHost.ViewType = viewType;
128        viewHost.Content = Content;
129      } else
130        base.itemsListView_SelectedIndexChanged(sender, e);
131    }
132
133    protected virtual void loadProblemDataButton_Click(object sender, EventArgs e) {
134      if (loadProblemDataFileDialog.ShowDialog(this) != DialogResult.OK) return;
135      object hlFile = XmlParser.Deserialize(loadProblemDataFileDialog.FileName);
136
137      IDataAnalysisProblemData problemData = null;
138      if (hlFile is IDataAnalysisProblemData) {
139        problemData = (IDataAnalysisProblemData)hlFile;
140      } else if (hlFile is IDataAnalysisProblem) {
141        problemData = ((IDataAnalysisProblem)hlFile).ProblemData;
142      } else if (hlFile is IDataAnalysisSolution) {
143        problemData = ((IDataAnalysisSolution)hlFile).ProblemData;
144      }
145
146      if (problemData == null) {
147        ErrorHandling.ShowErrorDialog(this, new NullReferenceException("The problem data is null." + Environment.NewLine
148                                      + "The .hl-file contains no DataAnalysisProblemData or DataAnylsisProblem."));
149        return;
150      }
151
152      if (CheckCompatibilityOfProblemData(problemData)) {
153        var solution = (IDataAnalysisSolution)Content.Clone();
154        solution.ProblemData = problemData;
155        solution.Name += " with loaded problem data (" + loadProblemDataFileDialog + ")";
156        MainFormManager.MainForm.ShowContent(solution);
157      }
158    }
159
160    private void exportButton_Click(object sender, EventArgs e) {
161      var exporters = ApplicationManager.Manager.GetInstances<IDataAnalysisSolutionExporter>()
162        .Where(exporter => exporter.Supports(Content)).ToArray();
163      exportFileDialog.Filter = exporters.Skip(1)
164        .Aggregate(exporters.First().FileTypeFilter, (s, exporter) => s + "|" + exporter.FileTypeFilter);
165      var result = exportFileDialog.ShowDialog();
166      if (result == DialogResult.OK) {
167
168        var name = exportFileDialog.FileName;
169        var selectedExporter = exporters.Single(exporter => exporter.FileTypeFilter == exportFileDialog.Filter);
170
171        using (BackgroundWorker bg = new BackgroundWorker()) {
172          MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, "Exportion solution to " + name + ".");
173          bg.DoWork += (_, __) => selectedExporter.Export(Content, name);
174          bg.RunWorkerCompleted += (_, __) => MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
175          bg.RunWorkerAsync();
176        }
177      }
178    }
179
180    protected void AddViewListViewItem(Type viewType, Image image) {
181      ListViewItem listViewItem = new ListViewItem();
182      listViewItem.Text = ViewAttribute.GetViewName(viewType);
183      itemsListView.SmallImageList.Images.Add(image);
184      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
185      listViewItem.Tag = viewType;
186      itemsListView.Items.Add(listViewItem);
187
188      AdjustListViewColumnSizes();
189    }
190
191    protected void RemoveViewListViewItem(Type viewType) {
192      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
193
194      foreach (ListViewItem item in itemsToRemove)
195        itemsListView.Items.Remove(item);
196    }
197
198    protected override void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
199      if (showDetailsCheckBox.Checked && itemsListView.SelectedItems.Count == 1 && itemsListView.SelectedItems[0].Tag is Type) {
200        Type viewType = (Type)itemsListView.SelectedItems[0].Tag;
201        viewHost.ViewType = viewType;
202        viewHost.Content = Content;
203        splitContainer.Panel2Collapsed = false;
204        detailsGroupBox.Enabled = true;
205      } else base.showDetailsCheckBox_CheckedChanged(sender, e);
206    }
207
208    protected override void RebuildImageList() {
209      itemsListView.SmallImageList.Images.Clear();
210      foreach (ListViewItem listViewItem in itemsListView.Items) {
211        IResult result = listViewItem.Tag as IResult;
212        Type viewType = listViewItem.Tag as Type;
213        if (result != null) itemsListView.SmallImageList.Images.Add(result.ItemImage);
214        else if (viewType != null && typeof(IDataAnalysisSolutionEvaluationView).IsAssignableFrom(viewType))
215          itemsListView.SmallImageList.Images.Add(((IDataAnalysisSolutionEvaluationView)Activator.CreateInstance(viewType)).ViewImage);
216        else itemsListView.SmallImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.Nothing);
217
218        listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
219      }
220    }
221
222    #region drag and drop
223    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
224      validDragOperation = false;
225      if (ReadOnly) return;
226
227      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
228      if (dropData is DataAnalysisProblemData) validDragOperation = true;
229      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
230      else if (dropData is IValueParameter) {
231        var param = (IValueParameter)dropData;
232        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
233      }
234    }
235
236    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
237      if (e.Effect == DragDropEffects.None) return;
238
239      IDataAnalysisProblemData problemData = null;
240      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
241      if (dropData is IDataAnalysisProblemData)
242        problemData = (IDataAnalysisProblemData)dropData;
243      else if (dropData is IDataAnalysisProblem)
244        problemData = ((IDataAnalysisProblem)dropData).ProblemData;
245      else if (dropData is IValueParameter) {
246        var param = (IValueParameter)dropData;
247        problemData = param.Value as DataAnalysisProblemData;
248      }
249      if (problemData == null) return;
250      CheckCompatibilityOfProblemData(problemData);
251      Content.ProblemData = (IDataAnalysisProblemData)problemData.Clone();
252    }
253    #endregion
254
255    #region load problem data
256    protected virtual bool CheckCompatibilityOfProblemData(IDataAnalysisProblemData problemData) {
257      StringBuilder message = new StringBuilder();
258      List<string> variables = problemData.InputVariables.Select(x => x.Value).ToList();
259      foreach (var item in Content.ProblemData.InputVariables.CheckedItems) {
260        if (!variables.Contains(item.Value.Value))
261          message.AppendLine("Input variable '" + item.Value.Value + "' is not in the new problem data.");
262      }
263
264      if (message.Length != 0) {
265        ErrorHandling.ShowErrorDialog(this, new InvalidOperationException(message.ToString()));
266        return false;
267      }
268      return true;
269    }
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.