Example #1
0
        private void tv_DragOver(object sender, DragEventArgs e)
        {
            // Retrieve the current selected node
            var treeview         = (TreeView)sender;
            var treeViewLocation = treeview.PointToScreen(Point.Empty);
            var currentNode      = treeview.GetNodeAt(e.X - treeViewLocation.X, e.Y - treeViewLocation.Y);

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                var files = (string[])e.Data.GetData(DataFormats.FileDrop);

                // File must be an expected file format
                // or a folder
                bool isExtensionValid = files.All(f => WebResource.IsValidExtension(Path.GetExtension(f)) || File.GetAttributes(f).HasFlag(FileAttributes.Directory));

                // Destination node must be a Root or Folder node
                bool isNodeValid = currentNode != null && (currentNode.ImageIndex <= 1 || currentNode.ImageIndex >= 14 && currentNode.ImageIndex <= 15);

                if (isNodeValid)
                {
                    treeview.SelectedNode = currentNode;
                }

                e.Effect = files.Length > 0 && isExtensionValid && isNodeValid
                    ? DragDropEffects.All
                    : DragDropEffects.None;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
Example #2
0
        private void UpdateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad)
        {
            var subNodes   = parentFolderNode.Nodes;
            var subFolders = di.GetDirectories();

            foreach (var subFolder in subFolders)
            {
                if (!subNodes.ContainsKey(subFolder.Name) || subNodes[subFolder.Name].ImageIndex != 1 && subNodes[subFolder.Name].ImageIndex != 15)
                {
                    var folderNode = new TreeNode(subFolder.Name)
                    {
                        ImageIndex = 15, SelectedImageIndex = 15, Tag = subFolder.FullName, Name = subFolder.Name
                    };
                    parentFolderNode.Nodes.Add(folderNode);

                    UpdateFolderStructure(folderNode, subFolder, invalidFilenames, extensionsToLoad);
                }
                else
                {
                    UpdateFolderStructure(subNodes[subFolder.Name], subFolder, invalidFilenames, extensionsToLoad);
                }
            }

            foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly))
            {
                if (WebResource.IsNameValid(fiChild.Name) && WebResource.IsValidExtension(fiChild.Extension))
                {
                    if (extensionsToLoad == null || extensionsToLoad.Contains(fiChild.Extension))
                    {
                        if (!subNodes.ContainsKey(fiChild.Name) || subNodes[fiChild.Name].ImageIndex <= 1 || subNodes[fiChild.Name].ImageIndex >= 14 && subNodes[fiChild.Name].ImageIndex <= 15)
                        {
                            CreateWebResourceNode(fiChild, parentFolderNode);
                        }
                        else
                        {
                            var wr = (WebResource)subNodes[fiChild.Name].Tag;
                            wr.EntityContent = Convert.ToBase64String(File.ReadAllBytes(wr.FilePath));
                            wr.RefreshAssociatedContent();
                        }
                    }
                }
                else if (!WebResource.IsNameValid(fiChild.Name) || !WebResource.SkipErrorForInvalidExtension(fiChild.Extension))
                {
                    invalidFilenames.Add(fiChild.FullName);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Create a TreeView folder structure depending on the DirectoryInfo
        /// element in parameters
        /// </summary>
        /// <param name="parentFolderNode">Current TreeNode</param>
        /// <param name="di">Current physical directory info</param>
        /// <param name="invalidFilenames"></param>
        /// <param name="extensionsToLoad"></param>
        private void CreateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad)
        {
            foreach (DirectoryInfo diChild in di.GetDirectories())
            {
                if (!WebResource.IsNameValid(diChild.Name))
                {
                    invalidFilenames.Add(diChild.FullName);
                    continue;
                }

                // If the current physical directory has sub directories or
                // javascript file, a new TreeNode has to be created
                if (diChild.GetDirectories().Length > 0 || diChild.GetFiles("*.*").Length > 0)
                {
                    var folderNode = new TreeNode(diChild.Name)
                    {
                        ImageIndex = 13, SelectedImageIndex = 13, Tag = diChild.FullName, Name = diChild.Name
                    };

                    parentFolderNode.Nodes.Add(folderNode);

                    CreateFolderStructure(folderNode, diChild, invalidFilenames, extensionsToLoad);
                }
            }

            foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly))
            {
                if (WebResource.IsNameValid(fiChild.Name) && WebResource.IsValidExtension(fiChild.Extension))
                {
                    if (extensionsToLoad == null || extensionsToLoad.Contains(fiChild.Extension))
                    {
                        // Create a TreeNode for each javascript file
                        CreateWebResourceNode(fiChild, parentFolderNode);
                    }
                }
                else if (!WebResource.IsNameValid(fiChild.Name) || !WebResource.SkipErrorForInvalidExtension(fiChild.Extension))
                {
                    invalidFilenames.Add(fiChild.FullName);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Retrieve from disk and store web resources in the current control
        /// </summary>
        /// <param name="folderPath">Path of the folder from where to load the resource</param>
        /// <param name="extensionsToLoad">Extensions valid for files to load</param>
        public List <string> LoadWebResourcesFromDisk(string folderPath, List <string> extensionsToLoad)
        {
            tv.Nodes.Clear();
            WebResources.Clear();
            Invoke(new Action(() =>
            {
                pnlWaitingPublish.Visible = false;
            }));

            var invalidFilenames = new List <string>();

            var di = new DirectoryInfo(folderPath);

            // Only folder ending with "_" character are processed. They represent
            // the prefix of customizations
            foreach (DirectoryInfo diChild in di.GetDirectories("*_", SearchOption.TopDirectoryOnly))
            {
                if (WebResource.IsNameValid(diChild.Name))
                {
                    // Create a root treenode
                    var rootFolderNode = new TreeNode(diChild.Name)
                    {
                        ImageIndex = 14,
                        Tag        = diChild.FullName
                    };
                    tv.Nodes.Add(rootFolderNode);

                    // Add child folders
                    CreateFolderStructure(rootFolderNode, diChild, invalidFilenames, extensionsToLoad);
                }
                else
                {
                    invalidFilenames.Add(diChild.FullName);
                }
            }

            // For each files that wouldn't use virtual folders structure, all
            // files are processed
            foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly))
            {
                // We cannot process files without extensions since we
                // won't be able to identify the type of file
                if (fiChild.Extension.Length == 0)
                {
                    invalidFilenames.Add(fiChild.FullName);
                    continue;
                }

                // Do not process files with invalid names or extensions not related
                // to web resources
                if (WebResource.IsNameValid(fiChild.Name) && WebResource.IsValidExtension(fiChild.Extension))
                {
                    // If the file is of type we want to load, the node is created
                    if (extensionsToLoad.Contains(fiChild.Extension))
                    {
                        CreateWebResourceNode(fiChild, tv);
                    }
                }
                else if (!WebResource.IsNameValid(fiChild.Name) || !WebResource.SkipErrorForInvalidExtension(fiChild.Extension))
                {
                    invalidFilenames.Add(fiChild.FullName);
                }
            }

            if (Options.Instance.ExpandAllOnLoadingResources)
            {
                tv.ExpandAll();
            }
            tv.TreeViewNodeSorter = new NodeSorter();
            tv.Sort();

            return(invalidFilenames);
        }