Ticket #20720: 20720-2021-04-13.patch

File 20720-2021-04-13.patch, 6.3 KB (added by ygramul, 5 years ago)

Got rid of the map after finding put/getClientProperty again

  • 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
    14 import javax.swing.JLabel;
    15 import javax.swing.SwingConstants;
    16 import javax.swing.SwingUtilities;
     20import javax.swing.*;
    1721
    1822import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    1923import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
     
    3842
    3943    private final transient PluginPreferencesModel model;
    4044
     45    /** Whether the plugin list has been built up already in the UI. */
     46    private boolean pluginListInitialized = false;
     47
    4148    /**
    4249     * Constructs a new {@code PluginListPanel} with a default model.
    4350     */
     
    104111                        tr("The filter returned no results."))
    105112                + "</html>"
    106113        );
     114        hint.putClientProperty("plugin", "empty");
    107115        add(hint, gbc);
    108116    }
    109117
     
    141149            gbc.insets = new Insets(5, 5, 0, 5);
    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
    150160            HtmlPanel description = new HtmlPanel();
     
    156166            gbc.gridy = ++row;
    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();
    177196        repaint();
    178197        SwingUtilities.invokeLater(() -> scrollRectToVisible(visibleRect));
    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) {
    183243        return visibleRect.height / 4;
  • src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java

     
    339339            if (!task.isCanceled()) {
    340340                SwingUtilities.invokeLater(() -> {
    341341                    model.setAvailablePlugins(task.getAvailablePlugins());
     342                    pnlPluginPreferences.resetDisplayedComponents();
    342343                    pnlPluginPreferences.refreshView();
    343344                });
    344345            }
     
    372373                if (!task.isCanceled()) {
    373374                    SwingUtilities.invokeLater(() -> {
    374375                        model.updateAvailablePlugins(task.getAvailablePlugins());
     376                        pnlPluginPreferences.resetDisplayedComponents();
    375377                        pnlPluginPreferences.refreshView();
    376378                        Config.getPref().putInt("pluginmanager.version", Version.getInstance().getVersion()); // fix #7030
    377379                    });