[17331] | 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 | 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;
|
---|
[17339] | 47 | File.WriteAllText(saveFileDialog.FileName, gen.GenerateTemplate(alg));
|
---|
[17331] | 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 | }
|
---|