Table of Contents

Class StructureEditorPresenter<T>

Namespace
NanoByte.StructureEditor
Assembly
NanoByte.StructureEditor.dll

Toolkit-neutral logic for an editor for hierarchical structures. Used by GUI frontends to implement IStructureEditor<T>.

public class StructureEditorPresenter<T> where T : class

Type Parameters

T

The type of object to edit.

Inheritance
object
StructureEditorPresenter<T>

Remarks

Holds the descriptions of the structure, the object being edited and the current selection. The frontend is responsible for displaying GetRootNodes()/GetNodes(object?) as a tree, reporting the selected node via Select(Node?, out bool, out bool) and rebuilding the tree when StructureChanged is raised.

Constructors

StructureEditorPresenter(Func<T>)

Creates a new structure editor presenter.

public StructureEditorPresenter(Func<T> factory)

Parameters

factory Func<T>

Callback to create a new instance of T.

Properties

CommandManager

Holds the object being edited and manages undo/redo operations on it.

public ICommandManager<T> CommandManager { get; }

Property Value

ICommandManager<T>

SelectedNode

The currently selected node; null if nothing is selected.

public Node? SelectedNode { get; }

Property Value

Node

SelectedTarget

The target object of the currently selected node; null if nothing is selected.

public object? SelectedTarget { get; }

Property Value

object

Serializer

The serializer used for the text representation of the selected node. Defaults to XmlStructureSerializer.

public IStructureSerializer Serializer { get; set; }

Property Value

IStructureSerializer

Remarks

Set automatically by Open(ICommandManager<T>) when passed a SerializingCommandManager<T>.

TargetComparer

Compares Targets by reference, so that they can be tracked across tree rebuilds (e.g., to restore expansion state).

public static IEqualityComparer<object> TargetComparer { get; }

Property Value

IEqualityComparer<object>

Methods

Add(NodeCandidate)

Adds a new node to the structure and marks it for selection once the tree has been rebuilt.

public void Add(NodeCandidate candidate)

Parameters

candidate NodeCandidate

One of the candidates returned by GetCandidates().

Close()

Stops listening for changes to the CommandManager. Call this when the frontend is disposed.

public void Close()

DescribeRoot<TEditor>(string)

Sets up handling for the root element with a custom editor.

public IContainerDescription<T> DescribeRoot<TEditor>(string name) where TEditor : INodeEditor<T>, new()

Parameters

name string

The name of the root element.

Returns

IContainerDescription<T>

The ContainerDescription<TContainer> for use in a "Fluent API" style.

Type Parameters

TEditor

An editor for modifying the content of the root.

Describe<TContainer>()

Adds a ContainerDescription<TContainer> used to describe the structure of the data being edited.

public IContainerDescription<TContainer> Describe<TContainer>() where TContainer : class

Returns

IContainerDescription<TContainer>

The ContainerDescription<TContainer> for use in a "Fluent API" style.

Type Parameters

TContainer

The type of the container to describe.

GetCandidates()

Returns the nodes that can be added as children of the currently selected node.

public IReadOnlyList<NodeCandidate?> GetCandidates()

Returns

IReadOnlyList<NodeCandidate>

A list of candidates. null entries mark the boundaries between groups (e.g., for separators in a menu); there are no leading, trailing or consecutive nulls.

GetEditorControl()

Gets a GUI control for editing the currently selected node; null if nothing is selected.

public INodeEditor? GetEditorControl()

Returns

INodeEditor

GetFallbackTarget()

Determines which node to select after rebuilding the tree if no node satisfies ShouldSelect(Node), e.g., because the selected node was removed by an undo.

public object? GetFallbackTarget()

Returns

object

The Target of the node that replaced the selected node at the same position (e.g., after undoing changes made via the text editor) or, failing that, of its closest remaining ancestor; null if none.

GetNodes(object?)

Returns the child nodes of a Target.

public IReadOnlyList<Node> GetNodes(object? target)

Parameters

target object

The Target to look into; can be null.

Returns

IReadOnlyList<Node>

GetRemoveCommand()

Gets a command for removing the currently selected node from the structure; null if nothing is selected or the node cannot be removed.

public IUndoCommand? GetRemoveCommand()

Returns

IUndoCommand

GetRootNodes()

Returns the top-level nodes of the structure.

public IReadOnlyList<Node> GetRootNodes()

Returns

IReadOnlyList<Node>

GetSerialized()

Returns the serialized representation of the currently selected node; empty if nothing is selected.

public string GetSerialized()

Returns

string

Open(ICommandManager<T>)

Opens an object for editing using the specified ICommandManager<T>.

public void Open(ICommandManager<T> commandManager)

Parameters

commandManager ICommandManager<T>

Holds the object being edited and manages undo/redo operations on it. If this is a SerializingCommandManager<T> its Serializer is also applied to Serializer.

Remarks

Does not raise StructureChanged. The frontend needs to rebuild its tree after calling this.

Select(Node?, out bool, out bool)

Reports that a different node has been selected in the tree.

public void Select(Node? node, out bool editorChanged, out bool textChanged)

Parameters

node Node

The newly selected node; null if nothing is selected.

editorChanged bool

true if the editor control needs to be replaced with GetEditorControl(); false if the existing editor control is still valid and only needs to be refreshed.

textChanged bool

true if the text representation needs to be replaced with GetSerialized(); false if the text currently shown already represents the node.

ShouldSelect(Node)

Determines whether a node should be selected after rebuilding the tree, in order to retain the previous selection or to select a newly created node.

public bool ShouldSelect(Node node)

Parameters

node Node

Returns

bool

UpdateFromText(string)

Replaces the currently selected node with a new value entered as text.

public bool UpdateFromText(string serializedValue)

Parameters

serializedValue string

A serialized representation of the new value.

Returns

bool

true if the structure was modified; false if nothing is selected or the new value is equivalent to the current one.

Exceptions

Exception

The serializedValue is not a valid serialized representation of the node.

Events

StructureChanged

Raised when the structure was modified and the tree needs to be rebuilt.

public event Action? StructureChanged

Event Type

Action

Remarks

May be raised multiple times in quick succession. Frontends should coalesce these into a single rebuild.