private static SupportedFileType GetCorrectedSupportedFileType(int fileTypeIndex, string fileName, IList <SupportedFileType> fileTypes)
        {
            var fileType = fileTypes[fileTypeIndex];

            // check if the selected type in the filter contains the extension of the path
            // if it doesn't, try to find the first best matching type
            var extension = Path.GetExtension(fileName);

            if (extension != null)
            {
                var info = SupportedFileManager.GetSupportedFileInfo(fileType);

                if (!info.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                {
                    var actualInfo = SupportedFileManager.SupportedFileInfos
                                     .Where(x => fileTypes.Contains(x.EnumType))
                                     .FirstOrDefault(
                        x => x.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase));

                    if (actualInfo != null)
                    {
                        fileType = actualInfo.EnumType;
                    }
                }
            }

            return(fileType);
        }
        public bool Replace()
        {
            if (mFileReplaceActions == null)
            {
                return(false);
            }

            using (var openFileDlg = new OpenFileDialog())
            {
                var fileTypes = mFileReplaceActions.Keys.ToArray();

                openFileDlg.AutoUpgradeEnabled           = true;
                openFileDlg.CheckPathExists              = true;
                openFileDlg.CheckFileExists              = true;
                openFileDlg.FileName                     = Text;
                openFileDlg.Filter                       = SupportedFileManager.GetFilteredFileFilter(fileTypes);
                openFileDlg.Multiselect                  = false;
                openFileDlg.SupportMultiDottedExtensions = true;
                openFileDlg.Title         = "Select a replacement file";
                openFileDlg.ValidateNames = true;

                if (openFileDlg.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                var fileType = GetCorrectedSupportedFileType(openFileDlg.FilterIndex - 1, openFileDlg.FileName, fileTypes);
                return(Replace(openFileDlg.FileName, fileType));
            }
        }
        public bool Export()
        {
            if (mFileExportActions == null)
            {
                return(false);
            }

            using (var saveFileDlg = new SaveFileDialog())
            {
                var fileTypes = mFileExportActions.Keys.ToArray();

                saveFileDlg.AutoUpgradeEnabled = true;
                saveFileDlg.CheckPathExists    = true;
                saveFileDlg.FileName           = Text.Trim();
                saveFileDlg.Filter             = SupportedFileManager.GetFilteredFileFilter(fileTypes);
                saveFileDlg.OverwritePrompt    = true;
                saveFileDlg.Title         = "Select a file to export to";
                saveFileDlg.ValidateNames = true;

                if (saveFileDlg.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                var fileType = GetCorrectedSupportedFileType(saveFileDlg.FilterIndex - 1, saveFileDlg.FileName, fileTypes);
                return(Export(saveFileDlg.FileName, fileType));
            }
        }
        protected ResourceWrapper(string text, TResource resource)
            : base(text)
        {
            Resource = resource;
            FileType = SupportedFileManager.GetSupportedFileType(typeof(TResource));

            Initialize();
            PopulateViewFully();
            mInitialized = true;
        }
Exemplo n.º 5
0
        protected ResourceWrapper(string text, TResource resource)
            : base(text)
        {
            Console.WriteLine($"{this}: Creating wrapper for {resource}");
            Resource = resource;
            FileType = SupportedFileManager.GetSupportedFileType(typeof(TResource));

            Initialize();
            PopulateViewFully();
            mInitialized = true;
        }
Exemplo n.º 6
0
        protected override void Initialize()
        {
            CommonContextMenuOptions = CommonContextMenuOptions.Add | CommonContextMenuOptions.Move | CommonContextMenuOptions.Rename | CommonContextMenuOptions.Delete;

            RegisterFileAddAction(SupportedFileManager.GetSupportedFileType(typeof(TResource)), DefaultFileAddAction);
            RegisterRebuildAction(wrap =>
            {
                List <TResource> list = new List <TResource>();

                foreach (IResourceWrapper node in Nodes)
                {
                    list.Add((TResource)node.Resource);
                }

                return(list);
            });

            PostInitialize();
        }
        public bool Add()
        {
            if (mFileAddActions == null)
            {
                return(false);
            }

            bool hasAdded = false;

            using (OpenFileDialog openFileDlg = new OpenFileDialog())
            {
                var fileTypes = mFileAddActions.Keys.ToArray();

                openFileDlg.AutoUpgradeEnabled           = true;
                openFileDlg.CheckPathExists              = true;
                openFileDlg.CheckFileExists              = true;
                openFileDlg.Filter                       = SupportedFileManager.GetFilteredFileFilter(fileTypes);
                openFileDlg.Multiselect                  = true;
                openFileDlg.SupportMultiDottedExtensions = true;
                openFileDlg.Title         = "Select file(s) to add";
                openFileDlg.ValidateNames = true;

                if (openFileDlg.ShowDialog() != DialogResult.OK)
                {
                    return(false);
                }

                foreach (string fileName in openFileDlg.FileNames)
                {
                    var fileType = GetCorrectedSupportedFileType(openFileDlg.FilterIndex - 1, fileName, fileTypes);
                    hasAdded |= Add(fileName, fileType);
                }
            }

            return(hasAdded);
        }
        public bool Add(string path)
        {
            var fileInfo = SupportedFileManager.GetSupportedFileInfo(Path.GetExtension(path));

            return(Add(path, fileInfo.EnumType));
        }
        public MemoryStream GetResourceMemoryStream()
        {
            var info = SupportedFileManager.GetSupportedFileInfo(typeof(TResource));

            return(info.GetStream(Resource));
        }