use of org.apache.pivot.collections.Sequence.Tree.Path in project pivot by apache.
the class Pivot734WithWorkaround method controlTree.
private void controlTree(BXMLSerializer bxmlSerializer) {
treeButtonAdd = (PushButton) bxmlSerializer.getNamespace().get("treeButtonAdd");
treeButtonRemove = (PushButton) bxmlSerializer.getNamespace().get("treeButtonRemove");
tree = (TreeView) bxmlSerializer.getNamespace().get("tree");
boolean treeStyleForShowEmptyBranchControls = tree.getStyles().getBoolean(Style.showEmptyBranchControls);
System.out.println("tree style for showEmptyBranchControls is " + treeStyleForShowEmptyBranchControls);
tree.getTreeViewSelectionListeners().add(new TreeViewSelectionListener() {
@Override
public void selectedPathAdded(TreeView treeView, Path path) {
System.out.println("selectedPathAdded");
}
@Override
public void selectedPathRemoved(TreeView treeView, Path path) {
System.out.println("selectedPathRemoved");
}
@Override
public void selectedPathsChanged(TreeView treeView, Sequence<Path> previousSelectedPaths) {
System.out.println("selectedPathsChanged");
}
@Override
public void selectedNodeChanged(TreeView treeView, Object previousSelectedNode) {
System.out.println("selectedNodeChanged");
}
});
treeButtonAdd.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Object x = tree.getSelectedNode();
System.out.println("add a 'new branch' element to the selected element :: " + x);
if (x != null && x instanceof TreeBranch) {
// workaround for PIVOT-734
Path path = tree.getSelectedPath();
tree.setBranchExpanded(path, true);
// end of workaround for PIVOT-734
((TreeBranch) x).add(newBranch);
}
}
});
treeButtonRemove.getButtonPressListeners().add(new ButtonPressListener() {
@Override
public void buttonPressed(Button button) {
Object x = tree.getSelectedNode();
System.out.println("remove a 'new branch' element under the selected element :: " + x);
if (x != null && x instanceof TreeBranch) {
((TreeBranch) x).remove(newBranch);
}
}
});
}
use of org.apache.pivot.collections.Sequence.Tree.Path in project pivot by apache.
the class TreeView method expandToBranch.
/**
* Expand "to" the given branch, which means opening up all the parents so that
* this branch is exposed.
*
* @param path The path to the branch to expose.
*/
public final void expandToBranch(Path path) {
checkNullOrEmpty(path);
for (int i = 0; i < path.getLength(); i++) {
Sequence.Tree.Path incrementalPath = new Sequence.Tree.Path(path, i + 1);
expandBranch(incrementalPath);
}
}
use of org.apache.pivot.collections.Sequence.Tree.Path in project pivot by apache.
the class TreeView method setNodeChecked.
/**
* Sets the check state of the node at the specified path. If the node
* already has the specified check state, nothing happens. <p> Note that it
* is impossible to set the check state of a node to <tt>MIXED</tt>. This is
* because the mixed check state is a derived state meaning "the node is not
* checked, but one or more of its descendants are."
*
* @param path The path to the node.
* @param checked <tt>true</tt> to check the node; <tt>false</tt> to uncheck it.
* @throws IllegalStateException If checkmarks are not enabled (see
* {@link #getCheckmarksEnabled()}).
* @see NodeCheckState#MIXED
*/
public void setNodeChecked(Path path, boolean checked) {
checkNullOrEmpty(path);
if (!checkmarksEnabled) {
throw new IllegalStateException("Checkmarks are not enabled.");
}
int index = checkedPaths.indexOf(path);
if ((index < 0 && checked) || (index >= 0 && !checked)) {
NodeCheckState previousCheckState = getNodeCheckState(path);
Sequence<NodeCheckState> ancestorCheckStates = null;
if (showMixedCheckmarkState) {
// Record the check states of our ancestors before we change
// anything so we know which events to fire after we're done
ancestorCheckStates = new ArrayList<>(path.getLength() - 1);
Path ancestorPath = new Path(path, path.getLength() - 1);
for (int i = ancestorPath.getLength() - 1; i >= 0; i--) {
ancestorCheckStates.insert(getNodeCheckState(ancestorPath), 0);
ancestorPath.remove(i, 1);
}
}
if (checked) {
// Monitor the path's parent
monitorBranch(path, false);
// Update the checked paths
checkedPaths.add(new ImmutablePath(path));
} else {
// Update the checked paths
checkedPaths.remove(index, 1);
}
// Notify listeners
treeViewNodeStateListeners.nodeCheckStateChanged(this, path, previousCheckState);
if (showMixedCheckmarkState && ancestorCheckStates != null) {
// Notify listeners of any changes to our ancestors' check
// states
Path ancestorPath = new Path(path, path.getLength() - 1);
for (int i = ancestorPath.getLength() - 1; i >= 0; i--) {
NodeCheckState ancestorPreviousCheckState = ancestorCheckStates.get(i);
NodeCheckState ancestorCheckState = getNodeCheckState(ancestorPath);
if (ancestorCheckState != ancestorPreviousCheckState) {
treeViewNodeStateListeners.nodeCheckStateChanged(this, ancestorPath, ancestorPreviousCheckState);
}
ancestorPath.remove(i, 1);
}
}
}
}
use of org.apache.pivot.collections.Sequence.Tree.Path in project pivot by apache.
the class TreeView method setSelectedPaths.
/**
* Set the new selected nodes in the tree.
*
* @param selectedPaths The new set of paths to the selected nodes.
* @return The new set of selected paths (with duplicates eliminated).
* @throws IllegalStateException If selection has been disabled (select mode
* <tt>NONE</tt>).
*/
public Sequence<Path> setSelectedPaths(Sequence<Path> selectedPaths) {
Utils.checkNull(selectedPaths, "Selected paths");
if (selectMode == SelectMode.NONE) {
throw new IllegalStateException("Selection is not enabled.");
}
if (selectMode == SelectMode.SINGLE && selectedPaths.getLength() > 1) {
throw new IllegalArgumentException("Cannot select more than one path in SINGLE select mode.");
}
Sequence<Path> previousSelectedPaths = this.selectedPaths;
Object previousSelectedNode = (selectMode == SelectMode.SINGLE) ? getSelectedNode() : null;
if (selectedPaths != previousSelectedPaths) {
this.selectedPaths = new ArrayList<>(PATH_COMPARATOR);
for (int i = 0, n = selectedPaths.getLength(); i < n; i++) {
Path path = selectedPaths.get(i);
// Monitor the branch itself, because if showEmptyBranchControls is false
// we need repaints as children are added/removed from this branch.
monitorBranch(path, true);
// Update the selection
this.selectedPaths.add(new ImmutablePath(path));
}
// Notify listeners
treeViewSelectionListeners.selectedPathsChanged(this, previousSelectedPaths);
if (selectMode == SelectMode.SINGLE) {
treeViewSelectionListeners.selectedNodeChanged(TreeView.this, previousSelectedNode);
}
}
return getSelectedPaths();
}
use of org.apache.pivot.collections.Sequence.Tree.Path in project pivot by apache.
the class JSONViewer method setValue.
private void setValue(Object value) {
assert (value instanceof Map<?, ?> || value instanceof List<?>);
// Remove prompt decorator
if (promptDecorator != null) {
treeView.getDecorators().remove(promptDecorator);
promptDecorator = null;
}
TreeBranch treeData = new TreeBranch();
treeData.add(build(value));
treeView.setTreeData(treeData);
treeView.expandBranch(new Path(0));
}
Aggregations