Esempio n. 1
0
        void OnComponentNameChanged(object s, Stetic.ComponentNameEventArgs args)
        {
            try {
                // Make sure the fields in the partial class are up to date.
                // Provide the args parameter to GenerateSteticCodeStructure, in this
                // way the component that has been renamed will be generated with the
                // old name, and UpdateField will be able to find it (to rename the
                // references to the field, it needs to have the old name).

                /*if (gproject.Project.UsePartialTypes)
                 *      GuiBuilderService.GenerateSteticCodeStructure ((DotNetProject)gproject.Project, designer.RootComponent, args, false, false);
                 */
                codeBinder.UpdateField(args.Component, args.OldName);
            }
            catch (Exception ex) {
                MessageService.ShowException(ex);
            }
        }
Esempio n. 2
0
        static string GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return(fileName);
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);
                type.Members.Add(
                    new CodeMemberMethod()
                {
                    Name = "Build"
                }
                    );

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return(fileName);
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            string text;
            var    pol = project.Policies.Get <TextStylePolicy> ();

            using (var fileStream = new StringWriter()) {
                var options = new CodeGeneratorOptions()
                {
                    IndentString             = pol.TabsToSpaces? new string (' ', pol.TabWidth) : "\t",
                    BlankLinesBetweenMembers = true,
                };
                provider.GenerateCodeFromCompileUnit(cu, fileStream, options);
                text = fileStream.ToString();
                text = FormatGeneratedFile(fileName, text, project, provider);
            }
            if (saveToFile)
            {
                File.WriteAllText(fileName, text);
            }
            TypeSystemService.NotifyFileChange(fileName, text);
//
//			if (ProjectDomService.HasDom (project)) {
//				// Only update the parser database if the project is actually loaded in the IDE.
//				ProjectDomService.Parse (project, fileName, text);
//				if (saveToFile)
//					FileService.NotifyFileChanged (fileName);
//			}

            return(fileName);
        }
Esempio n. 3
0
 public static string GenerateSteticCodeStructure(DotNetProject project, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
 {
     return(GenerateSteticCodeStructure(project, null, component, args, saveToFile, overwrite));
 }
Esempio n. 4
0
        static void GenerateSteticCodeStructure(DotNetProject project, Stetic.ProjectItemInfo item, Stetic.Component component, Stetic.ComponentNameEventArgs args, bool saveToFile, bool overwrite)
        {
            // Generate a class which contains fields for all bound widgets of the component

            string name     = item != null ? item.Name : component.Name;
            string fileName = GetBuildCodeFileName(project, name);

            if (fileName == null)
            {
                return;
            }

            string ns = "";
            int    i  = name.LastIndexOf('.');

            if (i != -1)
            {
                ns   = name.Substring(0, i);
                name = name.Substring(i + 1);
            }

            if (saveToFile && !overwrite && File.Exists(fileName))
            {
                return;
            }

            if (item != null)
            {
                component = item.Component;
            }

            CodeCompileUnit cu = new CodeCompileUnit();

            if (project.UsePartialTypes)
            {
                CodeNamespace cns = new CodeNamespace(ns);
                cu.Namespaces.Add(cns);

                CodeTypeDeclaration type = new CodeTypeDeclaration(name);
                type.IsPartial      = true;
                type.Attributes     = MemberAttributes.Public;
                type.TypeAttributes = System.Reflection.TypeAttributes.Public;
                cns.Types.Add(type);

                foreach (Stetic.ObjectBindInfo binfo in component.GetObjectBindInfo())
                {
                    // When a component is being renamed, we have to generate the
                    // corresponding field using the old name, since it will be renamed
                    // later using refactory
                    string nname = args != null && args.NewName == binfo.Name ? args.OldName : binfo.Name;
                    type.Members.Add(
                        new CodeMemberField(
                            binfo.TypeName,
                            nname
                            )
                        );
                }
            }
            else
            {
                if (!saveToFile)
                {
                    return;
                }
                CodeNamespace cns = new CodeNamespace();
                cns.Comments.Add(new CodeCommentStatement("Generated code for component " + component.Name));
                cu.Namespaces.Add(cns);
            }

            CodeDomProvider provider = project.LanguageBinding.GetCodeDomProvider();

            if (provider == null)
            {
                throw new UserException("Code generation not supported for language: " + project.LanguageName);
            }

            TextWriter fileStream;

            if (saveToFile)
            {
                fileStream = new StreamWriter(fileName);
            }
            else
            {
                fileStream = new StringWriter();
            }

            try {
                provider.GenerateCodeFromCompileUnit(cu, fileStream, new CodeGeneratorOptions());
            } finally {
                fileStream.Close();
            }

            if (ProjectDomService.HasDom(project))
            {
                // Only update the parser database if the project is actually loaded in the IDE.
                if (saveToFile)
                {
                    ProjectDomService.Parse(project, fileName, "");
                    FileService.NotifyFileChanged(fileName);
                }
                else
                {
                    ProjectDomService.Parse(project, fileName, ((StringWriter)fileStream).ToString());
                }
            }
        }