Browse Source

Python forms designer now checks the non-visual component has a constructor that takes an IContainer before generating code to create the component.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4082 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
22a376598c
  1. 17
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs
  2. 8
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs
  3. 89
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateBackgroundWorkerTestFixture.cs
  4. 11
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTimerTestFixture.cs
  5. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

17
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs

@ -572,6 +572,23 @@ namespace ICSharpCode.PythonBinding @@ -572,6 +572,23 @@ namespace ICSharpCode.PythonBinding
return "self._" + component.Site.Name;
}
/// <summary>
/// Determines whether the component has a constructor that takes a single IContainer parameter.
/// </summary>
public bool HasIContainerConstructor()
{
foreach (ConstructorInfo constructor in GetComponentType().GetConstructors()) {
ParameterInfo[] parameters = constructor.GetParameters();
if (parameters.Length == 1) {
ParameterInfo parameter = parameters[0];
if (parameter.ParameterType.IsAssignableFrom(typeof(IContainer))) {
return true;
}
}
}
return false;
}
protected IComponent Component {
get { return component; }
}

8
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerRootComponent.cs

@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;
namespace ICSharpCode.PythonBinding
@ -97,14 +98,17 @@ namespace ICSharpCode.PythonBinding @@ -97,14 +98,17 @@ namespace ICSharpCode.PythonBinding
public void AppendCreateNonVisualComponents(PythonCodeBuilder codeBuilder)
{
foreach (PythonDesignerComponent component in GetNonVisualChildComponents()) {
component.AppendCreateInstance(codeBuilder, "self._components");
if (component.HasIContainerConstructor()) {
component.AppendCreateInstance(codeBuilder, "self._components");
} else {
component.AppendCreateInstance(codeBuilder);
}
}
}
/// <summary>
/// Appends code to set all the non-visual component properties.
/// </summary>
/// <param name="codeBuilder"></param>
public void AppendNonVisualComponents(PythonCodeBuilder codeBuilder)
{
foreach (PythonDesignerComponent component in GetNonVisualChildComponents()) {

89
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateBackgroundWorkerTestFixture.cs

@ -0,0 +1,89 @@ @@ -0,0 +1,89 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class GenerateBackgroundWorkerTestFixture
{
string generatedPythonCode;
bool hasNonVisualChildComponents;
bool hasIContainerConstructor;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(284, 264);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor propertyDescriptor = descriptors.Find("Name", false);
propertyDescriptor.SetValue(form, "MainForm");
BackgroundWorker worker = (BackgroundWorker)host.CreateComponent(typeof(BackgroundWorker), "backgroundWorker1");
descriptors = TypeDescriptor.GetProperties(worker);
propertyDescriptor = descriptors.Find("WorkerReportsProgress", false);
propertyDescriptor.SetValue(worker, true);
string indentString = " ";
PythonDesignerRootComponent designerRootComponent = new PythonDesignerRootComponent(form);
hasNonVisualChildComponents = designerRootComponent.HasNonVisualChildComponents();
PythonDesignerComponent component = new PythonDesignerComponent(worker);
hasIContainerConstructor = component.HasIContainerConstructor();
PythonControl pythonControl = new PythonControl(indentString);
generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form);
}
}
[Test]
public void HasNonVisualChildComponents()
{
Assert.IsTrue(hasNonVisualChildComponents);
}
[Test]
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" self._components = System.ComponentModel.Container()\r\n" +
" self._backgroundWorker1 = System.ComponentModel.BackgroundWorker()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # backgroundWorker1\r\n" +
" # \r\n" +
" self._backgroundWorker1.WorkerReportsProgress = True\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(284, 264)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode, generatedPythonCode);
}
[Test]
public void HasIContainerConstructor()
{
Assert.IsFalse(hasIContainerConstructor);
}
}
}

11
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTimerTestFixture.cs

@ -21,7 +21,7 @@ namespace PythonBinding.Tests.Designer @@ -21,7 +21,7 @@ namespace PythonBinding.Tests.Designer
{
string generatedPythonCode;
bool hasNonVisualChildComponents;
bool hasIContainerConstructor;
[TestFixtureSetUp]
public void SetUpFixture()
{
@ -43,6 +43,9 @@ namespace PythonBinding.Tests.Designer @@ -43,6 +43,9 @@ namespace PythonBinding.Tests.Designer
PythonDesignerRootComponent designerRootComponent = new PythonDesignerRootComponent(form);
hasNonVisualChildComponents = designerRootComponent.HasNonVisualChildComponents();
PythonDesignerComponent component = new PythonDesignerComponent(timer);
hasIContainerConstructor = component.HasIContainerConstructor();
PythonControl pythonControl = new PythonControl(indentString);
generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form);
}
@ -75,5 +78,11 @@ namespace PythonBinding.Tests.Designer @@ -75,5 +78,11 @@ namespace PythonBinding.Tests.Designer
Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);
}
[Test]
public void HasIContainerConstructor()
{
Assert.IsTrue(hasIContainerConstructor);
}
}
}

1
src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

@ -168,6 +168,7 @@ @@ -168,6 +168,7 @@
<Compile Include="Designer\GenerateAccessibleRoleFormTestFixture.cs" />
<Compile Include="Designer\GenerateAutoScaleModeFormTestFixture.cs" />
<Compile Include="Designer\GenerateAutoScrollFormTestFixture.cs" />
<Compile Include="Designer\GenerateBackgroundWorkerTestFixture.cs" />
<Compile Include="Designer\GenerateComboBoxItemsTestFixture.cs" />
<Compile Include="Designer\GenerateCursorFormTestFixture.cs" />
<Compile Include="Designer\GeneratedControlOrderingTestFixture.cs" />

Loading…
Cancel
Save