Search in sources :

Example 11 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener 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 12 with TreeExpansionListener

use of javax.swing.event.TreeExpansionListener 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

TreeExpansionListener (javax.swing.event.TreeExpansionListener)12 TreeExpansionEvent (javax.swing.event.TreeExpansionEvent)11 TreePath (javax.swing.tree.TreePath)8 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)6 TreeNode (javax.swing.tree.TreeNode)5 JPanel (javax.swing.JPanel)4 JScrollPane (javax.swing.JScrollPane)4 BorderLayout (java.awt.BorderLayout)3 MouseEvent (java.awt.event.MouseEvent)3 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)3 Component (java.awt.Component)2 Dimension (java.awt.Dimension)2 Comparator (java.util.Comparator)2 Enumeration (java.util.Enumeration)2 JLabel (javax.swing.JLabel)2 JTree (javax.swing.JTree)2 TreeSelectionEvent (javax.swing.event.TreeSelectionEvent)2 TreeSelectionListener (javax.swing.event.TreeSelectionListener)2 ToolTipTreeNode (CCDD.CcddClassesComponent.ToolTipTreeNode)1 ColumnTreeBuilder (com.android.tools.adtui.common.ColumnTreeBuilder)1