Ticket #20720: 20720-2021-04-12.patch
| File 20720-2021-04-12.patch, 6.7 KB (added by , 5 years ago) |
|---|
-
src/org/openstreetmap/josm/gui/preferences/plugin/PluginListPanel.java
3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Component; 6 7 import java.awt.GridBagConstraints; 7 8 import java.awt.GridBagLayout; 8 9 import java.awt.Insets; 9 10 import java.awt.Rectangle; 10 11 import java.awt.event.MouseAdapter; 11 12 import java.awt.event.MouseEvent; 13 import java.util.ArrayList; 14 import java.util.HashMap; 15 import java.util.HashSet; 12 16 import java.util.List; 17 import java.util.Map; 18 import java.util.Set; 13 19 14 20 import javax.swing.JLabel; 15 21 import javax.swing.SwingConstants; … … 38 44 39 45 private final transient PluginPreferencesModel model; 40 46 47 /** Whether the plugin list has been built up already in the UI. */ 48 private boolean pluginListInitialized = false; 49 41 50 /** 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 without the filtering code having to 54 * know about the exact number and order of components belonging to a particular plugin. 55 */ 56 private final Map<PluginInformation, List<Component>> componentsForPlugin = new HashMap<>(); 57 58 /** 42 59 * Constructs a new {@code PluginListPanel} with a default model. 43 60 */ 44 61 public PluginListPanel() { … … 104 121 tr("The filter returned no results.")) 105 122 + "</html>" 106 123 ); 124 List<Component> components = new ArrayList<>(1); 125 components.add(hint); 126 componentsForPlugin.put(null, components); 107 127 add(hint, gbc); 108 128 } 109 129 … … 121 141 122 142 int row = -1; 123 143 for (final PluginInformation pi : displayedPlugins) { 144 List<Component> components = new ArrayList<>(3); 124 145 boolean selected = model.isSelectedPlugin(pi.getName()); 125 146 String remoteversion = formatPluginRemoteVersion(pi); 126 147 String localversion = formatPluginLocalVersion(model.getPluginInformation(pi.getName())); … … 141 162 gbc.insets = new Insets(5, 5, 0, 5); 142 163 gbc.weighty = 0.0; 143 164 gbc.weightx = 0.0; 165 components.add(cbPlugin); 144 166 add(cbPlugin, gbc); 145 167 146 168 gbc.gridx = 1; 147 169 gbc.weightx = 1.0; 170 components.add(lblPlugin); 148 171 add(lblPlugin, gbc); 149 172 150 173 HtmlPanel description = new HtmlPanel(); … … 156 179 gbc.gridy = ++row; 157 180 gbc.insets = new Insets(3, 25, 5, 5); 158 181 gbc.weighty = 1.0; 182 components.add(description); 159 183 add(description, gbc); 184 componentsForPlugin.put(pi, components); 160 185 } 186 187 pluginListInitialized = true; 161 188 } 162 189 163 190 /** 164 191 * Refreshes the list. 192 * 193 * If the list has been changed completely (i.e. not just filtered), 194 * call {@link #resetDisplayedComponents()} prior to calling this method. 165 195 */ 166 196 public void refreshView() { 167 197 final Rectangle visibleRect = getVisibleRect(); 168 198 List<PluginInformation> displayedPlugins = model.getDisplayedPlugins(); 169 removeAll();170 199 171 200 if (displayedPlugins.isEmpty()) { 201 hidePluginsNotInList(new ArrayList<>()); 172 202 displayEmptyPluginListInformation(); 203 } else if (!pluginListInitialized) { 204 removeAll(); 205 displayPluginList(displayedPlugins); 173 206 } else { 174 displayPluginList(displayedPlugins);207 hidePluginsNotInList(displayedPlugins); 175 208 } 176 209 revalidate(); 177 210 repaint(); 178 211 SwingUtilities.invokeLater(() -> scrollRectToVisible(visibleRect)); 179 212 } 180 213 214 /** 215 * Hides components in the list for plugins that are currently filtered away. 216 * 217 * Since those components are relatively heavyweight rebuilding them every time 218 * when the filter changes is fairly slow, so we build them once and just hide 219 * those that shouldn't be visible. 220 * 221 * @param displayedPlugins A collection of plugins that are currently visible. 222 */ 223 private void hidePluginsNotInList(List<PluginInformation> displayedPlugins) { 224 Set<PluginInformation> displayedPluginsSet = new HashSet<>(displayedPlugins); 225 componentsForPlugin.forEach((pi, components) -> { 226 if (displayedPluginsSet.contains(pi)) { 227 for (Component component : components) { 228 component.setVisible(true); 229 } 230 } else { 231 for (Component component : components) { 232 component.setVisible(false); 233 } 234 } 235 }); 236 } 237 238 /** 239 * Causes the components for the list items to be rebuilt from scratch. 240 * 241 * Should be called before calling {@link #refreshView()} whenever the 242 * underlying list changes to display a completely different set of 243 * plugins instead of merely hiding plugins by a filter. 244 */ 245 public void resetDisplayedComponents() { 246 pluginListInitialized = false; 247 componentsForPlugin.clear(); 248 } 249 181 250 @Override 182 251 public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { 183 252 return visibleRect.height / 4; -
src/org/openstreetmap/josm/gui/preferences/plugin/PluginPreference.java
339 339 if (!task.isCanceled()) { 340 340 SwingUtilities.invokeLater(() -> { 341 341 model.setAvailablePlugins(task.getAvailablePlugins()); 342 pnlPluginPreferences.resetDisplayedComponents(); 342 343 pnlPluginPreferences.refreshView(); 343 344 }); 344 345 } … … 372 373 if (!task.isCanceled()) { 373 374 SwingUtilities.invokeLater(() -> { 374 375 model.updateAvailablePlugins(task.getAvailablePlugins()); 376 pnlPluginPreferences.resetDisplayedComponents(); 375 377 pnlPluginPreferences.refreshView(); 376 378 Config.getPref().putInt("pluginmanager.version", Version.getInstance().getVersion()); // fix #7030 377 379 });
