private static void printImpl( JTree tree, Object root, Collection<String> strings, int level, boolean withSelection, @Nullable Condition<String> nodePrintCondition) { DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) root; final Object userObject = defaultMutableTreeNode.getUserObject(); String nodeText; if (userObject != null) { nodeText = toString(userObject, null); } else { nodeText = "null"; } if (nodePrintCondition != null && !nodePrintCondition.value(nodeText)) return; final StringBuilder buff = new StringBuilder(); StringUtil.repeatSymbol(buff, ' ', level); final boolean expanded = tree.isExpanded(new TreePath(defaultMutableTreeNode.getPath())); if (!defaultMutableTreeNode.isLeaf()) { buff.append(expanded ? "-" : "+"); } final boolean selected = tree.getSelectionModel().isPathSelected(new TreePath(defaultMutableTreeNode.getPath())); if (withSelection && selected) { buff.append("["); } buff.append(nodeText); if (withSelection && selected) { buff.append("]"); } strings.add(buff.toString()); int childCount = tree.getModel().getChildCount(root); if (expanded) { for (int i = 0; i < childCount; i++) { printImpl( tree, tree.getModel().getChild(root, i), strings, level + 1, withSelection, nodePrintCondition); } } }
public static List<TreePath> collectExpandedPaths(final JTree tree, TreePath path) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); if (!tree.isExpanded(path)) return result; final Object lastPathComponent = path.getLastPathComponent(); final TreeModel model = tree.getModel(); if (model.isLeaf(lastPathComponent)) { result.add(path); } else { boolean pathWasAdded = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { final TreePath childPath = path.pathByAddingChild(model.getChild(lastPathComponent, i)); if (model.isLeaf(lastPathComponent)) { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } else if (tree.isExpanded(childPath)) { result.addAll(collectExpandedPaths(tree, childPath)); } else { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } } } return result; }
public static List<TreePath> collectExpandedPaths(@NotNull final JTree tree) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); final Object root = tree.getModel().getRoot(); final TreePath rootPath = new TreePath(root); result.addAll(collectExpandedPaths(tree, rootPath)); return result; }
public void reset() { mySelectedSchema = new CustomActionsSchema(); mySelectedSchema.copyFrom(CustomActionsSchema.getInstance()); patchActionsTreeCorrespondingToSchema( (DefaultMutableTreeNode) myActionsTree.getModel().getRoot()); myRestoreAllDefaultButton.setEnabled(mySelectedSchema.isModified(new CustomActionsSchema())); }
public static Collection<String> printAsList( JTree tree, boolean withSelection, @Nullable Condition<String> nodePrintCondition) { Collection<String> strings = new ArrayList<String>(); Object root = tree.getModel().getRoot(); printImpl(tree, root, strings, 0, withSelection, nodePrintCondition); return strings; }
private void updateImmediately() { if (myProject.isDisposed()) return; checkNodeValidity((DefaultMutableTreeNode) myTree.getModel().getRoot()); if (myUsagePreviewPanel != null) { myUsagePreviewPanel.updateLayout(getSelectedUsageInfos()); } }
/** * @param tree JTree to collect expanded paths from. * @param paths output parameter. */ public static void collectExpandedPaths( @NotNull final JTree tree, @NotNull final List<TreePath> paths) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); LOG.assertTrue(root != null); collectExpandedPathsImpl(tree, paths, new TreePath(root)); }
public static ActionCallback selectFirstNode(final JTree tree) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); TreePath selectionPath = new TreePath(root); if (!tree.isRootVisible() && model.getChildCount(root) > 0) selectionPath = selectionPath.pathByAddingChild(model.getChild(root, 0)); return selectPath(tree, selectionPath); }
// If expanded, return only paths of nodes that are expanded. public TreePath[] getPaths(JTree tree, boolean expanded) { TreeNode root = (TreeNode) tree.getModel().getRoot(); List list = new ArrayList(); // Traverse tree from root adding treepaths for all nodes to list getPaths(tree, new TreePath(root), expanded, list); // Convert list to array return (TreePath[]) list.toArray(new TreePath[list.size()]); }
@Override public void adoptElement(SceneElement elem) { if (!(elem instanceof NenyaImageSceneElement || elem instanceof NenyaTileSceneElement || elem instanceof NenyaComponentSceneElement)) { enableEditor(false); return; } DefaultComboBoxModel dcm = (DefaultComboBoxModel) itemList.getModel(); // Important: Work on a copy, not on the original. Otherwise we mess up the undomanager sceneElement = elem.copy(); if ((sceneElement instanceof NenyaImageSceneElement) && !locked) { dcm.removeAllElements(); String[] tmp = ((NenyaImageSceneElement) sceneElement).getPath(); dcm.addElement(tmp[tmp.length - 1]); } if ((sceneElement instanceof NenyaTileSceneElement) && !locked) { dcm.removeAllElements(); dcm.addElement(((NenyaTileSceneElement) sceneElement).getTileName()); } if ((sceneElement instanceof NenyaComponentSceneElement) && !locked) { dcm.removeAllElements(); NenyaComponentItem[] ni = ((NenyaComponentSceneElement) sceneElement).getComponents(); for (NenyaComponentItem element : ni) { dcm.addElement(element); } } try { ClassedItem[] cols = null; if (elem instanceof NenyaTileSceneElement) cols = ((NenyaTileSceneElement) elem).getColorList(); if (elem instanceof NenyaImageSceneElement) cols = ((NenyaImageSceneElement) elem).getColorList(); if (elem instanceof NenyaComponentSceneElement) { NenyaComponentItem nci = (NenyaComponentItem) dcm.getSelectedItem(); cols = nci.getColorList(); } Vector<TreePath> collect = new Vector<TreePath>(); TreeNode root = (TreeNode) colors.getModel().getRoot(); for (ClassedItem col : cols) { String[] tmp = {root.toString(), col.getClassName(), col.getItemName()}; collect.add(TreeUtil.findPath(root, tmp)); } TreePath[] path = collect.toArray(new TreePath[0]); colors.getSelectionModel().setSelectionPaths(path); } catch (Exception e) { // Either the tree is filtered away or the selected item is not colorized. } enableEditor(true); itemList.setEnabled(elem instanceof NenyaComponentSceneElement); }
private void patchActionsTreeCorrespondingToSchema(DefaultMutableTreeNode root) { root.removeAllChildren(); if (mySelectedSchema != null) { mySelectedSchema.fillActionGroups(root); for (final ActionUrl actionUrl : mySelectedSchema.getActions()) { ActionUrl.changePathInActionsTree(myActionsTree, actionUrl); } } ((DefaultTreeModel) myActionsTree.getModel()).reload(); }
public UpdaterTreeState(AbstractTreeUi ui, boolean isEmpty) { myUi = ui; if (!isEmpty) { final JTree tree = myUi.getTree(); putAll(addPaths(tree.getSelectionPaths()), myToSelect); putAll( addPaths(tree.getExpandedDescendants(new TreePath(tree.getModel().getRoot()))), myToExpand); } }
public static void moveSelectedRow(final JTree tree, final int direction) { final TreePath selectionPath = tree.getSelectionPath(); final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) selectionPath.getLastPathComponent(); final DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeNode.getParent(); final int idx = parent.getIndex(treeNode); parent.remove(treeNode); parent.insert(treeNode, idx + direction); ((DefaultTreeModel) tree.getModel()).reload(parent); selectNode(tree, treeNode); }
public static void expandAll(final JTree tree) { tree.expandPath(new TreePath(tree.getModel().getRoot())); int oldRowCount = 0; do { int rowCount = tree.getRowCount(); if (rowCount == oldRowCount) break; oldRowCount = rowCount; for (int i = 0; i < rowCount; i++) { tree.expandRow(i); } } while (true); }
@Nullable private DefaultMutableTreeNode findNodeOnToolbar(String actionId) { final TreeNode toolbar = ((DefaultMutableTreeNode) myActionsTree.getModel().getRoot()).getChildAt(1); for (int i = 0; i < toolbar.getChildCount(); i++) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) toolbar.getChildAt(i); final String childId = getActionId(child); if (childId != null && childId.equals(actionId)) { return child; } } return null; }
private void restoreUsageExpandState(final Collection<UsageState> states) { // always expand the last level group final DefaultMutableTreeNode root = (DefaultMutableTreeNode) myTree.getModel().getRoot(); for (int i = root.getChildCount() - 1; i >= 0; i--) { final DefaultMutableTreeNode child = (DefaultMutableTreeNode) root.getChildAt(i); if (child instanceof GroupNode) { final TreePath treePath = new TreePath(child.getPath()); myTree.expandPath(treePath); } } myTree.getSelectionModel().clearSelection(); for (final UsageState usageState : states) { usageState.restore(); } }
private void setStructureViewSelection(@NotNull final String propertyName) { if (myStructureViewComponent.isDisposed()) { return; } JTree tree = myStructureViewComponent.getTree(); if (tree == null) { return; } Object root = tree.getModel().getRoot(); if (AbstractTreeUi.isLoadingChildrenFor(root)) { mySelectionChangeAlarm.cancelAllRequests(); mySelectionChangeAlarm.addRequest( new Runnable() { @Override public void run() { mySelectionChangeAlarm.cancelAllRequests(); setStructureViewSelection(propertyName); } }, 500); return; } Stack<TreeElement> toCheck = ContainerUtilRt.newStack(); toCheck.push(myStructureViewComponent.getTreeModel().getRoot()); while (!toCheck.isEmpty()) { TreeElement element = toCheck.pop(); PsiElement value = element instanceof ResourceBundlePropertyStructureViewElement ? ((ResourceBundlePropertyStructureViewElement) element).getValue() : null; if (value instanceof IProperty && propertyName.equals(((IProperty) value).getUnescapedKey())) { myStructureViewComponent.select(value, true); selectionChanged(); return; } else { for (TreeElement treeElement : element.getChildren()) { toCheck.push(treeElement); } } } }
// Public Methods public void appendNextTreeGeneration(Vector generation) { DefaultMutableTreeNode nextGeneration = generationNodeBuilder(generation); generations.add(nextGeneration); // If Generations contains leaf nodes (generated objects) // Enabled Save All Menu Item if (generations.getLeafCount() > 0) miSaveAll.setEnabled(true); else miSaveAll.setEnabled(false); // Update JTree View // affected nodes needing updating int[] nodeRangeToUpdate = {generations.getIndex(nextGeneration)}; ((DefaultTreeModel) tree.getModel()).nodesWereInserted(generations, nodeRangeToUpdate); // Expand Parent after first child node is displayed if (generationNumber == 1) tree.expandRow(0); ++generationNumber; }
private void rulesChanged() { ApplicationManager.getApplication().assertIsDispatchThread(); final ArrayList<UsageState> states = new ArrayList<UsageState>(); captureUsagesExpandState(new TreePath(myTree.getModel().getRoot()), states); final List<Usage> allUsages = new ArrayList<Usage>(myUsageNodes.keySet()); Collections.sort(allUsages, USAGE_COMPARATOR); final Set<Usage> excludedUsages = getExcludedUsages(); reset(); myBuilder.setGroupingRules(getActiveGroupingRules(myProject)); myBuilder.setFilteringRules(getActiveFilteringRules(myProject)); ApplicationManager.getApplication() .runReadAction( new Runnable() { @Override public void run() { for (Usage usage : allUsages) { if (!usage.isValid()) { continue; } if (usage instanceof MergeableUsage) { ((MergeableUsage) usage).reset(); } appendUsage(usage); } } }); excludeUsages(excludedUsages.toArray(new Usage[excludedUsages.size()])); if (myCentralPanel != null) { setupCentralPanel(); } SwingUtilities.invokeLater( new Runnable() { @Override public void run() { if (isDisposed) return; restoreUsageExpandState(states); updateImmediately(); } }); }
public static void collapseAll(final JTree tree, final int keepSelectionLevel) { final TreePath leadSelectionPath = tree.getLeadSelectionPath(); // Collapse all int row = tree.getRowCount() - 1; while (row >= 0) { tree.collapseRow(row); row--; } final DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot(); tree.expandPath(new TreePath(root)); if (leadSelectionPath != null) { final Object[] path = leadSelectionPath.getPath(); final Object[] pathToSelect = new Object [path.length > keepSelectionLevel && keepSelectionLevel >= 0 ? keepSelectionLevel : path.length]; System.arraycopy(path, 0, pathToSelect, 0, pathToSelect.length); if (pathToSelect.length == 0) return; selectPath(tree, new TreePath(pathToSelect)); } }
private static boolean collectExpandedPathsImpl( final JTree tree, final Collection<TreePath> paths, final TreePath path) { final TreeModel model = tree.getModel(); final Object lastPathComponent = path.getLastPathComponent(); if (model.isLeaf(lastPathComponent)) { return false; } boolean hasExpandedChildren = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { hasExpandedChildren |= collectExpandedPathsImpl( tree, paths, path.pathByAddingChild(model.getChild(lastPathComponent, i))); } if (!hasExpandedChildren) { paths.add(path); return true; } else { return false; } }
protected void doOKAction() { final ActionManager actionManager = ActionManager.getInstance(); TreeUtil.traverseDepth( (TreeNode) myTree.getModel().getRoot(), new TreeUtil.Traverse() { public boolean accept(Object node) { if (node instanceof DefaultMutableTreeNode) { final DefaultMutableTreeNode mutableNode = (DefaultMutableTreeNode) node; final Object userObject = mutableNode.getUserObject(); if (userObject instanceof Pair) { String actionId = (String) ((Pair) userObject).first; final AnAction action = actionManager.getAction(actionId); Icon icon = (Icon) ((Pair) userObject).second; action.getTemplatePresentation().setIcon(icon); action.setDefaultIcon(icon == null); editToolbarIcon(actionId, mutableNode); } } return true; } }); super.doOKAction(); setCustomizationSchemaForCurrentProjects(); }
@Nullable public static Range<Integer> getExpandControlRange(final JTree aTree, final TreePath path) { TreeModel treeModel = aTree.getModel(); final BasicTreeUI basicTreeUI = (BasicTreeUI) aTree.getUI(); Icon expandedIcon = basicTreeUI.getExpandedIcon(); Range<Integer> box = null; if (path != null && !treeModel.isLeaf(path.getLastPathComponent())) { int boxWidth; Insets i = aTree.getInsets(); if (expandedIcon != null) { boxWidth = expandedIcon.getIconWidth(); } else { boxWidth = 8; } int boxLeftX = i != null ? i.left : 0; boolean leftToRight = aTree.getComponentOrientation().isLeftToRight(); int depthOffset = getDepthOffset(aTree); int totalChildIndent = basicTreeUI.getLeftChildIndent() + basicTreeUI.getRightChildIndent(); if (leftToRight) { boxLeftX += ((path.getPathCount() + depthOffset - 2) * totalChildIndent + basicTreeUI.getLeftChildIndent()) - boxWidth / 2; } int boxRightX = boxLeftX + boxWidth; box = new Range<Integer>(boxLeftX, boxRightX); } return box; }
public DefaultMutableTreeNode getModelRoot() { return (DefaultMutableTreeNode) myTree.getModel().getRoot(); }
/** * Removes last component in the current selection path. * * @param tree to remove selected node from. */ public static void removeSelected(@NotNull final JTree tree) { final TreePath selectionPath = tree.getSelectionPath(); if (selectionPath == null) return; removeLastPathComponent((DefaultTreeModel) tree.getModel(), selectionPath) .restoreSelection(tree); }
public static void removeLastPathComponent( @NotNull final JTree tree, @NotNull final TreePath pathToBeRemoved) { removeLastPathComponent((DefaultTreeModel) tree.getModel(), pathToBeRemoved) .restoreSelection(tree); }
/** * Constructs a X509 certificate panel. * * @param certificates <tt>X509Certificate</tt> objects */ public X509CertificatePanel(Certificate[] certificates) { setLayout(new BorderLayout(5, 5)); // Certificate chain list TransparentPanel topPanel = new TransparentPanel(new BorderLayout()); topPanel.add( new JLabel( "<html><body><b>" + R.getI18NString("service.gui.CERT_INFO_CHAIN") + "</b></body></html>"), BorderLayout.NORTH); DefaultMutableTreeNode top = new DefaultMutableTreeNode(); DefaultMutableTreeNode previous = top; for (int i = certificates.length - 1; i >= 0; i--) { Certificate cert = certificates[i]; DefaultMutableTreeNode next = new DefaultMutableTreeNode(cert); previous.add(next); previous = next; } JTree tree = new JTree(top); tree.setBorder(new BevelBorder(BevelBorder.LOWERED)); tree.setRootVisible(false); tree.setExpandsSelectedPaths(true); tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); tree.setCellRenderer( new DefaultTreeCellRenderer() { @Override public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { JLabel component = (JLabel) super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus); if (value instanceof DefaultMutableTreeNode) { Object o = ((DefaultMutableTreeNode) value).getUserObject(); if (o instanceof X509Certificate) { component.setText(getSimplifiedName((X509Certificate) o)); } else { // We don't know how to represent this certificate type, // let's use the first 20 characters String text = o.toString(); if (text.length() > 20) { text = text.substring(0, 20); } component.setText(text); } } return component; } }); tree.getSelectionModel() .addTreeSelectionListener( new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { valueChangedPerformed(e); } }); tree.setSelectionPath( new TreePath((((DefaultTreeModel) tree.getModel()).getPathToRoot(previous)))); topPanel.add(tree, BorderLayout.CENTER); add(topPanel, BorderLayout.NORTH); // Certificate details pane Caret caret = infoTextPane.getCaret(); if (caret instanceof DefaultCaret) { ((DefaultCaret) caret).setUpdatePolicy(DefaultCaret.NEVER_UPDATE); } /* * Make JEditorPane respect our default font because we will be using it * to just display text. */ infoTextPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true); infoTextPane.setOpaque(false); infoTextPane.setEditable(false); infoTextPane.setContentType("text/html"); infoTextPane.setText(toString(certificates[0])); final JScrollPane certScroll = new JScrollPane(infoTextPane); certScroll.setPreferredSize(new Dimension(300, 500)); add(certScroll, BorderLayout.CENTER); }
/** * Expands n levels of the tree counting from the root * * @param tree to expand nodes of * @param levels depths of the expantion */ public static void expand(JTree tree, int levels) { expand(tree, new TreePath(tree.getModel().getRoot()), levels); }