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
TThe type of object to edit.
- Inheritance
-
objectStructureEditorPresenter<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
factoryFunc<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
SelectedNode
The currently selected node; null if nothing is selected.
public Node? SelectedNode { get; }
Property Value
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
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
candidateNodeCandidateOne 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
namestringThe name of the root element.
Returns
- IContainerDescription<T>
The ContainerDescription<TContainer> for use in a "Fluent API" style.
Type Parameters
TEditorAn 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
TContainerThe 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.
nullentries mark the boundaries between groups (e.g., for separators in a menu); there are no leading, trailing or consecutivenulls.
GetEditorControl()
Gets a GUI control for editing the currently selected node; null if nothing is selected.
public INodeEditor? GetEditorControl()
Returns
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;
nullif none.
GetNodes(object?)
Returns the child nodes of a Target.
public IReadOnlyList<Node> GetNodes(object? target)
Parameters
targetobjectThe Target to look into; can be
null.
Returns
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
GetRootNodes()
Returns the top-level nodes of the structure.
public IReadOnlyList<Node> GetRootNodes()
Returns
GetSerialized()
Returns the serialized representation of the currently selected node; empty if nothing is selected.
public string GetSerialized()
Returns
Open(ICommandManager<T>)
Opens an object for editing using the specified ICommandManager<T>.
public void Open(ICommandManager<T> commandManager)
Parameters
commandManagerICommandManager<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
nodeNodeThe newly selected node;
nullif nothing is selected.editorChangedbooltrueif the editor control needs to be replaced with GetEditorControl();falseif the existing editor control is still valid and only needs to be refreshed.textChangedbooltrueif the text representation needs to be replaced with GetSerialized();falseif 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
nodeNode
Returns
UpdateFromText(string)
Replaces the currently selected node with a new value entered as text.
public bool UpdateFromText(string serializedValue)
Parameters
serializedValuestringA serialized representation of the new value.
Returns
- bool
trueif the structure was modified;falseif nothing is selected or the new value is equivalent to the current one.
Exceptions
- Exception
The
serializedValueis 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
Remarks
May be raised multiple times in quick succession. Frontends should coalesce these into a single rebuild.