Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • deleted: ConvertableAttribute, DummyConverter, ObjectExtensions
  • renamed: CustomJsonWriter -> SingleLineArrayJsonWriter, JCInstantiator -> JsonTemplateInstantiator
  • added: JsonItemConverterFactory, UnsupportedJsonItem
  • IJsonItemConverter:
    • added two new properties: Priority and ConvertableType -> because converters are automatically collected by plugin infrastructure now
    • Extract, Inject references a root converter now -> typically an instance of JsonItemConverter -> to prevent cycles
  • JsonItemConverter:
    • now implements the interface IJsonItemConverter
    • is now a dynamic class
    • is only instantiable with an factory (JsonItemConverterFactory)
    • still has the old (but now public) static methods Extract and Inject (without ref param IJsonItemConverter root) -> creates instance with factory and calls methods of instance
    • removed register and unregister methods, because the factory collects all converters automatically now (on first call of Create)
    • has cycle detection for Extract and Inject
    • renamed method Get to GetConverter
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      // 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}
Note: See TracBrowser for help on using the repository browser.