Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.CEDMA.Server/3.3/ProblemView.cs @ 2515

Last change on this file since 2515 was 2391, checked in by gkronber, 15 years ago

Fixed #764 (Delete CEDMA console).

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.Core;
31using HeuristicLab.DataAnalysis;
32using System.Diagnostics;
33
34namespace HeuristicLab.CEDMA.Server {
35  public partial class ProblemView : ViewBase {
36    private Dataset dataset;
37
38    public ProblemView(Dataset dataset) {
39      this.dataset = dataset;
40      InitializeComponent();
41      datasetView.Dataset = dataset;
42      dataset.Changed += (sender, args) => UpdateControls();
43      UpdateControls();
44    }
45
46    private void importButton_Click(object sender, EventArgs e) {
47      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
48        DatasetParser parser = new DatasetParser();
49        bool success = false;
50        try {
51          try {
52            parser.Import(openFileDialog.FileName, true);
53            success = true;
54          }
55          catch (DataFormatException ex) {
56            ShowWarningMessageBox(ex);
57            // not possible to parse strictly => clear and try to parse non-strict
58            parser.Reset();
59            parser.Import(openFileDialog.FileName, false);
60            success = true;
61          }
62        }
63        catch (DataFormatException ex) {
64          // if the non-strict parsing also failed then show the exception
65          ShowErrorMessageBox(ex);
66        }
67        if (success) {
68          dataset.Rows = parser.Rows;
69          dataset.Columns = parser.Columns;
70          dataset.Name = parser.ProblemName;
71          dataset.Samples = new double[dataset.Rows * dataset.Columns];
72          Array.Copy(parser.Samples, dataset.Samples, dataset.Columns * dataset.Rows);
73          datasetView.Dataset = dataset;
74
75          for (int i = 0; i < parser.VariableNames.Length; i++) {
76            dataset.SetVariableName(i, parser.VariableNames[i]);
77          }
78
79
80          Refresh();
81        }
82      }
83    }
84
85    private void ShowWarningMessageBox(Exception ex) {
86      MessageBox.Show(ex.Message,
87                      "Warning - " + ex.GetType().Name,
88                      MessageBoxButtons.OK,
89                      MessageBoxIcon.Warning);
90    }
91    private void ShowErrorMessageBox(Exception ex) {
92      MessageBox.Show(BuildErrorMessage(ex),
93                      "Error - " + ex.GetType().Name,
94                      MessageBoxButtons.OK,
95                      MessageBoxIcon.Error);
96    }
97    private string BuildErrorMessage(Exception ex) {
98      StringBuilder sb = new StringBuilder();
99      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
100
101      while (ex.InnerException != null) {
102        ex = ex.InnerException;
103        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
104      }
105      return sb.ToString();
106    }
107
108  }
109}
Note: See TracBrowser for help on using the repository browser.