Changeset 17784 in josm for trunk/src/org


Ignore:
Timestamp:
2021-04-14T21:21:31+02:00 (4 years ago)
Author:
simon04
Message:

fix #20720 - Filtering in plugins list in preferences is slow (patch by ygramul)

Location:
trunk/src/org/openstreetmap/josm/gui/preferences/plugin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java

    r17733 r17784  
    1010import java.awt.event.MouseAdapter;
    1111import java.awt.event.MouseEvent;
     12import java.util.ArrayList;
     13import java.util.HashSet;
    1214import java.util.List;
    13 
     15import java.util.Set;
     16
     17import javax.swing.JComponent;
    1418import javax.swing.JLabel;
    1519import javax.swing.SwingConstants;
     
    3842
    3943    private final transient PluginPreferencesModel model;
     44
     45    /** Whether the plugin list has been built up already in the UI. */
     46    private boolean pluginListInitialized = false;
    4047
    4148    /**
     
    105112                + "</html>"
    106113        );
     114        hint.putClientProperty("plugin", "empty");
    107115        add(hint, gbc);
    108116    }
     
    142150            gbc.weighty = 0.0;
    143151            gbc.weightx = 0.0;
     152            cbPlugin.putClientProperty("plugin", pi);
    144153            add(cbPlugin, gbc);
    145154
    146155            gbc.gridx = 1;
    147156            gbc.weightx = 1.0;
     157            lblPlugin.putClientProperty("plugin", pi);
    148158            add(lblPlugin, gbc);
    149159
     
    157167            gbc.insets = new Insets(3, 25, 5, 5);
    158168            gbc.weighty = 1.0;
     169            description.putClientProperty("plugin", pi);
    159170            add(description, gbc);
    160171        }
     172
     173        pluginListInitialized = true;
    161174    }
    162175
    163176    /**
    164177     * Refreshes the list.
     178     *
     179     * If the list has been changed completely (i.e. not just filtered),
     180     * call {@link #resetDisplayedComponents()} prior to calling this method.
    165181     */
    166182    public void refreshView() {
    167183        final Rectangle visibleRect = getVisibleRect();
    168184        List<PluginInformation> displayedPlugins = model.getDisplayedPlugins();
    169         removeAll();
    170185
    171186        if (displayedPlugins.isEmpty()) {
     187            hidePluginsNotInList(new ArrayList<>());
    172188            displayEmptyPluginListInformation();
     189        } else if (!pluginListInitialized) {
     190            removeAll();
     191            displayPluginList(displayedPlugins);
    173192        } else {
    174             displayPluginList(displayedPlugins);
     193            hidePluginsNotInList(displayedPlugins);
    175194        }
    176195        revalidate();
     
    179198    }
    180199
     200    /**
     201     * Hides components in the list for plugins that are currently filtered away.
     202     *
     203     * Since those components are relatively heavyweight rebuilding them every time
     204     * when the filter changes is fairly slow, so we build them once and just hide
     205     * those that shouldn't be visible.
     206     *
     207     * @param displayedPlugins A collection of plugins that are currently visible.
     208     */
     209    private void hidePluginsNotInList(List<PluginInformation> displayedPlugins) {
     210        // Remove the empty plugin list warning if it's there
     211        synchronized (getTreeLock()) {
     212            for (int i = 0; i < getComponentCount(); i++) {
     213                JComponent component = (JComponent) getComponent(i);
     214                if ("empty".equals(component.getClientProperty("plugin"))) {
     215                    remove(component);
     216                }
     217            }
     218        }
     219
     220        Set<PluginInformation> displayedPluginsSet = new HashSet<>(displayedPlugins);
     221        synchronized (getTreeLock()) {
     222            for (int i = 0; i < getComponentCount(); i++) {
     223                JComponent component = (JComponent) getComponent(i);
     224                Object plugin = component.getClientProperty("plugin");
     225                component.setVisible(displayedPluginsSet.contains(plugin));
     226            }
     227        }
     228    }
     229
     230    /**
     231     * Causes the components for the list items to be rebuilt from scratch.
     232     *
     233     * Should be called before calling {@link #refreshView()} whenever the
     234     * underlying list changes to display a completely different set of
     235     * plugins instead of merely hiding plugins by a filter.
     236     */
     237    public void resetDisplayedComponents() {
     238        pluginListInitialized = false;
     239    }
     240
    181241    @Override
    182242    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
  • trunk/src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java

    r17768 r17784  
    323323                SwingUtilities.invokeLater(() -> {
    324324                    model.setAvailablePlugins(task.getAvailablePlugins());
     325                    pnlPluginPreferences.resetDisplayedComponents();
    325326                    pnlPluginPreferences.refreshView();
    326327                });
     
    356357                    SwingUtilities.invokeLater(() -> {
    357358                        model.updateAvailablePlugins(task.getAvailablePlugins());
     359                        pnlPluginPreferences.resetDisplayedComponents();
    358360                        pnlPluginPreferences.refreshView();
    359361                        Config.getPref().putInt("pluginmanager.version", Version.getInstance().getVersion()); // fix #7030
Note: See TracChangeset for help on using the changeset viewer.