[2546] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Forms;
|
---|
| 6 | using HeuristicLab.MainForm;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 9 | using HeuristicLab.Core.Views;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Optimizer {
|
---|
| 12 | internal static class FileManager {
|
---|
| 13 |
|
---|
| 14 | private class FileInfo {
|
---|
| 15 | public string Filename { get; set; }
|
---|
| 16 | public bool Compressed { get; set; }
|
---|
| 17 |
|
---|
| 18 | public FileInfo(string filename, bool compressed) {
|
---|
| 19 | Filename = filename;
|
---|
| 20 | Compressed = compressed;
|
---|
| 21 | }
|
---|
| 22 | public FileInfo(string filename)
|
---|
| 23 | : this(filename, true) {
|
---|
| 24 | }
|
---|
| 25 | public FileInfo()
|
---|
| 26 | : this(string.Empty, true) {
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | private static Dictionary<IItem, FileInfo> files = new Dictionary<IItem, FileInfo>();
|
---|
| 31 | private static NewItemDialog newItemDialog = null;
|
---|
| 32 | private static OpenFileDialog openFileDialog = null;
|
---|
| 33 | private static SaveFileDialog saveFileDialog = null;
|
---|
| 34 |
|
---|
| 35 | public static void New() {
|
---|
| 36 | if (newItemDialog == null) newItemDialog = new NewItemDialog();
|
---|
| 37 | if (newItemDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 38 | MainFormManager.MainForm.ShowView(MainFormManager.CreateDefaultView(newItemDialog.Item));
|
---|
| 39 | files.Add(newItemDialog.Item, new FileInfo());
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public static void Open() {
|
---|
| 44 | if (openFileDialog == null) {
|
---|
| 45 | openFileDialog = new OpenFileDialog();
|
---|
| 46 | openFileDialog.Title = "Open Item";
|
---|
| 47 | openFileDialog.FileName = "Item";
|
---|
| 48 | openFileDialog.Multiselect = true;
|
---|
| 49 | openFileDialog.DefaultExt = "hl";
|
---|
| 50 | openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | if (openFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 54 | foreach (string file in openFileDialog.FileNames) {
|
---|
| 55 | IItem item = (IItem)XmlParser.Deserialize(file);
|
---|
| 56 | MainFormManager.MainForm.ShowView(MainFormManager.CreateDefaultView(item));
|
---|
| 57 | files.Add(item, new FileInfo(file));
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public static void Save() {
|
---|
| 63 | IItemView activeView = MainFormManager.MainForm.ActiveView as IItemView;
|
---|
| 64 | if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Item.GetType()))) {
|
---|
| 65 | Save(activeView.Item);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | private static void Save(IItem item) {
|
---|
| 69 | if (files[item].Filename != string.Empty) {
|
---|
| 70 | if (files[item].Compressed)
|
---|
| 71 | XmlGenerator.Serialize(item, saveFileDialog.FileName, 9);
|
---|
| 72 | else
|
---|
| 73 | XmlGenerator.Serialize(item, saveFileDialog.FileName, 0);
|
---|
| 74 | } else {
|
---|
| 75 | SaveAs(item);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public static void SaveAs() {
|
---|
| 80 | IItemView activeView = MainFormManager.MainForm.ActiveView as IItemView;
|
---|
| 81 | if ((activeView != null) && (CreatableAttribute.IsCreatable(activeView.Item.GetType()))) {
|
---|
| 82 | SaveAs(activeView.Item);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | public static void SaveAs(IItem item) {
|
---|
| 86 | if (saveFileDialog == null) {
|
---|
| 87 | saveFileDialog = new SaveFileDialog();
|
---|
| 88 | saveFileDialog.Title = "Save Item";
|
---|
| 89 | saveFileDialog.DefaultExt = "hl";
|
---|
| 90 | saveFileDialog.Filter = "Uncompressed HeuristicLab Files|*.hl|HeuristicLab Files|*.hl|All Files|*.*";
|
---|
| 91 | saveFileDialog.FilterIndex = 2;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | saveFileDialog.FileName = files[item].Filename;
|
---|
| 95 | if (saveFileDialog.FileName == string.Empty) saveFileDialog.FileName = "Item";
|
---|
| 96 | if (! files[item].Compressed)
|
---|
| 97 | saveFileDialog.FilterIndex = 1;
|
---|
| 98 | else
|
---|
| 99 | saveFileDialog.FilterIndex = 2;
|
---|
| 100 |
|
---|
| 101 | if (saveFileDialog.ShowDialog() == DialogResult.OK) {
|
---|
| 102 | if (saveFileDialog.FilterIndex == 1) {
|
---|
| 103 | XmlGenerator.Serialize(item, saveFileDialog.FileName, 0);
|
---|
| 104 | } else {
|
---|
| 105 | XmlGenerator.Serialize(item, saveFileDialog.FileName, 9);
|
---|
| 106 | }
|
---|
| 107 | files[item].Filename = saveFileDialog.FileName;
|
---|
| 108 | files[item].Compressed = saveFileDialog.FilterIndex != 1;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public static void SaveAll() {
|
---|
| 113 | var views = from v in MainFormManager.MainForm.Views
|
---|
| 114 | where v is IItemView
|
---|
| 115 | where CreatableAttribute.IsCreatable(((IItemView)v).Item.GetType())
|
---|
| 116 | select v as IItemView;
|
---|
| 117 |
|
---|
| 118 | foreach (IItemView view in views) {
|
---|
| 119 | Save(view.Item);
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|