Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimizer/3.3/FileManager.cs @ 2546

Last change on this file since 2546 was 2546, checked in by swagner, 15 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

File size: 4.3 KB
RevLine 
[2546]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.MainForm;
7using HeuristicLab.Core;
8using HeuristicLab.Persistence.Default.Xml;
9using HeuristicLab.Core.Views;
10
11namespace 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}
Note: See TracBrowser for help on using the repository browser.