Ticket #20720: 20720.patch

File 20720.patch, 4.4 KB (added by ygramul, 5 years ago)

Patch to improve filtering performance in Plugins Preferences tab

  • src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java

     
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Component;
    67import java.awt.GridBagConstraints;
    78import java.awt.GridBagLayout;
    89import java.awt.Insets;
    910import java.awt.Rectangle;
    1011import java.awt.event.MouseAdapter;
    1112import java.awt.event.MouseEvent;
     13import java.util.ArrayList;
     14import java.util.HashMap;
     15import java.util.HashSet;
    1216import java.util.List;
     17import java.util.Map;
     18import java.util.Set;
    1319
    1420import javax.swing.JLabel;
    1521import javax.swing.SwingConstants;
     
    3844
    3945    private final transient PluginPreferencesModel model;
    4046
     47    /** Whether the plugin list has been built up already in the UI */
     48    private boolean pluginListInitialized = false;
     49
    4150    /**
     51     * A map from the plugin information to the UI components that are used to display that information.
     52     *
     53     * Used to selectively hide filtered elements.
     54     */
     55    private final Map<PluginInformation, List<Component>> componentsForPlugin = new HashMap<>();
     56
     57    /**
    4258     * Constructs a new {@code PluginListPanel} with a default model.
    4359     */
    4460    public PluginListPanel() {
     
    104120                        tr("The filter returned no results."))
    105121                + "</html>"
    106122        );
     123        List<Component> components = new ArrayList<>(1);
     124        components.add(hint);
     125        componentsForPlugin.put(null, components);
    107126        add(hint, gbc);
    108127    }
    109128
     
    121140
    122141        int row = -1;
    123142        for (final PluginInformation pi : displayedPlugins) {
     143            List<Component> components = new ArrayList<>(3);
    124144            boolean selected = model.isSelectedPlugin(pi.getName());
    125145            String remoteversion = formatPluginRemoteVersion(pi);
    126146            String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName()));
     
    141161            gbc.insets = new Insets(5, 5, 0, 5);
    142162            gbc.weighty = 0.0;
    143163            gbc.weightx = 0.0;
     164            components.add(cbPlugin);
    144165            add(cbPlugin, gbc);
    145166
    146167            gbc.gridx = 1;
    147168            gbc.weightx = 1.0;
     169            components.add(lblPlugin);
    148170            add(lblPlugin, gbc);
    149171
    150172            HtmlPanel description = new HtmlPanel();
     
    156178            gbc.gridy = ++row;
    157179            gbc.insets = new Insets(3, 25, 5, 5);
    158180            gbc.weighty = 1.0;
     181            components.add(description);
    159182            add(description, gbc);
     183            componentsForPlugin.put(pi, components);
    160184        }
     185
     186        pluginListInitialized = true;
    161187    }
    162188
    163189    /**
     
    166192    public void refreshView() {
    167193        final Rectangle visibleRect = getVisibleRect();
    168194        List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
    169         removeAll();
    170195
    171196        if (displayedPlugins.isEmpty()) {
     197            hidePluginsNotInList(new ArrayList<>());
    172198            displayEmptyPluginListInformation();
     199        } else if (!pluginListInitialized) {
     200            removeAll();
     201            displayPluginList(displayedPlugins);
    173202        } else {
    174             displayPluginList(displayedPlugins);
     203            hidePluginsNotInList(displayedPlugins);
    175204        }
    176205        revalidate();
    177206        repaint();
    178207        SwingUtilities.invokeLater(() -> scrollRectToVisible(visibleRect));
    179208    }
    180209
     210    private void hidePluginsNotInList(List<PluginInformation> displayedPlugins) {
     211        Set<PluginInformation> displayedPluginsSet = new HashSet<>(displayedPlugins);
     212        componentsForPlugin.forEach((pi, components) -> {
     213            if (displayedPluginsSet.contains(pi)) {
     214                for (Component component : components) {
     215                    component.setVisible(true);
     216                }
     217            } else {
     218                for (Component component : components) {
     219                    component.setVisible(false);
     220                }
     221            }
     222        });
     223    }
     224
    181225    @Override
    182226    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
    183227        return visibleRect.height / 4;