@Override
public boolean keyPressed(Component component, int keyCode, Keyboard.KeyLocation keyLocation) {
boolean consumed = false;
TreeView treeView = (TreeView)getComponent();
TreeView.SelectMode selectMode = treeView.getSelectMode();
switch (keyCode) {
case Keyboard.KeyCode.UP: {
if (selectMode != TreeView.SelectMode.NONE) {
Path firstSelectedPath = treeView.getFirstSelectedPath();
int index;
if (firstSelectedPath != null) {
NodeInfo previousSelectedNode = getNodeInfoAt(firstSelectedPath);
index = visibleNodes.indexOf(previousSelectedNode);
} else {
// Select the last visible node
index = visibleNodes.getLength();
}
NodeInfo newSelectedNode = null;
do {
newSelectedNode = (--index >= 0) ? visibleNodes.get(index) : null;
} while (newSelectedNode != null
&& newSelectedNode.isDisabled());
if (newSelectedNode != null) {
treeView.setSelectedPath(newSelectedNode.getPath());
treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
consumed = true;
}
}
break;
}
case Keyboard.KeyCode.DOWN: {
if (selectMode != TreeView.SelectMode.NONE) {
Path lastSelectedPath = treeView.getLastSelectedPath();
int index;
if (lastSelectedPath != null) {
NodeInfo previousSelectedNode = getNodeInfoAt(lastSelectedPath);
index = visibleNodes.indexOf(previousSelectedNode);
} else {
// Select the first visible node
index = -1;
}
NodeInfo newSelectedNode = null;
int n = visibleNodes.getLength();
do {
newSelectedNode = (++index <= n - 1) ? visibleNodes.get(index) : null;
} while (newSelectedNode != null
&& newSelectedNode.isDisabled());
if (newSelectedNode != null) {
treeView.setSelectedPath(newSelectedNode.getPath());
treeView.scrollAreaToVisible(getNodeBounds(newSelectedNode));
consumed = true;
}
}
break;
}
case Keyboard.KeyCode.LEFT: {
if (showBranchControls) {
Sequence<Path> paths = treeView.getSelectedPaths();
if (paths != null
&& paths.getLength() > 0) {
Path path = paths.get(paths.getLength() - 1);
NodeInfo nodeInfo = getNodeInfoAt(path);
if (nodeInfo instanceof BranchInfo) {
BranchInfo branchInfo = (BranchInfo)nodeInfo;
if (branchInfo.isExpanded()) {
treeView.collapseBranch(branchInfo.getPath());
}
}
consumed = true;
}
}
break;
}
case Keyboard.KeyCode.RIGHT: {
if (showBranchControls) {
Sequence<Path> paths = treeView.getSelectedPaths();
if (paths != null
&& paths.getLength() > 0) {
Path path = paths.get(paths.getLength() - 1);
NodeInfo nodeInfo = getNodeInfoAt(path);
if (nodeInfo instanceof BranchInfo) {
BranchInfo branchInfo = (BranchInfo)nodeInfo;
if (!branchInfo.isExpanded()) {
treeView.expandBranch(branchInfo.getPath());
}
}
consumed = true;
}