Browse Source

SD2-437. Source code now generated in InitializeComponent method for non-visual components.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@462 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 21 years ago
parent
commit
09daff2a33
  1. 24
      src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs
  2. 52
      src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs
  3. 24
      src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs

24
src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CSharpDesignerGenerator.cs

@ -53,6 +53,8 @@ namespace ICSharpCode.FormDesigner
FormDesignerViewContent viewContent; FormDesignerViewContent viewContent;
bool failedDesignerInitialize = false; bool failedDesignerInitialize = false;
string NonVisualComponentContainerName = "components";
public void Attach(FormDesignerViewContent viewContent) public void Attach(FormDesignerViewContent viewContent)
{ {
this.viewContent = viewContent; this.viewContent = viewContent;
@ -93,6 +95,11 @@ namespace ICSharpCode.FormDesigner
Reparse(viewContent.Document.TextContent); Reparse(viewContent.Document.TextContent);
int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine)); int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine));
viewContent.Document.Insert(endOffset, "\t\tprivate " + e.Component.GetType() + " " + e.Component.Site.Name + ";" + Environment.NewLine); viewContent.Document.Insert(endOffset, "\t\tprivate " + e.Component.GetType() + " " + e.Component.Site.Name + ";" + Environment.NewLine);
if (CodeDOMGenerator.IsNonVisualComponent(viewContent.Host, e.Component)) {
if (!IsNonVisualComponentContainerDefined) {
viewContent.Document.Insert(endOffset, "\t\tprivate " + typeof(Container) + " " + NonVisualComponentContainerName + ";" + Environment.NewLine);
}
}
} catch (Exception ex) { } catch (Exception ex) {
MessageService.ShowError(ex); MessageService.ShowError(ex);
} }
@ -270,5 +277,22 @@ namespace ICSharpCode.FormDesigner
} }
return compatibleMethods; return compatibleMethods;
} }
bool IsNonVisualComponentContainerDefined
{
get {
return GetField(c, NonVisualComponentContainerName) != null;
}
}
IField GetField(IClass c, string name)
{
foreach (IField field in c.Fields) {
if (field.Name == name) {
return field;
}
}
return null;
}
} }
} }

52
src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/CodeDOMGenerator.cs

@ -9,9 +9,11 @@ using System;
using System.IO; using System.IO;
using System.Xml; using System.Xml;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Reflection; using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel; using System.ComponentModel;
using System.ComponentModel.Design; using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization; using System.ComponentModel.Design.Serialization;
@ -38,6 +40,7 @@ namespace ICSharpCode.FormDesigner
CodeDomProvider codeProvider; CodeDomProvider codeProvider;
CodeDOMGeneratorUtility codeDOMGeneratorUtility = new CodeDOMGeneratorUtility(); CodeDOMGeneratorUtility codeDOMGeneratorUtility = new CodeDOMGeneratorUtility();
List<string> addedVariables = new List<string>();
public CodeDOMGenerator(IDesignerHost host, CodeDomProvider codeProvider) public CodeDOMGenerator(IDesignerHost host, CodeDomProvider codeProvider)
{ {
@ -53,29 +56,51 @@ namespace ICSharpCode.FormDesigner
DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService)); DesignerResourceService designerResourceService = (DesignerResourceService)host.GetService(typeof(System.ComponentModel.Design.IResourceService));
designerResourceService.SerializationStarted(true); designerResourceService.SerializationStarted(true);
Type componentType = host.RootComponent.GetType(); addedVariables.Clear();
ExpressionContext exprContext = new ExpressionContext(new CodeThisReferenceExpression(), componentType, host.RootComponent, host.RootComponent);
foreach (IComponent component in host.Container.Components) {
if (!IsComponentAdded(component)) {
GenerateComponentCode(component, writer, serializationManager);
}
}
designerResourceService.SerializationEnded(true);
session.Dispose();
LoggingService.Info("End CodeCOMGenerator.ConvertContentDefinition");
}
public static bool IsNonVisualComponent(IDesignerHost host, IComponent component)
{
IDesigner designer = host.GetDesigner(component);
return !(designer is ControlDesigner);
}
void GenerateComponentCode(IComponent component, TextWriter writer, DesignerSerializationManager serializationManager)
{
Type componentType = component.GetType();
ExpressionContext exprContext = new ExpressionContext(new CodeThisReferenceExpression(), componentType, component, component);
((IDesignerSerializationManager)serializationManager).Context.Append(exprContext); ((IDesignerSerializationManager)serializationManager).Context.Append(exprContext);
CodeDomSerializer rootSerializer = (CodeDomSerializer)serializationManager.GetSerializer(componentType, typeof(CodeDomSerializer)); CodeDomSerializer serializer = (CodeDomSerializer)serializationManager.GetSerializer(componentType, typeof(CodeDomSerializer));
if (rootSerializer == null) { if (serializer == null) {
throw new Exception("No root serializer found"); throw new Exception("No serializer found for component type=" + componentType.ToString());
} }
ICollection statements = rootSerializer.Serialize(serializationManager, host.RootComponent) as ICollection; ICollection statements = serializer.Serialize(serializationManager, component) as ICollection;
CodeGeneratorOptions options = codeDOMGeneratorUtility.CreateCodeGeneratorOptions; CodeGeneratorOptions options = codeDOMGeneratorUtility.CreateCodeGeneratorOptions;
options.IndentString = "\t\t\t"; options.IndentString = "\t\t\t";
// ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator outputGenerator = new ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator(); // ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator outputGenerator = new ICSharpCode.NRefactory.Parser.CodeDOMVerboseOutputGenerator();
foreach (CodeStatement statement in statements) { foreach (CodeStatement statement in statements) {
if (!(statement is CodeVariableDeclarationStatement)) { if ((statement is CodeVariableDeclarationStatement)) {
addedVariables.Add(((CodeVariableDeclarationStatement)statement).Name);
} else {
// indentation isn't generated when calling GenerateCodeFromStatement // indentation isn't generated when calling GenerateCodeFromStatement
writer.Write(options.IndentString); writer.Write(options.IndentString);
try { try {
// outputGenerator.PublicGenerateCodeFromStatement(statement, Console.Out, options); // outputGenerator.PublicGenerateCodeFromStatement(statement, Console.Out, options);
codeProvider.GenerateCodeFromStatement(statement, writer, options); codeProvider.GenerateCodeFromStatement(statement, writer, options);
} catch (Exception e) { } catch (Exception e) {
codeProvider.GenerateCodeFromStatement(new CodeCommentStatement("TODO: Error while generating statement : " + e.Message), codeProvider.GenerateCodeFromStatement(new CodeCommentStatement("TODO: Error while generating statement : " + e.Message),
@ -84,9 +109,14 @@ namespace ICSharpCode.FormDesigner
} }
} }
} }
designerResourceService.SerializationEnded(true); }
session.Dispose();
LoggingService.Info("End CodeCOMGenerator.ConvertContentDefinition"); bool IsComponentAdded(IComponent component)
{
if (component.Site != null) {
return addedVariables.Contains(component.Site.Name);
}
return false;
} }
} }
} }

24
src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerGenerator/VBNetDesignerGenerator.cs

@ -53,6 +53,8 @@ namespace ICSharpCode.FormDesigner
FormDesignerViewContent viewContent; FormDesignerViewContent viewContent;
bool failedDesignerInitialize = false; bool failedDesignerInitialize = false;
string NonVisualComponentContainerName = "components";
public void Attach(FormDesignerViewContent viewContent) public void Attach(FormDesignerViewContent viewContent)
{ {
this.viewContent = viewContent; this.viewContent = viewContent;
@ -93,6 +95,11 @@ namespace ICSharpCode.FormDesigner
Reparse(viewContent.Document.TextContent); Reparse(viewContent.Document.TextContent);
int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine)); int endOffset = viewContent.Document.PositionToOffset(new Point(0, initializeComponents.BodyRegion.EndLine));
viewContent.Document.Insert(endOffset, "\tPrivate " + e.Component.Site.Name + " As " + e.Component.GetType() + Environment.NewLine); viewContent.Document.Insert(endOffset, "\tPrivate " + e.Component.Site.Name + " As " + e.Component.GetType() + Environment.NewLine);
if (CodeDOMGenerator.IsNonVisualComponent(viewContent.Host, e.Component)) {
if (!IsNonVisualComponentContainerDefined) {
viewContent.Document.Insert(endOffset, "\tPrivate " + NonVisualComponentContainerName + " As " + typeof(Container) + Environment.NewLine);
}
}
} catch (Exception ex) { } catch (Exception ex) {
MessageService.ShowError(ex); MessageService.ShowError(ex);
} }
@ -269,5 +276,22 @@ namespace ICSharpCode.FormDesigner
} }
return compatibleMethods; return compatibleMethods;
} }
bool IsNonVisualComponentContainerDefined
{
get {
return GetField(c, NonVisualComponentContainerName) != null;
}
}
IField GetField(IClass c, string name)
{
foreach (IField field in c.Fields) {
if (field.Name == name) {
return field;
}
}
return null;
}
} }
} }

Loading…
Cancel
Save