Github user FSchumacher commented on a diff in the pull request:

    https://github.com/apache/jmeter/pull/300#discussion_r130236022
  
    --- Diff: src/core/org/apache/jmeter/gui/TreeState.java ---
    @@ -0,0 +1,84 @@
    +package org.apache.jmeter.gui;
    +
    +import java.util.ArrayList;
    +import java.util.List;
    +import javax.swing.JTree;
    +
    +public interface TreeState {
    +
    +    /**
    +     * Restore tree expanded and selected state
    +     *
    +     * @param guiInstance GuiPackage to be used
    +     */
    +    void restore(GuiPackage guiInstance);
    +
    +    final static TreeState NOTHING = new TreeState() {
    +        @Override
    +        public void restore(GuiPackage guiInstance) {
    +            //nothing
    +        }
    +    };
    +
    +    /**
    +     * Save tree expanded and selected state
    +     *
    +     * @param guiPackage {@link GuiPackage} to be used
    +     */
    +    public static TreeState from(GuiPackage guiPackage) {
    +        if (guiPackage == null) {
    +            return NOTHING;
    +        }
    +
    +        MainFrame mainframe = guiPackage.getMainFrame();
    +        if (mainframe != null) {
    +            final JTree tree = mainframe.getTree();
    +            int savedSelected = tree.getMinSelectionRow();
    +            ArrayList<Integer> savedExpanded = new ArrayList<>();
    +
    +            for (int rowN = 0; rowN < tree.getRowCount(); rowN++) {
    +                if (tree.isExpanded(rowN)) {
    +                    savedExpanded.add(rowN);
    +                }
    +            }
    +
    +            return new TreeStateImpl(savedSelected, savedExpanded);
    +        }
    +
    +        return NOTHING;
    +    }
    +
    +    static final class TreeStateImpl implements TreeState {
    +
    +        // GUI tree expansion state
    +        private final List<Integer> savedExpanded;
    +
    +        // GUI tree selected row
    +        private final int savedSelected;
    +
    +        public TreeStateImpl(int savedSelected, List<Integer> 
savedExpanded) {
    +            this.savedSelected = savedSelected;
    +            this.savedExpanded = savedExpanded;
    +        }
    +
    +        @Override
    +        public void restore(GuiPackage guiInstance) {
    +            MainFrame mainframe = guiInstance.getMainFrame();
    +            if (mainframe == null) {
    +                //log?
    +                return;
    +            }
    +
    +            final JTree tree = mainframe.getTree();
    +
    +            if (savedExpanded.size() > 0) {
    --- End diff --
    
    use `isEmpty()` can be more efficient


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to