Example #1
0
        public static void DisableSearch()
        {
            // Set Searching to true
            MacroFSNode.Searching = false;

            MacroFSNode.UnmatchAllNodes(MacroFSNode.RootNode);
        }
Example #2
0
        public MacrosToolWindow()
            : base(null)
        {
            this.owningPackage = VSMacrosPackage.Current;
            this.Caption = Resources.ToolWindowTitle;
            this.BitmapResourceID = 301;
            this.BitmapIndex = 1;

            // Instantiate Tool Window Toolbar
            this.ToolBar = new CommandID(GuidList.GuidVSMacrosCmdSet, PkgCmdIDList.MacrosToolWindowToolbar);

            Manager.CreateFileSystem();

            string macroDirectory = Manager.MacrosPath;

            // Create tree view root
            MacroFSNode root = new MacroFSNode(macroDirectory);

            // Make sure it is opened and selected by default
            root.IsExpanded = true;

            // Initialize Macros Control
            var macroControl = new MacrosControl(root);
            macroControl.Loaded += this.OnLoaded;
            this.Content = macroControl;

            Manager.Instance.LoadFolderExpansion();
        }
Example #3
0
        public static void EnableSearch()
        {
            // Set Searching to true
            MacroFSNode.Searching = true;

            // And then notify all node that their IsMatch property might have changed
            MacroFSNode.NotifyAllNode(MacroFSNode.RootNode, "IsMatch");
        }
        public MacrosControl(MacroFSNode rootNode)
        {
            Current = this;

            MacroFSNode.RootNode = rootNode;

            // Let the UI bind to the view-model
            this.DataContext = new MacroFSNode[] { rootNode };
            this.InitializeComponent();
        }
Example #5
0
 public static void CollapseAllNodes(MacroFSNode root)
 {
     if (root.Children != null)
     {
         foreach (var child in root.Children)
         {
             child.IsExpanded = false;
             MacroFSNode.CollapseAllNodes(child);
         }
     }
 }
Example #6
0
        // Notifies all the nodes of the tree rooted at 'node'
        public static void NotifyAllNode(MacroFSNode root, string property)
        {
            root.NotifyPropertyChanged(property);

            if (root.Children != null)
            {
                foreach (var child in root.Children)
                {
                    MacroFSNode.NotifyAllNode(child, property);
                }
            }
        }
Example #7
0
        public static MacroFSNode SelectNode(string path)
        {
            // Find node
            MacroFSNode node = FindNodeFromFullPath(path);

            if (node != null)
            {
                // Select it
                node.IsSelected = true;
            }

            return(node);
        }
Example #8
0
        private void AfterRefresh(MacroFSNode root, string selectedPath, HashSet <string> dirs)
        {
            // Set IsEnabled for each folders
            root.SetIsExpanded(root, dirs);

            // Selecte the previously selected macro
            MacroFSNode selected = MacroFSNode.FindNodeFromFullPath(selectedPath);

            selected.IsSelected = true;

            // Notify change
            root.NotifyPropertyChanged("Children");
        }
Example #9
0
        public MacroFSNode(string path, MacroFSNode parent = null)
        {
            this.IsDirectory = (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
            this.FullPath    = path;
            this.shortcut    = MacroFSNode.ToFetch;
            this.isEditable  = false;
            this.isSelected  = false;
            this.isExpanded  = false;
            this.isMatch     = false;
            this.parent      = parent;

            // Monitor that node
            //FileChangeMonitor.Instance.MonitorFileSystemEntry(this.FullPath, this.IsDirectory);
        }
Example #10
0
        public static void UnmatchAllNodes(MacroFSNode root)
        {
            root.isMatch = false;
            root.NotifyPropertyChanged("IsMatch");
            root.NotifyPropertyChanged("IsExpanded");

            if (root.Children != null)
            {
                foreach (var child in root.Children)
                {
                    MacroFSNode.UnmatchAllNodes(child);
                }
            }
        }
Example #11
0
        public MacroFSNode(string path, MacroFSNode parent = null)
        {
            this.IsDirectory = (File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory;
            this.FullPath = path;
            this.shortcut = MacroFSNode.ToFetch;
            this.isEditable = false;
            this.isSelected = false;
            this.isExpanded = false;
            this.isMatch = false;
            this.parent = parent;

            // Monitor that node
            //FileChangeMonitor.Instance.MonitorFileSystemEntry(this.FullPath, this.IsDirectory);
        }
Example #12
0
        public static void RefreshTree(MacroFSNode root)
        {
            MacroFSNode selected = MacrosControl.Current.MacroTreeView.SelectedItem as MacroFSNode;

            // Make a copy of the hashset
            HashSet <string> dirs = new HashSet <string>(enabledDirectories);

            // Clear enableDirectories
            enabledDirectories.Clear();

            // Retrieve children in a background thread
            //Task.Run(() => root.children = root.GetChildNodes())
            //    .ContinueWith(_ => root.AfterRefresh(root, selected.FullPath, dirs), TaskScheduler.FromCurrentSynchronizationContext());
            root.children = root.GetChildNodes();
            root.AfterRefresh(root, selected.FullPath, dirs);
        }
Example #13
0
        /// <summary>
        /// Finds the node with FullPath path in the entire tree
        /// </summary>
        /// <param name="path"></param>
        /// <returns>MacroFSNode  whose FullPath is path</returns>
        public static MacroFSNode FindNodeFromFullPath(string path)
        {
            if (MacroFSNode.RootNode == null)
            {
                return(null);
            }

            // Default node if search fails
            MacroFSNode defaultNode = MacroFSNode.RootNode.Children.Count > 0 ? MacroFSNode.RootNode.Children[0] : MacroFSNode.RootNode;

            // Make sure path is a valid string
            if (string.IsNullOrEmpty(path))
            {
                return(defaultNode);
            }

            // Split the string at '\'
            string shortenPath = path.Substring(path.IndexOf(@"\Macros"));

            string[] substrings = shortenPath.Split(new char[] { '\\' });

            // Starting from the root,
            MacroFSNode node = MacroFSNode.RootNode;

            try
            {
                // Go down the tree to find the right node
                // 2 because substrings[0] == "" and substrings[1] is root
                for (int i = 3; i < substrings.Length; i++)
                {
                    node = node.Children.Single(x => x.Name == Path.GetFileNameWithoutExtension(substrings[i]));
                }
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                {
                    throw;
                }

                // Return default node
                node = defaultNode;
            }

            return(node);
        }
Example #14
0
        /// <summary>
        /// Expands all the node marked as expanded in <paramref name="enabledDirs"/>.
        /// </summary>
        /// <param name="node">Tree rooted at node.</param>
        /// <param name="enabledDirs">Hash set containing the enabled dirs.</param>
        private void SetIsExpanded(MacroFSNode node, HashSet <string> enabledDirs)
        {
            node.IsExpanded = true;

            // OPTIMIZATION IDEA instead of iterating over the children, iterate over the enableDirs
            if (node.Children.Count > 0 && enabledDirs.Count > 0)
            {
                foreach (var item in node.children)
                {
                    if (item.IsDirectory && enabledDirs.Remove(item.FullPath))
                    {
                        // Set IsExpanded
                        item.IsExpanded = true;

                        // Recursion on children
                        this.SetIsExpanded(item, enabledDirs);
                    }
                }
            }
        }
Example #15
0
        /// <summary>
        /// Expands all the node marked as expanded in <paramref name="enabledDirs"/>.
        /// </summary>
        /// <param name="node">Tree rooted at node.</param>
        /// <param name="enabledDirs">Hash set containing the enabled dirs.</param>
        private void SetIsExpanded(MacroFSNode node, HashSet<string> enabledDirs)
        {
            node.IsExpanded = true;

            // OPTIMIZATION IDEA instead of iterating over the children, iterate over the enableDirs
            if (node.Children.Count > 0 && enabledDirs.Count > 0)
            {
                foreach (var item in node.children)
                {
                    if (item.IsDirectory && enabledDirs.Remove(item.FullPath))
                    {
                        // Set IsExpanded
                        item.IsExpanded = true;

                        // Recursion on children
                        this.SetIsExpanded(item, enabledDirs);
                    }
                }
            }
        }
Example #16
0
        private void AfterRefresh(MacroFSNode root, string selectedPath, HashSet<string> dirs)
        {
            // Set IsEnabled for each folders
            root.SetIsExpanded(root, dirs);

            // Selecte the previously selected macro
            MacroFSNode selected = MacroFSNode.FindNodeFromFullPath(selectedPath);
            selected.IsSelected = true;

            // Notify change
            root.NotifyPropertyChanged("Children");
        }
Example #17
0
 public bool Equals(MacroFSNode node)
 {
     return this.FullPath == node.FullPath;
 }
Example #18
0
        public static void RefreshTree()
        {
            MacroFSNode root = MacroFSNode.RootNode;

            MacroFSNode.RefreshTree(root);
        }
Example #19
0
        public static void UnmatchAllNodes(MacroFSNode root)
        {
            root.isMatch = false;
            root.NotifyPropertyChanged("IsMatch");
            root.NotifyPropertyChanged("IsExpanded");

            if (root.Children != null)
            {
                foreach (var child in root.Children)
                {
                    MacroFSNode.UnmatchAllNodes(child);
                }
            }
        }
Example #20
0
        public static void RefreshTree(MacroFSNode root)
        {
            MacroFSNode selected = MacrosControl.Current.MacroTreeView.SelectedItem as MacroFSNode;

            // Make a copy of the hashset
            HashSet<string> dirs = new HashSet<string>(enabledDirectories);

            // Clear enableDirectories
            enabledDirectories.Clear();

            // Retrieve children in a background thread
            //Task.Run(() => root.children = root.GetChildNodes())
            //    .ContinueWith(_ => root.AfterRefresh(root, selected.FullPath, dirs), TaskScheduler.FromCurrentSynchronizationContext());
            root.children = root.GetChildNodes();
            root.AfterRefresh(root, selected.FullPath, dirs);
        }
Example #21
0
        // Notifies all the nodes of the tree rooted at 'node'
        public static void NotifyAllNode(MacroFSNode root, string property)
        {
            root.NotifyPropertyChanged(property);

            if (root.Children != null)
            {
                foreach (var child in root.Children)
                {
                    MacroFSNode.NotifyAllNode(child, property);
                }
            }
        }
Example #22
0
        public void MoveItem(MacroFSNode sourceItem, MacroFSNode targetItem)
        {
            string sourcePath = sourceItem.FullPath;
            string targetPath = Path.Combine(targetItem.FullPath, sourceItem.Name);
            string extension = ".js";

            MacroFSNode selected;

            // We want to expand the node and all its parents if it was expanded before OR if it is a file
            bool wasExpanded = sourceItem.IsExpanded;

            try
            {
                // Move on disk
                if (sourceItem.IsDirectory)
                {
                    System.IO.Directory.Move(sourcePath, targetPath);
                }
                else
                {
                    targetPath = targetPath + extension;
                    System.IO.File.Move(sourcePath, targetPath);

                    // Close in the editor
                    this.Reopen(sourcePath, targetPath);
                }

                // Move shortcut as well
                if (sourceItem.Shortcut != MacroFSNode.None)
                {
                    int shortcutNumber = sourceItem.Shortcut;
                    Manager.Shortcuts[shortcutNumber] = targetPath;
                }
            }
            catch (Exception e)
            {
                if (ErrorHandler.IsCriticalException(e))
                {
                    throw;
                }

                targetPath = sourceItem.FullPath;

                Manager.Instance.ShowMessageBox(e.Message);
            }

            CreateCurrentMacro();

            // Refresh tree
            MacroFSNode.RefreshTree();

            // Restore previously selected node
            selected = MacroFSNode.SelectNode(targetPath);
            selected.IsExpanded = wasExpanded;
            selected.Parent.IsExpanded = true;

            // Notify change in shortcut
            selected.Shortcut = MacroFSNode.ToFetch;

            // Make editable if the macro is the current macro
            if (sourceItem.FullPath == Manager.CurrentMacroPath)
            {
                selected.IsEditable = true;
            }
        }
Example #23
0
        private string GetExecutingMacroNameForPossibleErrorDisplay(MacroFSNode node, string path)
        {
            if(node != null)
            {
                return node.Name;
            }

            if(path == null)
            {
                throw new ArgumentNullException("path");
            }

            int lastBackslash = path.LastIndexOf('\\');
            string fileName = Path.GetFileNameWithoutExtension(path.Substring(lastBackslash != -1 ? lastBackslash + 1 : 0));

            return fileName;
        }
Example #24
0
        public void SaveCurrent()
        {
            SaveCurrentDialog dlg = new SaveCurrentDialog();
            dlg.ShowDialog();

            if (dlg.DialogResult == true)
            {
                try
                {
                    string pathToNew = Path.Combine(Manager.MacrosPath, dlg.MacroName.Text + ".js");
                    string pathToCurrent = Manager.CurrentMacroPath;

                    int newShortcutNumber = dlg.SelectedShortcutNumber;

                    // Move Current to new file and create a new Current
                    File.Move(pathToCurrent, pathToNew);
                    CreateCurrentMacro();

                    MacroFSNode macro = new MacroFSNode(pathToNew, MacroFSNode.RootNode);

                    if (newShortcutNumber != MacroFSNode.None)
                    {
                        // Update dictionary
                        Manager.Shortcuts[newShortcutNumber] = macro.FullPath;
                    }

                    this.SaveShortcuts(true);

                    this.Refresh();

                    // Select new node
                    MacroFSNode.SelectNode(pathToNew);
                }
                catch (Exception e)
                {
                    if (ErrorHandler.IsCriticalException(e))
                    {
                        throw;
                    }

                    this.ShowMessageBox(e.Message);
                }
            }
        }
Example #25
0
 public static void CollapseAllNodes(MacroFSNode root)
 {
     if (root.Children != null)
     {
         foreach (var child in root.Children)
         {
             child.IsExpanded = false;
             MacroFSNode.CollapseAllNodes(child);
         }
     }
 }
Example #26
0
        private bool InSamples(MacroFSNode node)
        {
            do
            {
                if (node.FullPath == Manager.SamplesFolderPath)
                {
                    return true;
                }

                node = node.Parent;
            } while (node != MacroFSNode.RootNode);

            return false;
        }
Example #27
0
            private void TraverseAndMark(MacroFSNode root, string searchString, StringComparison comp, bool withinFileContents)
            {
                if (this.Contains(root.Name, searchString, comp))
                {
                    root.IsMatch = true;
                }
                else if (withinFileContents && !root.IsDirectory)
                {
                    System.Threading.Tasks.Task.Run(() =>
                    {
                        string allText = File.ReadAllText(root.FullPath);
                        if (this.Contains(allText, searchString, comp))
                        {
                            root.IsMatch = true;
                        }
                    });
                }
                else
                {
                    root.IsMatch = false;
                }

                if (root.Children != null)
                {
                    foreach (var child in root.Children)
                    {
                        this.TraverseAndMark(child, searchString, comp, withinFileContents);
                    }
                }
            }
Example #28
0
 private bool ValidDropTarget(MacroFSNode sourceItem, MacroFSNode targetItem)
 {
     // Check whether the target item is meeting your condition
     return !sourceItem.Equals(targetItem);
 }
Example #29
0
 public bool Equals(MacroFSNode node)
 {
     return(this.FullPath == node.FullPath);
 }