Beispiel #1
0
        public static void Save(IWorkspaceLoadable obj, string filepath = "")
        {
            // prüfen, ob Datei schon offen
            var  index     = Instance.openedFiles.FindIndex(x => x.Item2 == obj);
            bool addToList = false;

            if (index != -1)
            {
                // Datei schon offen, soll sie unter einem anderen Namen gespeichert werden?
                if (filepath != Instance.openedFiles[index].Item1 && filepath != "")
                {
                    obj       = Serializer.DeepClone(obj);
                    addToList = true;
                }
                filepath = Instance.openedFiles[index].Item1;
            }
            else
            {
                // Datei neu erstellt
                if (!new Uri(filepath, UriKind.RelativeOrAbsolute).IsAbsoluteUri)
                {
                    throw new IOException("If file will be newly created, save needs an absolute path");
                }
                addToList = true;
            }
            using (FileStream stream = new FileStream(filepath, FileMode.Create))
            {
                Serializer.Serialize(stream, obj);
                if (addToList)
                {
                    Add(filepath, obj);
                }
            }
        }
Beispiel #2
0
        private static Out LoadInternal <FullType, LoadType, Out>
            (string relativePath, IWorkspaceLoadable reference, Func <LoadType, Out> func, Func <FullType, Out> func2) where FullType : IWorkspaceLoadable where LoadType : IWorkspaceLoadable
        {
            var absPath = AbsolutePathFromReference(ref relativePath, reference);

            return(LoadInternal(absPath, func, func2));
        }
Beispiel #3
0
 private static void Add(string absolutePath, IWorkspaceLoadable obj)
 {
     if (Find <IWorkspaceLoadable>(absolutePath) == null)
     {
         Instance.openedFiles.Add(new Tuple <string, IWorkspaceLoadable>(absolutePath, obj));
         Console.WriteLine($"File {absolutePath} added to workspace");
     }
 }
Beispiel #4
0
        public static string Find(IWorkspaceLoadable obj)
        {
            var result = Instance.openedFiles.Where(x => x.Item2 == obj);

            if (result.Count() == 0)
            {
                return(null);
            }
            return(result.First().Item1);
        }
Beispiel #5
0
 public static PathResolution FindResolution(string relativePath, IWorkspaceLoadable reference)
 {
     if (!string.IsNullOrEmpty(Instance.FileInWork))
     {
         var result = Instance.resolvedPaths.Where(x => x.ParentPath == Instance.FileInWork && x.RelativePath == relativePath).FirstOrDefault();
         if (result != null)
         {
             return(result);
         }
     }
     return(Instance.resolvedPaths.Where(x => x.reference == reference && x.RelativePath == relativePath).FirstOrDefault());
 }
Beispiel #6
0
        public static string AbsolutePathFromReference(ref string relativePath, IWorkspaceLoadable reference)
        {
            string absolutePath   = "";
            var    referenceTuple = Instance.openedFiles.Where(x => x.Item2 == reference).FirstOrDefault();

            if (new Uri(relativePath, UriKind.RelativeOrAbsolute).IsAbsoluteUri)
            {
                absolutePath = relativePath;
            }
            else if (reference != null)
            {
                string basepath = "";
                if (referenceTuple != null)
                {
                    basepath = referenceTuple.Item1;
                }
                else if (!String.IsNullOrEmpty(Instance.FileInWork))
                {
                    basepath = Instance.FileInWork;
                }
                if (basepath != "")
                {
                    string directoryofreference = Path.GetDirectoryName(basepath);
                    if (relativePath != null)
                    {
                        absolutePath = Path.GetFullPath(Path.Combine(directoryofreference, relativePath));
                    }
                    if (relativePath == null || !File.Exists(absolutePath))
                    {
                        string temp = del.Invoke(absolutePath, basepath);
                        relativePath = temp != "" ? temp : relativePath;
                        absolutePath = Path.GetFullPath(Path.Combine(directoryofreference, relativePath));
                        Instance.ReferenceReplaced = true;
                    }
                    if (!File.Exists(absolutePath))
                    {
                        throw new FileNotFoundException("File not found, update failed", absolutePath);
                    }
                }
            }

            else
            {
                throw new IOException("When not providing a reference, the path must be absolute");
            }
            return(absolutePath);
        }
Beispiel #7
0
        public static void RenameFile(IWorkspaceLoadable reference, string newpath)
        {
            // Closing and reopening files is a terrible idea, especially for subassemblies, since it
            // messes up all the references. We don't have to do this however, as we can just rename
            // the reference in the workspace.
            if (newpath.Contains("\\"))
            {
                newpath = newpath.Replace("\\", "/");
            }
            var result = Instance.openedFiles.Where(x => x.Item2 == reference).ToList();

            Instance.openedFiles.RemoveAll(x => x.Item2 == reference);
            foreach (var r in result)
            {
                Instance.openedFiles.Add(new Tuple <string, IWorkspaceLoadable>(newpath, reference));
            }
        }
Beispiel #8
0
 public static string AbsolutePathFromReferenceLoseUpdate(string relativePath, IWorkspaceLoadable reference)
 {
     return(AbsolutePathFromReference(ref relativePath, reference));
 }
Beispiel #9
0
 public static void CloseFile(IWorkspaceLoadable reference)
 {
     Instance.openedFiles.RemoveAll(x => x.Item2 == reference);
 }
Beispiel #10
0
 public static void CloseFile(string relativePath, IWorkspaceLoadable reference)
 {
     CloseFile(AbsolutePathFromReference(ref
                                         relativePath, reference));
 }
Beispiel #11
0
 public static bool LoadHasProtocolDefinition <T>(string relativePath, IWorkspaceLoadable reference) where T : IWorkspaceLoadColorList
 {
     return(LoadHasProtocolDefinition <T>(AbsolutePathFromReference(ref relativePath, reference)));
 }
Beispiel #12
0
 public static bool LoadEditingState <T>(string relativePath, IWorkspaceLoadable reference) where T : IWorkspaceLoadColorList
 {
     return(LoadEditingState <T>(AbsolutePathFromReference(ref relativePath, reference)));
 }
Beispiel #13
0
 public static Tuple <string, int[]> LoadColorList <T>(string relativePath, IWorkspaceLoadable reference) where T : IWorkspaceLoadColorList
 {
     return(LoadColorList <T>(AbsolutePathFromReference(ref relativePath, reference)));
 }
Beispiel #14
0
 public static ObservableCollection <ImageFilter> LoadImageFilters <T>(string relativePath, IWorkspaceLoadable reference) where T : IWorkspaceLoadImageFilter
 {
     return(LoadImageFilters <T>(AbsolutePathFromReference(ref relativePath, reference)));
 }
Beispiel #15
0
 public static T Load <T>(string relativePath, IWorkspaceLoadable reference) where T : IWorkspaceLoadable
 {
     return(LoadInternal <T, T, T>(relativePath, reference, a => a, a => a));
 }
Beispiel #16
0
        public static string AbsolutePathFromReference(ref string relativePath, IWorkspaceLoadable reference)
        {
            if (relativePath.Contains("\\"))
            {
                relativePath = relativePath.Replace("\\", "/");
            }

            bool   resolved         = false;
            bool   createResolution = false;
            string absolutePath     = "";
            string oldrelpath       = relativePath;

            var referenceTuple = Instance.openedFiles.Where(x => x.Item2 == reference).FirstOrDefault();

            if (Path.IsPathRooted(relativePath) && File.Exists(relativePath))
            {
                // The relative path that was passed is in fact already absolute; return the path if the file exists.
                absolutePath = relativePath;
                resolved     = true;
                // If there is a reference, the path should not be absolute.
                if (reference != null && referenceTuple != null)
                {
                    relativePath = Workspace.MakeRelativePath(referenceTuple.Item1, relativePath);
                    Debug.WriteLine($"Updated Path {oldrelpath} because it was absolute when it should have been relative. New Path {relativePath}");
                }
            }
            else if (reference != null)
            {
                string basepath = "";
                if (referenceTuple != null)
                {
                    basepath = referenceTuple.Item1;
                }
                else if (!String.IsNullOrEmpty(Instance.FileInWork))
                {
                    basepath = Instance.FileInWork;
                }
                if (basepath != "")
                {
                    // try primitive resolution
                    string directoryofreference = Path.GetDirectoryName(basepath);
                    if (relativePath != null)
                    {
                        absolutePath = Path.GetFullPath(Path.Combine(directoryofreference, relativePath));
                        resolved     = true;
                    }
                    if (relativePath == null || !File.Exists(absolutePath))
                    {
                        resolved = false;
                    }
                    var resolution = FindResolution(relativePath, reference);
                    // if the resolution was already computed, return that instead.
                    if (resolution != null)
                    {
                        if (!string.IsNullOrEmpty(resolution.AbsolutePath) && File.Exists(resolution.AbsolutePath))
                        {
                            if (!Path.IsPathRooted(resolution.AbsolutePath))
                            {
                                relativePath = resolution.AbsolutePath;
                            }
                            else
                            {
                                relativePath = Workspace.MakeRelativePath(basepath, resolution.AbsolutePath);
                            }
                            if (oldrelpath != relativePath)
                            {
                                Debug.WriteLine($"Updated Path {oldrelpath}. New Path {relativePath}");
                            }


                            resolution.isResolved = true;
                            return(resolution.AbsolutePath);
                        }
                        else if (resolution.isResolved)
                        {
                            // resolution is marked as resolved, but file doesn't exist
                            resolution.isResolved = false;
                        }
                        if (resolved)
                        {
                            // primitive search was successful. Update the PathResolution
                            // this should only be called if a file was moved to its original filename while the program was running
                            resolution.AbsolutePath = absolutePath;
                            resolution.isResolved   = resolved;
                        }
                    }
                    else
                    {
                        // no resolution exists yet - create a new one
                        createResolution = true;
                    }
                    if (createResolution)
                    {
                        Instance.resolvedPaths.Add(new PathResolution()
                        {
                            AbsolutePath = absolutePath, isResolved = resolved, reference = reference, RelativePath = relativePath, ParentPath = basepath
                        });
                    }
                }
            }

            else
            {
                throw new IOException("When not providing a reference, the path must be absolute");
            }
            if (!resolved)
            {
                throw new FileNotFoundException("File not found, update failed", absolutePath);
            }
            return(absolutePath);
        }