- Type Parameters:
I- The identity type of the nodes, which selection paths are made of.N- The common type of the nodes of the tree managed by this builder.T- The type of theJTreeinstance which will be managed by this builder.
JTree instances.
The recommended way to get one is UIFactoryMethods.tree(Var, Configurator), which binds the
tree to a single property holding a deeply immutable, nested data structure and a
TreeConf saying which parts of it to zoom into:
UI.tree(fileSystem, conf -> conf
.nodesOf(Dir.class, dir -> dir
.children(Dir::entries, Dir::withEntries)
.text(Dir::name, Dir::withName)
)
.nodesOf(Doc.class, doc -> doc.text(Doc::name))
)
.withSelection(selectedNode);
Where the data has no single root but several top level nodes,
UIFactoryMethods.trees(Var, Configurator) binds a Var<Tuple<N>> of them instead. The
builder returned is this same one, and everything below configures both forms alike.
Please take a look at the living swing-tree documentation where you can browse a large collection of examples demonstrating how to use the API of this class.
-
Method Summary
Modifier and TypeMethodDescription_newBuilderWithState(swingtree.BuilderState<T> newState) An internal wither method which creates a new builder instance with the providedBuilderStatestored inside it.protected swingtree.BuilderState<T> _state()Returns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor.isRootVisibleIf(sprouts.Val<Boolean> rootVisible) Binds the visibility of the root node to a property, so that the tree can fold its root away and back in response to application state.onSelection(sprouts.Action<TreeSelectionDelegate<I, N>> action) Registers an action which is invoked whenever the selection of the tree changes.withCell(Configurator<CellConf<T, V>> cellConfigurator) Configures a single cell view used for every node of the tree, which is the shorter form ofwithCells(Configurator)when there is no need to distinguish between node types.withCellEditor(TreeCellEditor editor) Installs a plain SwingTreeCellEditor, replacing the in place rename theTreeNodeConf.text(java.util.function.Function, java.util.function.BiFunction)rule installs by itself.withCellRenderer(TreeCellRenderer renderer) Installs a plain SwingTreeCellRenderer.withCells(Configurator<CellBuilder<T, V>> renderBuilder) Use this to build a cell renderer covering several node types at once, through the same fluent API the list, combo box and table components expose:withInitialExpansionDepth(int depth) Expands every branch down to the given depth, once, at the point this builder method runs, where a depth of1opens the topmost visible level,2the level below that as well, and so on.Installs a plain SwingTreeModel, which is the escape hatch for a tree whose contents genuinely do not live in a property, or which already has a model you want to keep.withRootHandlesVisible(boolean showsRootHandles) Decides whether the handles which expand and collapse a branch are drawn next to the top level nodes as well.withRootVisible(boolean rootVisible) Decides whether the root node of the bound structure is drawn as a row of its own.withRowHeight(int rowHeight) Sets the height of every row of the tree, in the same scale independent developer pixels the rest of SwingTree speaks in.withSelection(sprouts.Val<sprouts.Tuple<I>> selection) Binds the selected position of the tree to a read only property holding a path of ids, so that the tree follows the property but a selection made by the user is not written back.withSelection(sprouts.Var<sprouts.Tuple<I>> selection) Binds the selected position of the tree to a mutable property holding a path of ids, which keeps the two in sync in both directions: selecting a node writes the path leading to it into the property, and assigning a path selects the node it names, opening every branch above it and scrolling it into view.withSelectionPaths(sprouts.Var<sprouts.Tuple<sprouts.Tuple<I>>> selection) Binds every selected position of the tree to a mutable property holding one path of ids per selected node, which also puts the tree into a mode where the user may select any number of nodes.Methods inherited from class swingtree.UIForAnySwing
_addComponentTo, _addViewableProps, _addViewableProps, _addViewableProps, _isUndefinedColor, _isUndefinedFont, _isUndefinedFont, _setBackground, _setEnabled, _setMinHeight, _setMinWidth, _setPrefWidth, _tryCalculatingDiffBetween, add, add, add, add, add, add, add, add, add, add, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, addAll, doUpdates, group, group, id, id, isEnabledIf, isEnabledIf, isEnabledIf, isEnabledIfNot, isEnabledIfNot, isEnabledIfNot, isFocusableIf, isFocusableIf, isFocusableIf, isFocusableIfNot, isFocusableIfNot, isFocusableIfNot, isValidIf, isVisibleIf, isVisibleIf, isVisibleIf, isVisibleIfNot, isVisibleIfNot, isVisibleIfNot, makeFocused, on, on, onCharTyped, onFocusGain, onFocusLoss, onHidden, onKeyPress, onKeyRelease, onKeyTyped, onMouseClick, onMouseDrag, onMouseEnter, onMouseEnter, onMouseEnterGreedy, onMouseExit, onMouseExit, onMouseExitGreedy, onMouseMove, onMousePress, onMouseRelease, onMouseWheelDown, onMouseWheelMove, onMouseWheelUp, onMoved, onPressed, onRelease, onResize, onShown, onTyped, onTyped, onView, withBackground, withBackground, withBackgroundColor, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBackgroundIf, withBorder, withBorderTitled, withBorderTitled, withBounds, withBounds, withBoxLayout, withCompoundBorder, withCompoundBorderTitled, withCursor, withCursor, withCursorIf, withCursorIf, withDragAway, withDropSite, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorder, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withEmptyBorderTitled, withFlowLayout, withFlowLayout, withFlowLayout, withFont, withFont, withFont, withFontSize, withFontSize, withForeground, withForeground, withForegroundColor, withForegroundIf, withForegroundIf, withForegroundIf, withForegroundIf, withGridBagLayout, withGridLayout, withGridLayout, withGridLayout, withHeight, withHeight, withHeightExactly, withHeightExactly, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLayout, withLineBorder, withLineBorder, withLineBorder, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withLineBorderTitled, withLocation, withLocation, withMatteBorder, withMatteBorder, withMatteBorder, withMatteBorderTitled, withMatteBorderTitled, withMatteBorderTitled, withMaxHeight, withMaxHeight, withMaxSize, withMaxSize, withMaxSize, withMaxSize, withMaxWidth, withMaxWidth, withMinHeight, withMinHeight, withMinSize, withMinSize, withMinSize, withMinSize, withMinWidth, withMinWidth, withPrefHeight, withPrefHeight, withPrefSize, withPrefSize, withPrefSize, withPrefSize, withPrefWidth, withPrefWidth, withProperty, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRepaintOn, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorder, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withRoundedLineBorderTitled, withSize, withSize, withSize, withSizeExactly, withSizeExactly, withSizeExactly, withSizeExactly, withStyle, withStyle, withStyle, withTooltip, withTooltip, withTransitionalStyle, withTransitoryStyle, withWidth, withWidth, withWidthExactly, withWidthExactlyMethods inherited from class swingtree.UIForAnything
_addBuildersTo, _addBuilderTo, _addComponentsTo, _disposeState, _onShow, _onShow, _onShowDelegated, _runInApp, _runInApp, _runInUI, _this, _with, _withOnShow, _withOnShow, add, add, add, add, apply, applyIf, applyIfPresent, equals, get, getType, hashCode, peek, toString
-
Method Details
-
_state
Description copied from class:UIForAnythingReturns the state of the builder, which is a container for the wrapped component as well as it's type and currentEventProcessor.- Specified by:
_statein classUIForAnything<UIForTree<I,N, T extends JTree>, T extends JTree, JComponent> - Returns:
- The state of the builder.
-
_newBuilderWithState
Description copied from class:UIForAnythingAn internal wither method which creates a new builder instance with the providedBuilderStatestored inside it.- Specified by:
_newBuilderWithStatein classUIForAnything<UIForTree<I,N, T extends JTree>, T extends JTree, JComponent> - Parameters:
newState- The new state which should be stored inside the new builder instance.- Returns:
- A new builder instance with the provided state stored inside it.
-
withModel
Installs a plain SwingTreeModel, which is the escape hatch for a tree whose contents genuinely do not live in a property, or which already has a model you want to keep. A tree installed like this takes no part in any of the property binding this builder otherwise offers.Installing one on a tree built by
UI.tree(rootProperty, ..)replaces the bound model, which leaves the binding with nothing to drive; SwingTree says so in the log rather than leaving you to work out why the tree stopped following the property. Build the tree withUIFactoryMethods.of(JTree)instead when the contents are not a property.- Parameters:
model- TheTreeModelto install.- Returns:
- This builder node, to allow for method chaining.
-
withSelection
Binds the selected position of the tree to a mutable property holding a path of ids, which keeps the two in sync in both directions: selecting a node writes the path leading to it into the property, and assigning a path selects the node it names, opening every branch above it and scrolling it into view.
A selection is a position, and only a path names a position. A node value cannot: the same value may sit in several places at once, and a tree only requires its ids to be unique among siblings, so two folders may each hold aVar<Tuple<String>> selectedPath = vm.zoomTo(Move::selectedPath, Move::withSelectedPath); UI.tree(move, conf -> ..).withSelection(selectedPath); // nothing selected -> Tuple.of(String.class) // the root -> ["move"] // the kettle -> ["move", "kitchen", "appliances", "kettle"]notes.txt. A path of ids has nothing to be ambiguous about, needs no search to resolve, and carries no data which could contradict the tree.The empty tuple means nothing is selected, so there is no null to handle. To get at the node a path names — for a detail view, say — ask the configuration:
TreeConf.nodeAt(Object, Tuple)andTreeConf.nodesAlong(Object, Tuple).On a tree bound through
UIFactoryMethods.trees(Var, Configurator)the first id names a top level node rather than a root, so its paths are one element shorter and no path at all names the forest. Resolve them withTreeConf.nodeAt(Tuple, Tuple).Binding a single path this way also puts the tree into single selection mode. Use
withSelectionPaths(Var)for a tree the user may select several nodes in.- Parameters:
selection- A property holding the ids leading to the selected node.- Returns:
- This builder node, to allow for method chaining.
-
withSelection
Binds the selected position of the tree to a read only property holding a path of ids, so that the tree follows the property but a selection made by the user is not written back. SeewithSelection(Var)for why a selection is a path.- Parameters:
selection- A read only property holding the ids leading to the selected node.- Returns:
- This builder node, to allow for method chaining.
-
withSelectionPaths
public final UIForTree<I,N, withSelectionPathsT> (sprouts.Var<sprouts.Tuple<sprouts.Tuple<I>>> selection) Binds every selected position of the tree to a mutable property holding one path of ids per selected node, which also puts the tree into a mode where the user may select any number of nodes. The two stay in sync in both directions, just likewithSelection(Var).- Parameters:
selection- A property holding one path of ids per selected node.- Returns:
- This builder node, to allow for method chaining.
-
onSelection
Registers an action which is invoked whenever the selection of the tree changes. The action receives aTreeSelectionDelegatespeaking in your own node values, so there is never a reason to reach into the tree's own path objects.
For the common case of mirroring the selection into a view model, preferUI.tree(fileSystem, conf -> ..) .onSelection( it -> it.lead().ifPresent(node -> open(node)) );withSelection(Var), which needs no action at all.- Parameters:
action- The action to invoke when the selection changes.- Returns:
- This builder node, to allow for method chaining.
-
withRootVisible
Decides whether the root node of the bound structure is drawn as a row of its own. Hiding it also turns on the handles of the level below, so that what is now the first visible level can still be opened.A tree bound through
UIFactoryMethods.trees(Var, Configurator)has no root node, so there is nothing here to show: showing it is ignored and reported in the log, and hiding it was already the case.- Parameters:
rootVisible- True to show the root node.- Returns:
- This builder node, to allow for method chaining.
-
isRootVisibleIf
Binds the visibility of the root node to a property, so that the tree can fold its root away and back in response to application state. A tree bound throughUIFactoryMethods.trees(Var, Configurator)has no root node, so seewithRootVisible(boolean)for what happens there.- Parameters:
rootVisible- A property telling whether the root node should be shown.- Returns:
- This builder node, to allow for method chaining.
-
withRootHandlesVisible
Decides whether the handles which expand and collapse a branch are drawn next to the top level nodes as well. Hiding the root turns these on, because they are then the only handles there are — and turning them off again on a tree with no visible root leaves the user nothing to click, so only do that where expansion is driven from code.- Parameters:
showsRootHandles- True to draw handles next to the top level nodes.- Returns:
- This builder node, to allow for method chaining.
-
withRowHeight
Sets the height of every row of the tree, in the same scale independent developer pixels the rest of SwingTree speaks in. Pass0to let each row take the height its own cell view asks for.- Parameters:
rowHeight- The height of a row.- Returns:
- This builder node, to allow for method chaining.
-
withInitialExpansionDepth
Expands every branch down to the given depth, once, at the point this builder method runs, where a depth of1opens the topmost visible level,2the level below that as well, and so on. This is a convenience for the initial view only: expansion the user performs afterwards is untouched by it, and so is everything the bound property grows later.Depth is counted from what is on screen, so a root hidden with
withRootVisible(boolean)— and the absent root of a forest — does not count as a level. Call this after hiding the root, so that it knows.- Parameters:
depth- How many visible levels to expand.- Returns:
- This builder node, to allow for method chaining.
-
withCells
Use this to build a cell renderer covering several node types at once, through the same fluent API the list, combo box and table components expose:
Note that for a label and an icon there is no need to come here at all: declare them as part of the node's own rule withUI.tree(fileSystem, conf -> ..) .withCells( it -> it .when(Dir.class).asText( cell -> cell.entry().map(Dir::name).orElse("") ) .when(Doc.class).asComponent( cell -> myFancyRow(cell) ) );TreeNodeConf.text(java.util.function.Function)andTreeNodeConf.icon(java.util.function.Function). Reach for this method when a node needs a view a label cannot give it.The two mix freely: a node type this builder says nothing about keeps the label, icon and tool tip declared in its own rule, so covering one type here does not oblige you to cover the rest.
- Type Parameters:
V- The type of the node values being rendered.- Parameters:
renderBuilder- Configures the cell renderer.- Returns:
- This builder node, to allow for method chaining.
-
withCell
Configures a single cell view used for every node of the tree, which is the shorter form ofwithCells(Configurator)when there is no need to distinguish between node types.- Type Parameters:
V- The type of the node values being rendered.- Parameters:
cellConfigurator- Configures the cell view.- Returns:
- This builder node, to allow for method chaining.
-
withCellRenderer
Installs a plain SwingTreeCellRenderer. A bound tree keeps internal handles inside its paths, so the renderer is wrapped in one which unwraps them first: what reaches your renderer is always your own node value.A renderer installed this way answers for every node, so the
text(..),icon(..)andtoolTip(..)rules declared per node type no longer apply. UsewithCells(Configurator)to cover only some node types and leave the rest to their own rules.- Parameters:
renderer- The renderer to paint the nodes with.- Returns:
- This builder node, to allow for method chaining.
-
withCellEditor
Installs a plain SwingTreeCellEditor, replacing the in place rename theTreeNodeConf.text(java.util.function.Function, java.util.function.BiFunction)rule installs by itself.- Parameters:
editor- The editor to edit the nodes with.- Returns:
- This builder node, to allow for method chaining.
-