AddChild() public method

public AddChild ( ProjectItem item ) : ProjectItemViewModel
item Gilgame.SEWorkbench.Models.ProjectItem
return ProjectItemViewModel
Exemplo n.º 1
0
        private void CreateNewProjectTemplate()
        {
            if (_RootItem == null)
            {
                return;
            }

            string demofolder = "Library";
            string rootpath   = Path.GetDirectoryName(_RootItem.Path);
            string folderpath = Path.Combine(rootpath, demofolder);
            string filepath   = Path.Combine(folderpath, String.Format("NewScript.csx"));

            try
            {
                Directory.CreateDirectory(folderpath);
                ProjectItem folder = new ProjectItem()
                {
                    Name    = demofolder,
                    Path    = folderpath,
                    Type    = ProjectItemType.Folder,
                    Project = this,
                };

                File.Write(filepath, Services.NewFile.Contents);
                ProjectItem file = new ProjectItem()
                {
                    Name    = Path.GetFileNameWithoutExtension(filepath),
                    Path    = filepath,
                    Type    = ProjectItemType.File,
                    Project = this,
                };

                ProjectItemViewModel newfile = null;

                _RootItem.AddChild(folder);
                if (_RootItem.Children.Count > 0)
                {
                    foreach (ProjectItemViewModel item in _RootItem.Children)
                    {
                        if (item.Type == ProjectItemType.Folder)
                        {
                            newfile = item.AddChild(file);
                            if (item.Children.Count > 0)
                            {
                                item.Children[0].IsSelected = true;
                            }
                            item.IsExpanded = true;

                            break;
                        }
                    }
                }

                if (newfile != null)
                {
                    RaiseFileCreated(newfile);
                }
            }
            catch (Exception ex)
            {
                MessageBox.ShowError("Unable to create new project template", ex);
            }
        }
Exemplo n.º 2
0
        public void PerformAddExistingFile()
        {
            ProjectItemViewModel selected = SelectedItem;

            if (selected == null)
            {
                return;
            }
            if (selected.Type != ProjectItemType.Blueprints && selected.Type != ProjectItemType.Collection && selected.Type != ProjectItemType.Folder)
            {
                selected = GetParentFolder(selected);
            }
            if (selected == null || selected.Type == ProjectItemType.Root)
            {
                return;
            }

            string initial = selected.Path;

            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog()
            {
                DefaultExt       = ".csx",
                Filter           = "Script File (.csx)|*.csx",
                InitialDirectory = initial,
                Multiselect      = true,
            };

            Nullable <bool> result = dialog.ShowDialog();

            if (result != null && result.Value == true)
            {
                foreach (string source in dialog.FileNames)
                {
                    string destination = Path.Combine(initial, Path.GetFileName(source));
                    if (File.Exists(destination))
                    {
                        string message = String.Format(
                            "A file with the name \"{0}\" already exists. Do you want to overwrite it?",
                            Path.GetFileName(source)
                            );

                        try
                        {
                            if (Path.GetDirectoryName(source) != Path.GetDirectoryName(destination))
                            {
                                System.Windows.MessageBoxResult overwrite = MessageBox.ShowQuestion(message);
                                if (overwrite == System.Windows.MessageBoxResult.No)
                                {
                                    continue;
                                }
                                if (overwrite == System.Windows.MessageBoxResult.Cancel)
                                {
                                    break;
                                }
                                File.Copy(source, destination);
                            }

                            string code = File.Read(destination);

                            string      name = Path.GetFileNameWithoutExtension(destination);
                            ProjectItem item = new ProjectItem()
                            {
                                Name    = name,
                                Path    = destination,
                                Type    = ProjectItemType.File,
                                Project = this
                            };
                            selected.AddChild(item);
                            selected.IsExpanded = true;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.ShowError("Unable to create new file", ex);
                            return;
                        }
                    }
                }
                SaveProject();
            }
        }