Browse Source

Adding and removing attributes from the XML tree editor is now possible.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1924 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 19 years ago
parent
commit
4db70538d8
  1. BIN
      data/resources/StringResources.cz.resources
  2. BIN
      data/resources/StringResources.es-mx.resources
  3. BIN
      data/resources/StringResources.es.resources
  4. BIN
      data/resources/StringResources.it.resources
  5. BIN
      data/resources/StringResources.nl.resources
  6. 59
      src/AddIns/DisplayBindings/XmlEditor/Project/Resources/AddAttributeDialog.xfrm
  7. 26
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeCommand.cs
  8. 87
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeDialog.cs
  9. 14
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/IXmlTreeView.cs
  10. 27
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveAttributeCommand.cs
  11. 82
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeEditor.cs
  12. 26
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs
  13. 85
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs
  14. 10
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewControl.cs
  15. 6
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs
  16. 15
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin
  17. 4
      src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj
  18. 147
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddAttributeTestFixture.cs
  19. 56
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/NoMatchingSchemaTestFixture.cs
  20. 97
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveAttributeTestFixture.cs
  21. 24
      src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewTestFixtureBase.cs
  22. 52
      src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockXmlTreeView.cs
  23. 3
      src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj
  24. BIN
      src/Main/StartUp/Project/Resources/StringResources.resources

BIN
data/resources/StringResources.cz.resources

Binary file not shown.

BIN
data/resources/StringResources.es-mx.resources

Binary file not shown.

BIN
data/resources/StringResources.es.resources

Binary file not shown.

BIN
data/resources/StringResources.it.resources

Binary file not shown.

BIN
data/resources/StringResources.nl.resources

Binary file not shown.

59
src/AddIns/DisplayBindings/XmlEditor/Project/Resources/AddAttributeDialog.xfrm

@ -0,0 +1,59 @@ @@ -0,0 +1,59 @@
<Components version="1.0">
<System.Windows.Forms.Form>
<Name value="AddAttributeDialog" />
<MinimizeBox value="False" />
<StartPosition value="CenterParent" />
<MinimumSize value="191, 155" />
<ShowInTaskbar value="False" />
<Text value="${res:ICSharpCode.XmlEditor.AddAttributeDialog.Title}" />
<MaximizeBox value="False" />
<ClientSize value="{Width=289, Height=244}" />
<ShowIcon value="False" />
<Controls>
<System.Windows.Forms.Label>
<Name value="label1" />
<Location value="5, 179" />
<Text value="${res:ICSharpCode.XmlEditor.AddAttributeDialog.CustomAttributeLabel}" />
<Size value="82, 23" />
<TabIndex value="1" />
</System.Windows.Forms.Label>
<System.Windows.Forms.TextBox>
<Name value="attributeTextBox" />
<TabIndex value="2" />
<Size value="183, 21" />
<Location value="100, 179" />
<Anchor value="Top, Left, Right" />
</System.Windows.Forms.TextBox>
<System.Windows.Forms.Button>
<Name value="okButton" />
<DialogResult value="OK" />
<Location value="121, 209" />
<Text value="${res:Global.OKButtonText}" />
<Anchor value="Bottom, Right" />
<UseVisualStyleBackColor value="True" />
<Size value="75, 23" />
<TabIndex value="3" />
</System.Windows.Forms.Button>
<System.Windows.Forms.Button>
<Name value="cancelButton" />
<DialogResult value="Cancel" />
<Location value="202, 209" />
<Text value="${res:Global.CancelButtonText}" />
<Anchor value="Bottom, Right" />
<UseVisualStyleBackColor value="True" />
<Size value="75, 23" />
<TabIndex value="4" />
</System.Windows.Forms.Button>
<System.Windows.Forms.ListBox>
<Name value="attributesListBox" />
<Size value="289, 173" />
<TabIndex value="0" />
<Sorted value="True" />
<SelectionMode value="MultiExtended" />
<FormattingEnabled value="True" />
<Location value="0, 0" />
<Anchor value="Top, Bottom, Left, Right" />
</System.Windows.Forms.ListBox>
</Controls>
</System.Windows.Forms.Form>
</Components>

26
src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeCommand.cs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
// <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 ICSharpCode.Core;
using System;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Adds a new attribute to the XML Tree's attribute property grid.
/// </summary>
public class AddAttributeCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.AddAttribute();
}
}
}
}

87
src/AddIns/DisplayBindings/XmlEditor/Project/Src/AddAttributeDialog.cs

@ -0,0 +1,87 @@ @@ -0,0 +1,87 @@
// <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 ICSharpCode.SharpDevelop.Gui.XmlForms;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ICSharpCode.XmlEditor
{
public class AddAttributeDialog : BaseSharpDevelopForm
{
ListBox attributesListBox;
Button okButton;
TextBox attributeTextBox;
public AddAttributeDialog(string[] attributeNames)
{
SetupFromXmlStream(GetType().Assembly.GetManifestResourceStream("ICSharpCode.XmlEditor.Resources.AddAttributeDialog.xfrm"));
okButton = (Button)ControlDictionary["okButton"];
okButton.Enabled = false;
AcceptButton = okButton;
CancelButton = (Button)ControlDictionary["cancelButton"];
attributeTextBox = (TextBox)ControlDictionary["attributeTextBox"];
attributeTextBox.TextChanged += AttributeTextBoxTextChanged;
attributesListBox = (ListBox)ControlDictionary["attributesListBox"];
attributesListBox.SelectedIndexChanged += AttributesListBoxSelectedIndexChanged;
foreach (string name in attributeNames) {
attributesListBox.Items.Add(name);
}
}
/// <summary>
/// Gets the attribute names selected.
/// </summary>
public string[] AttributeNames {
get {
List<string> attributeNames = new List<string>();
if (IsAttributeSelected) {
foreach (string attributeName in attributesListBox.SelectedItems) {
attributeNames.Add(attributeName);
}
}
string customAttributeName = attributeTextBox.Text.Trim();
if (customAttributeName.Length > 0) {
attributeNames.Add(customAttributeName);
}
return attributeNames.ToArray();
}
}
void AttributesListBoxSelectedIndexChanged(object source, EventArgs e)
{
okButton.Enabled = IsOkButtonEnabled;
}
void AttributeTextBoxTextChanged(object source, EventArgs e)
{
okButton.Enabled = IsOkButtonEnabled;
}
bool IsAttributeSelected {
get {
return attributesListBox.SelectedIndex >= 0;
}
}
bool IsOkButtonEnabled {
get {
return IsAttributeSelected || IsAttributeNameEntered;
}
}
bool IsAttributeNameEntered {
get {
return attributeTextBox.Text.Trim().Length > 0;
}
}
}
}

14
src/AddIns/DisplayBindings/XmlEditor/Project/Src/IXmlTreeView.cs

@ -60,5 +60,19 @@ namespace ICSharpCode.XmlEditor @@ -60,5 +60,19 @@ namespace ICSharpCode.XmlEditor
/// </summary>
XmlText SelectedTextNode {get;}
/// <summary>
/// Shows the add attribute dialog and allows the user
/// to select a new attribute to be added to the selected
/// xml element.
/// </summary>
/// <param name="attributes">The list of attributes to
/// be displayed to the user.</param>
/// <returns>The attributes selected; otherwise null.</returns>
string[] SelectNewAttributes(string[] attributes);
/// <summary>
/// Gets the name of the selected attribute.
/// </summary>
string SelectedAttribute {get;}
}
}

27
src/AddIns/DisplayBindings/XmlEditor/Project/Src/RemoveAttributeCommand.cs

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
// <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 ICSharpCode.Core;
using System;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Removes the selected attribute from the xml document being
/// displayed in the XML tree.
/// </summary>
public class RemoveAttributeCommand : AbstractMenuCommand
{
public override void Run()
{
XmlTreeViewContainerControl view = Owner as XmlTreeViewContainerControl;
if (view != null) {
view.RemoveAttribute();
}
}
}
}

82
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeEditor.cs

@ -5,7 +5,9 @@ @@ -5,7 +5,9 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using System;
using System.Collections.Generic;
using System.Xml;
namespace ICSharpCode.XmlEditor
@ -18,10 +20,12 @@ namespace ICSharpCode.XmlEditor @@ -18,10 +20,12 @@ namespace ICSharpCode.XmlEditor
{
IXmlTreeView view;
XmlDocument document;
XmlCompletionDataProvider completionDataProvider;
public XmlTreeEditor(IXmlTreeView view)
public XmlTreeEditor(IXmlTreeView view, XmlCompletionDataProvider completionDataProvider)
{
this.view = view;
this.completionDataProvider = completionDataProvider;
}
/// <summary>
@ -82,6 +86,41 @@ namespace ICSharpCode.XmlEditor @@ -82,6 +86,41 @@ namespace ICSharpCode.XmlEditor
view.IsDirty = true;
}
/// <summary>
/// Adds one or more new attribute to the selected element.
/// </summary>
public void AddAttribute()
{
XmlElement selectedElement = view.SelectedElement;
if (selectedElement != null) {
string[] attributesNames = GetMissingAttributes(selectedElement);
string[] selectedAttributeNames = view.SelectNewAttributes(attributesNames);
if (selectedAttributeNames.Length > 0) {
foreach (string attributeName in selectedAttributeNames) {
selectedElement.SetAttribute(attributeName, String.Empty);
}
view.IsDirty = true;
view.ShowAttributes(selectedElement.Attributes);
}
}
}
/// <summary>
/// Removes the selected attribute from the xml document.
/// </summary>
public void RemoveAttribute()
{
XmlElement selectedElement = view.SelectedElement;
if (selectedElement != null) {
string attribute = view.SelectedAttribute;
if (attribute != null) {
selectedElement.RemoveAttribute(attribute);
view.IsDirty = true;
view.ShowAttributes(selectedElement.Attributes);
}
}
}
/// <summary>
/// The text content has been changed in the view.
/// </summary>
@ -93,5 +132,46 @@ namespace ICSharpCode.XmlEditor @@ -93,5 +132,46 @@ namespace ICSharpCode.XmlEditor
textNode.Value = view.TextContent;
}
}
/// <summary>
/// Gets the missing attributes for the specified element based
/// on its associated schema.
/// </summary>
string[] GetMissingAttributes(XmlElement element)
{
XmlElementPath elementPath = GetElementPath(element);
List<string> attributes = new List<string>();
XmlSchemaCompletionData schemaCompletionData = completionDataProvider.FindSchema(elementPath);
if (schemaCompletionData != null) {
ICompletionData[] completionData = schemaCompletionData.GetAttributeCompletionData(elementPath);
foreach (ICompletionData attributeCompletionData in completionData) {
// Ignore existing attributes.
string attributeName = attributeCompletionData.Text;
if (!element.HasAttribute(attributeName)) {
attributes.Add(attributeName);
}
}
}
return attributes.ToArray();
}
/// <summary>
/// Returns the path to the specified element starting from the
/// root element.
/// </summary>
XmlElementPath GetElementPath(XmlElement element)
{
XmlElementPath path = new XmlElementPath();
XmlElement parentElement = element;
while (parentElement != null) {
QualifiedName name = new QualifiedName(parentElement.LocalName, parentElement.NamespaceURI, parentElement.Prefix);
path.Elements.Insert(0, name);
// Move to parent element.
parentElement = parentElement.ParentNode as XmlElement;
}
return path;
}
}
}

26
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs

@ -21,11 +21,13 @@ namespace ICSharpCode.XmlEditor @@ -21,11 +21,13 @@ namespace ICSharpCode.XmlEditor
XmlTreeViewContainerControl treeViewContainer = new XmlTreeViewContainerControl();
XmlView xmlView;
bool disposed;
bool ignoreDirtyChange;
public XmlTreeView(XmlView xmlView)
{
this.xmlView = xmlView;
treeViewContainer.DirtyChanged += TreeViewContainerDirtyChanged;
treeViewContainer.AttributesGrid.ContextMenuStrip = MenuService.CreateContextMenu(treeViewContainer, "/AddIns/XmlEditor/XmlTree/AttributesGrid/ContextMenu");
}
public override Control Control {
@ -40,6 +42,20 @@ namespace ICSharpCode.XmlEditor @@ -40,6 +42,20 @@ namespace ICSharpCode.XmlEditor
}
}
public override void NotifyBeforeSave()
{
Deselecting();
}
public override void NotifyAfterSave(bool successful)
{
if (!successful) {
ignoreDirtyChange = true;
treeViewContainer.IsDirty = xmlView.IsDirty;
ignoreDirtyChange = false;
}
}
public override void Dispose()
{
if (!disposed) {
@ -50,7 +66,9 @@ namespace ICSharpCode.XmlEditor @@ -50,7 +66,9 @@ namespace ICSharpCode.XmlEditor
public override void Selected()
{
treeViewContainer.LoadXml(xmlView.Text);
XmlEditorControl xmlEditor = xmlView.XmlEditor;
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(xmlEditor.SchemaCompletionDataItems, xmlEditor.DefaultSchemaCompletionData, xmlEditor.DefaultNamespacePrefix);
treeViewContainer.LoadXml(xmlView.Text, completionDataProvider);
xmlView.CheckIsWellFormed();
}
@ -59,7 +77,9 @@ namespace ICSharpCode.XmlEditor @@ -59,7 +77,9 @@ namespace ICSharpCode.XmlEditor
if (!disposed) {
if (treeViewContainer.IsDirty) {
xmlView.ReplaceAll(treeViewContainer.Document.OuterXml);
ignoreDirtyChange = true;
treeViewContainer.IsDirty = false;
ignoreDirtyChange = false;
}
}
}
@ -75,7 +95,9 @@ namespace ICSharpCode.XmlEditor @@ -75,7 +95,9 @@ namespace ICSharpCode.XmlEditor
void TreeViewContainerDirtyChanged(object source, EventArgs e)
{
xmlView.IsDirty = treeViewContainer.IsDirty;
if (!ignoreDirtyChange) {
xmlView.IsDirty = treeViewContainer.IsDirty;
}
}
}
}

85
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewContainerControl.cs

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.Core;
using System;
using System.ComponentModel;
using System.Drawing;
@ -18,7 +19,7 @@ namespace ICSharpCode.XmlEditor @@ -18,7 +19,7 @@ namespace ICSharpCode.XmlEditor
/// attributes property grid in a split container. This is separate from
/// the XmlTreeView class so we can use the forms designer to design this control.
/// </summary>
public class XmlTreeViewContainerControl : System.Windows.Forms.UserControl, IXmlTreeView
public class XmlTreeViewContainerControl : System.Windows.Forms.UserControl, IXmlTreeView, IOwnerState
{
XmlTreeEditor editor;
bool dirty;
@ -26,6 +27,13 @@ namespace ICSharpCode.XmlEditor @@ -26,6 +27,13 @@ namespace ICSharpCode.XmlEditor
bool attributesGridVisible = true;
bool textBoxVisible;
[Flags]
public enum XmlTreeViewContainerControlState {
Nothing = 0,
ElementSelected = 1,
AttributeSelected = 2
}
public event EventHandler DirtyChanged;
public XmlTreeViewContainerControl()
@ -34,6 +42,25 @@ namespace ICSharpCode.XmlEditor @@ -34,6 +42,25 @@ namespace ICSharpCode.XmlEditor
InitImages();
}
public Enum InternalState {
get {
XmlTreeViewContainerControlState state = XmlTreeViewContainerControlState.Nothing;
if (SelectedElement != null) {
state |= XmlTreeViewContainerControlState.ElementSelected;
}
if (SelectedAttribute != null) {
state |= XmlTreeViewContainerControlState.AttributeSelected;
}
return state;
}
}
public PropertyGrid AttributesGrid {
get {
return attributesGrid;
}
}
/// <summary>
/// Gets or sets whether the xml document needs saving.
/// </summary>
@ -42,7 +69,9 @@ namespace ICSharpCode.XmlEditor @@ -42,7 +69,9 @@ namespace ICSharpCode.XmlEditor
return dirty;
}
set {
bool previousDirty = dirty;
dirty = value;
OnXmlChanged(previousDirty);
}
}
@ -105,17 +134,16 @@ namespace ICSharpCode.XmlEditor @@ -105,17 +134,16 @@ namespace ICSharpCode.XmlEditor
/// <summary>
/// Displays the specified xml as a tree.
/// </summary>
public void LoadXml(string xml)
public void LoadXml(string xml, XmlCompletionDataProvider completionDataProvider)
{
textBox.Clear();
IsAttributesGridVisible = true;
ClearAttributes();
editor = new XmlTreeEditor(this);
editor = new XmlTreeEditor(this, completionDataProvider);
editor.LoadXml(xml);
// Expand document element node. This ensures that the view state
// can be restored since the child nodes are lazily added.
// Expand document element node.
if (xmlElementTreeView.Nodes.Count > 0) {
xmlElementTreeView.Nodes[0].Expand();
}
@ -183,6 +211,53 @@ namespace ICSharpCode.XmlEditor @@ -183,6 +211,53 @@ namespace ICSharpCode.XmlEditor
return xmlElementTreeView.SelectedTextNode;
}
}
/// <summary>
/// Gets the name of the attribute currently selected.
/// </summary>
public string SelectedAttribute {
get {
GridItem gridItem = attributesGrid.SelectedGridItem;
if (IsAttributesGridVisible && gridItem != null) {
return gridItem.PropertyDescriptor.Name;
}
return null;
}
}
/// <summary>
/// Shows the add attribute dialog so the user can add a new
/// attribute to the XML tree.
/// </summary>
public void AddAttribute()
{
editor.AddAttribute();
}
/// <summary>
/// Shows the add attribute dialog so the user can choose one or more
/// new attributes to be added to the selected element.
/// </summary>
/// <param name="attributes">The list of attributes the user
/// can choose from.</param>
/// <returns>The attributes selected by the user.</returns>
public string[] SelectNewAttributes(string[] attributes)
{
using (AddAttributeDialog addAttributeDialog = new AddAttributeDialog(attributes)) {
if (addAttributeDialog.ShowDialog() == DialogResult.OK) {
return addAttributeDialog.AttributeNames;
}
return new string[0];
}
}
/// <summary>
/// Removes the currently selected attribute.
/// </summary>
public void RemoveAttribute()
{
editor.RemoveAttribute();
}
/// <summary>
/// Disposes resources used by the control.

10
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeViewControl.cs

@ -96,7 +96,7 @@ namespace ICSharpCode.XmlEditor @@ -96,7 +96,7 @@ namespace ICSharpCode.XmlEditor
{
ExtTreeView.ApplyViewStateString(properties.Get(ViewStatePropertyName, String.Empty), this);
}
void ShowDocumentElement()
{
Nodes.Clear();
@ -105,5 +105,13 @@ namespace ICSharpCode.XmlEditor @@ -105,5 +105,13 @@ namespace ICSharpCode.XmlEditor
node.AddTo(this);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (SelectedNode == null) {
this.OnAfterSelect(new TreeViewEventArgs(null, TreeViewAction.ByMouse));
}
}
}
}

6
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -103,6 +103,12 @@ namespace ICSharpCode.XmlEditor @@ -103,6 +103,12 @@ namespace ICSharpCode.XmlEditor
}
}
public XmlEditorControl XmlEditor {
get {
return xmlEditor;
}
}
/// <summary>
/// Loads the string content into the view.
/// </summary>

15
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.addin

@ -181,4 +181,19 @@ @@ -181,4 +181,19 @@
class = "ICSharpCode.XmlEditor.RemoveXPathHighlightingCommand"/>
</Condition>
</Path>
<!-- XML Tree context menu options -->
<Path name = "/AddIns/XmlEditor/XmlTree/AttributesGrid/ContextMenu">
<Condition name = "Ownerstate" ownerstate="ElementSelected" action="Disable">
<MenuItem id = "AddAttribute"
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.AddAttributeMenuLabel}"
class = "ICSharpCode.XmlEditor.AddAttributeCommand"/>
</Condition>
<Condition name = "Ownerstate" ownerstate="AttributeSelected" action="Disable">
<MenuItem id = "RemoveAttribute"
label = "${res:ICSharpCode.XmlEditor.XmlTreeView.RemoveAttributeMenuLabel}"
icon = "Icons.16x16.DeleteIcon"
class = "ICSharpCode.XmlEditor.RemoveAttributeCommand"/>
</Condition>
</Path>
</AddIn>

4
src/AddIns/DisplayBindings/XmlEditor/Project/XmlEditor.csproj

@ -127,6 +127,10 @@ @@ -127,6 +127,10 @@
<Compile Include="Src\XmlAttributePropertyDescriptor.cs" />
<Compile Include="Src\XmlAttributeTypeDescriptor.cs" />
<Compile Include="Src\XmlEncoder.cs" />
<EmbeddedResource Include="Resources\AddAttributeDialog.xfrm" />
<Compile Include="Src\AddAttributeDialog.cs" />
<Compile Include="Src\AddAttributeCommand.cs" />
<Compile Include="Src\RemoveAttributeCommand.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj">

147
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/AddAttributeTestFixture.cs

@ -0,0 +1,147 @@ @@ -0,0 +1,147 @@
// <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 ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.IO;
using System.Xml;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that an attribute is added to the XML document by the
/// XmlTreeEditor.
/// </summary>
[TestFixture]
public class AddAttributeTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement bodyElement;
[SetUp]
public void Init()
{
base.InitFixture();
bodyElement = (XmlElement)editor.Document.SelectSingleNode("/html/body");
mockXmlTreeView.SelectedElement = bodyElement;
mockXmlTreeView.SelectedNewAttributesToReturn.Add("id");
editor.AddAttribute();
}
[Test]
public void ViewAddAttributeCalled()
{
Assert.IsTrue(mockXmlTreeView.IsSelectNewAttributesCalled);
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void ShowAttributesCalled()
{
XmlAttribute idAttribute = bodyElement.Attributes["id"];
Assert.IsTrue(mockXmlTreeView.AttributesDisplayed.Contains(idAttribute));
}
[Test]
public void NoElementSelected()
{
mockXmlTreeView.SelectedElement = null;
mockXmlTreeView.IsSelectNewAttributesCalled = false;
editor.AddAttribute();
Assert.IsFalse(mockXmlTreeView.IsSelectNewAttributesCalled);
}
[Test]
public void NoAttributeSelected()
{
XmlElement headElement = (XmlElement)editor.Document.SelectSingleNode("/html/head");
mockXmlTreeView.SelectedElement = headElement;
mockXmlTreeView.SelectedNewAttributesToReturn.Clear();
editor.AddAttribute();
Assert.IsFalse(headElement.HasAttribute("id"));
}
[Test]
public void IdAttributeAdded()
{
Assert.IsTrue(bodyElement.HasAttribute("id"));
}
[Test]
public void AttributeListDoesNotContainExistingTitleAttribute()
{
Assert.IsFalse(mockXmlTreeView.SelectNewAttributesList.Contains("title"));
}
[Test]
public void AttributeListContainsId()
{
Assert.IsTrue(mockXmlTreeView.SelectNewAttributesList.Contains("id"));
}
[Test]
public void AddTwoAttributes()
{
mockXmlTreeView.SelectedNewAttributesToReturn.Add("class");
mockXmlTreeView.SelectedNewAttributesToReturn.Add("onclick");
editor.AddAttribute();
XmlAttribute classAttribute = bodyElement.Attributes["class"];
XmlAttribute onclickAttribute = bodyElement.Attributes["onclick"];
Assert.IsNotNull(classAttribute);
Assert.IsNotNull(onclickAttribute);
Assert.IsTrue(mockXmlTreeView.AttributesDisplayed.Contains(classAttribute));
Assert.IsTrue(mockXmlTreeView.AttributesDisplayed.Contains(onclickAttribute));
}
/// <summary>
/// Tests that when all the attributes have been added the add
/// attribute dialog is still displayed.
/// </summary>
[Test]
public void AllAttributesAdded()
{
XmlElement htmlElement = (XmlElement)editor.Document.SelectSingleNode("/html");
mockXmlTreeView.SelectedElement = htmlElement;
mockXmlTreeView.SelectedNewAttributesToReturn.Clear();
mockXmlTreeView.IsSelectNewAttributesCalled = false;
editor.AddAttribute();
Assert.IsTrue(mockXmlTreeView.IsSelectNewAttributesCalled);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html dir='' id='' lang='' xml:lang=''>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body title='body text'>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

56
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/NoMatchingSchemaTestFixture.cs

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
// <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 ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.IO;
using System.Xml;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that no errors occur if there is no matching schema for the
/// xml document being edited.
/// </summary>
[TestFixture]
public class NoMatchingSchemaTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement bodyElement;
[SetUp]
public void Init()
{
base.InitFixture();
bodyElement = (XmlElement)editor.Document.SelectSingleNode("/html/body");
mockXmlTreeView.SelectedElement = bodyElement;
editor.AddAttribute();
}
/// <summary>
/// Can still add a new attribute even if there is no associated schema.
/// </summary>
[Test]
public void ViewAddAttributeCalled()
{
Assert.IsTrue(mockXmlTreeView.IsSelectNewAttributesCalled);
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

97
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/RemoveAttributeTestFixture.cs

@ -0,0 +1,97 @@ @@ -0,0 +1,97 @@
// <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 ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.IO;
using System.Xml;
using XmlEditor.Tests.Utils;
namespace XmlEditor.Tests.Tree
{
/// <summary>
/// Tests that an attribute is removed from the XML document by the
/// XmlTreeEditor.
/// </summary>
[TestFixture]
public class RemoveAttributeTestFixture : XmlTreeViewTestFixtureBase
{
XmlElement bodyElement;
[SetUp]
public void Init()
{
base.InitFixture();
bodyElement = (XmlElement)editor.Document.SelectSingleNode("/html/body");
mockXmlTreeView.SelectedElement = bodyElement;
mockXmlTreeView.SelectedAttribute = "id";
editor.RemoveAttribute();
}
[Test]
public void AttributesRedisplayed()
{
Assert.AreEqual(1, mockXmlTreeView.AttributesDisplayed.Count);
Assert.AreEqual("title", mockXmlTreeView.AttributesDisplayed[0].Name);
}
[Test]
public void IdAttributeRemoved()
{
Assert.IsFalse(bodyElement.HasAttribute("id"));
}
[Test]
public void IsDirty()
{
Assert.IsTrue(mockXmlTreeView.IsDirty);
}
[Test]
public void NoAttributeSelected()
{
mockXmlTreeView.SelectedElement = bodyElement;
mockXmlTreeView.SelectedAttribute = null;
mockXmlTreeView.IsDirty = false;
editor.RemoveAttribute();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
[Test]
public void NoElementSelected()
{
mockXmlTreeView.SelectedElement = null;
mockXmlTreeView.SelectedAttribute = "title";
mockXmlTreeView.IsDirty = false;
editor.RemoveAttribute();
Assert.IsFalse(mockXmlTreeView.IsDirty);
}
/// <summary>
/// Returns the xhtml strict schema as the default schema.
/// </summary>
protected override XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
XmlTextReader reader = ResourceManager.GetXhtmlStrictSchema();
return new XmlSchemaCompletionData(reader);
}
}
protected override string GetXml()
{
return "<html>\r\n" +
"\t<head>\r\n" +
"\t\t<title></title>\r\n" +
"\t</head>\r\n" +
"\t<body id='body' title='abc'>\r\n" +
"\t</body>\r\n" +
"</html>";
}
}
}

24
src/AddIns/DisplayBindings/XmlEditor/Test/Tree/XmlTreeViewTestFixtureBase.cs

@ -20,7 +20,8 @@ namespace XmlEditor.Tests.Tree @@ -20,7 +20,8 @@ namespace XmlEditor.Tests.Tree
public void InitFixture()
{
mockXmlTreeView = new MockXmlTreeView();
editor = new XmlTreeEditor(mockXmlTreeView);
XmlCompletionDataProvider completionDataProvider = new XmlCompletionDataProvider(SchemaDataItems, DefaultSchemaCompletionData, DefaultNamespacePrefix);
editor = new XmlTreeEditor(mockXmlTreeView, completionDataProvider);
editor.LoadXml(GetXml());
}
@ -28,5 +29,26 @@ namespace XmlEditor.Tests.Tree @@ -28,5 +29,26 @@ namespace XmlEditor.Tests.Tree
{
return String.Empty;
}
protected virtual XmlSchemaCompletionDataCollection SchemaDataItems {
get {
return new XmlSchemaCompletionDataCollection();
}
}
protected virtual XmlSchemaCompletionData DefaultSchemaCompletionData {
get {
return null;
}
}
/// <summary>
/// Gets the default element prefix.
/// </summary>
protected virtual string DefaultNamespacePrefix {
get {
return String.Empty;
}
}
}
}

52
src/AddIns/DisplayBindings/XmlEditor/Test/Utils/MockXmlTreeView.cs

@ -22,6 +22,10 @@ namespace XmlEditor.Tests.Utils @@ -22,6 +22,10 @@ namespace XmlEditor.Tests.Utils
string textContentDisplayed;
XmlText selectedTextNode;
bool dirty;
bool selectNewAttributesCalled = false;
List<string> selectNewAttributesList = new List<string>();
List<string> selectedNewAttributesToReturn = new List<string>();
string selectedAttribute = null;
public MockXmlTreeView()
{
@ -86,6 +90,22 @@ namespace XmlEditor.Tests.Utils @@ -86,6 +90,22 @@ namespace XmlEditor.Tests.Utils
{
textContentDisplayed = text;
}
public string[] SelectNewAttributes(string[] attributes)
{
selectNewAttributesCalled = true;
selectNewAttributesList.AddRange(attributes);
return selectedNewAttributesToReturn.ToArray();
}
public string SelectedAttribute {
get {
return selectedAttribute;
}
set {
selectedAttribute = value;
}
}
public string TextContent {
get {
@ -119,5 +139,37 @@ namespace XmlEditor.Tests.Utils @@ -119,5 +139,37 @@ namespace XmlEditor.Tests.Utils
return notWellFormedException;
}
}
/// <summary>
/// Gets whether the SelectNewAttributes method has been called.
/// </summary>
public bool IsSelectNewAttributesCalled {
get {
return selectNewAttributesCalled;
}
set {
selectNewAttributesCalled = value;
}
}
/// <summary>
/// Gets the set of attributes that will be returned from the
/// SelectNewAttributes method.
/// </summary>
public List<string> SelectedNewAttributesToReturn {
get {
return selectedNewAttributesToReturn;
}
}
/// <summary>
/// Returns the list of attributes passed to the
/// SelectNewAttribute method.
/// </summary>
public List<string> SelectNewAttributesList {
get {
return selectNewAttributesList;
}
}
}
}

3
src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj

@ -135,6 +135,9 @@ @@ -135,6 +135,9 @@
<Compile Include="Tree\AttributeChangedTestFixture.cs" />
<Compile Include="Tree\TextNodeTextChangedTestFixture.cs" />
<Compile Include="XPathQuery\EncodeXPathParameterTests.cs" />
<Compile Include="Tree\AddAttributeTestFixture.cs" />
<Compile Include="Tree\NoMatchingSchemaTestFixture.cs" />
<Compile Include="Tree\RemoveAttributeTestFixture.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Schema\" />

BIN
src/Main/StartUp/Project/Resources/StringResources.resources

Binary file not shown.
Loading…
Cancel
Save