コード例 #1
0
ファイル: PropertyBag.cs プロジェクト: olesar/Altaxo
        /// <summary>
        /// Converts a property from the lazy representation as Xml string into the real value. Then the lazy property entry
        /// is removed, and the property is stored in the regular property dictionary.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        /// <param name="xml">The xml element to convert from.</param>
        private bool ConvertFromLazy(string propName, string xml)
        {
            using (var info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
            {
                object propval;
                try // deserialization may fail, e.g. because the DLL the type is in is not loaded (e.g. if it is an addin DLL)
                {
                    info.BeginReading(xml);
                    propval = info.GetValue("Value", this);
                }
                catch (Exception)
                {
                    // if deserialization fails, we leave everything as it is.
                    return(false);
                }

                if (propval is IDocumentLeafNode documentLeafNode)
                {
                    documentLeafNode.ParentObject = this;
                }

                _propertiesLazyLoaded.Remove(propName);
                _properties[propName] = propval;
                return(true);
            }
        }
コード例 #2
0
        /// <summary>
        /// Converts a property from the lazy representation as Xml string into the real value. Then the lazy property entry
        /// is removed, and the property is stored in the regular property dictionary.
        /// </summary>
        /// <param name="propName">Name of the property.</param>
        /// <param name="xml">The XML.</param>
        public void ConvertFromLazy(string propName, string xml)
        {
            using (var info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
            {
                info.BeginReading(xml);
                object propval = info.GetValue("Value", this);

                if (propval is IDocumentLeafNode documentLeafNode)
                {
                    documentLeafNode.ParentObject = this;
                }

                _propertiesLazyLoaded.Remove(propName);
                _properties[propName] = propval;
            }
        }
コード例 #3
0
            /// <summary>
            /// This reads a fit function, that is stored in xml format onto disc.
            /// </summary>
            /// <param name="info">The fit function information (only the file name is used from it).</param>
            /// <returns>The fit function, or null if the fit function could not be read.</returns>
            public static IFitFunction ReadFileBasedFitFunction(Main.Services.FileBasedFitFunctionInformation info)
            {
                IFitFunction func = null;

                try
                {
                    Altaxo.Serialization.Xml.XmlStreamDeserializationInfo str = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
                    str.BeginReading(new FileStream(info.FileName, FileMode.Open, FileAccess.Read, FileShare.Read));
                    func = (IFitFunction)str.GetValue(null);
                    str.EndReading();
                    return(func);
                }
                catch (Exception ex)
                {
                    Current.Console.WriteLine("Error reading fit function from file {0}, error details: {1}", info.FileName, ex.ToString());
                }
                return(null);
            }
コード例 #4
0
        /// <summary>
        /// Opens a Altaxo project from a project file (without asking the user).
        /// </summary>
        /// <param name="filename"></param>
        private void Load(string filename)
        {
            System.Text.StringBuilder errorText = new System.Text.StringBuilder();

            System.IO.FileStream myStream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
            ZipFile zipFile = new ZipFile(myStream);

            Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
            AltaxoDocument newdocument    = new AltaxoDocument();
            ZipFileWrapper zipFileWrapper = new ZipFileWrapper(zipFile);

            try
            {
                newdocument.RestoreFromZippedFile(zipFileWrapper, info);
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }

            try
            {
                Current.Workbench.CloseAllViews();
                this.CurrentOpenProject = newdocument;
                RestoreWindowStateFromZippedFile(zipFile, info, newdocument);
                this.CurrentOpenProject.IsDirty = false;
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
                System.Windows.Forms.MessageBox.Show(Current.MainWindow, errorText.ToString(), "An error occured");
            }
            finally
            {
                myStream.Close();
            }


            if (errorText.Length != 0)
            {
                throw new ApplicationException(errorText.ToString());
            }
        }
コード例 #5
0
ファイル: PropertyService.cs プロジェクト: olesar/Altaxo
 protected virtual PropertyBagLazyLoaded InternalLoadUserSettingsBag(FileName fileName)
 {
     if (!File.Exists(fileName))
     {
         var result = new PropertyBagLazyLoaded()
         {
             ParentObject = SuspendableDocumentNode.StaticInstance
         };
         return(result);
     }
     try
     {
         using (LockPropertyFile())
         {
             using (var str = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
             {
                 str.BeginReading(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read));
                 var result = (PropertyBagLazyLoaded)str.GetValue("UserSettings", null);
                 result.ParentObject = SuspendableDocumentNode.StaticInstance;
                 str.EndReading();
                 return(result);
             }
         }
     }
     catch (XmlException ex)
     {
         Altaxo.Current.MessageService.ShowError("Error loading properties: " + ex.Message + "\nSettings have been restored to default values.");
     }
     catch (IOException ex)
     {
         Altaxo.Current.MessageService.ShowError("Error loading properties: " + ex.Message + "\nSettings have been restored to default values.");
     }
     catch (Exception ex)
     {
         Altaxo.Current.MessageService.ShowError("Error loading properties: " + ex.Message + "\nSettings have been restored to default values.");
     }
     return(new PropertyBagLazyLoaded()
     {
         ParentObject = SuspendableDocumentNode.StaticInstance
     });
 }
コード例 #6
0
        /// <summary>
        /// Deserializes an object from a string containing Altaxo's XML format.
        /// </summary>
        /// <typeparam name="T">Type of the object that is expected to be deserialized.</typeparam>
        /// <param name="s">The string containing Altaxo's XML format. Is is expected that the root node is named 'Object'.</param>
        /// <returns>The deserialized object, or default(T) if either the object was null or had a type that was not the expected type.</returns>
        public static T DeserializeObjectFromString <T>(string s)
        {
            object readObject = null;

            using (var info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
            {
                info.BeginReading(s);
                readObject = info.GetValue("Object", null);

                info.AnnounceDeserializationEnd(Current.Project, true);
                info.AnnounceDeserializationHasCompletelyFinished();
                info.EndReading();
            }

            if ((null != readObject) && (readObject is T))
            {
                return((T)readObject);
            }
            else
            {
                return(default(T));
            }
        }
コード例 #7
0
        /// <summary>
        /// Restores the state of the main window from a zipped Altaxo project file.
        /// </summary>
        /// <param name="zipFile">The zip file where the state file can be found into.</param>
        /// <param name="info">The deserialization info used to retrieve the data.</param>
        /// <param name="restoredDoc">The previously (also from the zip file!) restored Altaxo document.</param>
        public void RestoreWindowStateFromZippedFile(ZipFile zipFile, Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info, AltaxoDocument restoredDoc)
        {
            System.Collections.ArrayList restoredControllers = new System.Collections.ArrayList();
            foreach (ZipEntry zipEntry in zipFile)
            {
                if (!zipEntry.IsDirectory && zipEntry.Name.StartsWith("Workbench/Views/"))
                {
                    System.IO.Stream zipinpstream = zipFile.GetInputStream(zipEntry);
                    info.BeginReading(zipinpstream);
                    object readedobject = info.GetValue("Table", this);
                    if (readedobject is ICSharpCode.SharpDevelop.Gui.IViewContent)
                    {
                        restoredControllers.Add(readedobject);
                    }
                    info.EndReading();
                }
            }

            info.AnnounceDeserializationEnd(restoredDoc);
            info.AnnounceDeserializationEnd(this);

            // now give all restored controllers a view and show them in the Main view

            foreach (IViewContent viewcontent in restoredControllers)
            {
                IMVCControllerWrapper wrapper = viewcontent as IMVCControllerWrapper;
                if (wrapper != null && wrapper.MVCController.ViewObject == null)
                {
                    Current.Gui.FindAndAttachControlTo(wrapper.MVCController);
                }

                if (viewcontent.Control != null)
                {
                    Current.Workbench.ShowView(viewcontent);
                }
            }
        }
コード例 #8
0
ファイル: FileCommands.cs プロジェクト: Altaxo/Altaxo
		public static void OpenWorksheetOrGraph(string filename)
		{
			object deserObject;

			using (System.IO.Stream myStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
			{
				using (var info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
				{
					info.BeginReading(myStream);
					deserObject = info.GetValue("Table", null);
					info.EndReading();
					myStream.Close();

					if (deserObject is IProjectItem)
					{
						Current.Project.AddItemWithThisOrModifiedName((IProjectItem)deserObject);
						info.AnnounceDeserializationEnd(Current.Project, false); // fire the event to resolve path references
						Current.ProjectService.OpenOrCreateViewContentForDocument((IProjectItem)deserObject);
					}
					else if (deserObject is Altaxo.Worksheet.TablePlusLayout)
					{
						Altaxo.Worksheet.TablePlusLayout tableAndLayout = deserObject as Altaxo.Worksheet.TablePlusLayout;
						var table = tableAndLayout.Table;

						Current.Project.AddItemWithThisOrModifiedName(table);

						if (tableAndLayout.Layout != null)
							Current.Project.TableLayouts.Add(tableAndLayout.Layout);

						tableAndLayout.Layout.DataTable = table; // this is the table for the layout now

						info.AnnounceDeserializationEnd(Current.Project, false); // fire the event to resolve path references

						Current.ProjectService.CreateNewWorksheet(table, tableAndLayout.Layout);
					}

					info.AnnounceDeserializationEnd(Current.Project, true); // final deserialization end
				}
			}
		}
コード例 #9
0
    /// <summary>
    /// Opens a Altaxo project from a project file (without asking the user).
    /// </summary>
    /// <param name="filename"></param>
    private void Load(string filename)
    {
      System.Text.StringBuilder errorText = new System.Text.StringBuilder();

      System.IO.FileStream myStream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
      ZipFile zipFile = new ZipFile(myStream);
      Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
      AltaxoDocument newdocument = new AltaxoDocument();
      ZipFileWrapper zipFileWrapper = new ZipFileWrapper(zipFile);

      try
      {
        newdocument.RestoreFromZippedFile(zipFileWrapper, info);
      }
      catch (Exception exc)
      {
        errorText.Append(exc.ToString());
      }

      try
      {
        Current.Workbench.CloseAllViews();
        this.CurrentOpenProject = newdocument;
        RestoreWindowStateFromZippedFile(zipFile, info, newdocument);
        this.CurrentOpenProject.IsDirty = false;
      }
      catch (Exception exc)
      {
        errorText.Append(exc.ToString());
        System.Windows.Forms.MessageBox.Show(Current.MainWindow, errorText.ToString(), "An error occured");
      }
      finally
      {
        myStream.Close();
      }


      if (errorText.Length != 0)
        throw new ApplicationException(errorText.ToString());
    }
コード例 #10
0
ファイル: FileCommands.cs プロジェクト: carlhuth/GenXSource
        public static void OpenWorksheetOrGraph(string filename)
        {
            object deserObject;

            Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info;
            using (System.IO.Stream myStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
                info.BeginReading(myStream);
                deserObject = info.GetValue("Table", null);
                info.EndReading();
                myStream.Close();
            }

            // if it is a table, add it to the DataTableCollection
            if (deserObject is Altaxo.Data.DataTable)
            {
                Altaxo.Data.DataTable table = deserObject as Altaxo.Data.DataTable;
                if (table.Name == null || table.Name == string.Empty)
                {
                    table.Name = Current.Project.DataTableCollection.FindNewTableName();
                }
                else if (Current.Project.DataTableCollection.ContainsTable(table.Name))
                {
                    table.Name = Current.Project.DataTableCollection.FindNewTableName(table.Name);
                }

                Current.Project.DataTableCollection.Add(table);
                info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references

                Current.ProjectService.CreateNewWorksheet(table);
            }
            // if it is a table, add it to the DataTableCollection
            else if (deserObject is Altaxo.Worksheet.TablePlusLayout)
            {
                Altaxo.Worksheet.TablePlusLayout tableAndLayout = deserObject as Altaxo.Worksheet.TablePlusLayout;
                Altaxo.Data.DataTable            table          = tableAndLayout.Table;
                if (table.Name == null || table.Name == string.Empty)
                {
                    table.Name = Current.Project.DataTableCollection.FindNewTableName();
                }
                else if (Current.Project.DataTableCollection.ContainsTable(table.Name))
                {
                    table.Name = Current.Project.DataTableCollection.FindNewTableName(table.Name);
                }
                Current.Project.DataTableCollection.Add(table);

                if (tableAndLayout.Layout != null)
                {
                    Current.Project.TableLayouts.Add(tableAndLayout.Layout);
                }

                info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references

                tableAndLayout.Layout.DataTable = table;          // this is the table for the layout now

                Current.ProjectService.CreateNewWorksheet(table, tableAndLayout.Layout);
            }
            else if (deserObject is Altaxo.Graph.Gdi.GraphDocument)
            {
                Altaxo.Graph.Gdi.GraphDocument graph = deserObject as Altaxo.Graph.Gdi.GraphDocument;
                if (graph.Name == null || graph.Name == string.Empty)
                {
                    graph.Name = Current.Project.GraphDocumentCollection.FindNewName();
                }
                else if (Current.Project.GraphDocumentCollection.Contains(graph.Name))
                {
                    graph.Name = Current.Project.GraphDocumentCollection.FindNewName(graph.Name);
                }

                Current.Project.GraphDocumentCollection.Add(graph);
                info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references in the graph

                Current.ProjectService.CreateNewGraph(graph);
            }
        }
コード例 #11
0
    public static void OpenWorksheetOrGraph(string filename)
    {
      object deserObject;
      Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info;
      using (System.IO.Stream myStream = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
      {
        info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
        info.BeginReading(myStream);
        deserObject = info.GetValue("Table", null);
        info.EndReading();
        myStream.Close();
      }

      // if it is a table, add it to the DataTableCollection
      if (deserObject is Altaxo.Data.DataTable)
      {
        Altaxo.Data.DataTable table = deserObject as Altaxo.Data.DataTable;
        if (table.Name == null || table.Name == string.Empty)
          table.Name = Current.Project.DataTableCollection.FindNewTableName();
        else if (Current.Project.DataTableCollection.ContainsTable(table.Name))
          table.Name = Current.Project.DataTableCollection.FindNewTableName(table.Name);

        Current.Project.DataTableCollection.Add(table);
        info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references

        Current.ProjectService.CreateNewWorksheet(table);
      }
      // if it is a table, add it to the DataTableCollection
      else if (deserObject is Altaxo.Worksheet.TablePlusLayout)
      {
        Altaxo.Worksheet.TablePlusLayout tableAndLayout = deserObject as Altaxo.Worksheet.TablePlusLayout;
        Altaxo.Data.DataTable table = tableAndLayout.Table;
        if (table.Name == null || table.Name == string.Empty)
          table.Name = Current.Project.DataTableCollection.FindNewTableName();
        else if (Current.Project.DataTableCollection.ContainsTable(table.Name))
          table.Name = Current.Project.DataTableCollection.FindNewTableName(table.Name);
        Current.Project.DataTableCollection.Add(table);

        if (tableAndLayout.Layout != null)
          Current.Project.TableLayouts.Add(tableAndLayout.Layout);

        info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references

        tableAndLayout.Layout.DataTable = table; // this is the table for the layout now

        Current.ProjectService.CreateNewWorksheet(table, tableAndLayout.Layout);
      }
      else if (deserObject is Altaxo.Graph.Gdi.GraphDocument)
      {
        Altaxo.Graph.Gdi.GraphDocument graph = deserObject as Altaxo.Graph.Gdi.GraphDocument;
        if (graph.Name == null || graph.Name == string.Empty)
          graph.Name = Current.Project.GraphDocumentCollection.FindNewName();
        else if (Current.Project.GraphDocumentCollection.Contains(graph.Name))
          graph.Name = Current.Project.GraphDocumentCollection.FindNewName(graph.Name);

        Current.Project.GraphDocumentCollection.Add(graph);
        info.AnnounceDeserializationEnd(Current.Project); // fire the event to resolve path references in the graph

        Current.ProjectService.CreateNewGraph(graph);
      }
    }
コード例 #12
0
ファイル: ProjectService.cs プロジェクト: olesar/Altaxo
        /// <summary>
        /// Restores the state of the main window from a zipped Altaxo project file.
        /// </summary>
        /// <param name="zipFile">The zip file where the state file can be found into.</param>
        /// <param name="info">The deserialization info used to retrieve the data.</param>
        /// <param name="restoredDoc">The previously (also from the zip file!) restored Altaxo document.</param>
        public void RestoreWindowStateFromZippedFile(ZipArchive zipFile, Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info, AltaxoDocument restoredDoc)
        {
            var restoredDocModels = new List <(object Document, string ZipEntryName)>();
            var restoredPadModels = new List <object>();

            Altaxo.Gui.Workbench.ViewStatesMemento selectedViewsMemento = null;

            foreach (var zipEntry in zipFile.Entries)
            {
                try
                {
                    if (zipEntry.FullName.StartsWith("Workbench/Views/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("ViewContentModel", null);
                            restoredDocModels.Add((readedobject, zipEntry.FullName));
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("Workbench/Pads/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("Model", null);
                            if (readedobject != null)
                            {
                                restoredPadModels.Add(readedobject);
                            }
                            else
                            {
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName == "Workbench/ViewStates.xml")
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            selectedViewsMemento = info.GetValue("ViewStates", null) as Altaxo.Gui.Workbench.ViewStatesMemento;
                            info.EndReading();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Current.Console.WriteLine("Exception during serialization of {0}, message: {1}", zipEntry.FullName, ex.Message);
                }
            }

            info.AnnounceDeserializationEnd(restoredDoc, false);
            info.AnnounceDeserializationEnd(restoredDoc, false);

            // now give all restored controllers a view and show them in the Main view

            foreach ((object doc, string entryName) in restoredDocModels)
            {
                var isSelected = entryName == selectedViewsMemento?.SelectedView_EntryName;
                Current.Workbench.ShowView(doc, isSelected);
            }

            foreach (var o in restoredPadModels)
            {
                var content = (IPadContent)Current.Gui.GetControllerAndControl(new object[] { o }, typeof(IPadContent));
                if (null != content)
                {
                    Current.Workbench.ShowPad(content, false);
                }
            }
        }
コード例 #13
0
ファイル: ProjectService.cs プロジェクト: olesar/Altaxo
        /// <summary>
        /// Opens a Altaxo project from a stream. Any existing old project will be closed without confirmation.
        /// </summary>
        /// <param name="myStream">The stream from which to load the project.</param>
        /// <param name="filename">Either the filename of the file which stored the document, or null (e.g. myStream is a MemoryStream).</param>
        protected override string InternalLoadProjectFromStream(System.IO.Stream myStream, string filename)
        {
            var errorText = new System.Text.StringBuilder();

            var oldProject = CurrentOpenProject;

            if (null != oldProject)
            {
                OnProjectChanged(new ProjectEventArgs(oldProject, oldProject.Name, ProjectEventKind.ProjectClosing));
            }

            try
            {
                Current.Workbench.CloseAllViews();
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }

            try
            {
                SetCurrentProject(null, string.Empty);
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }

            // Old project is now closed
            if (null != oldProject)
            {
                OnProjectChanged(new ProjectEventArgs(oldProject, oldProject.Name, ProjectEventKind.ProjectClosed));
            }

            // Now open new project

            OnProjectChanged(new ProjectEventArgs(null, filename, ProjectEventKind.ProjectOpening));

            ZipArchive zipFile = null;

            ;
            AltaxoDocument newdocument = null;

            ;
            Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info;

            try
            {
                newdocument = new AltaxoDocument();

                zipFile = new ZipArchive(myStream, ZipArchiveMode.Read);
                info    = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
                return(errorText.ToString()); // this is unrecoverable - we must return
            }

            try
            {
                using (var suspendToken = newdocument.SuspendGetToken())
                {
                    newdocument.RestoreFromZippedFile(zipFile, info);
                }
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }

            try
            {
                SetCurrentProject(newdocument, filename);

                RestoreWindowStateFromZippedFile(zipFile, info, newdocument);
                info.AnnounceDeserializationEnd(newdocument, true); // Final call to deserialization end

                CurrentOpenProject.IsDirty = false;

                info.AnnounceDeserializationHasCompletelyFinished(); // Annonce completly finished deserialization, activate data sources of the Altaxo document

                OnProjectChanged(new ProjectEventArgs(CurrentOpenProject, filename, ProjectEventKind.ProjectOpened));
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }
            return(errorText.ToString());
        }
コード例 #14
0
ファイル: ProjectService.cs プロジェクト: Altaxo/Altaxo
		/// <summary>
		/// Opens a Altaxo project from a stream. Any existing old project will be closed without confirmation.
		/// </summary>
		/// <param name="myStream">The stream from which to load the project.</param>
		/// <param name="filename">Either the filename of the file which stored the document, or null (e.g. myStream is a MemoryStream).</param>
		private string InternalLoadProjectFromStream(System.IO.Stream myStream, string filename)
		{
			var errorText = new System.Text.StringBuilder();

			var oldProject = CurrentOpenProject;

			if (null != oldProject)
				OnProjectChanged(new ProjectEventArgs(oldProject, oldProject.Name, ProjectEventKind.ProjectClosing));

			try
			{
				Current.Workbench.CloseAllViews();
			}
			catch (Exception exc)
			{
				errorText.Append(exc.ToString());
			}

			try
			{
				this.SetCurrentProject(null, string.Empty);
			}
			catch (Exception exc)
			{
				errorText.Append(exc.ToString());
			}

			// Old project is now closed
			if (null != oldProject)
				OnProjectChanged(new ProjectEventArgs(oldProject, oldProject.Name, ProjectEventKind.ProjectClosed));

			// Now open new project

			OnProjectChanged(new ProjectEventArgs(null, filename, ProjectEventKind.ProjectOpening));

			ZipFile zipFile = null; ;
			AltaxoDocument newdocument = null; ;
			Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info;
			ZipFileWrapper zipFileWrapper;

			try
			{
				newdocument = new AltaxoDocument();

				zipFile = new ZipFile(myStream);
				info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
				zipFileWrapper = new ZipFileWrapper(zipFile);
			}
			catch (Exception exc)
			{
				errorText.Append(exc.ToString());
				return errorText.ToString(); // this is unrecoverable - we must return
			}

			try
			{
				using (var suspendToken = newdocument.SuspendGetToken())
				{
					newdocument.RestoreFromZippedFile(zipFileWrapper, info);
				}
			}
			catch (Exception exc)
			{
				errorText.Append(exc.ToString());
			}

			try
			{
				this.SetCurrentProject(newdocument, filename);

				RestoreWindowStateFromZippedFile(zipFile, info, newdocument);
				info.AnnounceDeserializationEnd(newdocument, true); // Final call to deserialization end

				this.CurrentOpenProject.IsDirty = false;

				info.AnnounceDeserializationHasCompletelyFinished(); // Annonce completly finished deserialization, activate data sources of the Altaxo document

				OnProjectChanged(new ProjectEventArgs(this.CurrentOpenProject, filename, ProjectEventKind.ProjectOpened));
			}
			catch (Exception exc)
			{
				errorText.Append(exc.ToString());
			}
			return errorText.ToString();
		}
コード例 #15
0
        public void RestoreFromZippedFile(ZipArchive zipFile, Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info)
        {
            var errorText = new System.Text.StringBuilder();

            foreach (var zipEntry in zipFile.Entries)
            {
                try
                {
                    if (zipEntry.FullName.StartsWith("Tables/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("Table", null);
                            if (readedobject is Altaxo.Data.DataTable)
                            {
                                _dataTables.Add((Altaxo.Data.DataTable)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("Graphs/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("Graph", null);
                            if (readedobject is Graph.Gdi.GraphDocument)
                            {
                                _graphs.Add((Graph.Gdi.GraphDocument)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("Graphs3D/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("Graph", null);
                            if (readedobject is Graph.Graph3D.GraphDocument)
                            {
                                _graphs3D.Add((Graph.Graph3D.GraphDocument)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("Texts/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("Text", null);
                            if (readedobject is Text.TextDocument noteDoc)
                            {
                                _textDocuments.Add(noteDoc);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("TableLayouts/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("WorksheetLayout", null);
                            if (readedobject is Altaxo.Worksheet.WorksheetLayout)
                            {
                                _tableLayouts.Add((Altaxo.Worksheet.WorksheetLayout)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("FitFunctionScripts/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("FitFunctionScript", null);
                            if (readedobject is Altaxo.Scripting.FitFunctionScript)
                            {
                                _fitFunctionScripts.Add((Altaxo.Scripting.FitFunctionScript)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName.StartsWith("FolderProperties/"))
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("FolderProperty", null);
                            if (readedobject is Altaxo.Main.Properties.ProjectFolderPropertyDocument)
                            {
                                _projectFolderProperties.Add((Altaxo.Main.Properties.ProjectFolderPropertyDocument)readedobject);
                            }
                            info.EndReading();
                        }
                    }
                    else if (zipEntry.FullName == "DocumentInformation.xml")
                    {
                        using (var zipinpstream = zipEntry.Open())
                        {
                            info.BeginReading(zipinpstream);
                            object readedobject = info.GetValue("DocumentInformation", null);
                            if (readedobject is DocumentInformation)
                            {
                                _documentInformation = (DocumentInformation)readedobject;
                            }
                            info.EndReading();
                        }
                    }
                }
                catch (Exception exc)
                {
                    errorText.Append("Error deserializing ");
                    errorText.Append(zipEntry.Name);
                    errorText.Append(", ");
                    errorText.Append(exc.ToString());
                }
            }

            try
            {
                info.AnnounceDeserializationEnd(this, false);
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }

            if (errorText.Length != 0)
            {
                throw new ApplicationException(errorText.ToString());
            }
        }
コード例 #16
0
 /// <summary>
 /// This reads a fit function, that is stored in xml format onto disc.
 /// </summary>
 /// <param name="info">The fit function information (only the file name is used from it).</param>
 /// <returns>The fit function, or null if the fit function could not be read.</returns>
 public static IFitFunction ReadFileBasedFitFunction(Main.Services.FileBasedFitFunctionInformation info)
 {
   IFitFunction func = null;
   try
   {
     Altaxo.Serialization.Xml.XmlStreamDeserializationInfo str = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo();
     str.BeginReading(new FileStream(info.FileName, FileMode.Open, FileAccess.Read, FileShare.Read));
     func = (IFitFunction)str.GetValue(null);
     str.EndReading();
     return func;
   }
   catch (Exception ex)
   {
     Current.Console.WriteLine("Error reading fit function from file {0}, error details: {1}", info.FileName, ex.ToString());
   }
   return null;
 }
コード例 #17
0
        public void RestoreFromZippedFile(ICompressedFileContainer zipFile, Altaxo.Serialization.Xml.XmlStreamDeserializationInfo info)
        {
            System.Text.StringBuilder errorText = new System.Text.StringBuilder();

            foreach (IFileContainerItem zipEntry in zipFile)
            {
                try
                {
                    if (!zipEntry.IsDirectory && zipEntry.Name.StartsWith("Tables/"))
                    {
                        System.IO.Stream zipinpstream = zipFile.GetInputStream(zipEntry);
                        info.BeginReading(zipinpstream);
                        object readedobject = info.GetValue("Table", this);
                        if (readedobject is Altaxo.Data.DataTable)
                        {
                            this.m_DataSet.Add((Altaxo.Data.DataTable)readedobject);
                        }
                        info.EndReading();
                    }
                    else if (!zipEntry.IsDirectory && zipEntry.Name.StartsWith("Graphs/"))
                    {
                        System.IO.Stream zipinpstream = zipFile.GetInputStream(zipEntry);
                        info.BeginReading(zipinpstream);
                        object readedobject = info.GetValue("Graph", this);
                        if (readedobject is GraphDocument)
                        {
                            this.m_GraphSet.Add((GraphDocument)readedobject);
                        }
                        info.EndReading();
                    }
                    else if (!zipEntry.IsDirectory && zipEntry.Name.StartsWith("TableLayouts/"))
                    {
                        System.IO.Stream zipinpstream = zipFile.GetInputStream(zipEntry);
                        info.BeginReading(zipinpstream);
                        object readedobject = info.GetValue("WorksheetLayout", this);
                        if (readedobject is Altaxo.Worksheet.WorksheetLayout)
                        {
                            this.m_TableLayoutList.Add((Altaxo.Worksheet.WorksheetLayout)readedobject);
                        }
                        info.EndReading();
                    }
                    else if (!zipEntry.IsDirectory && zipEntry.Name.StartsWith("FitFunctionScripts/"))
                    {
                        System.IO.Stream zipinpstream = zipFile.GetInputStream(zipEntry);
                        info.BeginReading(zipinpstream);
                        object readedobject = info.GetValue("FitFunctionScript", this);
                        if (readedobject is Altaxo.Scripting.FitFunctionScript)
                        {
                            this._FitFunctionScripts.Add((Altaxo.Scripting.FitFunctionScript)readedobject);
                        }
                        info.EndReading();
                    }
                }
                catch (Exception exc)
                {
                    errorText.Append("Error deserializing ");
                    errorText.Append(zipEntry.Name);
                    errorText.Append(", ");
                    errorText.Append(exc.ToString());
                }
            }

            try
            {
                info.AnnounceDeserializationEnd(this);
            }
            catch (Exception exc)
            {
                errorText.Append(exc.ToString());
            }


            if (errorText.Length != 0)
            {
                throw new ApplicationException(errorText.ToString());
            }
        }
コード例 #18
0
ファイル: ProjectService.cs プロジェクト: olesar/Altaxo
        /// <inheritdoc/>
        public override bool TryOpenProjectDocumentFile(string fileName, bool forceTrialRegardlessOfExtension)
        {
            if (null == fileName)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            if (!forceTrialRegardlessOfExtension)
            {
                var extension = System.IO.Path.GetExtension(fileName).ToLowerInvariant();

                if (!ProjectDocumentExtensions.Contains(extension))
                {
                    return(false);
                }
            }

            object deserializedObject;

            using (System.IO.Stream myStream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
            {
                using (var info = new Altaxo.Serialization.Xml.XmlStreamDeserializationInfo())
                {
                    try
                    {
                        info.BeginReading(myStream);
                        deserializedObject = info.GetValue("Table", null);
                        info.EndReading();
                        myStream.Close();
                    }
                    catch (Exception ex)
                    {
                        return(false);
                    }

                    if (deserializedObject is IProjectItem projectItem)
                    {
                        Current.Project.AddItemWithThisOrModifiedName(projectItem);
                        info.AnnounceDeserializationEnd(Current.Project, false); // fire the event to resolve path references
                        Current.ProjectService.OpenOrCreateViewContentForDocument(projectItem);
                    }
                    else if (deserializedObject is Altaxo.Worksheet.TablePlusLayout tableAndLayout)
                    {
                        var table = tableAndLayout.Table;
                        Current.Project.AddItemWithThisOrModifiedName(table);

                        if (tableAndLayout.Layout != null)
                        {
                            Current.Project.TableLayouts.Add(tableAndLayout.Layout);
                        }

                        tableAndLayout.Layout.DataTable = table;                 // this is the table for the layout now

                        info.AnnounceDeserializationEnd(Current.Project, false); // fire the event to resolve path references

                        Current.ProjectService.CreateNewWorksheet(table, tableAndLayout.Layout);
                    }
                    else
                    {
                        return(false);
                    }

                    info.AnnounceDeserializationEnd(Current.Project, true); // final deserialization end
                    return(true);
                }
            }
        }