Search in sources :

Example 16 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project intellij-community by JetBrains.

the class SmartExpander method installOn.

public static void installOn(final JTree tree) {
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillCollapse(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            Enumeration children = ((TreeNode) path.getLastPathComponent()).children();
            while (children.hasMoreElements()) {
                TreePath childPath = path.pathByAddingChild(children.nextElement());
                if (tree.isExpanded(childPath)) {
                    tree.collapsePath(childPath);
                }
            }
        }

        @Override
        public void treeWillExpand(TreeExpansionEvent event) {
        }
    });
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        }

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            TreeNode lastPathComponent = (TreeNode) path.getLastPathComponent();
            if (lastPathComponent.getChildCount() == 1) {
                TreePath firstChildPath = path.pathByAddingChild(lastPathComponent.getChildAt(0));
                tree.expandPath(firstChildPath);
            }
        }
    });
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreeExpansionListener(javax.swing.event.TreeExpansionListener) Enumeration(java.util.Enumeration) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 17 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project intellij-community by JetBrains.

the class UsageViewImpl method initTree.

private void initTree() {
    ApplicationManager.getApplication().assertIsDispatchThread();
    myTree.setShowsRootHandles(true);
    SmartExpander.installOn(myTree);
    TreeUtil.installActions(myTree);
    EditSourceOnDoubleClickHandler.install(myTree);
    myTree.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                TreePath leadSelectionPath = myTree.getLeadSelectionPath();
                if (leadSelectionPath == null)
                    return;
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) leadSelectionPath.getLastPathComponent();
                if (node instanceof UsageNode) {
                    final Usage usage = ((UsageNode) node).getUsage();
                    usage.navigate(false);
                    usage.highlightInEditor();
                } else if (node.isLeaf()) {
                    Navigatable navigatable = getNavigatableForNode(node);
                    if (navigatable != null && navigatable.canNavigate()) {
                        navigatable.navigate(false);
                    }
                }
            }
        }
    });
    TreeUtil.selectFirstNode(myTree);
    PopupHandler.installPopupHandler(myTree, IdeActions.GROUP_USAGE_VIEW_POPUP, ActionPlaces.USAGE_VIEW_POPUP);
    myTree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            clearRendererCache();
            TreePath path = event.getPath();
            Object component = path.getLastPathComponent();
            if (component instanceof Node) {
                Node node = (Node) component;
                if (!expandingAll && node.needsUpdate()) {
                    checkNodeValidity(node, path);
                }
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
            clearRendererCache();
        }
    });
    TreeUIHelper.getInstance().installTreeSpeedSearch(myTree, o -> {
        Object value = o.getLastPathComponent();
        TreeCellRenderer renderer = myTree.getCellRenderer();
        if (renderer instanceof UsageViewTreeCellRenderer) {
            UsageViewTreeCellRenderer coloredRenderer = (UsageViewTreeCellRenderer) renderer;
            return coloredRenderer.getPlainTextForNode(value);
        }
        return value == null ? null : value.toString();
    }, true);
}
Also used : TreeExpansionListener(javax.swing.event.TreeExpansionListener) Navigatable(com.intellij.pom.Navigatable) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 18 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project CCDD by nasa.

the class CcddTableTreeHandler method createTreePanel.

/**
 ********************************************************************************************
 * Create a table tree panel. The table tree is placed in a scroll pane. A check box is added
 * that allows tree expansion/collapse
 *
 * @param label
 *            table tree title
 *
 * @param selectionMode
 *            tree item selection mode (single versus multiple)
 *
 * @param parent
 *            GUI component calling this method
 *
 * @return JPanel containing the table tree components
 ********************************************************************************************
 */
protected JPanel createTreePanel(String label, int selectionMode, final Component parent) {
    // Create an empty border
    Border emptyBorder = BorderFactory.createEmptyBorder();
    // Set the initial layout manager characteristics
    GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing() / 2, 0), 0, 0);
    // Set the table tree selection mode
    getSelectionModel().setSelectionMode(selectionMode);
    // Create a panel to contain the table tree
    JPanel treePnl = new JPanel(new GridBagLayout());
    treePnl.setBorder(emptyBorder);
    // Check if a label is provided
    if (label != null && !label.isEmpty()) {
        // Create the tree labels
        JLabel treeLbl = new JLabel(label);
        treeLbl.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        treeLbl.setForeground(ModifiableColorInfo.SPECIAL_LABEL_TEXT.getColor());
        treePnl.add(treeLbl, gbc);
        gbc.gridy++;
    }
    // Create the tree scroll pane
    JScrollPane treeScroll = new JScrollPane(this);
    treeScroll.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.GRAY), BorderFactory.createEmptyBorder(ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing(), ModifiableSpacingInfo.INPUT_FIELD_PADDING.getSpacing())));
    // Add the tree to the panel
    gbc.weighty = 1.0;
    treePnl.add(treeScroll, gbc);
    // Set the table tree font and number of rows to display
    setFont(ModifiableFontInfo.TREE_NODE.getFont());
    setVisibleRowCount(10);
    // Add a listener for changes to the table tree
    addTreeSelectionListener(new TreeSelectionListener() {

        /**
         ************************************************************************************
         * Handle a change to the table tree selection
         ************************************************************************************
         */
        @Override
        public void valueChanged(TreeSelectionEvent lse) {
            // tree selection value changes that should not be processed
            if (!isBuilding) {
                // Update the groups based on the tables selected
                updateTableSelection();
            }
        }
    });
    // Add a listener for table tree expand and collapse events
    addTreeExpansionListener(new TreeExpansionListener() {

        /**
         ************************************************************************************
         * Handle an expansion of the table tree
         ************************************************************************************
         */
        @Override
        public void treeExpanded(TreeExpansionEvent tee) {
            // Update the table selection based on the selected group
            updateGroupSelection();
        }

        /**
         ************************************************************************************
         * Handle a collapse of the table tree
         ************************************************************************************
         */
        @Override
        public void treeCollapsed(TreeExpansionEvent tee) {
            // Update the table selection based on the selected group
            updateGroupSelection();
        }
    });
    // Create a tree expansion check box
    expandChkBx = new JCheckBox("Expand all");
    expandChkBx.setBorder(emptyBorder);
    expandChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
    expandChkBx.setSelected(false);
    // Check if this is the last component to add
    if (!showGroupFilter && !showTypeFilter && !addHiddenCheckBox) {
        gbc.insets.bottom = 0;
    }
    gbc.insets.top = ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing() / 2;
    gbc.weighty = 0.0;
    gbc.gridy++;
    treePnl.add(expandChkBx, gbc);
    // Create a listener for changes in selection of the tree expansion check box
    expandChkBx.addActionListener(new ActionListener() {

        /**
         ************************************************************************************
         * Handle a change to the tree expansion check box selection
         ************************************************************************************
         */
        @Override
        public void actionPerformed(ActionEvent ae) {
            setTreeExpansion(expandChkBx.isSelected());
        }
    });
    // Check if instance tables are displayed in the tree
    if (treeType != TableTreeType.PROTOTYPE_TABLES) {
        // Create a hide data type check box
        final JCheckBox hideTypeChkBx = new JCheckBox("Hide data type");
        hideTypeChkBx.setBorder(emptyBorder);
        hideTypeChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        hideTypeChkBx.setSelected(false);
        // Check if this is the last component to add
        if (!showGroupFilter && !showTypeFilter && !addHiddenCheckBox) {
            gbc.insets.bottom = 0;
        }
        gbc.insets.top = ModifiableSpacingInfo.LABEL_VERTICAL_SPACING.getSpacing() / 2;
        gbc.weighty = 0.0;
        gbc.gridy++;
        treePnl.add(hideTypeChkBx, gbc);
        // Create a listener for changes in selection of the hide data type check box
        hideTypeChkBx.addActionListener(new ActionListener() {

            /**
             ********************************************************************************
             * Handle a change to the hide data type check box selection
             ********************************************************************************
             */
            @Override
            public void actionPerformed(ActionEvent ae) {
                setEnableDataType(!hideTypeChkBx.isSelected());
                // Store the tree's current expansion state
                String expState = getExpansionState();
                // Force the root node to draw with the node additions
                ((DefaultTreeModel) treeModel).nodeStructureChanged(root);
                setExpansionState(expState);
            }
        });
    }
    // Create the filtering node prefix storage and check boxes
    final List<String> prefixes = new ArrayList<String>();
    final JCheckBox groupFilterChkBx = new JCheckBox("Filter by group");
    final JCheckBox typeFilterChkBx = new JCheckBox("Filter by type");
    // table/variable divisions (e.g., 'Prototype', 'Parents & Children')
    for (int index = 0; index < root.getChildCount(); index++) {
        // Add the child node name with its path to the prefix list
        prefixes.add("[" + root.getUserObject() + ", " + ((ToolTipTreeNode) root.getChildAt(index)).getUserObject());
    }
    // Check if the group check box is valid for this tree type
    if (showGroupFilter) {
        // Create a group filter check box
        groupFilterChkBx.setBorder(emptyBorder);
        groupFilterChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        groupFilterChkBx.setSelected(false);
        groupFilterChkBx.setEnabled(!groupHandler.getGroupInformation().isEmpty());
        // Check if this is the last component to add
        if (!showTypeFilter && !addHiddenCheckBox) {
            gbc.insets.bottom = 0;
        }
        gbc.gridy++;
        treePnl.add(groupFilterChkBx, gbc);
        // Create a listener for changes in selection of the group filter check box
        groupFilterChkBx.addActionListener(new ActionListener() {

            /**
             ********************************************************************************
             * Handle a change to the group filter check box selection
             ********************************************************************************
             */
            @Override
            public void actionPerformed(ActionEvent ae) {
                // Set the filter by group flag based on the check box status
                isByGroup = groupFilterChkBx.isSelected();
                // Store the tree's current expansion state
                String expState = getExpansionState();
                // Rebuild the tree based on the filter selection
                buildTableTree(expandChkBx.isSelected(), rateName, rateFilter, parent);
                // Adjust the expansion state to account for the change in filtering, then
                // restore the expansion state
                expState = adjustExpansionState(expState, groupFilterChkBx.isSelected(), true, typeFilterChkBx.isSelected(), false, false, prefixes, groupHandler, tableTypeHandler);
                setExpansionState(expState);
            }
        });
    }
    // Check if the type check box is valid for this tree type
    if (showTypeFilter) {
        // Create a type filter check box
        typeFilterChkBx.setBorder(emptyBorder);
        typeFilterChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        typeFilterChkBx.setSelected(false);
        // Check if this is the last component to add
        if (!addHiddenCheckBox) {
            gbc.insets.bottom = 0;
        }
        gbc.gridy++;
        treePnl.add(typeFilterChkBx, gbc);
        // Create a listener for changes in selection of the type filter check box
        typeFilterChkBx.addActionListener(new ActionListener() {

            /**
             ********************************************************************************
             * Handle a change to the type filter check box selection
             ********************************************************************************
             */
            @Override
            public void actionPerformed(ActionEvent ae) {
                // Set the filter by type flag based on the check box status
                isByType = typeFilterChkBx.isSelected();
                // Store the tree's current expansion state
                String expState = getExpansionState();
                // Rebuild the tree based on the filter selection
                buildTableTree(expandChkBx.isSelected(), rateName, rateFilter, parent);
                // Adjust the expansion state to account for the change in filtering, then
                // restore the expansion state
                expState = adjustExpansionState(expState, groupFilterChkBx.isSelected(), false, typeFilterChkBx.isSelected(), true, false, prefixes, groupHandler, tableTypeHandler);
                setExpansionState(expState);
            }
        });
    }
    // tree panel. Check if the flag is set to add this check box
    if (addHiddenCheckBox) {
        // Create the hidden check box. To prevent display of the check box components an empty
        // panel is placed over it
        JPanel hiddenPnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
        hiddenPnl.setBorder(emptyBorder);
        JCheckBox hiddenChkBx = new JCheckBox(" ");
        hiddenChkBx.setBorder(emptyBorder);
        gbc.insets.bottom = 0;
        gbc.gridy++;
        treePnl.add(hiddenPnl, gbc);
        hiddenChkBx.setFont(ModifiableFontInfo.LABEL_BOLD.getFont());
        hiddenChkBx.setFocusable(false);
        hiddenChkBx.setDisabledIcon(null);
        hiddenChkBx.setEnabled(false);
        treePnl.add(hiddenChkBx, gbc);
    }
    return treePnl;
}
Also used : JScrollPane(javax.swing.JScrollPane) JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) TreeExpansionListener(javax.swing.event.TreeExpansionListener) Insets(java.awt.Insets) FlowLayout(java.awt.FlowLayout) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) ArrayList(java.util.ArrayList) JLabel(javax.swing.JLabel) TreeSelectionListener(javax.swing.event.TreeSelectionListener) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent) Border(javax.swing.border.Border) BevelBorder(javax.swing.border.BevelBorder) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) ToolTipTreeNode(CCDD.CcddClassesComponent.ToolTipTreeNode)

Example 19 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project cayenne by apache.

the class ProjectTreeView method initTreeExpandListener.

private void initTreeExpandListener() {
    treeWillExpandListener = new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(TreeExpansionEvent e) {
            TreePath path = e.getPath();
            if (!isPathSelected(path) && !isSelectionEmpty()) {
                setSelectionPath(path);
            }
        }

        @Override
        public void treeWillCollapse(TreeExpansionEvent e) {
            TreePath path = e.getPath();
            if (!isPathSelected(path) && !isSelectionEmpty()) {
                setSelectionPath(path);
            }
        }
    };
    addTreeWillExpandListener(treeWillExpandListener);
}
Also used : TreeWillExpandListener(javax.swing.event.TreeWillExpandListener) TreePath(javax.swing.tree.TreePath) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Example 20 with TreeExpansionEvent

use of javax.swing.event.TreeExpansionEvent in project jadx by skylot.

the class QuarkReportPanel method buildTree.

private JTree buildTree() {
    JTree tree = new JTree(treeRoot);
    tree.setLayout(new BorderLayout());
    tree.setBorder(BorderFactory.createEmptyBorder());
    tree.setShowsRootHandles(false);
    tree.setScrollsOnExpand(false);
    tree.setSelectionModel(null);
    tree.setCellRenderer(cellRenderer);
    tree.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent event) {
            if (SwingUtilities.isLeftMouseButton(event)) {
                Object node = getNodeUnderMouse(tree, event);
                if (node instanceof MethodTreeNode) {
                    JMethod method = ((MethodTreeNode) node).getJMethod();
                    BackgroundExecutor executor = tabbedPane.getMainWindow().getBackgroundExecutor();
                    executor.execute("Decompiling class", () -> tabbedPane.codeJump(method), // TODO: fix bug with incorrect jump on just decompiled code
                    status -> tabbedPane.codeJump(method));
                }
            }
        }
    });
    tree.addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(TreeExpansionEvent event) {
            TreePath path = event.getPath();
            Object leaf = path.getLastPathComponent();
            if (leaf instanceof CrimeTreeNode) {
                CrimeTreeNode node = (CrimeTreeNode) leaf;
                Enumeration<TreeNode> children = node.children();
                while (children.hasMoreElements()) {
                    TreeNode child = children.nextElement();
                    tree.expandPath(path.pathByAddingChild(child));
                }
            }
        }

        @Override
        public void treeCollapsed(TreeExpansionEvent event) {
        }
    });
    return tree;
}
Also used : TreeExpansionEvent(javax.swing.event.TreeExpansionEvent) Enumeration(java.util.Enumeration) TreeNode(javax.swing.tree.TreeNode) LoggerFactory(org.slf4j.LoggerFactory) TabbedPane(jadx.gui.ui.TabbedPane) JsonElement(com.google.gson.JsonElement) SwingUtilities(javax.swing.SwingUtilities) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) JEditorPane(javax.swing.JEditorPane) BorderLayout(java.awt.BorderLayout) ScrollPaneConstants(javax.swing.ScrollPaneConstants) JMethod(jadx.gui.treemodel.JMethod) IdentityHashMap(java.util.IdentityHashMap) Logger(org.slf4j.Logger) TreePath(javax.swing.tree.TreePath) Font(java.awt.Font) BorderFactory(javax.swing.BorderFactory) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ContentPanel(jadx.gui.ui.panel.ContentPanel) JTree(javax.swing.JTree) JavaClass(jadx.api.JavaClass) Component(java.awt.Component) MouseEvent(java.awt.event.MouseEvent) MutableTreeNode(javax.swing.tree.MutableTreeNode) JadxWrapper(jadx.gui.JadxWrapper) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JScrollPane(javax.swing.JScrollPane) BackgroundExecutor(jadx.gui.jobs.BackgroundExecutor) List(java.util.List) JsonArray(com.google.gson.JsonArray) TreeCellRenderer(javax.swing.tree.TreeCellRenderer) Strings(com.beust.jcommander.Strings) JLabel(javax.swing.JLabel) JNodeCache(jadx.gui.utils.JNodeCache) Comparator(java.util.Comparator) JavaMethod(jadx.api.JavaMethod) MainWindow(jadx.gui.ui.MainWindow) TreeExpansionListener(javax.swing.event.TreeExpansionListener) JPanel(javax.swing.JPanel) Utils(jadx.core.utils.Utils) BackgroundExecutor(jadx.gui.jobs.BackgroundExecutor) TreeExpansionListener(javax.swing.event.TreeExpansionListener) MouseEvent(java.awt.event.MouseEvent) Enumeration(java.util.Enumeration) MouseAdapter(java.awt.event.MouseAdapter) JTree(javax.swing.JTree) BorderLayout(java.awt.BorderLayout) TreePath(javax.swing.tree.TreePath) TreeNode(javax.swing.tree.TreeNode) MutableTreeNode(javax.swing.tree.MutableTreeNode) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) JMethod(jadx.gui.treemodel.JMethod) TreeExpansionEvent(javax.swing.event.TreeExpansionEvent)

Aggregations

TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)20 TreePath (javax.swing.tree.TreePath)14 TreeExpansionListener (javax.swing.event.TreeExpansionListener)11 TreeWillExpandListener (javax.swing.event.TreeWillExpandListener)10 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)9 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)7 MouseEvent (java.awt.event.MouseEvent)5 JPanel (javax.swing.JPanel)5 JScrollPane (javax.swing.JScrollPane)5 ExpandVetoException (javax.swing.tree.ExpandVetoException)5 TreeNode (javax.swing.tree.TreeNode)5 BorderLayout (java.awt.BorderLayout)4 JTree (javax.swing.JTree)4 Component (java.awt.Component)3 Dimension (java.awt.Dimension)3 MouseAdapter (java.awt.event.MouseAdapter)3 DefaultTreeCellRenderer (javax.swing.tree.DefaultTreeCellRenderer)3 Module (com.intellij.openapi.module.Module)2 Navigatable (com.intellij.pom.Navigatable)2 Tree (com.intellij.ui.treeStructure.Tree)2