Пример #1
0
        public static Item CreateItem(DirectoryInfo dirInfo, Project.Directory pFolder, Item parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent", "Item parent cannot be null. For root items use the other overloaded method.");
            }

            return(CreateItem(dirInfo, pFolder, parent.State, parent, parent.logic));
        }
Пример #2
0
        public Item CreateFile(string path, ItemType type)
        {
            var dirs     = path.Split('/', '\\');
            var fname    = dirs.Last();                              //File name
            var fullName = Project.CurrentProject._Directory + path; //Complete file path

            Array.Resize(ref dirs, dirs.Length - 1);                 //Remove filename
            var fullDirs = new string[dirs.Length];

            fullDirs[0] = Project.CurrentProject._Directory + dirs[0];
            for (int x = 1; x < dirs.Length; x++)
            {
                fullDirs[x] = fullDirs[x - 1] + "/" + dirs[x];
            }

            int  i = 0;
            Item d = (from it in RootDirectories where it.Name == dirs[i] select it).FirstOrDefault();

            if (d == null)
            {
                //New Root Directory
                Project.Directory dir = new Project.Directory(dirs[i], fullDirs[i]);
                Project.CurrentProject.ProjectFiles.SubDirectories.Add(dir);
                RootDirectories.Add(d = new Item(null, this, ItemTypes.Directory, dirs[i], fullDirs[i], Item.ItemState.NORMAL, dir));
            }
            i++;

            Directory.CreateDirectory(fullDirs.Last());
            File.Create(fullName)?.Close();

            //Go through all existing directories
            for (; i < dirs.Length; i++)
            {
                d.MakeStateNormal();
                Item d2 = (from it in d.DirectoryChildren where it.Name == dirs[i] select it).FirstOrDefault();
                if (d2 != null)
                {
                    d = d2;
                }
                else
                {
                    return(CreateItems(d, type, fname, fullName, dirs, fullDirs, i));
                }
            }
            d.MakeStateNormal();
            //Create file
            var f = new Project.File(fname);

            (d.FileObject as Project.Directory).Files.Add(f);
            var result = new FileBrowser.Item(d, this, type, fname, fullName, Item.ItemState.NORMAL, f);

            d.AddChild(result);
            return(result);
        }
Пример #3
0
        public bool CreateFile(string name, ItemType type, out Item item, bool addToProject = true)
        {
            if (this.type != ItemTypes.Directory)
            {
                throw new InvalidOperationException("Item is not a directory");
            }
            if (name.Contains('/') || name.Contains('\\'))
            {
                throw new ArgumentException("Name has to be the filename not the path", "name");
            }
            if (this.State != ItemState.NORMAL)
            {
                throw new InvalidOperationException("Itemstate must be normal");
            }

            string path = this.Path + "/" + name;

            if (File.Exists(path))
            {
                MessageBox.Show("File already exists");
                item = null;
                return(false);
            }
            try
            {
                var stream = File.Create(path);
                stream.Close();
            } catch (Exception e)
            {
                MessageBox.Show(e.Message);
                item = null;
                return(false);
            }
            if (addToProject)
            {
                var dir = new Project.Directory(name, path);
                (this.FileObject as Project.Directory).Content.Add(dir);
                (this.FileObject as Project.Directory).SubDirectories.Add(dir);
                item = new Item(this, this.logic, type, name, path, ItemState.NORMAL, dir);
            }
            else
            {
                item = new Item(this, this.logic, type, name, path, ItemState.NOT_PROJECT, null);
            }
            this.AllChildren.Add(item);
            return(true);
        }
Пример #4
0
        private Item CreateItems(Item root, ItemType type, string fname, string fullName, string[] dirs, string[] fullDirs, int i)
        {
            Item item = root;
            Item item2;

            Project.File      f;
            Project.Directory d;
            //Create all directories
            for (; i < dirs.Length; i++)
            {
                (item.FileObject as Project.Directory).SubDirectories.Add(d = new Project.Directory(dirs[i], fullDirs[i]));
                item.AddChild(item2 = new FileBrowser.Item(item, this, ItemTypes.Directory, dirs[i], fullDirs[i], Item.ItemState.NORMAL, d));
                item = item2;
            }
            //Create file
            (item.FileObject as Project.Directory).Files.Add(f = new Project.File(fname));
            item.AddChild(item2 = new FileBrowser.Item(item, this, type, fname, fullName, Item.ItemState.NORMAL, f));
            return(item2);
        }
Пример #5
0
        public static Item CreateItem(DirectoryInfo dirInfo, Project.Directory pFolder, Item.ItemState state, Item parent, Logic logic)
        {
            if (!dirInfo.Exists && state == Item.ItemState.NOT_PROJECT)
            {
                throw new ArgumentException("Directory doesn't exist but is in a NOT_PROJECT content!");
            }
            if (dirInfo.Exists && state == Item.ItemState.NOT_FOUND)
            {
                throw new ArgumentException("Directory exists but is in a NOT_FOUND content!");
            }

            var  newState = GetNewItemState(dirInfo.Exists, state, pFolder != null);
            Item result   = new Item(parent, logic, ItemTypes.Directory, dirInfo.Name, newState == Item.ItemState.NOT_FOUND ? null : dirInfo.FullName, newState, pFolder);

            ObservableCollection <Project.Directory> folders = pFolder != null ? new ObservableCollection <Project.Directory>(pFolder.SubDirectories) : null;
            ObservableCollection <Project.File>      files   = pFolder != null ? new ObservableCollection <Project.File>(pFolder.Files) : null;

            try
            {
                foreach (var d in dirInfo.GetDirectories())
                {
                    if ((d.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    {
                        continue;
                    }

                    Project.Directory folder = null;

                    if (pFolder != null)
                    {
                        foreach (var f in folders)
                        {
                            if (f.Name.Equals(d.Name))
                            {
                                folder = f;
                                folders.Remove(f);
                                break;
                            }
                        }
                    }

                    Item i = CreateItem(d, folder, result);
                    result.AddChildSilently(i);
                }
            }
            catch (UnauthorizedAccessException) { }
            catch (DirectoryNotFoundException) { }
            if (pFolder != null)
            {
                foreach (var f in folders)
                {
                    Item i = CreateItem(new DirectoryInfo(f.Path), f, result);
                    result.AddChildSilently(i);
                }
            }
            try
            {
                foreach (var f in dirInfo.GetFiles())
                {
                    bool inProj = false;

                    if (pFolder != null)
                    {
                        foreach (var file in files)
                        {
                            if (file.Name.Equals(f.Name))
                            {
                                inProj = true;
                                files.Remove(file);
                                break;
                            }
                        }
                    }

                    Item i = CreateItem(f, result, inProj, null);
                    result.AddChildSilently(i);
                }
            }
            catch (UnauthorizedAccessException) { }
            catch (DirectoryNotFoundException) { }
            if (pFolder != null)
            {
                foreach (var file in files)
                {
                    Item i = CreateItem(new FileInfo(file.Path), result, true, file);
                    result.AddChildSilently(i);
                }
            }

            result.UpdateChildren();
            return(result);
        }