focus parent of deleted key path

This commit is contained in:
Marcel Haßlinger 2021-11-09 21:21:55 +01:00
parent f0d83f0d12
commit 47fde38168
3 changed files with 20 additions and 9 deletions

View File

@ -65,8 +65,10 @@ public class InstanceManager {
if(success) {
this.bus.propagate().onUpdateData(this.store.getData());
if(!update.isDeletion()) { // TODO: maybe focus parent if key was deleted
if(!update.isDeletion()) {
this.bus.propagate().onFocusKey(update.getChange().getKey());
} else {
this.bus.propagate().onFocusKey(update.getOrigin().getKey());
}
}
});

View File

@ -87,7 +87,12 @@ public class TreeView implements BusListener {
@Override
public void onFocusKey(@Nullable String key) {
if(key != null && currentMapper != null) {
this.tree.scrollPathToVisible(currentMapper.findTreePath(key));
TreePath path = currentMapper.findTreePath(key);
this.tree.scrollPathToVisible(path);
if(this.tree.isCollapsed(path)) {
this.tree.expandPath(path);
}
}
}

View File

@ -14,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.tree.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -111,19 +112,22 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
public @NotNull TreePath findTreePath(@NotNull String fullPath) {
List<String> sections = new PathUtil(this.state.isNestedKeys()).split(fullPath);
Object[] nodes = new Object[sections.size() + 1];
List<Object> nodes = new ArrayList<>();
int pos = 0;
TreeNode currentNode = (TreeNode) this.getRoot();
nodes[pos] = currentNode;
nodes.add(currentNode);
for(String section : sections) {
pos++;
currentNode = this.findNode(currentNode, section);
nodes[pos] = currentNode;
if(currentNode == null) {
break;
}
nodes.add(currentNode);
}
return new TreePath(nodes);
return new TreePath(nodes.toArray());
}
public @Nullable DefaultMutableTreeNode findNode(@NotNull TreeNode parent, @NotNull String key) {
@ -144,6 +148,6 @@ public class TreeModelMapper extends DefaultTreeModel implements SearchQueryList
}
}
throw new NullPointerException("Cannot find node by key: " + key);
return null;
}
}