Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs @ 15242

Last change on this file since 15242 was 15185, checked in by gkronber, 7 years ago

#2801: renamed classes for import dialogs

File size: 12.0 KB
RevLine 
[10539]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10539]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;
[13508]23using System.IO;
24using System.Threading.Tasks;
[10239]25using System.Windows.Forms;
26using HeuristicLab.Common;
[10625]27using HeuristicLab.Core;
[10254]28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
[13508]30using HeuristicLab.Problems.DataAnalysis;
31using HeuristicLab.Problems.Instances.DataAnalysis;
32using HeuristicLab.Problems.Instances.DataAnalysis.Views;
[10239]33
[10558]34namespace HeuristicLab.DataPreprocessing.Views {
[10239]35  [View("DataPreprocessing View")]
[10614]36  [Content(typeof(PreprocessingContext), true)]
[13507]37  public partial class DataPreprocessingView : NamedItemView {
[15110]38    public new PreprocessingContext Content {
39      get { return (PreprocessingContext)base.Content; }
40      set { base.Content = value; }
41    }
[10998]42
[10239]43    public DataPreprocessingView() {
44      InitializeComponent();
[10258]45    }
[10239]46
[10614]47    protected override void OnContentChanged() {
48      base.OnContentChanged();
49      if (Content != null) {
[10617]50        var data = Content.Data;
[12676]51        var filterLogic = new FilterLogic(data);
52        var searchLogic = new SearchLogic(data, filterLogic);
[10617]53        var statisticsLogic = new StatisticsLogic(data, searchLogic);
[11002]54        var manipulationLogic = new ManipulationLogic(data, searchLogic, statisticsLogic);
[10970]55
[10904]56        var viewShortcuts = new ItemList<IViewShortcut> {
[11002]57          new DataGridContent(data, manipulationLogic, filterLogic),
[15110]58          new StatisticsContent(data, statisticsLogic),
[10921]59
[10992]60          new LineChartContent(data),
61          new HistogramContent(data),
[15110]62          new SingleScatterPlotContent(data),
63          new MultiScatterPlotContent(data),
[10934]64          new CorrelationMatrixContent(Content),
[11002]65          new DataCompletenessChartContent(searchLogic),
[13502]66
[10921]67          new FilterContent(filterLogic),
[11002]68          new ManipulationContent(manipulationLogic, searchLogic, filterLogic),
[10977]69          new TransformationContent(data, filterLogic)
[10617]70        };
[10625]71
[10904]72        viewShortcutListView.Content = viewShortcuts.AsReadOnly();
73        viewShortcutListView.ItemsListView.Items[0].Selected = true;
74        viewShortcutListView.Select();
[10735]75
[13508]76        applyTypeContextMenuStrip.Items.Clear();
77        exportTypeContextMenuStrip.Items.Clear();
78        foreach (var exportOption in Content.GetSourceExportOptions()) {
79          var applyMenuItem = new ToolStripMenuItem(exportOption.Key) { Tag = exportOption.Value };
80          applyMenuItem.Click += applyToolStripMenuItem_Click;
81          applyTypeContextMenuStrip.Items.Add(applyMenuItem);
82
83          var exportMenuItem = new ToolStripMenuItem(exportOption.Key) { Tag = exportOption.Value };
84          exportMenuItem.Click += exportToolStripMenuItem_Click;
85          exportTypeContextMenuStrip.Items.Add(exportMenuItem);
86        }
87        var exportCsvMenuItem = new ToolStripMenuItem(".csv");
88        exportCsvMenuItem.Click += exportCsvMenuItem_Click;
89        exportTypeContextMenuStrip.Items.Add(exportCsvMenuItem);
[10614]90      } else {
[10904]91        viewShortcutListView.Content = null;
[10254]92      }
[10239]93    }
[10969]94    protected override void RegisterContentEvents() {
95      base.RegisterContentEvents();
[13508]96      Content.Reset += Content_Reset;
[10969]97      Content.Data.FilterChanged += Data_FilterChanged;
98    }
99
100    protected override void DeregisterContentEvents() {
101      base.DeregisterContentEvents();
[13508]102      Content.Reset -= Content_Reset;
[10969]103      Content.Data.FilterChanged -= Data_FilterChanged;
104    }
105
[13508]106    void Content_Reset(object sender, EventArgs e) {
107      OnContentChanged(); // Reset by setting new content
108    }
109
[10969]110    void Data_FilterChanged(object sender, EventArgs e) {
111      lblFilterActive.Visible = Content.Data.IsFiltered;
112    }
113
[10614]114    protected override void SetEnabledStateOfControls() {
115      base.SetEnabledStateOfControls();
[10904]116      viewShortcutListView.Enabled = Content != null;
[10614]117      applyInNewTabButton.Enabled = Content != null;
[13502]118      exportProblemButton.Enabled = Content != null && Content.CanExport;
[10614]119      undoButton.Enabled = Content != null;
[10239]120    }
121
[13508]122    #region New
123    private void newButton_Click(object sender, EventArgs e) {
124      newProblemDataTypeContextMenuStrip.Show(Cursor.Position);
125    }
126    private void newRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
[15110]127      if (CheckNew("Regression"))
128        Content.Import(new RegressionProblemData());
[13508]129    }
130    private void newClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
[15110]131      if (CheckNew("Classification"))
132        Content.Import(new ClassificationProblemData());
[13508]133    }
134    private void newTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
[15110]135      if (CheckNew("Time Series Prognosis"))
136        Content.Import(new TimeSeriesPrognosisProblemData());
[13508]137    }
[15110]138
139    private bool CheckNew(string type) {
140      return DialogResult.OK == MessageBox.Show(
141               this,
142               string.Format("When creating a new {0}, all previous information will be lost.", type),
143               "Continue?",
144               MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
145    }
[13508]146    #endregion
147
148    #region Import
149    private void importButton_Click(object sender, EventArgs e) {
150      importProblemDataTypeContextMenuStrip.Show(Cursor.Position);
151    }
152    private void importRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
[15185]153      Import(new RegressionCSVInstanceProvider(), new RegressionImportDialog(),
154        (dialog => ((RegressionImportDialog)dialog).ImportType));
[13508]155    }
156    private void importClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
[15185]157      Import(new ClassificationCSVInstanceProvider(), new ClassificationImportDialog(),
158        (dialog => ((ClassificationImportDialog)dialog).ImportType));
[13508]159    }
160    private void importTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
[15185]161      Import(new TimeSeriesPrognosisCSVInstanceProvider(), new TimeSeriesPrognosisImportDialog(),
162        (dialog => ((TimeSeriesPrognosisImportDialog)dialog).ImportType));
[13508]163    }
[15185]164    private void Import<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider, DataAnalysisImportDialog importDialog,
165      Func<DataAnalysisImportDialog, TImportType> getImportType)
[13508]166      where TProblemData : class, IDataAnalysisProblemData
167      where TImportType : DataAnalysisImportType {
[15185]168      if (importDialog.ShowDialog() == DialogResult.OK) {
[13508]169        Task.Run(() => {
170          TProblemData instance;
171          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
172          // lock active view and show progress bar
173          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
174
175          try {
176            var progress = mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
177
178            instanceProvider.ProgressChanged += (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; };
179
[15185]180            instance = instanceProvider.ImportData(importDialog.Path, getImportType(importDialog), importDialog.CSVFormat);
[13508]181          } catch (IOException ex) {
182            MessageBox.Show(string.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
183            mainForm.RemoveOperationProgressFromContent(activeView.Content);
184            return;
185          }
186          try {
187            Content.Import(instance);
188          } catch (IOException ex) {
[15185]189            MessageBox.Show(string.Format("This problem does not support loading the instance {0}: {1}", Path.GetFileName(importDialog.Path), Environment.NewLine + ex.Message), "Cannot load instance");
[13508]190          } finally {
191            mainForm.RemoveOperationProgressFromContent(activeView.Content);
192          }
193        });
194      }
195    }
196    #endregion
197
198    #region Apply
199    private void applyInNewTabButton_Click(object sender, EventArgs e) {
200      applyTypeContextMenuStrip.Show(Cursor.Position);
201    }
202    private void applyToolStripMenuItem_Click(object sender, EventArgs e) {
203      var menuItem = (ToolStripMenuItem)sender;
204      var itemCreator = (Func<IItem>)menuItem.Tag;
205      MainFormManager.MainForm.ShowContent(itemCreator());
206    }
207    #endregion
208
209    #region Export
[10304]210    private void exportProblemButton_Click(object sender, EventArgs e) {
[13508]211      exportTypeContextMenuStrip.Show(Cursor.Position);
212    }
213    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
214      var menuItem = (ToolStripMenuItem)sender;
215      var itemCreator = (Func<IItem>)menuItem.Tag;
[10307]216      var saveFileDialog = new SaveFileDialog {
217        Title = "Save Item",
218        DefaultExt = "hl",
219        Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*",
220        FilterIndex = 2
221      };
222
223      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
[13508]224        Task.Run(() => {
225          bool compressed = saveFileDialog.FilterIndex != 1;
226          var storable = itemCreator() as IStorableContent;
227          if (storable != null) {
228            var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
229            var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
230            try {
231              mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
232              ContentManager.Save(storable, saveFileDialog.FileName, compressed);
233            } finally {
234              mainForm.RemoveOperationProgressFromContent(activeView.Content);
235            }
236          }
237        });
[10307]238      }
[10304]239    }
[13508]240    private void exportCsvMenuItem_Click(object sender, EventArgs e) {
241      var saveFileDialog = new SaveFileDialog {
242        Title = "Save Data",
243        DefaultExt = "csv",
244        Filter = "CSV files|*.csv|All files|*.*",
245        FilterIndex = 1
246      };
[10304]247
[13508]248      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
249        Task.Run(() => {
250          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
251          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
252          try {
253            var problemData = Content.CreateNewProblemData();
254            mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
255            if (problemData is TimeSeriesPrognosisProblemData)
256              Export(new TimeSeriesPrognosisCSVInstanceProvider(), problemData, saveFileDialog.FileName);
257            else if (problemData is RegressionProblemData)
258              Export(new RegressionCSVInstanceProvider(), problemData, saveFileDialog.FileName);
259            else if (problemData is ClassificationProblemData)
260              Export(new ClassificationCSVInstanceProvider(), problemData, saveFileDialog.FileName);
261          } finally {
262            mainForm.RemoveOperationProgressFromContent(activeView.Content);
263          }
264        });
265      }
[10254]266    }
[13508]267    private void Export<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider,
268      IDataAnalysisProblemData problemData, string path)
269      where TProblemData : class, IDataAnalysisProblemData where TImportType : DataAnalysisImportType {
270      instanceProvider.ExportData((TProblemData)problemData, path);
271    }
272    #endregion
[10550]273
[13508]274    #region Undo / Redo
[10550]275    private void undoButton_Click(object sender, EventArgs e) {
276      Content.Data.Undo();
277    }
[13508]278    #endregion
[10239]279  }
[10310]280}
Note: See TracBrowser for help on using the repository browser.