Browse Source

Code now generated for TreeView TreeNodes in the python forms designer.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@4387 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
0f64ad149a
  1. 3
      src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj
  2. 19
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/IArrayItem.cs
  3. 80
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponent.cs
  4. 2
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponentFactory.cs
  5. 42
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerTreeNode.cs
  6. 105
      src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonTreeViewComponent.cs
  7. 8
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CreateDesignerComponentTests.cs
  8. 110
      src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTreeViewTestFixture.cs
  9. 1
      src/AddIns/BackendBindings/Python/PythonBinding/Test/PythonBinding.Tests.csproj

3
src/AddIns/BackendBindings/Python/PythonBinding/Project/PythonBinding.csproj

@ -70,6 +70,7 @@ @@ -70,6 +70,7 @@
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\AddInOptions.cs" />
<Compile Include="Src\CompilingOptionsPanel.cs" />
<Compile Include="Src\IArrayItem.cs" />
<Compile Include="Src\IComponentCreator.cs" />
<Compile Include="Src\IMemberProvider.cs" />
<Compile Include="Src\IPadDescriptor.cs" />
@ -94,6 +95,7 @@ @@ -94,6 +95,7 @@
<Compile Include="Src\PythonDesignerLoader.cs" />
<Compile Include="Src\PythonDesignerLoaderProvider.cs" />
<Compile Include="Src\PythonDesignerRootComponent.cs" />
<Compile Include="Src\PythonDesignerTreeNode.cs" />
<Compile Include="Src\PythonExpressionFinder.cs" />
<Compile Include="Src\PythonControl.cs" />
<Compile Include="Src\PythonFormsDesignerDisplayBinding.cs" />
@ -111,6 +113,7 @@ @@ -111,6 +113,7 @@
<Compile Include="Src\PythonProject.cs" />
<Compile Include="Src\PythonPropertyValueAssignment.cs" />
<Compile Include="Src\PythonResolver.cs" />
<Compile Include="Src\PythonTreeViewComponent.cs" />
<Compile Include="Src\RunDebugPythonCommand.cs" />
<Compile Include="Src\RunPythonCommand.cs" />
<Compile Include="Src\StandardPythonModules.cs" />

19
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/IArrayItem.cs

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
// <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;
namespace ICSharpCode.PythonBinding
{
/// <summary>
/// Represents a named item in an array.
/// </summary>
public interface IArrayItem
{
string Name { get; }
}
}

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

@ -659,7 +659,48 @@ namespace ICSharpCode.PythonBinding @@ -659,7 +659,48 @@ namespace ICSharpCode.PythonBinding
public PythonDesignerComponent Parent {
get { return parent; }
}
public static void AppendSystemArray(PythonCodeBuilder codeBuilder, string componentName, string methodName, string typeName, ICollection components)
{
if (components.Count > 0) {
codeBuilder.AppendIndented("self._" + componentName + "." + methodName + "(");
AppendSystemArray(codeBuilder, typeName, components);
codeBuilder.Append(")");
codeBuilder.AppendLine();
}
}
public static void AppendSystemArray(PythonCodeBuilder codeBuilder, string typeName, ICollection components)
{
if (components.Count > 0) {
codeBuilder.Append("System.Array[" + typeName + "](");
codeBuilder.AppendLine();
codeBuilder.IncreaseIndent();
int i = 0;
foreach (object component in components) {
if (i == 0) {
codeBuilder.AppendIndented("[");
} else {
codeBuilder.Append(",");
codeBuilder.AppendLine();
codeBuilder.AppendIndented(String.Empty);
}
if (component is IComponent) {
codeBuilder.Append("self._" + ((IComponent)component).Site.Name);
} else if (component is String) {
codeBuilder.Append(PythonPropertyValueAssignment.ToString(component));
} else if (component is IArrayItem) {
codeBuilder.Append(((IArrayItem)component).Name);
} else {
codeBuilder.Append(GetVariableName(component, i + 1));
}
++i;
}
codeBuilder.Append("])");
}
codeBuilder.DecreaseIndent();
}
protected IComponent Component {
get { return component; }
}
@ -707,45 +748,6 @@ namespace ICSharpCode.PythonBinding @@ -707,45 +748,6 @@ namespace ICSharpCode.PythonBinding
return sitedComponents.ToArray();
}
static void AppendSystemArray(PythonCodeBuilder codeBuilder, string componentName, string methodName, string typeName, ICollection components)
{
if (components.Count > 0) {
codeBuilder.AppendIndented("self._" + componentName + "." + methodName + "(");
AppendSystemArray(codeBuilder, typeName, components);
codeBuilder.Append(")");
codeBuilder.AppendLine();
codeBuilder.DecreaseIndent();
}
}
static void AppendSystemArray(PythonCodeBuilder codeBuilder, string typeName, ICollection components)
{
if (components.Count > 0) {
codeBuilder.Append("System.Array[" + typeName + "](");
codeBuilder.AppendLine();
codeBuilder.IncreaseIndent();
int i = 0;
foreach (object component in components) {
if (i == 0) {
codeBuilder.AppendIndented("[");
} else {
codeBuilder.Append(",");
codeBuilder.AppendLine();
codeBuilder.AppendIndented(String.Empty);
}
if (component is IComponent) {
codeBuilder.Append("self._" + ((IComponent)component).Site.Name);
} else if (component is String) {
codeBuilder.Append(PythonPropertyValueAssignment.ToString(component));
} else {
codeBuilder.Append(GetVariableName(component, i + 1));
}
++i;
}
codeBuilder.Append("])");
}
}
void AppendChildComponentsMethodCalls(PythonCodeBuilder codeBuilder, string[] methods)
{
foreach (PythonDesignerComponent designerComponent in GetChildComponents()) {

2
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerComponentFactory.cs

@ -36,6 +36,8 @@ namespace ICSharpCode.PythonBinding @@ -36,6 +36,8 @@ namespace ICSharpCode.PythonBinding
return new PythonContextMenuComponent(parent, component);
} else if (component is ImageList) {
return new PythonImageListComponent(parent, component);
} else if (component is TreeView) {
return new PythonTreeViewComponent(parent, component);
}
return new PythonDesignerComponent(parent, component);
}

42
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonDesignerTreeNode.cs

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
// <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.Windows.Forms;
namespace ICSharpCode.PythonBinding
{
public class PythonDesignerTreeNode : IArrayItem
{
TreeNode node;
int num;
List<PythonDesignerTreeNode> childNodes = new List<PythonDesignerTreeNode>();
public PythonDesignerTreeNode(TreeNode node, int num)
{
this.node = node;
this.num = num;
}
public TreeNode TreeNode {
get { return node; }
}
public int Number {
get { return num; }
}
public string Name {
get { return "treeNode" + num.ToString(); }
}
public List<PythonDesignerTreeNode> ChildNodes {
get { return childNodes; }
}
}
}

105
src/AddIns/BackendBindings/Python/PythonBinding/Project/Src/PythonTreeViewComponent.cs

@ -0,0 +1,105 @@ @@ -0,0 +1,105 @@
// <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.Windows.Forms;
namespace ICSharpCode.PythonBinding
{
/// <summary>
/// Used to generate code for a TreeView component currently being designed.
/// </summary>
public class PythonTreeViewComponent : PythonDesignerComponent
{
List<PythonDesignerTreeNode> rootTreeNodes;
int treeNodeCount = 1;
public PythonTreeViewComponent(IComponent component) : this(null, component)
{
}
public PythonTreeViewComponent(PythonDesignerComponent parent, IComponent component)
: base(parent, component)
{
}
/// <summary>
/// Appends code that creates an instance of the tree view.
/// </summary>
public override void AppendCreateInstance(PythonCodeBuilder codeBuilder)
{
// Append tree node creation first.
AppendCreateInstance(codeBuilder, GetRootTreeNodes(Component));
// Append tree view creation.
base.AppendCreateInstance(codeBuilder);
}
/// <summary>
/// Appends the component's properties.
/// </summary>
public override void AppendComponent(PythonCodeBuilder codeBuilder)
{
AppendComment(codeBuilder);
AppendTreeNodeProperties(codeBuilder, GetRootTreeNodes(Component));
AppendComponentProperties(codeBuilder, true, false);
}
void AppendCreateInstance(PythonCodeBuilder codeBuilder, List<PythonDesignerTreeNode> nodes)
{
object[] parameters = new object[0];
foreach (PythonDesignerTreeNode node in nodes) {
AppendCreateInstance(codeBuilder, node.TreeNode, node.Number, parameters);
AppendCreateInstance(codeBuilder, node.ChildNodes);
}
}
List<PythonDesignerTreeNode> GetRootTreeNodes(IComponent component)
{
if (rootTreeNodes == null) {
rootTreeNodes = new List<PythonDesignerTreeNode>();
TreeView treeView = (TreeView)component;
AddTreeNodes(rootTreeNodes, treeView.Nodes);
}
return rootTreeNodes;
}
void AddTreeNodes(List<PythonDesignerTreeNode> designerNodes, TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes) {
PythonDesignerTreeNode designerNode = new PythonDesignerTreeNode(node, treeNodeCount);
designerNodes.Add(designerNode);
++treeNodeCount;
// Add child nodes.
AddTreeNodes(designerNode.ChildNodes, node.Nodes);
}
}
void AppendTreeNodeProperties(PythonCodeBuilder codeBuilder, List<PythonDesignerTreeNode> nodes)
{
foreach (PythonDesignerTreeNode node in nodes) {
AppendObjectProperties(codeBuilder, node.TreeNode, node.Number);
// Append child nodes to parent tree node.
if (node.ChildNodes.Count > 0) {
codeBuilder.AppendIndented(node.Name);
codeBuilder.Append(".Nodes.AddRange(");
AppendSystemArray(codeBuilder, typeof(TreeNode).FullName, node.ChildNodes);
codeBuilder.Append(")");
codeBuilder.AppendLine();
}
// Append child node properties.
AppendTreeNodeProperties(codeBuilder, node.ChildNodes);
}
}
}
}

8
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/CreateDesignerComponentTests.cs

@ -49,5 +49,13 @@ namespace PythonBinding.Tests.Designer @@ -49,5 +49,13 @@ namespace PythonBinding.Tests.Designer
Assert.IsInstanceOf(typeof(PythonImageListComponent), PythonDesignerComponentFactory.CreateDesignerComponent(imageList));
}
}
[Test]
public void TreeViewComponent()
{
using (TreeView treeView = new TreeView()) {
Assert.IsInstanceOf(typeof(PythonTreeViewComponent), PythonDesignerComponentFactory.CreateDesignerComponent(treeView));
}
}
}
}

110
src/AddIns/BackendBindings/Python/PythonBinding/Test/Designer/GenerateTreeViewTestFixture.cs

@ -0,0 +1,110 @@ @@ -0,0 +1,110 @@
// <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.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Windows.Forms;
using ICSharpCode.PythonBinding;
using NUnit.Framework;
using PythonBinding.Tests.Utils;
namespace PythonBinding.Tests.Designer
{
[TestFixture]
public class GenerateTreeViewTestFixture
{
string generatedPythonCode;
[TestFixtureSetUp]
public void SetUpFixture()
{
using (DesignSurface designSurface = new DesignSurface(typeof(Form))) {
IDesignerHost host = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
IEventBindingService eventBindingService = new MockEventBindingService(host);
Form form = (Form)host.RootComponent;
form.ClientSize = new Size(200, 300);
PropertyDescriptorCollection descriptors = TypeDescriptor.GetProperties(form);
PropertyDescriptor namePropertyDescriptor = descriptors.Find("Name", false);
namePropertyDescriptor.SetValue(form, "MainForm");
// Add tree view.
TreeView treeView = (TreeView)host.CreateComponent(typeof(TreeView), "treeView1");
treeView.LineColor = Color.Black;
treeView.Location = new Point(0, 0);
treeView.Size = new Size(100, 100);
// Add first root node.
TreeNode firstRootNode = new TreeNode();
firstRootNode.Name = "RootNode0";
firstRootNode.Text = "RootNode0.Text";
treeView.Nodes.Add(firstRootNode);
// Add first child node.
TreeNode firstChildNode = new TreeNode();
firstChildNode.Name = "ChildNode0";
firstChildNode.Text = "ChildNode0.Text";
firstRootNode.Nodes.Add(firstChildNode);
// Add second child node.
TreeNode secondChildNode = new TreeNode();
secondChildNode.Name = "ChildNode1";
secondChildNode.Text = "ChildNode1.Text";
firstChildNode.Nodes.Add(secondChildNode);
form.Controls.Add(treeView);
PythonControl pythonControl = new PythonControl(" ");
generatedPythonCode = pythonControl.GenerateInitializeComponentMethod(form);
}
}
[Test]
public void GeneratedCode()
{
string expectedCode = "def InitializeComponent(self):\r\n" +
" treeNode1 = System.Windows.Forms.TreeNode()\r\n" +
" treeNode2 = System.Windows.Forms.TreeNode()\r\n" +
" treeNode3 = System.Windows.Forms.TreeNode()\r\n" +
" self._treeView1 = System.Windows.Forms.TreeView()\r\n" +
" self.SuspendLayout()\r\n" +
" # \r\n" +
" # treeView1\r\n" +
" # \r\n" +
" treeNode1.Name = \"RootNode0\"\r\n" +
" treeNode1.Text = \"RootNode0.Text\"\r\n" +
" treeNode1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" +
" [treeNode2]))\r\n" +
" treeNode2.Name = \"ChildNode0\"\r\n" +
" treeNode2.Text = \"ChildNode0.Text\"\r\n" +
" treeNode2.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" +
" [treeNode3]))\r\n" +
" treeNode3.Name = \"ChildNode1\"\r\n" +
" treeNode3.Text = \"ChildNode1.Text\"\r\n" +
" self._treeView1.Location = System.Drawing.Point(0, 0)\r\n" +
" self._treeView1.Name = \"treeView1\"\r\n" +
" self._treeView1.Nodes.AddRange(System.Array[System.Windows.Forms.TreeNode](\r\n" +
" [treeNode1]))\r\n" +
" self._treeView1.Size = System.Drawing.Size(100, 100)\r\n" +
" self._treeView1.TabIndex = 0\r\n" +
" # \r\n" +
" # MainForm\r\n" +
" # \r\n" +
" self.ClientSize = System.Drawing.Size(200, 300)\r\n" +
" self.Controls.Add(self._treeView1)\r\n" +
" self.Name = \"MainForm\"\r\n" +
" self.ResumeLayout(False)\r\n" +
" self.PerformLayout()\r\n";
Assert.AreEqual(expectedCode, generatedPythonCode, generatedPythonCode);
}
}
}

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

@ -206,6 +206,7 @@ @@ -206,6 +206,7 @@
<Compile Include="Designer\GenerateTextBoxFormTestFixture.cs" />
<Compile Include="Designer\GenerateTimerTestFixture.cs" />
<Compile Include="Designer\GenerateToolTipFormTestFixture.cs" />
<Compile Include="Designer\GenerateTreeViewTestFixture.cs" />
<Compile Include="Designer\GeneratorMergeFindsInitializeComponentsTestFixture.cs" />
<Compile Include="Designer\GetComponentFromDesignerLoaderTestFixture.cs" />
<Compile Include="Designer\GetInstanceFromDesignerLoaderTestFixture.cs" />

Loading…
Cancel
Save