Ignore:
Timestamp:
07/14/16 22:38:23 (6 years ago)
Author:
mkommend
Message:

#2559: Merged r13502, r13504, r13507, r13508, r13512, r13514, r13517 into stable.

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.DataPreprocessing.Views

  • stable/HeuristicLab.DataPreprocessing.Views/3.4/DataPreprocessingView.cs

    r12718 r14075  
    2121
    2222using System;
     23using System.IO;
     24using System.Threading.Tasks;
    2325using System.Windows.Forms;
    2426using HeuristicLab.Common;
     
    2628using HeuristicLab.Core.Views;
    2729using HeuristicLab.MainForm;
     30using HeuristicLab.Problems.DataAnalysis;
     31using HeuristicLab.Problems.Instances.DataAnalysis;
     32using HeuristicLab.Problems.Instances.DataAnalysis.Views;
    2833
    2934namespace HeuristicLab.DataPreprocessing.Views {
    3035  [View("DataPreprocessing View")]
    3136  [Content(typeof(PreprocessingContext), true)]
    32   [Content(typeof(IPreprocessingContext), false)]
    33   public partial class DataPreprocessingView : ItemView {
     37  public partial class DataPreprocessingView : NamedItemView {
    3438
    3539    public DataPreprocessingView() {
     
    3741    }
    3842
    39     public new IPreprocessingContext Content {
    40       get { return (IPreprocessingContext)base.Content; }
     43    public new PreprocessingContext Content {
     44      get { return (PreprocessingContext)base.Content; }
    4145      set { base.Content = value; }
    4246    }
     
    6064          new CorrelationMatrixContent(Content),
    6165          new DataCompletenessChartContent(searchLogic),
    62          
     66
    6367          new FilterContent(filterLogic),
    6468          new ManipulationContent(manipulationLogic, searchLogic, filterLogic),
     
    6771
    6872        viewShortcutListView.Content = viewShortcuts.AsReadOnly();
    69 
    7073        viewShortcutListView.ItemsListView.Items[0].Selected = true;
    7174        viewShortcutListView.Select();
    7275
     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);
    7390      } else {
    7491        viewShortcutListView.Content = null;
    7592      }
    7693    }
    77 
    7894    protected override void RegisterContentEvents() {
    7995      base.RegisterContentEvents();
     96      Content.Reset += Content_Reset;
    8097      Content.Data.FilterChanged += Data_FilterChanged;
    8198    }
     
    83100    protected override void DeregisterContentEvents() {
    84101      base.DeregisterContentEvents();
     102      Content.Reset -= Content_Reset;
    85103      Content.Data.FilterChanged -= Data_FilterChanged;
     104    }
     105
     106    void Content_Reset(object sender, EventArgs e) {
     107      OnContentChanged(); // Reset by setting new content
    86108    }
    87109
     
    94116      viewShortcutListView.Enabled = Content != null;
    95117      applyInNewTabButton.Enabled = Content != null;
    96       exportProblemButton.Enabled = Content != null && Content.Problem != null;
     118      exportProblemButton.Enabled = Content != null && Content.CanExport;
    97119      undoButton.Enabled = Content != null;
    98120    }
    99121
     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) {
     127      Content.Import(new RegressionProblemData());
     128    }
     129    private void newClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
     130      Content.Import(new ClassificationProblemData());
     131    }
     132    private void newTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
     133      Content.Import(new TimeSeriesPrognosisProblemData());
     134    }
     135    #endregion
     136
     137    #region Import
     138    private void importButton_Click(object sender, EventArgs e) {
     139      importProblemDataTypeContextMenuStrip.Show(Cursor.Position);
     140    }
     141    private void importRegressionToolStripMenuItem_Click(object sender, EventArgs e) {
     142      Import(new RegressionCSVInstanceProvider(), new RegressionImportTypeDialog(),
     143        (dialog => ((RegressionImportTypeDialog)dialog).ImportType));
     144    }
     145    private void importClassificationToolStripMenuItem_Click(object sender, EventArgs e) {
     146      Import(new ClassificationCSVInstanceProvider(), new ClassificationImportTypeDialog(),
     147        (dialog => ((ClassificationImportTypeDialog)dialog).ImportType));
     148    }
     149    private void importTimeSeriesToolStripMenuItem_Click(object sender, EventArgs e) {
     150      Import(new TimeSeriesPrognosisCSVInstanceProvider(), new TimeSeriesPrognosisImportTypeDialog(),
     151        (dialog => ((TimeSeriesPrognosisImportTypeDialog)dialog).ImportType));
     152    }
     153    private void Import<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider, DataAnalysisImportTypeDialog importTypeDialog,
     154      Func<DataAnalysisImportTypeDialog, TImportType> getImportType)
     155      where TProblemData : class, IDataAnalysisProblemData
     156      where TImportType : DataAnalysisImportType {
     157      if (importTypeDialog.ShowDialog() == DialogResult.OK) {
     158        Task.Run(() => {
     159          TProblemData instance;
     160          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
     161          // lock active view and show progress bar
     162          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
     163
     164          try {
     165            var progress = mainForm.AddOperationProgressToContent(activeView.Content, "Loading problem instance.");
     166
     167            instanceProvider.ProgressChanged += (o, args) => { progress.ProgressValue = args.ProgressPercentage / 100.0; };
     168
     169            instance = instanceProvider.ImportData(importTypeDialog.Path, getImportType(importTypeDialog), importTypeDialog.CSVFormat);
     170          } catch (IOException ex) {
     171            MessageBox.Show(string.Format("There was an error parsing the file: {0}", Environment.NewLine + ex.Message), "Error while parsing", MessageBoxButtons.OK, MessageBoxIcon.Error);
     172            mainForm.RemoveOperationProgressFromContent(activeView.Content);
     173            return;
     174          }
     175          try {
     176            Content.Import(instance);
     177          } catch (IOException ex) {
     178            MessageBox.Show(string.Format("This problem does not support loading the instance {0}: {1}", Path.GetFileName(importTypeDialog.Path), Environment.NewLine + ex.Message), "Cannot load instance");
     179          } finally {
     180            mainForm.RemoveOperationProgressFromContent(activeView.Content);
     181          }
     182        });
     183      }
     184    }
     185    #endregion
     186
     187    #region Apply
     188    private void applyInNewTabButton_Click(object sender, EventArgs e) {
     189      applyTypeContextMenuStrip.Show(Cursor.Position);
     190    }
     191    private void applyToolStripMenuItem_Click(object sender, EventArgs e) {
     192      var menuItem = (ToolStripMenuItem)sender;
     193      var itemCreator = (Func<IItem>)menuItem.Tag;
     194      MainFormManager.MainForm.ShowContent(itemCreator());
     195    }
     196    #endregion
     197
     198    #region Export
    100199    private void exportProblemButton_Click(object sender, EventArgs e) {
    101       var problem = Content.ExportProblem();
    102 
     200      exportTypeContextMenuStrip.Show(Cursor.Position);
     201    }
     202    private void exportToolStripMenuItem_Click(object sender, EventArgs e) {
     203      var menuItem = (ToolStripMenuItem)sender;
     204      var itemCreator = (Func<IItem>)menuItem.Tag;
    103205      var saveFileDialog = new SaveFileDialog {
    104206        Title = "Save Item",
     
    108210      };
    109211
    110       var content = problem as IStorableContent;
    111212      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    112         bool compressed = saveFileDialog.FilterIndex != 1;
    113         ContentManager.Save(content, saveFileDialog.FileName, compressed);
    114       }
    115     }
    116 
    117     private void applyInNewTabButton_Click(object sender, EventArgs e) {
    118       var item = Content.Export();
    119 
    120       MainFormManager.MainForm.ShowContent(item);
    121     }
    122 
     213        Task.Run(() => {
     214          bool compressed = saveFileDialog.FilterIndex != 1;
     215          var storable = itemCreator() as IStorableContent;
     216          if (storable != null) {
     217            var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
     218            var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
     219            try {
     220              mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
     221              ContentManager.Save(storable, saveFileDialog.FileName, compressed);
     222            } finally {
     223              mainForm.RemoveOperationProgressFromContent(activeView.Content);
     224            }
     225          }
     226        });
     227      }
     228    }
     229    private void exportCsvMenuItem_Click(object sender, EventArgs e) {
     230      var saveFileDialog = new SaveFileDialog {
     231        Title = "Save Data",
     232        DefaultExt = "csv",
     233        Filter = "CSV files|*.csv|All files|*.*",
     234        FilterIndex = 1
     235      };
     236
     237      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
     238        Task.Run(() => {
     239          var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
     240          var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
     241          try {
     242            var problemData = Content.CreateNewProblemData();
     243            mainForm.AddOperationProgressToContent(activeView.Content, "Exporting data.");
     244            if (problemData is TimeSeriesPrognosisProblemData)
     245              Export(new TimeSeriesPrognosisCSVInstanceProvider(), problemData, saveFileDialog.FileName);
     246            else if (problemData is RegressionProblemData)
     247              Export(new RegressionCSVInstanceProvider(), problemData, saveFileDialog.FileName);
     248            else if (problemData is ClassificationProblemData)
     249              Export(new ClassificationCSVInstanceProvider(), problemData, saveFileDialog.FileName);
     250          } finally {
     251            mainForm.RemoveOperationProgressFromContent(activeView.Content);
     252          }
     253        });
     254      }
     255    }
     256    private void Export<TProblemData, TImportType>(DataAnalysisInstanceProvider<TProblemData, TImportType> instanceProvider,
     257      IDataAnalysisProblemData problemData, string path)
     258      where TProblemData : class, IDataAnalysisProblemData where TImportType : DataAnalysisImportType {
     259      instanceProvider.ExportData((TProblemData)problemData, path);
     260    }
     261    #endregion
     262
     263    #region Undo / Redo
    123264    private void undoButton_Click(object sender, EventArgs e) {
    124265      Content.Data.Undo();
    125266    }
     267    #endregion
    126268  }
    127269}
Note: See TracChangeset for help on using the changeset viewer.