Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10355 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
RevLine 
[3884]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3884]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
[5829]21
[3884]22using System;
[5829]23using System.Collections.Generic;
[9973]24using System.ComponentModel;
[6612]25using System.Drawing;
26using System.Linq;
[10174]27using System.Text;
[3884]28using System.Windows.Forms;
[6692]29using HeuristicLab.Core;
[6652]30using HeuristicLab.Core.Views;
[3884]31using HeuristicLab.MainForm;
[6652]32using HeuristicLab.Optimization;
[5829]33using HeuristicLab.Optimization.Views;
[10175]34using HeuristicLab.Persistence.Default.Xml;
[9973]35using HeuristicLab.PluginInfrastructure;
[3884]36
37namespace HeuristicLab.Problems.DataAnalysis.Views {
[9973]38
[5834]39  [View("DataAnalysisSolution View")]
[6652]40  [Content(typeof(DataAnalysisSolution), false)]
[9974]41  public partial class DataAnalysisSolutionView : NamedItemCollectionView<IResult> {
[3884]42    public DataAnalysisSolutionView() {
43      InitializeComponent();
[6652]44      viewHost.ViewsLabelVisible = false;
[3884]45    }
46
[5829]47    public new DataAnalysisSolution Content {
48      get { return (DataAnalysisSolution)base.Content; }
[3884]49      set { base.Content = value; }
50    }
51
[6666]52    protected override void SetEnabledStateOfControls() {
53      base.SetEnabledStateOfControls();
54      addButton.Enabled = false;
55      removeButton.Enabled = false;
[9973]56      if (Content == null) {
57        exportButton.Enabled = false;
[10175]58        loadProblemDataButton.Enabled = false;
[9973]59      } else {
60        exportButton.Enabled = !Locked;
[10175]61        loadProblemDataButton.Enabled = !Locked;
[9973]62      }
[6666]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
[3884]77    protected override void OnContentChanged() {
[5829]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
[3884]82      base.OnContentChanged();
[6642]83      AddEvaluationViewTypes();
[5829]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        }
[3884]91      }
92    }
93
[6652]94    protected override IResult CreateItem() {
95      return null;
96    }
97
[6642]98    protected virtual void AddEvaluationViewTypes() {
[6666]99      if (Content != null && !Content.ProblemData.IsEmpty) {
[6642]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
[5829]107    protected override void itemsListView_DoubleClick(object sender, EventArgs e) {
[6652]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) {
[5829]119        MainFormManager.MainForm.ShowContent(Content, viewType);
[6652]120      }
[3884]121    }
[3915]122
[5829]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);
[3884]131    }
[3915]132
[10175]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);
[9973]136
[10175]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
[9973]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
[6612]180    protected void AddViewListViewItem(Type viewType, Image image) {
[5829]181      ListViewItem listViewItem = new ListViewItem();
182      listViewItem.Text = ViewAttribute.GetViewName(viewType);
[6612]183      itemsListView.SmallImageList.Images.Add(image);
[5829]184      listViewItem.ImageIndex = itemsListView.SmallImageList.Images.Count - 1;
185      listViewItem.Tag = viewType;
186      itemsListView.Items.Add(listViewItem);
187
188      AdjustListViewColumnSizes();
[3915]189    }
[5829]190
191    protected void RemoveViewListViewItem(Type viewType) {
[6612]192      List<ListViewItem> itemsToRemove = itemsListView.Items.Cast<ListViewItem>().Where(item => item.Tag as Type == viewType).ToList();
[5829]193
194      foreach (ListViewItem item in itemsToRemove)
195        itemsListView.Items.Remove(item);
[3884]196    }
[6653]197
[8125]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
[8798]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
[6653]222    #region drag and drop
223    protected override void itemsListView_DragEnter(object sender, DragEventArgs e) {
224      validDragOperation = false;
[6692]225      if (ReadOnly) return;
226
227      var dropData = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
228      if (dropData is DataAnalysisProblemData) validDragOperation = true;
[10173]229      else if (dropData is IDataAnalysisProblem) validDragOperation = true;
[6692]230      else if (dropData is IValueParameter) {
231        var param = (IValueParameter)dropData;
[10173]232        if (param.Value is IDataAnalysisProblemData) validDragOperation = true;
[6653]233      }
234    }
235
236    protected override void itemsListView_DragDrop(object sender, DragEventArgs e) {
[10174]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;
[6653]248      }
[10174]249      if (problemData == null) return;
250      CheckCompatibilityOfProblemData(problemData);
251      Content.ProblemData = (IDataAnalysisProblemData)problemData.Clone();
[6653]252    }
253    #endregion
[10174]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
[3884]271  }
272}
Note: See TracBrowser for help on using the repository browser.