Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/FileManager.cs @ 17339

Last change on this file since 17339 was 17339, checked in by dpiringe, 5 years ago

#3026 fixed a bug with path generation

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using System.Windows.Forms;
8using HeuristicLab.Common;
9using HeuristicLab.Core;
10using HeuristicLab.MainForm;
11using HeuristicLab.Optimization;
12using HeuristicLab.PluginInfrastructure;
13
14namespace HeuristicLab.JsonInterface.OptimizerIntegration {
15  internal  static class FileManager {
16    private static SaveFileDialog saveFileDialog;
17    private static OpenFileDialog openFileDialog;
18
19    public static void ExportJsonTemplate() {
20      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
21      if (activeView != null) {
22        ExportJsonTemplate(activeView);
23      }
24    }
25    public static void ExportJsonTemplate(IContentView view) {
26      IStorableContent content = view.Content as IStorableContent;
27      if (!view.Locked && content != null) {
28        if (saveFileDialog == null) {
29          saveFileDialog = new SaveFileDialog();
30          saveFileDialog.Title = "Export .json-Template";
31          saveFileDialog.DefaultExt = "json";
32          saveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
33          saveFileDialog.FilterIndex = 1;
34        }
35
36        INamedItem namedItem = content as INamedItem;
37        string suggestedFileName = string.Empty;
38        if (!string.IsNullOrEmpty(content.Filename)) suggestedFileName = content.Filename;
39        else if (namedItem != null) suggestedFileName = namedItem.Name;
40        else suggestedFileName = "Item";
41
42        saveFileDialog.FileName = suggestedFileName + " " + namedItem.GetType().Name;
43
44        if (saveFileDialog.ShowDialog() == DialogResult.OK) {
45          JCGenerator gen = new JCGenerator();
46          IAlgorithm alg = namedItem as IAlgorithm;
47          File.WriteAllText(saveFileDialog.FileName, gen.GenerateTemplate(alg));
48        }
49      }
50    }
51
52
53    public static void ImportJsonTemplate() {
54      if (openFileDialog == null) {
55        openFileDialog = new OpenFileDialog();
56        openFileDialog.Title = "Import .json-Template";
57        openFileDialog.FileName = "Item";
58        openFileDialog.Multiselect = false;
59        openFileDialog.DefaultExt = "json";
60        openFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
61      }
62
63      if (openFileDialog.ShowDialog() == DialogResult.OK) {
64        JCInstantiator instantiator = new JCInstantiator();
65        try {
66          var content = instantiator.Instantiate(openFileDialog.FileName);
67          IView view = MainFormManager.MainForm.ShowContent(content);
68          if (view == null)
69            ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
70        } catch (Exception ex) {
71          ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
72        } finally {
73          ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
74        }
75      }
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.