Пример #1
0
        void EditItemHeader(FolderWindowItem item, bool IsNewFile)
        {
            var txtbox = item.Template.FindName("PART_Header", item) as TextBox;

            if (txtbox == null)
            {
                return;
            }

            txtbox.IsEnabled = true;

            Keyboard.Focus(txtbox);

            var dot = txtbox.Text.LastIndexOf('.');

            if (dot == -1)
            {
                txtbox.SelectAll();
            }
            else
            {
                txtbox.Select(0, dot);
            }

            txtbox.KeyDown += txtbox_KeyDown;

            edit_isNewFile    = IsNewFile;
            edit_item         = item;
            edit_txtbox       = txtbox;
            txtbox.LostFocus += txtbox_LostFocus;
        }
Пример #2
0
        List <FolderWindowItem> MakeTree(string folder)
        {
            var list = new List <FolderWindowItem>();

            foreach (var f in Directory.EnumerateDirectories(folder))
            {
                if (File.GetAttributes(f).HasFlag(FileAttributes.Hidden))
                {
                    continue;
                }
                var item = new FolderWindowItem(f.Substring(f.LastIndexOf(Path.DirectorySeparatorChar) + 1), true);

                foreach (var l in MakeTree(Path.Combine(folder, f)))
                {
                    item.Items.Add(l);
                }
                list.Add(item);
            }

            foreach (var f in Directory.EnumerateFiles(folder, "*.lua").Where((name) => { return(name.ToLower().EndsWith(".lua")); }))
            {
                if (File.GetAttributes(f).HasFlag(FileAttributes.Hidden))
                {
                    continue;
                }

                list.Add(new FolderWindowItem(Path.GetFileName(f)));

                Files.Add(f.Replace(LuaPath + Path.DirectorySeparatorChar, ""));
            }

            return(list);
        }
Пример #3
0
        void AsyncFileChangesToTreeView(string oldName, string newName, bool isFolder)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (oldName != null)
                {
                    var item = FindItemInTree(oldName);
                    if (item != null)
                    {
                        if (newName == null)
                        {
                            (item.Parent as ItemsControl).Items.Remove(item);
                        }
                        else
                        {
                            (item as FolderWindowItem).SetFileName(newName);
                        }
                    }
                }
                else
                {
                    var layers = newName.Split(Path.DirectorySeparatorChar);
                    var pat    = TreeView as ItemsControl;

                    //找到新建项的位置,添加项
                    for (int i = 0; i < layers.Length - 1; i++)
                    {
                        bool find = false;
                        foreach (FolderWindowItem item in pat.Items)
                        {
                            if (item.FileName == layers[i])
                            {
                                pat  = item;
                                find = true;
                                break;
                            }
                        }
                        if (find)
                        {
                            continue;
                        }
                        var folder = new FolderWindowItem(layers[i], true);
                        pat.Items.Add(folder);
                        pat = folder;
                    }
                    foreach (FolderWindowItem item in pat.Items)
                    {
                        if (item.FileName == layers.Last())
                        {
                            return;
                        }
                    }
                    var file = new FolderWindowItem(layers.Last(), isFolder);
                    pat.Items.Add(file);
                }
            });
        }
Пример #4
0
        string GetItemPath(FolderWindowItem item)
        {
            List <string> paths = new List <string>();

            var pat = (item.Parent as FolderWindowItem);

            while (pat != null)
            {
                paths.Add(pat.FileName);
                pat = (pat.Parent as FolderWindowItem);
            }
            paths.Add(LuaPath);
            paths.Reverse();

            string path = Path.Combine(paths.ToArray());

            return(path);
        }
Пример #5
0
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (sender == Menu_NewFile)
            {
                ItemsControl parent = TreeView;
                //获取创建位置
                if (TreeView.SelectedItem != null)
                {
                    var pat = TreeView.SelectedItem as FolderWindowItem;
                    if (pat.IsFolder)
                    {
                        parent = pat;
                    }
                    else
                    {
                        parent = pat.Parent as ItemsControl;
                    }
                }

                var item = new FolderWindowItem(string.Format("File{0}.lua", FindStartIndex(parent, "File", false)));
                parent.Items.Add(item);
                if (parent is TreeViewItem)
                {
                    (parent as TreeViewItem).IsExpanded = true;
                }
                parent.UpdateLayout();
                EditItemHeader(item, true);
            }
            else if (sender == Menu_NewFolder)
            {
                ItemsControl parent = TreeView;
                if (TreeView.SelectedItem != null)
                {
                    var pat = TreeView.SelectedItem as FolderWindowItem;

                    if (pat.IsFolder)
                    {
                        parent = pat;
                    }
                    else
                    {
                        parent = pat.Parent as ItemsControl;
                    }
                }

                var item = new FolderWindowItem(string.Format("Folder{0}", FindStartIndex(parent, "Folder", true)), true);
                parent.Items.Add(item);
                if (parent is TreeViewItem)
                {
                    (parent as TreeViewItem).IsExpanded = true;
                }
                parent.UpdateLayout();
                EditItemHeader(item, true);
            }
            else if (sender == Menu_ReName && TreeView.SelectedItem != null)
            {
                EditItemHeader(TreeView.SelectedItem as FolderWindowItem, false);
            }
            else if (sender == Menu_Delete && TreeView.SelectedItem != null)
            {
                var item = TreeView.SelectedItem as FolderWindowItem;
                if (Delete(GetItemPath(item), item.FileName, item.IsFolder))
                {
                    (item.Parent as ItemsControl).Items.Remove(item);
                }
            }
            else if (sender == Menu_OpenContainFolder)
            {
                string path = LuaPath;

                if (TreeView.SelectedItem != null)
                {
                    var item = TreeView.SelectedItem as FolderWindowItem;

                    path = GetItemPath(item);

                    path = Path.Combine(path, item.FileName);
                }

                try
                {
                    System.Diagnostics.Process.Start("Explorer.exe", string.Format("/select,{0}", path));
                }
                catch
                {
                    MessageBox.Show("open fail", "Error");
                }
            }
        }