1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using System.Windows.Forms;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Core;
|
---|
10 | using HeuristicLab.MainForm;
|
---|
11 | using HeuristicLab.Optimization;
|
---|
12 | using HeuristicLab.PluginInfrastructure;
|
---|
13 |
|
---|
14 | namespace 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 | // TODO: view to select free params, warning if no results are generated
|
---|
27 | IStorableContent content = view.Content as IStorableContent;
|
---|
28 | if (!view.Locked && content != null) {
|
---|
29 | if (saveFileDialog == null) {
|
---|
30 | saveFileDialog = new SaveFileDialog();
|
---|
31 | saveFileDialog.Title = "Export .json-Template";
|
---|
32 | saveFileDialog.DefaultExt = "json";
|
---|
33 | saveFileDialog.Filter = ".json-Template|*.json|All Files|*.*";
|
---|
34 | saveFileDialog.FilterIndex = 1;
|
---|
35 | }
|
---|
36 |
|
---|
37 | INamedItem namedItem = content as INamedItem;
|
---|
38 | string suggestedFileName = string.Empty;
|
---|
39 | if (!string.IsNullOrEmpty(content.Filename)) suggestedFileName = content.Filename;
|
---|
40 | else if (namedItem != null) suggestedFileName = namedItem.Name;
|
---|
41 | else suggestedFileName = "Item";
|
---|
42 |
|
---|
43 | saveFileDialog.FileName = suggestedFileName + " " + namedItem.GetType().Name;
|
---|
44 |
|
---|
45 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
46 | IAlgorithm alg = namedItem as IAlgorithm;
|
---|
47 | File.WriteAllText(saveFileDialog.FileName, JCGenerator.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 | try {
|
---|
65 | var content = JsonTemplateInstantiator.Instantiate(openFileDialog.FileName);
|
---|
66 | IView view = MainFormManager.MainForm.ShowContent(content);
|
---|
67 | if (view == null)
|
---|
68 | ErrorHandling.ShowErrorDialog("There is no view for the loaded item. It cannot be displayed.", new InvalidOperationException("No View Available"));
|
---|
69 | } catch (Exception ex) {
|
---|
70 | ErrorHandling.ShowErrorDialog((Control)MainFormManager.MainForm, "Cannot open file.", ex);
|
---|
71 | } finally {
|
---|
72 | ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).ResetAppStartingCursor();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|