private void TemplateNodeItemDelete(NodeItem param)
        {
            if (param.IsDir)
            {
                if (Directory.Exists(param.FullName))
                {
                    _watcher.EnableRaisingEvents = false;
                    Directory.Delete(param.FullName, true);
                    _watcher.EnableRaisingEvents = true;
                }
            }
            else
            {
                if (File.Exists(param.FullName))
                {
                    _watcher.EnableRaisingEvents = false;
                    File.Delete(param.FullName);
                    _watcher.EnableRaisingEvents = true;
                }
            }

            var parent = GetParentNodeItem(param);

            parent.Children.Remove(param);
        }
 private void TemplateNodeItemRenameBegin(NodeItem param)
 {
     if (!param.IsRenameing)
     {
         param.NewName     = param.Name;
         param.IsRenameing = true;
     }
 }
 private void TemplatePreviewSave(NodeItem nodeItem)
 {
     if (nodeItem?.FileContent != null)
     {
         File.WriteAllText(nodeItem.FullName, nodeItem.FileContent);
         nodeItem.IsModifed = false;
     }
 }
 private void TemplatePreviewDiscard(NodeItem nodeItem)
 {
     if (nodeItem?.FileContent != null)
     {
         nodeItem.FileContent = File.ReadAllText(nodeItem.FullName);
         nodeItem.IsModifed   = false;
     }
 }
 private void TemplateNodeItemOpen(NodeItem param)
 {
     if (param == TemplateNodeItemRoot)
     {
         Process.Start("explorer.exe", param.FullName);
     }
     else
     {
         DTE.ExecuteCommand("File.OpenFile", param.FullName);
     }
 }
 private void TemplateNodeItemRenameEnd(NodeItem nodeItem)
 {
     if (nodeItem.IsRenameing)
     {
         if (nodeItem.NewName != nodeItem.Name)
         {
             TemplateNodeItemRename(nodeItem, nodeItem.NewName, true);
         }
         nodeItem.IsRenameing = false;
         nodeItem.NewName     = null;
     }
 }
        private void TemplatePreviewFile(NodeItem param)
        {
            if (!param.IsDir)
            {
                using (var fs = new FileStream(param.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (var sr = new StreamReader(fs))
                    {
                        param.FileContent = sr.ReadToEnd();
                    }

                param.IsModifed = false;
            }
        }
        public NodeItem GetParentNodeItem(NodeItem nodeItem)
        {
            var parent = TemplateNodeItemRoot.GetAllChildren().FirstOrDefault(p => p.FullName == nodeItem.ParentFullName);

            if (parent == null)
            {
                if (TemplateNodeItemRoot.FullName == nodeItem.ParentFullName)
                {
                    parent = TemplateNodeItemRoot;
                }
                else
                {
                    return(null);
                }
            }
            return(parent);
        }
        private void TemplateOpen(string fileName)
        {
            TemplatePath    = fileName;
            TemplateTempDir = Directory.CreateDirectory($"{Path.GetTempPath()}\\{Path.GetRandomFileName()}").FullName;
            ZipFile.ExtractToDirectory(TemplatePath, TemplateTempDir);
            var files = Directory.GetFiles(TemplateTempDir, "*", SearchOption.AllDirectories);

            var nodes = new ObservableCollection <NodeItem>();

            nodes.Add(new NodeItem {
                Name = Path.GetFileName(TemplatePath), FullName = TemplateTempDir, IsDir = true
            });
            foreach (var f in files)
            {
                var n = new NodeItem
                {
                    Name           = Path.GetFileName(f),
                    ParentFullName = Path.GetDirectoryName(f),
                    FullName       = f,
                    IsDir          = Directory.Exists(f)
                };

                if (!n.IsDir)
                {
                    n.PreviewType = GetPreviewType(n.FullName);
                }
                n.PropertyChanged += NodeItem_PropertyChanged;
                nodes[0].Children.Add(n);
            }

            TemplateNodeItemList = nodes;
            TemplateNodeItemRoot = nodes[0];

            _watcher                       = new FileSystemWatcher();
            _watcher.Path                  = TemplateTempDir;
            _watcher.NotifyFilter          = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            _watcher.Changed              += Watcher_Changed;
            _watcher.Deleted              += Watcher_Changed;
            _watcher.Renamed              += Watcher_Changed;
            _watcher.Created              += Watcher_Changed;
            _watcher.IncludeSubdirectories = true;
            _watcher.EnableRaisingEvents   = true;
            InputParameterListRefreshCommand.ExecuteIfCan();
        }
        private void TemplateNodeItemRename(NodeItem nodeItem, string newName, bool changeFile)
        {
            var parent = GetParentNodeItem(nodeItem);

            newName = Path.GetFileName(newName);
            if (parent.Children.Any(p => p.Name == newName))
            {
                MessageBox.Show($"File with name '{newName}' already exists");
                return;
            }

            var newFullName = Directory.GetParent(nodeItem.FullName) + "\\" + newName;

            if (changeFile)
            {
                try
                {
                    if (nodeItem.IsDir)
                    {
                        Directory.Move(nodeItem.FullName, newFullName);

                        foreach (var item in nodeItem.GetAllChildren())
                        {
                            item.FullName = item.FullName.Replace(nodeItem.FullName, newFullName);
                        }
                    }
                    else
                    {
                        File.Move(nodeItem.FullName, newFullName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Cannot rename item.\n{ex.Message}");
                    return;
                }
            }

            nodeItem.FullName = newFullName;
            nodeItem.Name     = newName;
        }
 private bool CanTemplateNodeItemDelete(NodeItem param)
 {
     return(param != null);
 }
 private bool CanTemplatePreviewDiscard(NodeItem nodeItem)
 {
     return(nodeItem?.IsModifed == true);
 }
 private bool CanTemplateNodeItemOpen(NodeItem param)
 {
     return(param != null);
 }
 private bool CanTemplatePreviewFile(NodeItem param)
 {
     return(param != null);
 }
 private bool CanTemplateNodeItemRenameBegin(NodeItem param)
 {
     return(param != null && TemplateNodeItemCurrent != TemplateNodeItemRoot);
 }
 private bool CanTemplateNodeItemRenameCancel(NodeItem param)
 {
     return(param != null);
 }
 private void TemplateNodeItemRenameCancel(NodeItem param)
 {
     param.IsRenameing = false;
     param.NewName     = null;
 }