From 4a261f62025e1689fb183c6b72f6e29a08f9f181 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 16 Aug 2009 17:51:44 +0000 Subject: [PATCH] Python form designer now supports assigning a local variable to a property on a control. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4699 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/PythonComponentWalker.cs | 2 +- .../Src/PythonControlFieldExpression.cs | 54 +++++++++++++--- ...alVariablePropertyAssignmentTestFixture.cs | 64 +++++++++++++++++++ .../Test/PythonBinding.Tests.csproj | 1 + 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalVariablePropertyAssignmentTestFixture.cs diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs index 24acf33d5e..43441a703e 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonComponentWalker.cs @@ -148,7 +148,7 @@ namespace ICSharpCode.PythonBinding return false; } - fieldExpression.SetPropertyValue(componentCreator, node.Name.ToString()); + fieldExpression.SetPropertyValue(componentCreator, node); return false; } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs index 9326d9d3d3..0fa5d82b04 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs @@ -261,6 +261,30 @@ namespace ICSharpCode.PythonBinding return currentComponent; } + /// + /// Sets the property value that is referenced by this field expression. + /// + /// + /// This method checks that the name expression matches a created instance before + /// converting the name expression as a string and setting the property value. + /// + public bool SetPropertyValue(IComponentCreator componentCreator, NameExpression nameExpression) + { + object component = GetComponent(componentCreator); + PropertyDescriptor property = GetProperty(component, memberName); + if (property != null) { + string name = nameExpression.Name.ToString(); + if (property.PropertyType != typeof(bool)) { + object instance = componentCreator.GetInstance(name); + if (instance != null) { + return SetPropertyValue(component, memberName, instance); + } + } + return SetPropertyValue(component, memberName, name); + } + return false; + } + /// /// Sets the property value that is referenced by this field expression. /// @@ -270,13 +294,7 @@ namespace ICSharpCode.PythonBinding /// public bool SetPropertyValue(IComponentCreator componentCreator, object propertyValue) { - object component = null; - if (IsSelfReference) { - component = GetObject(componentCreator); - component = GetObjectForMemberName(component); - } else { - component = componentCreator.GetInstance(variableName); - } + object component = GetComponent(componentCreator); return SetPropertyValue(component, memberName, propertyValue); } @@ -437,7 +455,7 @@ namespace ICSharpCode.PythonBinding /// static bool SetPropertyValue(object component, string name, object propertyValue) { - PropertyDescriptor property = TypeDescriptor.GetProperties(component).Find(name, true); + PropertyDescriptor property = GetProperty(component, name); if (property != null) { propertyValue = ConvertPropertyValue(property, propertyValue); property.SetValue(component, propertyValue); @@ -445,5 +463,25 @@ namespace ICSharpCode.PythonBinding } return false; } + + /// + /// Gets the component that this field refers to. + /// + object GetComponent(IComponentCreator componentCreator) + { + object component = null; + if (IsSelfReference) { + component = GetObject(componentCreator); + component = GetObjectForMemberName(component); + } else { + component = componentCreator.GetInstance(variableName); + } + return component; + } + + static PropertyDescriptor GetProperty(object component, string name) + { + return TypeDescriptor.GetProperties(component).Find(name, true); + } } } diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalVariablePropertyAssignmentTestFixture.cs b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalVariablePropertyAssignmentTestFixture.cs new file mode 100644 index 0000000000..9264d8454c --- /dev/null +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadLocalVariablePropertyAssignmentTestFixture.cs @@ -0,0 +1,64 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Windows.Forms; + +using ICSharpCode.PythonBinding; +using NUnit.Framework; +using PythonBinding.Tests.Utils; + +namespace PythonBinding.Tests.Designer +{ + [TestFixture] + public class LoadLocalVariablePropertyAssignmentTestFixture : LoadFormTestFixtureBase + { + public override string PythonCode { + get { + return "class MainForm(System.Windows.Forms.Form):\r\n" + + " def InitializeComponent(self):\r\n" + + " button1 = System.Windows.Forms.Button()\r\n" + + " self.SuspendLayout()\r\n" + + " # \r\n" + + " # MainForm\r\n" + + " # \r\n" + + " self.AcceptButton = button1\r\n" + + " self.ClientSize = System.Drawing.Size(300, 400)\r\n" + + " self.Name = \"MainForm\"\r\n" + + " self.ResumeLayout(False)\r\n"; + } + } + + [Test] + public void OneComponentCreated() + { + Assert.AreEqual(1, ComponentCreator.CreatedComponents.Count); + } + + [Test] + public void TwoObjectsCreated() + { + Assert.AreEqual(2, ComponentCreator.CreatedInstances.Count); + } + + [Test] + public void ButtonInstance() + { + CreatedInstance expectedInstance = new CreatedInstance(typeof(Button), new object[0], "button1", false); + Assert.AreEqual(expectedInstance, ComponentCreator.CreatedInstances[0]); + } + + [Test] + public void AcceptButtonPropertyIsNotNull() + { + Assert.IsNotNull(Form.AcceptButton); + } + } +} diff --git a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj index 2e54ed8f42..8412ca8dd9 100644 --- a/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj @@ -246,6 +246,7 @@ +