Пример #1
0
        void OpenEvent(object sender, EventArgs e)
        {
            if (categoryTreeView.SelectedNode != null)
            {
                PropertyService.Set("Dialogs.NewProjectDialog.LargeImages", ((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked);
                PropertyService.Set("Dialogs.NewFileDialog.CategoryViewState", TreeViewHelper.GetViewStateString(categoryTreeView));
                PropertyService.Set("Dialogs.NewFileDialog.LastSelectedCategory", TreeViewHelper.GetPath(categoryTreeView.SelectedNode));
            }
            if (templateListView.SelectedItems.Count == 1)
            {
                TemplateItem item = (TemplateItem)templateListView.SelectedItems[0];

                PropertyService.Set("Dialogs.NewFileDialog.LastSelectedTemplate", item.Template.Name);

                string fileName;
                string standardNamespace = "DefaultNamespace";
                if (allowUntitledFiles)
                {
                    fileName = GenerateCurrentFileName();
                }
                else
                {
                    fileName = ControlDictionary["fileNameTextBox"].Text.Trim();
                    if (!FileUtility.IsValidPath(fileName) ||
                        fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0 ||
                        fileName.IndexOf(Path.DirectorySeparatorChar) >= 0)
                    {
                        MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.SaveFile.InvalidFileNameError}", new StringTagPair("FileName", fileName)));
                        return;
                    }
                    if (Path.GetExtension(fileName).Length == 0)
                    {
                        fileName += Path.GetExtension(item.Template.SuggestFileName(null));
                    }
                    fileName = Path.Combine(basePath, fileName);
                    fileName = FileUtility.NormalizePath(fileName);
                    if (project != null)
                    {
                        standardNamespace = CustomToolsService.GetDefaultNamespace(project, fileName);
                    }
                }

                options                     = new FileTemplateOptions();
                options.ClassName           = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false);
                options.FileName            = FileName.Create(fileName);
                options.IsUntitled          = allowUntitledFiles;
                options.Namespace           = standardNamespace;
                options.CustomizationObject = localizedTypeDescriptor;
                options.Project             = project;

                result       = SelectedTemplate.Create(options);
                DialogResult = DialogResult.OK;
                if (result != null)
                {
                    SelectedTemplate.RunActions(result);
                }
            }
        }
Пример #2
0
        void OpenEvent(object sender, EventArgs e)
        {
            if (categoryTreeView.SelectedNode != null)
            {
                PropertyService.Set("Dialogs.NewProjectDialog.LargeImages", ((RadioButton)ControlDictionary["largeIconsRadioButton"]).Checked);
                PropertyService.Set("Dialogs.NewFileDialog.CategoryViewState", TreeViewHelper.GetViewStateString(categoryTreeView));
                PropertyService.Set("Dialogs.NewFileDialog.LastSelectedCategory", TreeViewHelper.GetPath(categoryTreeView.SelectedNode));
            }
            createdFiles.Clear();
            if (templateListView.SelectedItems.Count == 1)
            {
                if (!AllPropertiesHaveAValue)
                {
                    MessageService.ShowMessage("${res:Dialog.NewFile.FillOutFirstMessage}", "${res:Dialog.NewFile.FillOutFirstCaption}");
                    return;
                }
                TemplateItem item = (TemplateItem)templateListView.SelectedItems[0];

                PropertyService.Set("Dialogs.NewFileDialog.LastSelectedTemplate", item.Template.Name);

                string fileName;
                StringParserPropertyContainer.FileCreation["StandardNamespace"] = "DefaultNamespace";
                if (allowUntitledFiles)
                {
                    fileName = GenerateCurrentFileName();
                }
                else
                {
                    fileName = ControlDictionary["fileNameTextBox"].Text.Trim();
                    if (!FileUtility.IsValidPath(fileName) ||
                        fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0 ||
                        fileName.IndexOf(Path.DirectorySeparatorChar) >= 0)
                    {
                        MessageService.ShowError(StringParser.Parse("${res:ICSharpCode.SharpDevelop.Commands.SaveFile.InvalidFileNameError}", new StringTagPair("FileName", fileName)));
                        return;
                    }
                    if (Path.GetExtension(fileName).Length == 0)
                    {
                        fileName += Path.GetExtension(item.Template.DefaultName);
                    }
                    fileName = Path.Combine(basePath, fileName);
                    fileName = FileUtility.NormalizePath(fileName);
                    IProject project = ProjectService.CurrentProject;
                    if (project != null)
                    {
                        StringParserPropertyContainer.FileCreation["StandardNamespace"] = CustomToolsService.GetDefaultNamespace(project, fileName);
                    }
                }

                FileTemplateOptions options = new FileTemplateOptions();
                options.ClassName  = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false);
                options.FileName   = FileName.Create(fileName);
                options.IsUntitled = allowUntitledFiles;
                options.Namespace  = StringParserPropertyContainer.FileCreation["StandardNamespace"];

                StringParserPropertyContainer.FileCreation["FullName"] = fileName;
                StringParserPropertyContainer.FileCreation["FileName"] = Path.GetFileName(fileName);
                StringParserPropertyContainer.FileCreation["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension(fileName);
                StringParserPropertyContainer.FileCreation["Extension"] = Path.GetExtension(fileName);
                StringParserPropertyContainer.FileCreation["Path"]      = Path.GetDirectoryName(fileName);

                StringParserPropertyContainer.FileCreation["ClassName"] = options.ClassName;

                // when adding a file to a project (but not when creating a standalone file while a project is open):
                if (ProjectService.CurrentProject != null && !this.allowUntitledFiles)
                {
                    options.Project = ProjectService.CurrentProject;
                    // add required assembly references to the project
                    bool changes = false;
                    foreach (ReferenceProjectItem reference in item.Template.RequiredAssemblyReferences)
                    {
                        IEnumerable <ProjectItem> refs = ProjectService.CurrentProject.GetItemsOfType(ItemType.Reference);
                        if (!refs.Any(projItem => string.Equals(projItem.Include, reference.Include, StringComparison.OrdinalIgnoreCase)))
                        {
                            ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(ProjectService.CurrentProject);
                            ProjectService.AddProjectItem(ProjectService.CurrentProject, projItem);
                            changes = true;
                        }
                    }
                    if (changes)
                    {
                        ProjectService.CurrentProject.Save();
                    }
                }

                foreach (FileDescriptionTemplate newfile in item.Template.FileDescriptionTemplates)
                {
                    if (!IsFilenameAvailable(StringParser.Parse(newfile.Name)))
                    {
                        MessageService.ShowError(string.Format("Filename {0} is in use.\nChoose another one", StringParser.Parse(newfile.Name)));                         // TODO : translate
                        return;
                    }
                }
                ScriptRunner scriptRunner = new ScriptRunner();
                foreach (FileDescriptionTemplate newFile in item.Template.FileDescriptionTemplates)
                {
                    FileOperationResult result = FileUtility.ObservedSave(
                        () => {
                        if (!String.IsNullOrEmpty(newFile.BinaryFileName))
                        {
                            SaveFile(newFile, null, newFile.BinaryFileName);
                        }
                        else
                        {
                            SaveFile(newFile, scriptRunner.CompileScript(item.Template, newFile), null);
                        }
                    }, StringParser.Parse(newFile.Name)
                        );
                    if (result != FileOperationResult.OK)
                    {
                        return;
                    }
                }

                DialogResult = DialogResult.OK;

                // raise FileCreated event for the new files.
                foreach (KeyValuePair <string, FileDescriptionTemplate> entry in createdFiles)
                {
                    FileService.FireFileCreated(entry.Key, false);
                }
                item.Template.RunActions(options);
            }
        }