source: trunk/CrypPlugins/WorkspaceManager/Model/ModelPersistance.cs @ 1718

Last change on this file since 1718 was 1718, checked in by kopal, 12 years ago
  • added update icon
  • ModelPersistance added handlers to necessary events
File size: 4.3 KB
Line 
1/*                             
2   Copyright 2010 Nils Kopal, Viktor M.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.IO;
22using System.Xml.Serialization;
23using System.Runtime.Serialization.Formatters.Binary;
24using System.Reflection;
25using Cryptool.PluginBase;
26
27namespace WorkspaceManager.Model
28{
29    /// <summary>
30    /// Class with static methods for loading and saving of WorkspaceModels
31    /// </summary>
32    public class ModelPersistance
33    {
34        /// <summary>
35        /// Deserializes a model from the given file with the given filename
36        /// </summary>
37        /// <param name="filename"></param>
38        /// <returns></returns>
39        public static WorkspaceModel loadModel(string filename, WorkspaceManager workspaceManagerEditor)
40        {
41            FileStream fileStream = null;
42
43            try
44            {
45                fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
46                BinaryFormatter binaryFormatter = new BinaryFormatter();
47
48                WorkspaceModel workspacemodel =  (WorkspaceModel)binaryFormatter.Deserialize(fileStream);
49                workspacemodel.WorkspaceManagerEditor = workspaceManagerEditor;
50
51                foreach (PluginModel pluginModel in workspacemodel.AllPluginModels)
52                {
53                    pluginModel.Plugin.OnGuiLogNotificationOccured += workspaceManagerEditor.GuiLogNotificationOccured;
54                    pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
55                    pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
56                }
57               
58                foreach (ConnectorModel connectorModel in workspacemodel.AllConnectorModels)
59                {
60                    if(connectorModel.Outgoing == true){
61                        connectorModel.PluginModel.Plugin.PropertyChanged += connectorModel.PropertyChangedOnPlugin;
62                    }
63
64                    if (connectorModel.IsDynamic == true)
65                    {
66                        DynamicPropertyInfoAttribute dynamicPropertyInfoAttribute = connectorModel.PluginModel.Plugin.GetDynamicPropertyInfo();
67                        EventInfo eventinfo = connectorModel.PluginModel.PluginType.GetEvent(dynamicPropertyInfoAttribute.UpdateDynamicPropertiesEvent);
68                        eventinfo.AddEventHandler(connectorModel.PluginModel.Plugin, new DynamicPropertiesChanged(connectorModel.PropertyTypeChangedOnPlugin));
69                    }
70                }
71
72                return workspacemodel;
73            }
74            finally
75            {
76                if (fileStream != null)
77                {
78                    fileStream.Close();
79                }
80            }
81        }
82
83        /// <summary>
84        /// Serializes the given model to a file with the given filename
85        /// </summary>
86        /// <param name="workspaceModel"></param>
87        /// <param name="filename"></param>
88        /// <returns></returns>
89        public static void saveModel(WorkspaceModel workspaceModel, string filename)
90        {
91            FileStream fileStream = null;
92
93            try
94            {
95                fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
96                BinaryFormatter binaryFormatter = new BinaryFormatter();
97                binaryFormatter.Serialize(fileStream, workspaceModel);               
98            }               
99            finally
100            {
101                if (fileStream != null)
102                {
103                    fileStream.Flush();
104                    fileStream.Close();
105                }
106            }
107        }
108    }
109}
Note: See TracBrowser for help on using the repository browser.