public ProjectItemViewModel(ProjectItem item, ProjectItemViewModel parent)
            : base(parent)
        {
            _Model = item;

            _Children = new Services.ObservableSortedList<ProjectItemViewModel>(
                (from child in _Model.Children select new ProjectItemViewModel(child, this)).ToList<ProjectItemViewModel>(),
                new Comparers.ProjectItemComparer()
            );

            _Grid = new Services.ObservableSortedList<GridItemViewModel>(
                new GridItemViewModel[] { },
                new Comparers.GridItemComparer()
            );
        }
 public ProjectItemViewModel(ProjectItem item)
     : this(item, null)
 {
     _FileRequestedCommand = new Commands.DelegateCommand(RaiseFileRequested);
 }
 public ProjectItemViewModel AddChild(ProjectItem item)
 {
     return AddChild(item, null);
 }
        public ProjectItemViewModel AddChild(ProjectItem item, Interop.Grid grid)
        {
            ProjectItemViewModel vm = new ProjectItemViewModel(item, this);
            if (grid != null)
            {
                vm.Grid.Add(CreateGridViewModel(grid));
            }

            _Children.Add(vm);
            Model.Children.Add(item);

            return vm;
        }
Example #5
0
        public void PerformAddReference()
        {
            ProjectItemViewModel selected = SelectedItem;
            if (selected == null)
            {
                return;
            }
            if (selected.Type != ProjectItemType.References)
            {
                return;
            }

            string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string initial = Path.Combine(documents, "SEWorkbench");

            Views.AddReferenceView view = new Views.AddReferenceView()
            {
                Initial = initial
            };

            Nullable<bool> result = view.ShowDialog();
            if (result != null && result.Value == true)
            {
                string path = view.Filename;
                string name = Path.GetFileNameWithoutExtension(path);

                ProjectItemViewModel existing = GetReferences().FirstOrDefault(i => i.Name == name);
                if (existing != null)
                {
                    Services.MessageBox.ShowMessage(String.Format("A reference with this name already exists ({0}).", name));
                    return;
                }

                try
                {
                    ProjectItemViewModel reference = null;

                    ProjectItem item = new ProjectItem()
                    {
                        Name = name,
                        Path = path,
                        Type = ProjectItemType.Reference,
                        Project = this
                    };

                    reference = selected.AddChild(item);
                    if (reference != null)
                    {
                        RaiseReferenceAdded(reference);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.ShowError("Unable to create new file", ex);
                    return;
                }
                SaveProject();
            }
        }
Example #6
0
        public void PerformNewProject()
        {
            if (!IsClosed)
            {
                return;
            }

            string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string initial = Path.Combine(documents, "SEWorkbench");

            bool success = false;
            try
            {
                // TODO Move all IO to a separate class
                Directory.CreateDirectory(initial);

                Views.NewProjectDialog view = new Views.NewProjectDialog()
                {
                    ProjectLocation = initial,
                };

                Nullable<bool> result = view.ShowDialog();
                if (result != null && result.Value == true)
                {
                    string name = view.ProjectName;
                    string filename = String.Format("{0}.seproj", name);
                    string fullpath = Path.Combine(initial, name, filename);

                    Directory.CreateDirectory(Path.GetDirectoryName(fullpath));

                    ProjectViewModel project = new ViewModels.ProjectViewModel(null);
                    ProjectItem root = new ProjectItem()
                    {
                        Name = name,
                        Type = Models.ProjectItemType.Root,
                        Path = fullpath,
                        Project = project,
                    };

                    root.Children.Add(new ProjectItem() { Name = "References", Type = ProjectItemType.References });

                    SetRootItem(root);

                    success = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.ShowError("Unable to create new project", ex);
                return;
            }
            finally
            {
                if (success)
                {
                    CreateNewProjectTemplate();
                    SaveProject();

                    RaiseProjectCreated();
                }
            }
        }
Example #7
0
        public void PerformAddFile()
        {
            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;
            }

            Views.NewItemDialog view = new Views.NewItemDialog();
            Nullable<bool> result = view.ShowDialog();
            if (result != null && result.Value == true)
            {
                string fullpath = Path.Combine(selected.Path, String.Format("{0}.csx", view.ItemName));
                try
                {
                    string starter = Services.NewFile.Contents;

                    File.Write(fullpath, starter);

                    ProjectItemViewModel newfile = null;

                    string name = Path.GetFileNameWithoutExtension(fullpath);
                    ProjectItem item = new ProjectItem()
                    {
                        Name = name,
                        Path = fullpath,
                        Type = ProjectItemType.File,
                        Project = this
                    };
                    newfile = selected.AddChild(item);
                    selected.IsExpanded = true;

                    if (newfile != null)
                    {
                        RaiseFileCreated(newfile);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.ShowError("Unable to create new file", ex);
                    return;
                }
                SaveProject();
            }
        }
Example #8
0
        public void PerformAddFolder()
        {
            ProjectItemViewModel selected = SelectedItem;
            if (selected == null)
            {
                return;
            }
            if (selected.Type != ProjectItemType.Root && selected.Type != ProjectItemType.Blueprints && selected.Type != ProjectItemType.Collection && selected.Type != ProjectItemType.Folder)
            {
                selected = GetParentFolder(selected);
            }
            if (selected == null)
            {
                return;
            }

            Views.NewItemDialog view = new Views.NewItemDialog();
            Nullable<bool> result = view.ShowDialog();
            if (result != null && result.Value == true)
            {
                string name = view.ItemName;

                string fullpath = Path.Combine(selected.Path, name);
                if (selected.Type == ProjectItemType.Root)
                {
                    fullpath = Path.Combine(Path.GetDirectoryName(selected.Path), name);
                }

                try
                {
                    Directory.CreateDirectory(fullpath);
                    ProjectItem item = new ProjectItem()
                    {
                        Name = name,
                        Path = fullpath,
                        Type = ProjectItemType.Folder,
                        Project = this,
                    };
                    selected.AddChild(item);
                    selected.IsExpanded = true;
                }
                catch (Exception ex)
                {
                    MessageBox.ShowError("Unable to create new directory", ex);
                    return;
                }
                SaveProject();
            }
        }
Example #9
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();
            }
        }
Example #10
0
        public void PerformAddBlueprints()
        {
            ProjectItemViewModel rootitem = _RootItem;
            if (rootitem == null)
            {
                return;
            }

            string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string blueprints = Path.Combine(appdata, "SpaceEngineers", "Blueprints");

            // TODO implement my own open file dialog to avoid API
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog()
            {
                DefaultExt = ".sbc",
                Filter = "Blueprints File (.sbc)|*.sbc",
                Multiselect = false,
                InitialDirectory = blueprints,
            };

            Nullable<bool> result = dialog.ShowDialog();
            if (result != null && result.Value == true)
            {
                string fullpath = dialog.FileName;

                string name;
                Interop.Grid grid;

                Interop.Blueprint.Import(fullpath, out name, out grid);
                if (grid != null)
                {
                    string safename = CreateSafeName(name);
                    string savepath = Path.Combine(Path.GetDirectoryName(rootitem.Path), safename);
                    try
                    {
                        Directory.CreateDirectory(savepath);
                        ProjectItem item = new ProjectItem()
                        {
                            Name = name,
                            Path = savepath,
                            Blueprint = fullpath,
                            Type = ProjectItemType.Blueprints,
                            Project = this,
                        };
                        rootitem.AddChild(item, grid);
                        rootitem.IsExpanded = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.ShowError("Unable to create add blueprint", ex);
                        return;
                    }
                    SaveProject();
                }
            }
        }
Example #11
0
 private void SetProject(ProjectItem parent, ProjectViewModel project)
 {
     parent.Project = project;
     foreach (ProjectItem child in parent.Children)
     {
         SetProject(child, project);
     }
 }
Example #12
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);
            }
        }
Example #13
0
        public void SetRootItem(ProjectItem root)
        {
            if (root == null)
            {
                _RootItem = null;
                _First.Clear();
                return;
            }

            SetProject(root, this);

            _RootItem = new ProjectItemViewModel(root);

            _First.Clear();
            _First.Add(_RootItem);
        }