Merge pull request #7047 from gdicristofaro/7708-preExpandNodes

7708 preExpandNodes fix
This commit is contained in:
Richard Cordovano 2021-06-21 10:41:27 -04:00 committed by GitHub
commit 054ad81e9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 47 deletions

View File

@ -228,7 +228,7 @@ public class HostNode extends DisplayableItemNode {
* @param hostGrouping The HostGrouping key. * @param hostGrouping The HostGrouping key.
*/ */
HostNode(HostGrouping hostGrouping) { HostNode(HostGrouping hostGrouping) {
this(Children.create(new HostGroupingChildren(HOST_GROUPING_CONVERTER, hostGrouping.getHost()), false), hostGrouping.getHost()); this(Children.create(new HostGroupingChildren(HOST_GROUPING_CONVERTER, hostGrouping.getHost()), true), hostGrouping.getHost());
} }
/** /**

View File

@ -33,6 +33,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.PreferenceChangeEvent;
@ -44,14 +45,11 @@ import javax.swing.SwingUtilities;
import javax.swing.SwingWorker; import javax.swing.SwingWorker;
import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener; import javax.swing.event.PopupMenuListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.TreeSelectionModel; import javax.swing.tree.TreeSelectionModel;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openide.explorer.ExplorerManager; import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils; import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView; import org.openide.explorer.view.BeanTreeView;
import org.openide.explorer.view.Visualizer;
import org.openide.nodes.AbstractNode; import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children; import org.openide.nodes.Children;
import org.openide.nodes.Node; import org.openide.nodes.Node;
@ -126,6 +124,10 @@ public final class DirectoryTreeTopComponent extends TopComponent implements Dat
private static final String GROUPING_THRESHOLD_NAME = "GroupDataSourceThreshold"; private static final String GROUPING_THRESHOLD_NAME = "GroupDataSourceThreshold";
private static final String SETTINGS_FILE = "CasePreferences.properties"; //NON-NLS private static final String SETTINGS_FILE = "CasePreferences.properties"; //NON-NLS
// nodes to be opened if present at top level
private static final Set<String> NODES_TO_EXPAND = Stream.of(AnalysisResults.getName(), DataArtifacts.getName(), ViewsNode.NAME)
.collect(Collectors.toSet());
/** /**
* the constructor * the constructor
*/ */
@ -134,31 +136,7 @@ public final class DirectoryTreeTopComponent extends TopComponent implements Dat
// only allow one item to be selected at a time // only allow one item to be selected at a time
getTree().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); getTree().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//Hook into the JTree and pre-expand the Views Node and Results node when a user
//expands an item in the tree that makes these nodes visible.
((ExpansionBeanTreeView) getTree()).addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
//Bail immediately if we are not in the Group By view.
//Assumption here is that the views are already expanded.
if (!CasePreferences.getGroupItemsInTreeByDataSource()) {
return;
}
Node expandedNode = Visualizer.findNode(event.getPath().getLastPathComponent());
for (Node child : em.getRootContext().getChildren().getNodes()) {
if (child.equals(expandedNode)) {
preExpandNodes(child.getChildren());
}
}
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
//Do nothing
}
});
// remove the close button // remove the close button
putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE); putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);
setName(NbBundle.getMessage(DirectoryTreeTopComponent.class, "CTL_DirectoryTreeTopComponent")); setName(NbBundle.getMessage(DirectoryTreeTopComponent.class, "CTL_DirectoryTreeTopComponent"));
@ -201,30 +179,23 @@ public final class DirectoryTreeTopComponent extends TopComponent implements Dat
*/ */
private void preExpandNodes(Children rootChildren) { private void preExpandNodes(Children rootChildren) {
BeanTreeView tree = getTree(); BeanTreeView tree = getTree();
for (String categoryKey : new String[]{AnalysisResults.getName(), DataArtifacts.getName()}) {
Node categoryNode = rootChildren.findChild(categoryKey);
if (!Objects.isNull(categoryNode)) { // using getNodes(true) to fetch children so that async nodes are loaded
tree.expandNode(categoryNode); Node[] rootChildrenNodes = rootChildren.getNodes(true);
Children resultsChildren = categoryNode.getChildren(); if (rootChildrenNodes == null || rootChildrenNodes.length < 1) {
Arrays.stream(resultsChildren.getNodes()).forEach(tree::expandNode); return;
}
}
Node views = rootChildren.findChild(ViewsNode.NAME);
if (!Objects.isNull(views)) {
tree.expandNode(views);
} }
// expand all nodes parents of and including hosts if group by host/person // expand all nodes parents of and including hosts if group by host/person
if (Objects.equals(CasePreferences.getGroupItemsInTreeByDataSource(), true)) { if (Objects.equals(CasePreferences.getGroupItemsInTreeByDataSource(), true)) {
Node[] rootNodes = rootChildren.getNodes(); Stream.of(rootChildrenNodes)
if (rootNodes != null) {
Stream.of(rootNodes)
.flatMap((n) -> getHostNodesAndParents(n).stream()) .flatMap((n) -> getHostNodesAndParents(n).stream())
.filter((n) -> n != null) .filter((n) -> n != null)
.forEach((n) -> tree.expandNode(n)); .forEach(tree::expandNode);
} } else {
Stream.of(rootChildrenNodes)
.filter(n -> n != null && NODES_TO_EXPAND.contains(n.getName()))
.forEach(tree::expandNode);
} }
} }