Browse Source

Inherited protected controls are now supported in the Python forms designer.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4571 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
2ea6c93ce1
  1. 26
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs
  2. 51
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs
  3. 109
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateInheritedProtectedPanelFormTestFixture.cs
  4. 90
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadInheritedProtectedPanelFormTestFixture.cs
  5. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

26
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonControlFieldExpression.cs

@ -202,14 +202,38 @@ namespace ICSharpCode.PythonBinding @@ -202,14 +202,38 @@ namespace ICSharpCode.PythonBinding
}
return null;
}
/// <summary>
/// Looks for a field in the component with the given name.
/// </summary>
public static object GetInheritedObject(string name, object component)
{
if (component != null) {
FieldInfo[] fields = component.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (FieldInfo field in fields) {
if (String.Equals(name, field.Name, StringComparison.InvariantCultureIgnoreCase)) {
return field.GetValue(component);
}
}
}
return null;
}
/// <summary>
/// Gets the object that the field expression variable refers to.
/// </summary>
/// <remarks>
/// This method will also check form's base class for any inherited objects that match
/// the object being referenced.
/// </remarks>
public object GetObject(IComponentCreator componentCreator)
{
if (variableName.Length > 0) {
return componentCreator.GetComponent(variableName);
object component = componentCreator.GetComponent(variableName);
if (component != null) {
return component;
}
return GetInheritedObject(variableName, componentCreator.RootComponent);
}
return componentCreator.RootComponent;
}

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

@ -284,25 +284,46 @@ namespace ICSharpCode.PythonBinding @@ -284,25 +284,46 @@ namespace ICSharpCode.PythonBinding
}
/// <summary>
/// Returns true if the component has an InheritanceAttribute set to InheritanceLevel.InheritedReadOnly
/// Returns true if the component has an InheritanceAttribute set to InheritanceLevel.Inherited or
/// InheritanceLevel.InheritedReadOnly
/// </summary>
public static bool IsInheritedReadOnlyComponent(object component)
public static bool IsInheritedComponent(object component)
{
if (component != null) {
AttributeCollection attributes = TypeDescriptor.GetAttributes(component);
InheritanceAttribute attribute = (InheritanceAttribute)attributes[typeof(InheritanceAttribute)];
if (attribute != null) {
return attribute.InheritanceLevel == InheritanceLevel.InheritedReadOnly;
}
InheritanceAttribute attribute = GetInheritanceAttribute(component);
return attribute.InheritanceLevel != InheritanceLevel.NotInherited;
}
return false;
}
/// <summary>
/// Returns true if the component has an InheritanceAttribute set to InheritanceLevel.Inherited or
/// InheritanceLevel.InheritedReadOnly
/// </summary>
public bool IsInherited {
get { return IsInheritedComponent(component); }
}
/// <summary>
/// Returns true if the component has an InheritanceAttribute set to InheritanceLevel.InheritedReadOnly
/// </summary>
public bool IsInheritedReadOnly {
get { return IsInheritedReadOnlyComponent(component); }
get {
InheritanceAttribute attribute = GetInheritanceAttribute(component);
if (attribute != null) {
return attribute.InheritanceLevel == InheritanceLevel.InheritedReadOnly;
}
return false;
}
}
public static InheritanceAttribute GetInheritanceAttribute(object component)
{
if (component != null) {
AttributeCollection attributes = TypeDescriptor.GetAttributes(component);
return attributes[typeof(InheritanceAttribute)] as InheritanceAttribute;
}
return null;
}
/// <summary>
@ -495,7 +516,7 @@ namespace ICSharpCode.PythonBinding @@ -495,7 +516,7 @@ namespace ICSharpCode.PythonBinding
}
foreach (object item in collectionProperty) {
IComponent collectionComponent = item as IComponent;
if (PythonDesignerComponent.IsSitedComponent(collectionComponent) && !PythonDesignerComponent.IsInheritedReadOnlyComponent(collectionComponent)) {
if (PythonDesignerComponent.IsSitedComponent(collectionComponent) && !PythonDesignerComponent.IsInheritedComponent(collectionComponent)) {
codeBuilder.AppendIndentedLine(propertyOwnerName + "." + propertyDescriptor.Name + "." + addMethod.Name + "(self._" + collectionComponent.Site.Name + ")");
}
}
@ -639,9 +660,15 @@ namespace ICSharpCode.PythonBinding @@ -639,9 +660,15 @@ namespace ICSharpCode.PythonBinding
/// <summary>
/// Gets the owner of any properties generated (e.g. "self._textBox1").
/// For an inherited component the actual component name is used without any underscore prefix.
/// </summary>
public virtual string GetPropertyOwnerName()
{
return "self._" + component.Site.Name;
string componentName = component.Site.Name;
if (IsInherited) {
return "self." + componentName;
}
return "self._" + componentName;
}
/// <summary>
@ -741,7 +768,7 @@ namespace ICSharpCode.PythonBinding @@ -741,7 +768,7 @@ namespace ICSharpCode.PythonBinding
static bool HasSitedComponents(PythonDesignerComponent[] components)
{
foreach (PythonDesignerComponent component in components) {
if (component.IsSited) {
if (component.IsSited && !component.IsInherited) {
return true;
}
}
@ -751,7 +778,7 @@ namespace ICSharpCode.PythonBinding @@ -751,7 +778,7 @@ namespace ICSharpCode.PythonBinding
void AppendCreateChildComponents(PythonCodeBuilder codeBuilder, PythonDesignerComponent[] childComponents)
{
foreach (PythonDesignerComponent designerComponent in childComponents) {
if (designerComponent.IsSited) {
if (designerComponent.IsSited && !designerComponent.IsInherited) {
designerComponent.AppendCreateInstance(codeBuilder);
}
}

109
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateInheritedProtectedPanelFormTestFixture.cs

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
// <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
{
class ProtectedPanelBaseForm : Form
{
protected Panel panel1 = new Panel();
Button button1 = new Button();
public ProtectedPanelBaseForm()
{
button1.Name = "button1";
panel1.Name = "panel1";
panel1.Location = new Point(5, 10);
panel1.Size = new Size(200, 100);
panel1.Controls.Add(button1);
Controls.Add(panel1);
}
}
class ProtectedPanelDerivedForm : ProtectedPanelBaseForm
{
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
internal Point PanelLocation {
get { return panel1.Location; }
set { panel1.Location = value; }
}
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
internal Size PanelSize {
get { return panel1.Size; }
set { panel1.Size = value; }
}
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
internal Panel GetPanel()
{
return panel1;
}
}
/// <summary>
/// Tests that no code is generated for a protected panel control in the base class
/// that has child controls.
/// </summary>
[TestFixture]
public class GenerateInheritedProtectedPanelFormTestFixture
{
string generatedPythonCode;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (DesignSurface designSurface = new DesignSurface(typeof(ProtectedPanelDerivedForm))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(284, 264);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
namePropertyDescriptor.SetValue(form, "MainForm");
// Move protected panel so we generate code for the new location.
ProtectedPanelDerivedForm derivedForm = (ProtectedPanelDerivedForm)form;
derivedForm.PanelLocation = new Point(10, 15);
string indentString = " ";
PythonControl pythonForm = new PythonControl(indentString);
generatedPythonCode = pythonForm.GenerateInitializeComponentMethod(form);
}
}
[Test]
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # panel1\r\n" +
" # \r\n" +
" self.panel1.Location = System.Drawing.Point(10, 15)\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);
}
}
}

90
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/LoadInheritedProtectedPanelFormTestFixture.cs

@ -0,0 +1,90 @@ @@ -0,0 +1,90 @@
// <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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using IronPython.Compiler.Ast;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class LoadInheritedProtectedPanelFormTestFixture : LoadFormTestFixtureBase
{
public override string PythonCode {
get {
base.ComponentCreator.AddType("PythonBinding.Tests.Designer.ProtectedPanelDerivedForm", typeof(ProtectedPanelDerivedForm));
return "class MainForm(PythonBinding.Tests.Designer.ProtectedPanelDerivedForm):\r\n" +
" def InitializeComponent(self):\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # panel1\r\n" +
" # \r\n" +
" self.panel1.Location = System.Drawing.Point(10, 15)\r\n" +
" self.panel1.Size = System.Drawing.Size(108, 120)\r\n" +
" # \r\n" +
" # form1\r\n" +
" # \r\n" +
" self.Location = System.Drawing.Point(10, 20)\r\n" +
" self.Name = \"form1\"\r\n" +
" self.Controls.Add(self._textBox1)\r\n" +
" self.ResumeLayout(False)\r\n";
}
}
ProtectedPanelDerivedForm DerivedForm {
get { return Form as ProtectedPanelDerivedForm; }
}
[Test]
public void PanelLocation()
{
Assert.AreEqual(new Point(10, 15), DerivedForm.PanelLocation);
}
[Test]
public void PanelSize()
{
Assert.AreEqual(new Size(108, 120), DerivedForm.PanelSize);
}
[Test]
public void GetProtectedPanelObject()
{
Assert.AreEqual(DerivedForm.GetPanel(), PythonControlFieldExpression.GetInheritedObject("panel1", DerivedForm));
}
[Test]
public void GetProtectedPanelObjectIncorrectCase()
{
Assert.AreEqual(DerivedForm.GetPanel(), PythonControlFieldExpression.GetInheritedObject("PANEL1", DerivedForm));
}
[Test]
public void GetInheritedObjectPassedNull()
{
Assert.IsNull(PythonControlFieldExpression.GetInheritedObject("panel1", null));
}
[Test]
public void GetInheritedPanelObjectFromFieldExpression()
{
AssignmentStatement statement = PythonParserHelper.GetAssignmentStatement("self.panel1.Name = \"abc\"");
PythonControlFieldExpression field = PythonControlFieldExpression.Create(statement.Left[0] as MemberExpression);
Assert.AreEqual(DerivedForm.GetPanel(), field.GetObject(ComponentCreator));
}
}
}

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

@ -197,6 +197,7 @@ @@ -197,6 +197,7 @@
<Compile Include="Designer\GenerateImageListResourcesTestFixture.cs" />
<Compile Include="Designer\GenerateImeModeFormTestFixture.cs" />
<Compile Include="Designer\GenerateInheritedFormTestFixture.cs" />
<Compile Include="Designer\GenerateInheritedProtectedPanelFormTestFixture.cs" />
<Compile Include="Designer\GenerateListViewItemTestFixture.cs" />
<Compile Include="Designer\GenerateListViewSubItemsTestFixture.cs" />
<Compile Include="Designer\GenerateListViewWithImageListTestFixture.cs" />
@ -235,6 +236,7 @@ @@ -235,6 +236,7 @@
<Compile Include="Designer\LoadFormTestFixtureBase.cs" />
<Compile Include="Designer\LoadFormWithBooleanPropertiesSetTestFixture.cs" />
<Compile Include="Designer\LoadFormWithSysPathAppendStatementTestFixture.cs" />
<Compile Include="Designer\LoadInheritedProtectedPanelFormTestFixture.cs" />
<Compile Include="Designer\LoadListViewFormTestFixture.cs" />
<Compile Include="Designer\LoadLocalImageResourceTestFixture.cs" />
<Compile Include="Designer\LoadMenuStripFormTestFixture.cs" />

Loading…
Cancel
Save