// Copyright (C) Stichting Deltares and State of the Netherlands 2025. All rights reserved.
//
// This file is part of Riskeer.
//
// Riskeer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Core.Components.PointedTree.Properties;
namespace Core.Components.PointedTree.Data
{
///
/// Class for data with the purpose of becoming visible in pointed tree components.
///
public class GraphNode
{
private readonly GraphNode[] childNodes;
///
/// Creates a new instance of with default styling.
///
/// The content of the node.
/// The child nodes of the node.
/// Indicator whether the node is selectable.
/// Thrown when
/// or is null.
/// Thrown when the content is not valid.
/// Content should have the following format: <text>Content</text>.
/// The following tags are supported as content:
///
/// - <bold></bold>
///
///
public GraphNode(string content, GraphNode[] childNodes, bool isSelectable)
: this(content, childNodes, isSelectable, CreateDefaultGraphNodeStyle()) {}
///
/// Creates a new instance of .
///
/// The content of the node.
/// The child nodes of the node.
/// Indicator whether the node is selectable.
/// The style of the node.
/// Thrown when ,
/// or is null.
/// Thrown when the content is not valid.
/// Content should have the following format: <text>Content</text>.
/// The following tags are supported as content:
///
/// - <bold></bold>
///
///
public GraphNode(string content, GraphNode[] childNodes, bool isSelectable, GraphNodeStyle style)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
if (childNodes == null)
{
throw new ArgumentNullException(nameof(childNodes));
}
if (style == null)
{
throw new ArgumentNullException(nameof(style));
}
ValidateContent(content);
Content = content;
this.childNodes = childNodes;
IsSelectable = isSelectable;
Style = style;
}
///
/// Gets the content of the node.
///
public string Content { get; }
///
/// Gets the child nodes of the node.
///
public IEnumerable ChildNodes
{
get
{
return childNodes;
}
}
///
/// Gets an indicator whether the node is selectable.
///
public bool IsSelectable { get; }
///
/// Gets the style of the node.
///
public GraphNodeStyle Style { get; }
///
/// Validates whether the content is valid XML.
///
/// The content to validate.
/// Thrown when the
/// content is not valid.
private static void ValidateContent(string content)
{
var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.Add(XmlSchema.Read(new StringReader(Resources.GraphNodeContentSchema), null));
xmlSchemaSet.Compile();
try
{
XDocument doc = XDocument.Parse(content);
doc.Validate(xmlSchemaSet, null);
}
catch (Exception e) when (e is XmlException || e is XmlSchemaValidationException)
{
throw new ArgumentException("Content is of invalid format.", e);
}
}
private static GraphNodeStyle CreateDefaultGraphNodeStyle()
{
return new GraphNodeStyle(GraphNodeShape.Rectangle, Color.Gray, Color.Black, 2);
}
}
}