@Nullable private TreePath getSelectedUniquePath() { JTree tree = getTree(); if (tree == null) return null; TreePath[] paths = tree.getSelectionPaths(); return paths == null || paths.length != 1 ? null : paths[0]; }
private static boolean isMoveSupported(JTree tree, int dir) { final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths != null) { DefaultMutableTreeNode parent = null; for (TreePath treePath : selectionPaths) if (treePath.getLastPathComponent() != null) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (parent == null) { parent = (DefaultMutableTreeNode) node.getParent(); } if (parent != node.getParent()) { return false; } if (dir > 0) { if (parent.getIndex(node) == parent.getChildCount() - 1) { return false; } } else { if (parent.getIndex(node) == 0) { return false; } } } return true; } return false; }
/** A value has changed. This is used as a TreeSelectionListener. */ public void valueChanged(TreeSelectionEvent e) { JHelpNavigator navigator = getHelpNavigator(); HelpModel helpmodel = navigator.getModel(); debug("ValueChanged: " + e); debug(" model: " + helpmodel); // send selected items into navigator TreeItem[] items = null; TreePath[] paths = tree.getSelectionPaths(); if (paths != null) { items = new TreeItem[paths.length]; for (int i = 0; i < paths.length; i++) { if (paths[i] != null) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) paths[i].getLastPathComponent(); items[i] = (TreeItem) node.getUserObject(); } } } navigator.setSelectedItems(items); // change current id only if one items is selected if (items != null && items.length == 1) { SearchTOCItem item = (SearchTOCItem) items[0]; if (item != null) { if (item.getID() != null) { try { // navigator.setCurrentID(item.getID()); helpmodel.setCurrentID(item.getID(), item.getName(), navigator); } catch (InvalidHelpSetContextException ex) { System.err.println("BadID: " + item.getID()); return; } } else if (item.getURL() != null) { // navigator.setCurrentURL(item.getURL()); helpmodel.setCurrentURL(item.getURL(), item.getName(), navigator); } else { // no ID, no URL return; } if (helpmodel instanceof TextHelpModel) { DefaultHighlight h[] = new DefaultHighlight[item.hitCount()]; int i = 0; Enumeration enum1 = item.getSearchHits(); while (enum1.hasMoreElements()) { SearchHit info = (SearchHit) enum1.nextElement(); h[i] = new DefaultHighlight(info.getBegin(), info.getEnd()); i++; } // using setHighlights() instead of removeAll + add // avoids one highlighting event ((TextHelpModel) helpmodel).setHighlights(h); } } } }
public static void unselect(JTree tree, final DefaultMutableTreeNode node) { final TreePath rootPath = new TreePath(node.getPath()); final TreePath[] selectionPaths = tree.getSelectionPaths(); if (selectionPaths != null) { for (TreePath selectionPath : selectionPaths) { if (selectionPath.getPathCount() > rootPath.getPathCount() && rootPath.isDescendant(selectionPath)) { tree.removeSelectionPath(selectionPath); } } } }
public static List<TreePath> collectSelectedPaths(final JTree tree, final TreePath treePath) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); final TreePath[] selections = tree.getSelectionPaths(); if (selections != null) { for (TreePath selection : selections) { if (treePath.isDescendant(selection)) { result.add(selection); } } } return result; }
@Nullable private List<UsageInfo> getSelectedUsageInfos() { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return null; final ArrayList<UsageInfo> result = new ArrayList<UsageInfo>(); for (TreePath path : paths) { SliceNode sliceNode = fromPath(path); if (sliceNode != null) { result.add(sliceNode.getValue().getUsageInfo()); } } if (result.isEmpty()) return null; return result; }
@Nullable private Node[] getSelectedNodes() { TreePath[] leadSelectionPath = myTree.getSelectionPaths(); if (leadSelectionPath == null || leadSelectionPath.length == 0) return null; final List<Node> result = new ArrayList<Node>(); for (TreePath comp : leadSelectionPath) { final Object lastPathComponent = comp.getLastPathComponent(); if (lastPathComponent instanceof Node) { final Node node = (Node) lastPathComponent; result.add(node); } } return result.isEmpty() ? null : result.toArray(new Node[result.size()]); }
@Nullable public Set<Object> getTreeSelectedActionIds() { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return null; Set<Object> actions = new HashSet<Object>(); for (TreePath path : paths) { Object node = path.getLastPathComponent(); if (node instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode defNode = (DefaultMutableTreeNode) node; Object userObject = defNode.getUserObject(); actions.add(userObject); } } return actions; }
@Override @Nullable public Set<Usage> getSelectedUsages() { TreePath[] selectionPaths = myTree.getSelectionPaths(); if (selectionPaths == null) { return null; } Set<Usage> usages = new THashSet<Usage>(); for (TreePath selectionPath : selectionPaths) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); collectUsages(node, usages); } return usages; }
@NotNull public static <T> List<T> collectSelectedObjectsOfType(JTree tree, Class<T> clazz) { final TreePath[] selections = tree.getSelectionPaths(); if (selections != null) { final ArrayList<T> result = new ArrayList<T>(); for (TreePath selection : selections) { final DefaultMutableTreeNode node = (DefaultMutableTreeNode) selection.getLastPathComponent(); final Object userObject = node.getUserObject(); if (clazz.isInstance(userObject)) { //noinspection unchecked result.add((T) userObject); } } return result; } return Collections.emptyList(); }
@NotNull private List<Navigatable> getNavigatables() { TreePath[] paths = myTree.getSelectionPaths(); if (paths == null) return Collections.emptyList(); final ArrayList<Navigatable> navigatables = new ArrayList<Navigatable>(); for (TreePath path : paths) { Object lastPathComponent = path.getLastPathComponent(); if (lastPathComponent instanceof DefaultMutableTreeNode) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) lastPathComponent; Object userObject = node.getUserObject(); if (userObject instanceof Navigatable) { navigatables.add((Navigatable) userObject); } else if (node instanceof Navigatable) { navigatables.add((Navigatable) node); } } } return navigatables; }
public static void ensureSelection(JTree tree) { final TreePath[] paths = tree.getSelectionPaths(); if (paths != null) { for (TreePath each : paths) { if (tree.getRowForPath(each) >= 0 && tree.isVisible(each)) { return; } } } for (int eachRow = 0; eachRow < tree.getRowCount(); eachRow++) { TreePath eachPath = tree.getPathForRow(eachRow); if (eachPath != null && tree.isVisible(eachPath)) { tree.setSelectionPath(eachPath); break; } } }
@Nullable private UsageTarget[] getSelectedUsageTargets() { TreePath[] selectionPaths = myTree.getSelectionPaths(); if (selectionPaths == null) return null; Set<UsageTarget> targets = new THashSet<UsageTarget>(); for (TreePath selectionPath : selectionPaths) { Object lastPathComponent = selectionPath.getLastPathComponent(); if (lastPathComponent instanceof UsageTargetNode) { UsageTargetNode usageTargetNode = (UsageTargetNode) lastPathComponent; UsageTarget target = usageTargetNode.getTarget(); if (target != null && target.isValid()) { targets.add(target); } } } return targets.isEmpty() ? null : targets.toArray(new UsageTarget[targets.size()]); }
private List<ActionUrl> findActionsUnderSelection() { final ArrayList<ActionUrl> actions = new ArrayList<ActionUrl>(); final TreePath[] selectionPaths = myActionsTree.getSelectionPaths(); if (selectionPaths != null) { for (TreePath path : selectionPaths) { final ActionUrl selectedUrl = CustomizationUtil.getActionUrl(path, ActionUrl.MOVE); final ArrayList<String> selectedGroupPath = new ArrayList<String>(selectedUrl.getGroupPath()); final Object component = selectedUrl.getComponent(); if (component instanceof Group) { selectedGroupPath.add(((Group) component).getName()); for (ActionUrl action : mySelectedSchema.getActions()) { final ArrayList<String> groupPath = action.getGroupPath(); final int idx = Collections.indexOfSubList(groupPath, selectedGroupPath); if (idx > -1) { actions.add(action); } } } } } return actions; }
@Nullable private Object[] getSelectedTreeElements() { final JTree tree = getTree(); return tree != null ? convertPathsToTreeElements(tree.getSelectionPaths()) : null; }
private Object[] getSelectedElements() { final JTree tree = getTree(); return tree != null ? convertPathsToValues(tree.getSelectionPaths()) : ArrayUtil.EMPTY_OBJECT_ARRAY; }